bookmark_forms.py 12.7 KB
Newer Older
mathieui's avatar
mathieui committed
1 2 3 4 5
"""
Windows used inthe bookmarkstab
"""
import curses

mathieui's avatar
mathieui committed
6 7
from poezio.windows import base_wins
from poezio.windows.base_wins import Win
mathieui's avatar
mathieui committed
8
from poezio.windows.inputs import Input
mathieui's avatar
mathieui committed
9
from poezio.windows.data_forms import FieldInput, FieldInputMixin
10 11
from poezio.theming import to_curses_attr, get_theme
from poezio.common import safeJID
mathieui's avatar
mathieui committed
12

mathieui's avatar
mathieui committed
13

14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
class BookmarkNameInput(FieldInput, Input):
    def __init__(self, field):
        FieldInput.__init__(self, field)
        Input.__init__(self)
        self.text = field.name
        self.pos = len(self.text)
        self.color = get_theme().COLOR_NORMAL_TEXT

    def save(self):
        self._field.name = self.get_text()

    def get_help_message(self):
        return 'Edit the text'


mathieui's avatar
mathieui committed
29 30 31 32 33
class BookmarkJIDInput(FieldInput, Input):
    def __init__(self, field):
        FieldInput.__init__(self, field)
        Input.__init__(self)
        jid = safeJID(field.jid)
34
        jid.resource = field.nick or None
mathieui's avatar
mathieui committed
35 36 37 38 39 40 41 42 43 44 45 46
        self.text = jid.full
        self.pos = len(self.text)
        self.color = get_theme().COLOR_NORMAL_TEXT

    def save(self):
        jid = safeJID(self.get_text())
        self._field.jid = jid.bare
        self._field.nick = jid.resource

    def get_help_message(self):
        return 'Edit the text'

mathieui's avatar
mathieui committed
47

mathieui's avatar
mathieui committed
48
class BookmarkMethodInput(FieldInputMixin):
mathieui's avatar
mathieui committed
49 50 51 52 53 54 55 56 57 58 59 60
    def __init__(self, field):
        FieldInput.__init__(self, field)
        Win.__init__(self)
        self.options = ('local', 'remote')
        # val_pos is the position of the currently selected option
        self.val_pos = self.options.index(field.method)

    def do_command(self, key):
        if key == 'KEY_LEFT':
            if self.val_pos > 0:
                self.val_pos -= 1
        elif key == 'KEY_RIGHT':
mathieui's avatar
mathieui committed
61
            if self.val_pos < len(self.options) - 1:
mathieui's avatar
mathieui committed
62 63 64 65 66 67 68 69
                self.val_pos += 1
        else:
            return
        self.refresh()

    def refresh(self):
        self._win.erase()
        self._win.attron(to_curses_attr(self.color))
mathieui's avatar
mathieui committed
70
        self.addnstr(0, 0, ' ' * self.width, self.width)
mathieui's avatar
mathieui committed
71 72
        if self.val_pos > 0:
            self.addstr(0, 0, '←')
mathieui's avatar
mathieui committed
73 74
        if self.val_pos < len(self.options) - 1:
            self.addstr(0, self.width - 1, '→')
mathieui's avatar
mathieui committed
75 76
        if self.options:
            option = self.options[self.val_pos]
