Philipp Hörist pushed to branch master at gajim / gajim

Commits:

3 changed files:

Changes:

  • gajim/gtk/application.py
    ... ... @@ -42,7 +42,6 @@
    42 42
     
    
    43 43
     import os
    
    44 44
     import sys
    
    45
    -
    
    46 45
     from urllib.parse import unquote
    
    47 46
     
    
    48 47
     from nbxmpp.namespaces import Namespace
    
    ... ... @@ -74,6 +73,11 @@
    74 73
     from gajim.gui.about import AboutDialog
    
    75 74
     from gajim.gui.avatar import AvatarStorage
    
    76 75
     from gajim.gui.builder import get_builder
    
    76
    +from gajim.gui.const import ACCOUNT_ACTIONS
    
    77
    +from gajim.gui.const import ALWAYS_ACCOUNT_ACTIONS
    
    78
    +from gajim.gui.const import APP_ACTIONS
    
    79
    +from gajim.gui.const import FEATURE_ACCOUNT_ACTIONS
    
    80
    +from gajim.gui.const import ONLINE_ACCOUNT_ACTIONS
    
    77 81
     from gajim.gui.dialogs import DialogButton
    
    78 82
     from gajim.gui.dialogs import ConfirmationDialog
    
    79 83
     from gajim.gui.dialogs import ShortcutsWindow
    
    ... ... @@ -85,7 +89,6 @@
    85 89
     
    
    86 90
     
    
    87 91
     ActionListT = list[tuple[str,
    
    88
    -                         Optional[str],
    
    89 92
                              Callable[[Gio.SimpleAction, GLib.Variant], Any]]]
    
    90 93
     
    
    91 94
     
    
    ... ... @@ -246,7 +249,11 @@ def _startup(self, _application: GajimApplication) -> None:
    246 249
             from gajim.gui.status_icon import StatusIcon
    
    247 250
             self.systray = StatusIcon()
    
    248 251
     
    
    249
    -        self.add_actions()
    
    252
    +        self._add_app_actions()
    
    253
    +        accounts = app.settings.get_accounts()
    
    254
    +        for account in accounts:
    
    255
    +            self.add_account_actions(account)
    
    256
    +
    
    250 257
             self._load_shortcuts()
    
    251 258
             menus.build_accounts_menu()
    
    252 259
             self.update_app_actions_state()
    
    ... ... @@ -389,109 +396,109 @@ def _handle_local_options(self,
    389 396
     
    
    390 397
             return -1
    
    391 398
     
    
    392
    -    def add_actions(self) -> None:
    
    393
    -        actions: ActionListT = [
    
    394
    -            ('quit', None, self._on_quit_action),
    
    395
    -            ('add-account', None,  self._on_add_account_action),
    
    396
    -            ('manage-proxies', None,  self._on_manage_proxies_action),
    
    397
    -            ('preferences', None,  self._on_preferences_action),
    
    398
    -            ('plugins', None,  self._on_plugins_action),
    
    399
    -            ('xml-console', None,  self._on_xml_console_action),
    
    400
    -            ('file-transfer', None,  self._on_file_transfer_action),
    
    401
    -            ('shortcuts', None,  self._on_shortcuts_action),
    
    402
    -            ('features', None,  self._on_features_action),
    
    403
    -            ('content', None,  self._on_content_action),
    
    404
    -            ('join-support-chat', None, self._on_join_support_chat),
    
    405
    -            ('about', None,  self._on_about_action),
    
    406
    -            ('faq', None,  self._on_faq_action),
    
    407
    -            ('ipython', None,  self._on_ipython_action),
    
    408
    -            ('start-chat', 's', self._on_new_chat_action),
    
    409
    -            ('accounts', 's', self._on_accounts_action),
    
    410
    -            ('add-contact', 's', self._on_add_contact_action),
    
    411
    -            ('copy-text', 's', self._on_copy_text_action),
    
    412
    -            ('open-link', 'as', self._on_open_link_action),
    
    413
    -            ('open-mail', 's', self._on_open_mail_action),
    
    414
    -            ('remove-history', 'a{sv}', self._on_remove_history_action),
    
    415
    -            ('create-groupchat', 's', self._on_create_groupchat_action),
    
    416
    -            ('forget-groupchat', 'a{sv}', self._on_forget_groupchat_action),
    
    417
    -            ('groupchat-join', 'as', self._on_groupchat_join_action),
    
    418
    -        ]
    
    419
    -
    
    420
    -        for action in actions:
    
    421
    -            action_name, variant, func = action
    
    399
    +    def _add_app_actions(self) -> None:
    
    400
    +        for action in APP_ACTIONS:
    
    401
    +            action_name, variant = action
    
    422 402
                 if variant is not None:
    
    423 403
                     variant = GLib.VariantType.new(variant)
    
    424 404
     
    
    425 405
                 act = Gio.SimpleAction.new(action_name, variant)
    
    426
    -            act.connect('activate', func)
    
    427 406
                 self.add_action(act)
    
    428 407
     
    
    429
    -        accounts_list = sorted(app.settings.get_accounts())
    
    430
    -        if not accounts_list:
    
    431
    -            return
    
    432
    -        if len(accounts_list) > 1:
    
    433
    -            for acc in accounts_list:
    
    434
    -                self.add_account_actions(acc)
    
    435
    -        else:
    
    436
    -            self.add_account_actions(accounts_list[0])
    
    437
    -
    
    438
    -    def _get_account_actions(self,
    
    439
    -                             account: str) -> list[tuple[str, Any, str, str]]:
    
    440
    -        # pylint: disable=line-too-long
    
    441
    -        return [
    
    442
    -            ('-bookmarks', self._on_bookmarks_action, 'online', 's'),
    
    443
    -            ('-open-chat', self._on_open_chat_action, 'online', 'as'),
    
    444
    -            ('-add-contact', self._on_add_contact_account_action, 'online', 'as'),
    
    445
    -            ('-services', self._on_services_action, 'online', 's'),
    
    446
    -            ('-profile', self._on_profile_action, 'online', 's'),
    
    447
    -            ('-server-info', self._on_server_info_action, 'online', 's'),
    
    448
    -            ('-archive', self._on_archive_action, 'feature', 's'),
    
    449
    -            ('-pep-config', self._on_pep_config_action, 'online', 's'),
    
    450
    -            ('-sync-history', self._on_sync_history_action, 'online', 's'),
    
    451
    -            ('-blocking', self._on_blocking_action, 'feature', 's'),
    
    452
    -            ('-open-event', self._on_open_event_action, 'always', 'a{sv}'),
    
    453
    -            ('-mark-as-read', self._on_mark_as_read_action, 'always', 'a{sv}'),
    
    454
    -            ('-import-contacts', self._on_import_contacts_action, 'online', 's'),
    
    455
    -            ('-export-history', self._on_export_history, 'always', 's'),
    
    408
    +        self._connect_app_actions()
    
    409
    +
    
    410
    +    def _connect_app_actions(self) -> None:
    
    411
    +        actions: ActionListT = [
    
    412
    +            ('quit', self._on_quit_action),
    
    413
    +            ('add-account', self._on_add_account_action),
    
    414
    +            ('manage-proxies', self._on_manage_proxies_action),
    
    415
    +            ('preferences', self._on_preferences_action),
    
    416
    +            ('plugins', self._on_plugins_action),
    
    417
    +            ('xml-console', self._on_xml_console_action),
    
    418
    +            ('file-transfer', self._on_file_transfer_action),
    
    419
    +            ('shortcuts', self._on_shortcuts_action),
    
    420
    +            ('features', self._on_features_action),
    
    421
    +            ('content', self._on_content_action),
    
    422
    +            ('join-support-chat', self._on_join_support_chat),
    
    423
    +            ('about', self._on_about_action),
    
    424
    +            ('faq', self._on_faq_action),
    
    425
    +            ('ipython', self._on_ipython_action),
    
    426
    +            ('start-chat', self._on_new_chat_action),
    
    427
    +            ('accounts', self._on_accounts_action),
    
    428
    +            ('add-contact', self._on_add_contact_action),
    
    429
    +            ('copy-text', self._on_copy_text_action),
    
    430
    +            ('open-link', self._on_open_link_action),
    
    431
    +            ('open-mail', self._on_open_mail_action),
    
    432
    +            ('remove-history', self._on_remove_history_action),
    
    433
    +            ('create-groupchat', self._on_create_groupchat_action),
    
    434
    +            ('forget-groupchat', self._on_forget_groupchat_action),
    
    435
    +            ('groupchat-join', self._on_groupchat_join_action),
    
    456 436
             ]
    
    457
    -        # pylint: enable=line-too-long
    
    437
    +
    
    438
    +        for action in actions:
    
    439
    +            action_name, func = action
    
    440
    +            act = self.lookup_action(action_name)
    
    441
    +            assert act is not None
    
    442
    +            act.connect('activate', func)
    
    443
    +            self.add_action(act)
    
    458 444
     
    
    459 445
         def add_account_actions(self, account: str) -> None:
    
    460
    -        for action in self._get_account_actions(account):
    
    461
    -            action_name, func, state, type_ = action
    
    462
    -            action_name = account + action_name
    
    463
    -            if self.lookup_action(action_name) is not None:
    
    464
    -                # We already added this action
    
    465
    -                continue
    
    466
    -            act = Gio.SimpleAction.new(
    
    467
    -                action_name, GLib.VariantType.new(type_))
    
    468
    -            act.connect("activate", func)
    
    469
    -            if state != 'always':
    
    470
    -                act.set_enabled(False)
    
    446
    +        for action_name, type_ in ACCOUNT_ACTIONS:
    
    447
    +            account_action_name = f'{account}-{action_name}'
    
    448
    +            if self.has_action(account_action_name):
    
    449
    +                raise ValueError('Trying to add action more than once')
    
    450
    +
    
    451
    +            act = Gio.SimpleAction.new(account_action_name,
    
    452
    +                                       GLib.VariantType.new(type_))
    
    453
    +            act.set_enabled(action_name in ALWAYS_ACCOUNT_ACTIONS)
    
    471 454
                 self.add_action(act)
    
    472 455
     
    
    456
    +        self._connect_account_actions(account)
    
    457
    +
    
    458
    +    def _connect_account_actions(self, account: str) -> None:
    
    459
    +        actions = [
    
    460
    +            ('bookmarks', self._on_bookmarks_action),
    
    461
    +            ('open-chat', self._on_open_chat_action),
    
    462
    +            ('add-contact', self._on_add_contact_account_action),
    
    463
    +            ('services', self._on_services_action),
    
    464
    +            ('profile', self._on_profile_action),
    
    465
    +            ('server-info', self._on_server_info_action),
    
    466
    +            ('archive', self._on_archive_action),
    
    467
    +            ('pep-config', self._on_pep_config_action),
    
    468
    +            ('sync-history', self._on_sync_history_action),
    
    469
    +            ('blocking', self._on_blocking_action),
    
    470
    +            ('open-event', self._on_open_event_action),
    
    471
    +            ('mark-as-read', self._on_mark_as_read_action),
    
    472
    +            ('import-contacts', self._on_import_contacts_action),
    
    473
    +            ('export-history', self._on_export_history),
    
    474
    +        ]
    
    475
    +
    
    476
    +        for action_name, func in actions:
    
    477
    +            account_action_name = f'{account}-{action_name}'
    
    478
    +            act = self.lookup_action(account_action_name)
    
    479
    +            assert act is not None
    
    480
    +            act.connect('activate', func)
    
    481
    +
    
    473 482
         def remove_account_actions(self, account: str) -> None:
    
    474
    -        for action in self._get_account_actions(account):
    
    475
    -            action_name = account + action[0]
    
    476
    -            self.remove_action(action_name)
    
    483
    +        for action_name in self.list_actions():
    
    484
    +            if action_name.startswith(f'{account}-'):
    
    485
    +                self.remove_action(action_name)
    
    477 486
     
    
    478 487
         def set_action_state(self, action_name: str, state: bool) -> None:
    
    479 488
             action = self.lookup_action(action_name)
    
    480
    -        if action is None:
    
    481
    -            raise ValueError('Action %s does not exist' % action_name)
    
    489
    +        assert action is not None
    
    482 490
             action.set_enabled(state)
    
    483 491
     
    
    484 492
         def set_account_actions_state(self,
    
    485 493
                                       account: str,
    
    486 494
                                       new_state: bool = False) -> None:
    
    487
    -        for action in self._get_account_actions(account):
    
    488
    -            action_name, _, state, _ = action
    
    489
    -            if not new_state and state in ('online', 'feature'):
    
    490
    -                # We go offline
    
    491
    -                self.set_action_state(account + action_name, False)
    
    492
    -            elif new_state and state == 'online':
    
    493
    -                # We go online
    
    494
    -                self.set_action_state(account + action_name, True)
    
    495
    +
    
    496
    +        for action_name in ONLINE_ACCOUNT_ACTIONS:
    
    497
    +            self.set_action_state(f'{account}-{action_name}', new_state)
    
    498
    +
    
    499
    +        if not new_state:
    
    500
    +            for action_name in FEATURE_ACCOUNT_ACTIONS:
    
    501
    +                self.set_action_state(f'{account}-{action_name}', new_state)
    
    495 502
     
    
    496 503
         def update_app_actions_state(self) -> None:
    
    497 504
             active_accounts = bool(app.get_connected_accounts(exclude_local=True))
    

  • gajim/gtk/const.py
    ... ... @@ -156,3 +156,75 @@ def __str__(self):
    156 156
         'WorkspaceDialog': 'gajim.gui.workspace_dialog',
    
    157 157
         'XMLConsoleWindow': 'gajim.gui.xml_console',
    
    158 158
     }
    
    159
    +
    
    160
    +
    
    161
    +APP_ACTIONS = [
    
    162
    +    ('about', None),
    
    163
    +    ('accounts', 's'),
    
    164
    +    ('add-account', None),
    
    165
    +    ('add-contact', 's'),
    
    166
    +    ('content', None),
    
    167
    +    ('copy-text', 's'),
    
    168
    +    ('create-groupchat', 's'),
    
    169
    +    ('faq', None),
    
    170
    +    ('features', None),
    
    171
    +    ('file-transfer', None),
    
    172
    +    ('forget-groupchat', 'a{sv}'),
    
    173
    +    ('groupchat-join', 'as'),
    
    174
    +    ('ipython', None),
    
    175
    +    ('join-support-chat', None),
    
    176
    +    ('manage-proxies', None),
    
    177
    +    ('open-link', 'as'),
    
    178
    +    ('open-mail', 's'),
    
    179
    +    ('plugins', None),
    
    180
    +    ('preferences', None),
    
    181
    +    ('quit', None),
    
    182
    +    ('remove-history', 'a{sv}'),
    
    183
    +    ('shortcuts', None),
    
    184
    +    ('start-chat', 's'),
    
    185
    +    ('xml-console', None),
    
    186
    +]
    
    187
    +
    
    188
    +
    
    189
    +ACCOUNT_ACTIONS = [
    
    190
    +    ('add-contact', 'as'),
    
    191
    +    ('archive', 's'),
    
    192
    +    ('blocking', 's'),
    
    193
    +    ('bookmarks', 's'),
    
    194
    +    ('export-history', 's'),
    
    195
    +    ('import-contacts', 's'),
    
    196
    +    ('mark-as-read', 'a{sv}'),
    
    197
    +    ('open-chat', 'as'),
    
    198
    +    ('open-event', 'a{sv}'),
    
    199
    +    ('pep-config', 's'),
    
    200
    +    ('profile', 's'),
    
    201
    +    ('server-info', 's'),
    
    202
    +    ('services', 's'),
    
    203
    +    ('sync-history', 's'),
    
    204
    +]
    
    205
    +
    
    206
    +
    
    207
    +ALWAYS_ACCOUNT_ACTIONS = {
    
    208
    +    'export-history',
    
    209
    +    'import-contacts',
    
    210
    +    'open-event',
    
    211
    +}
    
    212
    +
    
    213
    +
    
    214
    +ONLINE_ACCOUNT_ACTIONS = {
    
    215
    +    'add-contact',
    
    216
    +    'bookmarks',
    
    217
    +    'import-contacts',
    
    218
    +    'open-chat',
    
    219
    +    'pep-config',
    
    220
    +    'profile',
    
    221
    +    'server-info',
    
    222
    +    'services',
    
    223
    +    'sync-history',
    
    224
    +}
    
    225
    +
    
    226
    +
    
    227
    +FEATURE_ACCOUNT_ACTIONS = {
    
    228
    +    'archive',
    
    229
    +    'blocking',
    
    230
    +}

  • gajim/gtk/status_icon.py
    ... ... @@ -188,12 +188,10 @@ def _on_sounds_mute(widget: Gtk.CheckMenuItem) -> None:
    188 188
             app.settings.set('sounds_on', not widget.get_active())
    
    189 189
     
    
    190 190
         def _on_toggle_window(self, _widget: Gtk.MenuItem) -> None:
    
    191
    -        # When using Gtk.StatusIcon, app.window will never return True for
    
    192
    -        # 'has-toplevel-focus' while clicking the menu item
    
    193
    -        GLib.idle_add(self._on_activate)
    
    191
    +        self._on_activate()
    
    194 192
     
    
    195 193
         def _on_activate(self, *args: Any) -> None:
    
    196
    -        if app.window.get_property('has-toplevel-focus'):
    
    194
    +        if app.settings.get('last_main_window_visible'):
    
    197 195
                 save_main_window_position()
    
    198 196
                 app.window.hide()
    
    199 197
                 return