2015-01-03 22:09:43 +00:00
|
|
|
"""
|
|
|
|
``reorder`` plugin: Reorder the tabs according to a layout
|
|
|
|
|
|
|
|
Commands
|
|
|
|
--------
|
|
|
|
|
|
|
|
.. glossary::
|
|
|
|
|
|
|
|
/reorder
|
|
|
|
**Usage:** ``/reorder``
|
|
|
|
|
|
|
|
Reorder the tabs according to the configuration.
|
|
|
|
|
2016-07-01 17:50:43 +00:00
|
|
|
/save_order
|
|
|
|
**Usage:** ``/save_order``
|
|
|
|
|
|
|
|
Save the current tab order to the configuration.
|
2015-01-03 22:09:43 +00:00
|
|
|
|
|
|
|
Configuration
|
|
|
|
-------------
|
|
|
|
|
|
|
|
The configuration file must contain a section ``[reorder]`` and each option
|
|
|
|
must be formatted like ``[tab number] = [tab type]:[tab name]``.
|
|
|
|
|
|
|
|
For example:
|
|
|
|
|
|
|
|
.. code-block:: ini
|
|
|
|
|
|
|
|
[reorder]
|
|
|
|
1 = muc:toto@conference.example.com
|
|
|
|
2 = muc:example@muc.example.im
|
|
|
|
3 = dynamic:robert@example.org
|
|
|
|
|
|
|
|
The ``[tab number]`` must be at least ``1``; if the range is not entirely
|
|
|
|
covered, e.g.:
|
|
|
|
|
|
|
|
.. code-block:: ini
|
|
|
|
|
|
|
|
[reorder]
|
|
|
|
1 = muc:toto@conference.example.com
|
|
|
|
3 = dynamic:robert@example.org
|
|
|
|
|
|
|
|
Poezio will insert gaps between the tabs in order to keep the specified
|
|
|
|
numbering (so in this case, there will be a tab 1, a tab 3, but no tab 2).
|
|
|
|
|
|
|
|
|
|
|
|
The ``[tab type]`` must be one of:
|
|
|
|
|
|
|
|
- ``muc`` (for multi-user chats)
|
|
|
|
- ``private`` (for chats with a specific user inside a multi-user chat)
|
|
|
|
- ``dynamic`` (for normal, dynamic conversations tabs)
|
|
|
|
- ``static`` (for conversations with a specific resource)
|
|
|
|
|
|
|
|
And finally, the ``[tab name]`` must be:
|
|
|
|
|
|
|
|
- For a type ``muc``, the bare JID of the room
|
|
|
|
- For a type ``private``, the full JID of the user (room JID with the username as a resource)
|
|
|
|
- For a type ``dynamic``, the bare JID of the contact
|
|
|
|
- For a type ``static``, the full JID of the contact
|
|
|
|
"""
|
2016-07-01 17:50:43 +00:00
|
|
|
|
2020-03-31 23:16:11 +00:00
|
|
|
from slixmpp import InvalidJID, JID
|
|
|
|
|
2016-06-27 23:10:52 +00:00
|
|
|
from poezio import tabs
|
|
|
|
from poezio.decorators import command_args_parser
|
2016-07-01 17:50:43 +00:00
|
|
|
from poezio.plugin import BasePlugin
|
2016-10-15 00:47:27 +00:00
|
|
|
from poezio.config import config
|
2015-01-03 22:09:43 +00:00
|
|
|
|
2016-07-01 17:50:43 +00:00
|
|
|
TEXT_TO_TAB = {
|
2015-01-03 22:09:43 +00:00
|
|
|
'muc': tabs.MucTab,
|
|
|
|
'private': tabs.PrivateTab,
|
|
|
|
'dynamic': tabs.DynamicConversationTab,
|
|
|
|
'static': tabs.StaticConversationTab,
|
|
|
|
'empty': tabs.GapTab
|
|
|
|
}
|
|
|
|
|
2016-07-01 17:50:43 +00:00
|
|
|
TAB_TO_TEXT = {
|
|
|
|
tabs.MucTab: 'muc',
|
|
|
|
tabs.DynamicConversationTab: 'dynamic',
|
|
|
|
tabs.PrivateTab: 'private',
|
|
|
|
tabs.StaticConversationTab: 'static',
|
|
|
|
tabs.GapTab: 'empty'
|
|
|
|
}
|
|
|
|
|
2018-08-15 11:13:17 +00:00
|
|
|
|
2018-07-21 19:36:46 +00:00
|
|
|
def parse_config(tab_config):
|
2015-01-03 22:09:43 +00:00
|
|
|
result = {}
|
2018-07-21 19:36:46 +00:00
|
|
|
for option in tab_config.options('reorder'):
|
2015-01-03 22:09:43 +00:00
|
|
|
if not option.isdecimal():
|
|
|
|
continue
|
|
|
|
pos = int(option)
|
|
|
|
if pos in result or pos <= 0:
|
2018-07-21 19:36:46 +00:00
|
|
|
return None
|
2015-01-03 22:09:43 +00:00
|
|
|
|
2018-07-21 19:36:46 +00:00
|
|
|
typ, name = tab_config.get(option, default=':').split(':', maxsplit=1)
|
2016-07-01 17:50:43 +00:00
|
|
|
if typ not in TEXT_TO_TAB:
|
2018-07-21 19:36:46 +00:00
|
|
|
return None
|
2016-07-01 17:50:43 +00:00
|
|
|
result[pos] = (TEXT_TO_TAB[typ], name)
|
2015-01-03 22:09:43 +00:00
|
|
|
|
|
|
|
return result
|
|
|
|
|
2018-08-15 11:13:17 +00:00
|
|
|
|
2016-07-01 17:50:43 +00:00
|
|
|
def check_tab(tab):
|
|
|
|
for cls, rep in TAB_TO_TEXT.items():
|
|
|
|
if isinstance(tab, cls):
|
|
|
|
return rep
|
|
|
|
return ''
|
|
|
|
|
2018-08-15 11:13:17 +00:00
|
|
|
|
2016-07-01 17:50:43 +00:00
|
|
|
def parse_runtime_tablist(tablist):
|
|
|
|
props = []
|
|
|
|
i = 0
|
|
|
|
for tab in tablist[1:]:
|
|
|
|
i += 1
|
|
|
|
result = check_tab(tab)
|
|
|
|
if result:
|
2019-04-28 00:02:49 +00:00
|
|
|
props.append((i, '%s:%s' % (result, tab.jid.full)))
|
2016-07-01 17:50:43 +00:00
|
|
|
return props
|
|
|
|
|
2018-08-15 11:13:17 +00:00
|
|
|
|
2015-01-03 22:09:43 +00:00
|
|
|
class Plugin(BasePlugin):
|
2020-03-13 01:35:57 +00:00
|
|
|
"""reorder plugin"""
|
|
|
|
|
2015-01-03 22:09:43 +00:00
|
|
|
def init(self):
|
2018-08-15 11:13:17 +00:00
|
|
|
self.api.add_command(
|
|
|
|
'reorder',
|
|
|
|
self.command_reorder,
|
|
|
|
help='Reorder all tabs using the pre-defined'
|
|
|
|
' layout from the configuration file.')
|
|
|
|
self.api.add_command(
|
|
|
|
'save_order',
|
|
|
|
self.command_save_order,
|
|
|
|
help='Save the current tab layout')
|
2016-07-01 17:50:43 +00:00
|
|
|
|
|
|
|
@command_args_parser.ignored
|
2020-03-13 01:35:57 +00:00
|
|
|
def command_save_order(self) -> None:
|
|
|
|
"""
|
|
|
|
/save_order
|
|
|
|
"""
|
2016-07-01 17:50:43 +00:00
|
|
|
conf = parse_runtime_tablist(self.core.tabs)
|
|
|
|
for key, value in conf:
|
|
|
|
self.config.set(key, value)
|
|
|
|
self.api.information('Tab order saved', 'Info')
|
2015-01-03 22:09:43 +00:00
|
|
|
|
|
|
|
@command_args_parser.ignored
|
2020-03-13 01:35:57 +00:00
|
|
|
def command_reorder(self) -> None:
|
2015-01-03 22:09:43 +00:00
|
|
|
"""
|
|
|
|
/reorder
|
|
|
|
"""
|
|
|
|
tabs_spec = parse_config(self.config)
|
|
|
|
if not tabs_spec:
|
2020-03-13 01:35:57 +00:00
|
|
|
self.api.information('Invalid reorder config', 'Error')
|
|
|
|
return None
|
2015-01-03 22:09:43 +00:00
|
|
|
|
2018-07-21 19:36:46 +00:00
|
|
|
old_tabs = self.core.tabs.get_tabs()
|
|
|
|
roster = old_tabs.pop(0)
|
2015-01-03 22:09:43 +00:00
|
|
|
|
2016-10-15 00:47:27 +00:00
|
|
|
create_gaps = config.get('create_gaps')
|
|
|
|
|
2018-07-21 19:36:46 +00:00
|
|
|
new_tabs = [roster]
|
2015-01-03 22:09:43 +00:00
|
|
|
last = 0
|
|
|
|
for pos in sorted(tabs_spec):
|
2016-10-15 00:47:27 +00:00
|
|
|
if create_gaps and pos > last + 1:
|
2018-08-15 11:13:17 +00:00
|
|
|
new_tabs += [
|
|
|
|
tabs.GapTab(self.core) for i in range(pos - last - 1)
|
|
|
|
]
|
2020-03-31 23:16:11 +00:00
|
|
|
cls, jid = tabs_spec[pos]
|
|
|
|
try:
|
|
|
|
jid = JID(jid)
|
|
|
|
except InvalidJID:
|
|
|
|
self.api.information('Reorder: Invalid JID \'%s\'.' % jid, 'Warning')
|
|
|
|
continue
|
|
|
|
tab = self.core.tabs.by_name_and_class(str(jid), cls=cls)
|
2015-01-03 22:09:43 +00:00
|
|
|
if tab and tab in old_tabs:
|
|
|
|
new_tabs.append(tab)
|
|
|
|
old_tabs.remove(tab)
|
|
|
|
else:
|
2020-03-31 23:16:11 +00:00
|
|
|
self.api.information('Tab %s not found' % jid, 'Warning')
|
2016-10-15 00:47:27 +00:00
|
|
|
if create_gaps:
|
|
|
|
new_tabs.append(tabs.GapTab(self.core))
|
2015-01-03 22:09:43 +00:00
|
|
|
last = pos
|
|
|
|
|
|
|
|
for tab in old_tabs:
|
|
|
|
if tab:
|
|
|
|
new_tabs.append(tab)
|
|
|
|
|
2020-03-31 22:51:21 +00:00
|
|
|
# TODO: Ensure we don't break poezio and call this with whatever
|
|
|
|
# tablist we have. The roster tab at least needs to be in there.
|
2018-07-21 19:36:46 +00:00
|
|
|
self.core.tabs.replace_tabs(new_tabs)
|
2015-01-03 22:09:43 +00:00
|
|
|
self.core.refresh_window()
|
2020-03-13 01:35:57 +00:00
|
|
|
|
|
|
|
return None
|