mathieui's avatar
mathieui committed
77
            self.addstr(0, self.width // 2 - len(option) // 2, option)
mathieui's avatar
mathieui committed
78 79 80 81 82 83 84 85 86
        self._win.attroff(to_curses_attr(self.color))
        self._refresh()

    def save(self):
        self._field.method = self.options[self.val_pos]

    def get_help_message(self):
        return '←, →: Select a value amongst the others'

mathieui's avatar
mathieui committed
87

mathieui's avatar
mathieui committed
88 89 90 91 92 93 94 95 96 97 98 99
class BookmarkPasswordInput(FieldInput, Input):
    def __init__(self, field):
        FieldInput.__init__(self, field)
        Input.__init__(self)
        self.text = field.password or ''
        self.pos = len(self.text)
        self.color = get_theme().COLOR_NORMAL_TEXT

    def rewrite_text(self):
        self._win.erase()
        if self.color:
            self._win.attron(to_curses_attr(self.color))
mathieui's avatar
mathieui committed
100 101
        self.addstr(
            '*' * len(self.text[self.view_pos:self.view_pos + self.width - 1]))
mathieui's avatar
mathieui committed
102 103
        if self.color:
            (y, x) = self._win.getyx()
mathieui's avatar
mathieui committed
104 105
            size = self.width - x
            self.addnstr(' ' * size, size, to_curses_attr(self.color))
mathieui's avatar
mathieui committed
106 107 108 109 110 111 112 113 114 115 116
        self.addstr(0, self.pos, '')
        if self.color:
            self._win.attroff(to_curses_attr(self.color))
        self._refresh()

    def save(self):
        self._field.password = self.get_text() or None

    def get_help_message(self):
        return 'Edit the secret text'

mathieui's avatar
mathieui committed
117

mathieui's avatar
mathieui committed
118
class BookmarkAutojoinWin(FieldInputMixin):
mathieui's avatar
mathieui committed
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 152
    def __init__(self, field):
        FieldInput.__init__(self, field)
        Win.__init__(self)
        self.last_key = 'KEY_RIGHT'
        self.value = field.autojoin

    def do_command(self, key):
        if key == 'KEY_LEFT' or key == 'KEY_RIGHT':
            self.value = not self.value
            self.last_key = key
        self.refresh()

    def refresh(self):
        self._win.erase()
        self._win.attron(to_curses_attr(self.color))
        format_string = '←{:^%s}→' % 7
        inp = format_string.format(repr(self.value))
        self.addstr(0, 0, inp)
        if self.last_key == 'KEY_RIGHT':
            self.move(0, 8)
        else:
            self.move(0, 0)
        self._win.attroff(to_curses_attr(self.color))
        self._refresh()

    def save(self):
        self._field.autojoin = self.value

    def get_help_message(self):
        return '← and →: change the value between True and False'


class BookmarksWin(Win):
    def __init__(self, bookmarks, height, width, y, x):
mathieui's avatar
mathieui committed
153
        self._win = base_wins.TAB_WIN.derwin(height, width, y, x)
mathieui's avatar
mathieui committed
154 155 156 157 158 159
        self.scroll_pos = 0
        self._current_input = 0
        self.current_horizontal_input = 0
        self._bookmarks = list(bookmarks)
        self.lines = []
        for bookmark in sorted(self._bookmarks, key=lambda x: x.jid):
160 161
            self.lines.append((BookmarkNameInput(bookmark),
                               BookmarkJIDInput(bookmark),
mathieui's avatar
mathieui committed
162 163 164
                               BookmarkPasswordInput(bookmark),
                               BookmarkAutojoinWin(bookmark),
                               BookmarkMethodInput(bookmark)))
mathieui's avatar
mathieui committed
165 166 167 168 169 170 171 172 173

    @property
    def current_input(self):
        return self._current_input

    @current_input.setter
    def current_input(self, value):
        if 0 <= self._current_input < len(self.lines):
            if 0 <= value < len(self.lines):
mathieui's avatar
mathieui committed
174 175 176
                self.lines[self._current_input][
                    self.current_horizontal_input].set_color(
                        get_theme().COLOR_NORMAL_TEXT)
mathieui's avatar
mathieui committed
177 178 179 180 181
                self._current_input = value
        else:
            self._current_input = 0

    def add_bookmark(self, bookmark):
182 183
        self.lines.append((BookmarkNameInput(bookmark),
                           BookmarkJIDInput(bookmark),
mathieui's avatar
mathieui committed
184 185 186
                           BookmarkPasswordInput(bookmark),
                           BookmarkAutojoinWin(bookmark),
                           BookmarkMethodInput(bookmark)))
mathieui's avatar
mathieui committed
187 188
        self.lines[self.current_input][
            self.current_horizontal_input].set_color(
mathieui's avatar
mathieui committed
189
                get_theme().COLOR_NORMAL_TEXT)
mathieui's avatar
mathieui committed
190 191
        self.current_horizontal_input = 0
        self.current_input = len(self.lines) - 1
mathieui's avatar
mathieui committed
192
        if self.current_input - self.scroll_pos > self.height - 1:
mathieui's avatar
mathieui committed
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
            self.scroll_pos = self.current_input - self.height + 1
        self.refresh()

    def del_current_bookmark(self):
        if self.lines:
            bm = self.lines[self.current_input][0]._field
            to_delete = self.current_input
            self.current_input -= 1
            del self.lines[to_delete]
            if self.scroll_pos:
                self.scroll_pos -= 1
            self.refresh()
            return bm

    def resize(self, height, width, y, x):
        self.height = height
        self.width = width
mathieui's avatar
mathieui committed
210
        self._win = base_wins.TAB_WIN.derwin(height, width, y, x)
mathieui's avatar
mathieui committed
211 212
        # Adjust the scroll position, if resizing made the window too small
        # for the cursor to be visible
mathieui's avatar
mathieui committed
213
        while self.current_input - self.scroll_pos > self.height - 1:
mathieui's avatar
mathieui committed
214 215 216 217 218 219 220
            self.scroll_pos += 1

    def go_to_next_line_input(self):
        if not self.lines:
            return
        if self.current_input == len(self.lines) - 1:
            return
mathieui's avatar
mathieui committed
221 222
        self.lines[self.current_input][
            self.current_horizontal_input].set_color(
mathieui's avatar
mathieui committed
223
                get_theme().COLOR_NORMAL_TEXT)
mathieui's avatar
mathieui committed
224 225
        # Adjust the scroll position if the current_input would be outside
        # of the visible area
mathieui's avatar
mathieui committed
226
        if self.current_input + 1 - self.scroll_pos > self.height - 1:
mathieui's avatar
mathieui committed
227 228 229 230 231
            self.current_input += 1
            self.scroll_pos += 1
            self.refresh()
        else:
            self.current_input += 1
mathieui's avatar
mathieui committed
232 233 234
            self.lines[self.current_input][
                self.current_horizontal_input].set_color(
                    get_theme().COLOR_SELECTED_ROW)
mathieui's avatar
mathieui committed
235 236 237 238 239 240

    def go_to_previous_line_input(self):
        if not self.lines:
            return
        if self.current_input == 0:
            return
mathieui's avatar
mathieui committed
241 242
        self.lines[self.current_input][
            self.current_horizontal_input].set_color(
mathieui's avatar
mathieui committed
243
                get_theme().COLOR_NORMAL_TEXT)
mathieui's avatar
mathieui committed
244 245 246 247 248 249
        self.current_input -= 1
        # Adjust the scroll position if the current_input would be outside
        # of the visible area
        if self.current_input < self.scroll_pos:
            self.scroll_pos = self.current_input
            self.refresh()
mathieui's avatar
mathieui committed
250 251
        self.lines[self.current_input][
            self.current_horizontal_input].set_color(
mathieui's avatar
mathieui committed
252
                get_theme().COLOR_SELECTED_ROW)
mathieui's avatar
mathieui committed
253 254 255 256

    def go_to_next_horizontal_input(self):
        if not self.lines:
            return
mathieui's avatar
mathieui committed
257 258
        self.lines[self.current_input][
            self.current_horizontal_input].set_color(
mathieui's avatar
mathieui committed
259
                get_theme().COLOR_NORMAL_TEXT)
mathieui's avatar
mathieui committed
260 261 262
        self.current_horizontal_input += 1
        if self.current_horizontal_input > 3:
            self.current_horizontal_input = 0
mathieui's avatar
mathieui committed
263 264
        self.lines[self.current_input][
            self.current_horizontal_input].set_color(
mathieui's avatar
mathieui committed
265
                get_theme().COLOR_SELECTED_ROW)
mathieui's avatar
mathieui committed
266

267 268 269 270 271 272 273
    def go_to_next_page(self):
        if not self.lines:
            return

        if self.current_input == len(self.lines) - 1:
            return

mathieui's avatar
mathieui committed
274 275
        self.lines[self.current_input][
            self.current_horizontal_input].set_color(
mathieui's avatar
mathieui committed
276
                get_theme().COLOR_NORMAL_TEXT)
277 278 279 280 281 282 283 284
        inc = min(self.height, len(self.lines) - self.current_input - 1)

        if self.current_input + inc - self.scroll_pos > self.height - 1:
            self.current_input += inc
            self.scroll_pos += inc
            self.refresh()
        else:
            self.current_input += inc
mathieui's avatar
mathieui committed
285 286 287
            self.lines[self.current_input][
                self.current_horizontal_input].set_color(
                    get_theme().COLOR_SELECTED_ROW)
288 289 290 291 292 293 294 295 296
        return True

    def go_to_previous_page(self):
        if not self.lines:
            return

        if self.current_input == 0:
            return

mathieui's avatar
mathieui committed
297 298
        self.lines[self.current_input][
            self.current_horizontal_input].set_color(
mathieui's avatar
mathieui committed
299
                get_theme().COLOR_NORMAL_TEXT)
300 301 302 303 304 305 306 307

        dec = min(self.height, self.current_input)
        self.current_input -= dec
        # Adjust the scroll position if the current_input would be outside
        # of the visible area
        if self.current_input < self.scroll_pos:
            self.scroll_pos = self.current_input
            self.refresh()
mathieui's avatar
mathieui committed
308 309
        self.lines[self.current_input][
            self.current_horizontal_input].set_color(
mathieui's avatar
mathieui committed
310
                get_theme().COLOR_SELECTED_ROW)
311 312
        return True

mathieui's avatar
mathieui committed
313 314 315 316 317
    def go_to_previous_horizontal_input(self):
        if not self.lines:
            return
        if self.current_horizontal_input == 0:
            return
mathieui's avatar
mathieui committed
318 319
        self.lines[self.current_input][
            self.current_horizontal_input].set_color(
mathieui's avatar
mathieui committed
320
                get_theme().COLOR_NORMAL_TEXT)
mathieui's avatar
mathieui committed
321
        self.current_horizontal_input -= 1
mathieui's avatar
mathieui committed
322 323
        self.lines[self.current_input][
            self.current_horizontal_input].set_color(
mathieui's avatar
mathieui committed
324
                get_theme().COLOR_SELECTED_ROW)
mathieui's avatar
mathieui committed
325 326 327 328

    def on_input(self, key):
        if not self.lines:
            return
mathieui's avatar
mathieui committed
329 330
        self.lines[self.current_input][
            self.current_horizontal_input].do_command(key)
mathieui's avatar
mathieui committed
331 332 333 334

    def refresh(self):
        # store the cursor status
        self._win.erase()
mathieui's avatar
mathieui committed
335
        y = -self.scroll_pos
mathieui's avatar
mathieui committed
336
        for i in range(len(self.lines)):
337 338
            self.lines[i][0].resize(1, self.width // 4, y + 1, 0)
            self.lines[i][1].resize(1, self.width // 4, y + 1, self.width // 4)
mathieui's avatar
mathieui committed
339 340 341 342 343 344
            self.lines[i][2].resize(1, self.width // 6, y + 1,
                                    3 * self.width // 6)
            self.lines[i][3].resize(1, self.width // 6, y + 1,
                                    4 * self.width // 6)
            self.lines[i][4].resize(1, self.width // 6, y + 1,
                                    5 * self.width // 6)
mathieui's avatar
mathieui committed
345 346 347 348 349 350 351 352 353 354
            y += 1
        self._refresh()
        for i, inp in enumerate(self.lines):
            if i < self.scroll_pos:
                continue
            if i >= self.height + self.scroll_pos:
                break
            for j in range(4):
                inp[j].refresh()

mathieui's avatar
mathieui committed
355 356 357 358 359 360
        if self.lines and self.current_input < self.height - 1:
            self.lines[self.current_input][
                self.current_horizontal_input].set_color(
                    get_theme().COLOR_SELECTED_ROW)
            self.lines[self.current_input][
                self.current_horizontal_input].refresh()
mathieui's avatar
mathieui committed
361 362 363 364 365 366 367
        if not self.lines:
            curses.curs_set(0)
        else:
            curses.curs_set(1)

    def refresh_current_input(self):
        if self.lines:
mathieui's avatar
mathieui committed
368 369
            self.lines[self.current_input][
                self.current_horizontal_input].refresh()
mathieui's avatar
mathieui committed
370 371 372 373 374

    def save(self):
        for line in self.lines:
            for item in line:
                item.save()