bookmarkstab.py 5.53 KB
Newer Older
mathieui's avatar
mathieui committed
1 2 3 4 5 6 7
"""
Defines the data-forms Tab
"""

import logging
log = logging.getLogger(__name__)

8 9 10 11
from poezio import windows
from poezio.bookmarks import Bookmark, BookmarkList, stanza_storage
from poezio.tabs import Tab
from poezio.common import safeJID
mathieui's avatar
mathieui committed
12 13 14 15 16 17 18 19


class BookmarksTab(Tab):
    """
    A tab displaying lines of bookmarks, each bookmark having
    a 4 widgets to set the jid/password/autojoin/storage method
    """
    plugin_commands = {}
20 21
    def __init__(self, core, bookmarks: BookmarkList):
        Tab.__init__(self, core)
mathieui's avatar
mathieui committed
22 23 24 25 26 27 28 29 30 31 32
        self.name = "Bookmarks"
        self.bookmarks = bookmarks
        self.new_bookmarks = []
        self.removed_bookmarks = []
        self.header_win = windows.ColumnHeaderWin(('room@server/nickname',
                                                   'password',
                                                   'autojoin',
                                                   'storage'))
        self.bookmarks_win = windows.BookmarksWin(self.bookmarks,
                                                  self.height-4,
                                                  self.width, 1, 0)
33 34 35 36
        self.help_win = windows.HelpText('Ctrl+Y: save, Ctrl+G: cancel, '
                                         '↑↓: change lines, tab: change '
                                         'column, M-a: add bookmark, C-k'
                                         ': delete bookmark')
mathieui's avatar
mathieui committed
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
        self.info_header = windows.BookmarksInfoWin()
        self.key_func['KEY_UP'] = self.bookmarks_win.go_to_previous_line_input
        self.key_func['KEY_DOWN'] = self.bookmarks_win.go_to_next_line_input
        self.key_func['^I'] = self.bookmarks_win.go_to_next_horizontal_input
        self.key_func['^G'] = self.on_cancel
        self.key_func['^Y'] = self.on_save
        self.key_func['M-a'] = self.add_bookmark
        self.key_func['^K'] = self.del_bookmark
        self.resize()
        self.update_commands()

    def add_bookmark(self):
        new_bookmark = Bookmark(safeJID('room@example.tld/nick'), method='local')
        self.new_bookmarks.append(new_bookmark)
        self.bookmarks_win.add_bookmark(new_bookmark)

    def del_bookmark(self):
        current = self.bookmarks_win.del_current_bookmark()
        if current in self.new_bookmarks:
            self.new_bookmarks.remove(current)
        else:
            self.removed_bookmarks.append(current)

    def on_cancel(self):
61
        self.core.close_tab(self)
mathieui's avatar
mathieui committed
62 63
        return True

64 65 66 67 68 69
    def on_scroll_down(self):
        return self.bookmarks_win.go_to_next_page()

    def on_scroll_up(self):
        return self.bookmarks_win.go_to_previous_page()

mathieui's avatar
mathieui committed
70 71 72
    def on_save(self):
        self.bookmarks_win.save()
        if find_duplicates(self.new_bookmarks):
73
            self.core.information('Duplicate bookmarks in list (saving aborted)', 'Error')
mathieui's avatar
mathieui committed
74 75 76 77 78 79
            return
        for bm in self.new_bookmarks:
            if safeJID(bm.jid):
                if not self.bookmarks[bm.jid]:
                    self.bookmarks.append(bm)
            else:
80
                self.core.information('Invalid JID for bookmark: %s/%s' % (bm.jid, bm.nick), 'Error')
mathieui's avatar
mathieui committed
81 82 83 84 85 86 87 88
                return

        for bm in self.removed_bookmarks:
            if bm in self.bookmarks:
                self.bookmarks.remove(bm)

        def send_cb(success):
            if success:
89
                self.core.information('Bookmarks saved.', 'Info')
mathieui's avatar
mathieui committed
90
            else:
91
                self.core.information('Remote bookmarks not saved.', 'Error')
mathieui's avatar
mathieui committed
92 93
        log.debug('alerte %s', str(stanza_storage(self.bookmarks.bookmarks)))
        self.bookmarks.save(self.core.xmpp, callback=send_cb)
94
        self.core.close_tab(self)
mathieui's avatar
mathieui committed
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
        return True

    def on_input(self, key, raw=False):
        if key in self.key_func:
            res = self.key_func[key]()
            if res:
                return res
            self.bookmarks_win.refresh_current_input()
        else:
            self.bookmarks_win.on_input(key)

    def resize(self):
        self.need_resize = False
        self.header_win.resize_columns({
            'room@server/nickname': self.width//3,
            'password': self.width//3,
            'autojoin': self.width//6,
            'storage': self.width//6
            })
        info_height = self.core.information_win_size
        tab_height = Tab.tab_win_height()
        self.header_win.resize(1, self.width, 0, 0)
        self.bookmarks_win.resize(self.height - 3 - tab_height - info_height,
                             self.width, 1, 0)
        self.help_win.resize(1, self.width, self.height - 1, 0)
        self.info_header.resize(1, self.width,
                                 self.height - 2 - tab_height - info_height, 0)

    def on_info_win_size_changed(self):
        if self.core.information_win_size >= self.height - 3:
            return
        info_height = self.core.information_win_size
        tab_height = Tab.tab_win_height()
        self.bookmarks_win.resize(self.height - 3 - tab_height - info_height,
                             self.width, 1, 0)
        self.info_header.resize(1, self.width,
                                 self.height - 2 - tab_height - info_height, 0)

    def refresh(self):
        if self.need_resize:
            self.resize()
        self.header_win.refresh()
        self.refresh_tab_win()
        self.help_win.refresh()
        self.info_header.refresh(self.bookmarks.preferred)
        self.info_win.refresh()
        self.bookmarks_win.refresh()


def find_duplicates(bm_list):
    jids = set()
    for bookmark in bm_list:
        if bookmark.jid in jids:
            return True
        jids.add(bookmark.jid)
    return False