Merge lp:~plprgt/zim/bookmarksbar-update into lp:~jaap.karssenberg/zim/pyzim
- bookmarksbar-update
- Merge into pyzim
Proposed by
Pl
Status: | Merged | ||||
---|---|---|---|---|---|
Merged at revision: | 851 | ||||
Proposed branch: | lp:~plprgt/zim/bookmarksbar-update | ||||
Merge into: | lp:~jaap.karssenberg/zim/pyzim | ||||
Diff against target: |
790 lines (+351/-259) 3 files modified
data/manual/Plugins/BookmarksBar.txt (+3/-2) tests/bookmarksbar.py (+287/-225) zim/plugins/bookmarksbar.py (+61/-32) |
||||
To merge this branch: | bzr merge lp:~plprgt/zim/bookmarksbar-update | ||||
Related bugs: |
|
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Jaap Karssenberg | Pending | ||
Review via email: mp+307016@code.launchpad.net |
Commit message
Description of the change
A small update to the BookmarksBar plugin.
The main changes:
- A new option to change maximum number of bookmarks is available from the plugin settings.
- If a bookmark has a special name it will not be changed after setting this bookmark to another page.
- If an open page has a corresponding bookmark it will be selected in the bar.
- Tests for the plugin are updated.
To post a comment you must log in.
Preview Diff
[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1 | === modified file 'data/manual/Plugins/BookmarksBar.txt' | |||
2 | --- data/manual/Plugins/BookmarksBar.txt 2015-09-24 09:50:39 +0000 | |||
3 | +++ data/manual/Plugins/BookmarksBar.txt 2016-09-28 12:26:36 +0000 | |||
4 | @@ -11,6 +11,7 @@ | |||
5 | 11 | ===== Plugin options ===== | 11 | ===== Plugin options ===== |
6 | 12 | The option **Save bookmarks** allows to save and load bookmarks and their names between different sessions. If it is unchecked all bookmarks will be automatically deleted after closing the program. | 12 | The option **Save bookmarks** allows to save and load bookmarks and their names between different sessions. If it is unchecked all bookmarks will be automatically deleted after closing the program. |
7 | 13 | The option **Add new bookmarks to the beginning of the bar** allows to choose the place where new bookmarks will be added. If checked all new bookmarks will be added to the beginning of the bar, if unchecked bookmarks will be added to the end of the bar. | 13 | The option **Add new bookmarks to the beginning of the bar** allows to choose the place where new bookmarks will be added. If checked all new bookmarks will be added to the beginning of the bar, if unchecked bookmarks will be added to the end of the bar. |
8 | 14 | It is possible to set the maximum number of bookmarks in the interval from 5 to 20. | ||
9 | 14 | 15 | ||
10 | 15 | ===== Basic operations ===== | 16 | ===== Basic operations ===== |
11 | 16 | To show/hide the bar with bookmarks press **Bookmarks** button in the toolbar or press **<F4>** on keyboard. | 17 | To show/hide the bar with bookmarks press **Bookmarks** button in the toolbar or press **<F4>** on keyboard. |
12 | @@ -24,7 +25,7 @@ | |||
13 | 24 | **Remove All** to delete all bookmarks from the bar, | 25 | **Remove All** to delete all bookmarks from the bar, |
14 | 25 | **Open in New Window** to open the bookmark in a new window, | 26 | **Open in New Window** to open the bookmark in a new window, |
15 | 26 | **Copy** to mark the bookmark for following "Paste", | 27 | **Copy** to mark the bookmark for following "Paste", |
17 | 27 | **Paste** to transfer the marked bookmark to the new position, | 28 | **Paste** to transfer the marked bookmark to the new position. The position will be determined by coordinates of the mouse click which was made to invoke the popup menu, |
18 | 28 | **Set New Name** to set a new name to the bookmark. The name is taken from the clipboard (if the name is too long, the plugin will take only first 25 symbols), | 29 | **Set New Name** to set a new name to the bookmark. The name is taken from the clipboard (if the name is too long, the plugin will take only first 25 symbols), |
19 | 29 | **Back to Original Name** to return the bookmark to its default name. This option will appear only if the bookmark is renamed, | 30 | **Back to Original Name** to return the bookmark to its default name. This option will appear only if the bookmark is renamed, |
20 | 30 | **Set to Current Page** to change the bookmark into a bookmark of the currently opened page. | 31 | **Set to Current Page** to change the bookmark into a bookmark of the currently opened page. |
21 | @@ -36,6 +37,6 @@ | |||
22 | 36 | ===== Restrictions ===== | 37 | ===== Restrictions ===== |
23 | 37 | Only existing pages (which have content or have sub-pages) can be added as bookmarks. If a page is deleted from the notebook its bookmark also will be deleted. | 38 | Only existing pages (which have content or have sub-pages) can be added as bookmarks. If a page is deleted from the notebook its bookmark also will be deleted. |
24 | 38 | No duplicate bookmarks are allowed in the bar, though there can be different bookmarks with identical names. | 39 | No duplicate bookmarks are allowed in the bar, though there can be different bookmarks with identical names. |
26 | 39 | The number of bookmarks is limited to 15. | 40 | |
27 | 40 | 41 | ||
28 | 41 | 42 | ||
29 | 42 | 43 | ||
30 | === modified file 'tests/bookmarksbar.py' | |||
31 | --- tests/bookmarksbar.py 2015-09-24 12:33:11 +0000 | |||
32 | +++ tests/bookmarksbar.py 2016-09-28 12:26:36 +0000 | |||
33 | @@ -1,225 +1,287 @@ | |||
259 | 1 | # -*- coding: utf-8 -*- | 1 | # -*- coding: utf-8 -*- |
260 | 2 | 2 | ||
261 | 3 | # Copyright 2015 Pavel_M <plprgt@gmail.com>. | 3 | # Copyright 2015-2016 Pavel_M <plprgt@gmail.com>, |
262 | 4 | # This is the test for BookmarksBar plugin. | 4 | # released under the GNU GPL version 3. |
263 | 5 | # BookmarksBar is the plugin for Zim program | 5 | |
264 | 6 | # by Jaap Karssenberg <jaap.karssenberg@gmail.com>. | 6 | # This is the test for BookmarksBar plugin. |
265 | 7 | 7 | # BookmarksBar is the plugin for Zim program | |
266 | 8 | 8 | # by Jaap Karssenberg <jaap.karssenberg@gmail.com>. | |
267 | 9 | import tests | 9 | |
268 | 10 | import gtk | 10 | |
269 | 11 | 11 | import tests | |
270 | 12 | from zim.notebook import Path | 12 | import gtk |
271 | 13 | from zim.plugins.bookmarksbar import * | 13 | |
272 | 14 | from zim.config import ConfigDict | 14 | from zim.notebook import Path |
273 | 15 | from zim.gui.clipboard import Clipboard | 15 | from zim.plugins.bookmarksbar import * |
274 | 16 | 16 | from zim.config import ConfigDict | |
275 | 17 | import logging | 17 | from zim.gui.clipboard import Clipboard |
276 | 18 | logger = logging.getLogger('zim.plugins.bookmarksbar') | 18 | |
277 | 19 | 19 | import logging | |
278 | 20 | 20 | logger = logging.getLogger('zim.plugins.bookmarksbar') | |
279 | 21 | class TestBookmarksBar(tests.TestCase): | 21 | |
280 | 22 | 22 | ||
281 | 23 | def setUp(self): | 23 | class TestBookmarksBar(tests.TestCase): |
282 | 24 | self.notebook = tests.new_notebook() | 24 | |
283 | 25 | self.index = self.notebook.index | 25 | @classmethod |
284 | 26 | 26 | def setUpClass(cls): | |
285 | 27 | def runTest(self): | 27 | cls.notebook = tests.new_notebook() |
286 | 28 | '''There is one long test.''' | 28 | cls.index = cls.notebook.index |
287 | 29 | 29 | cls.ui = MockUI() | |
288 | 30 | ui = MockUI() | 30 | cls.ui.notebook = cls.notebook |
289 | 31 | ui.notebook = self.notebook | 31 | cls.ui.page = Path('Test:foo') |
290 | 32 | ui.page = Path('Test:foo') | 32 | |
291 | 33 | uistate = ConfigDict() | 33 | |
292 | 34 | self.assertTrue(self.notebook.get_page(ui.page).exists()) | 34 | def setUp(self): |
293 | 35 | 35 | self.PATHS = ('Parent:Daughter:Granddaughter', | |
294 | 36 | PATHS = ('Parent:Daughter:Granddaughter', | 36 | 'Test:tags', 'Test:foo', 'Books') |
295 | 37 | 'Test:tags', 'Test:foo', 'Books') | 37 | self.LEN_PATHS = len(self.PATHS) |
296 | 38 | LEN_PATHS = len(PATHS) | 38 | self.PATHS_NAMES = {self.PATHS[0]:'name 1', self.PATHS[1]:'name 2', self.PATHS[2]:'name 3'} |
297 | 39 | PATHS_NAMES = {PATHS[0]:'name 1', PATHS[1]:'name 2', PATHS[2]:'name 3'} | 39 | |
298 | 40 | 40 | self.uistate = ConfigDict() | |
299 | 41 | # Check correctness of reading uistate. | 41 | self.uistate.setdefault('bookmarks', []) |
300 | 42 | uistate.setdefault('bookmarks', []) | 42 | self.uistate.setdefault('bookmarks_names', {}) |
301 | 43 | uistate.setdefault('bookmarks_names', {}) | 43 | self.uistate.setdefault('show_full_page_name', True) |
302 | 44 | 44 | ||
303 | 45 | uistate['bookmarks'] = list(PATHS) | 45 | |
304 | 46 | uistate['bookmarks_names'] = dict(PATHS_NAMES) | 46 | def testGeneral(self): |
305 | 47 | Bar = BookmarkBar(ui, uistate, get_page_func = lambda: '') | 47 | '''Test general functions: add, delete bookmarks.''' |
306 | 48 | self.assertTrue(Bar.paths == list(PATHS)) | 48 | |
307 | 49 | self.assertTrue(Bar.paths_names == PATHS_NAMES) | 49 | self.assertTrue(self.notebook.get_page(self.ui.page).exists()) |
308 | 50 | 50 | ||
309 | 51 | uistate['bookmarks'] = [] | 51 | Bar = BookmarkBar(self.ui, self.uistate, get_page_func = lambda: '') |
310 | 52 | uistate['bookmarks_names'] = {} | 52 | Bar.max_bookmarks = 15 # set maximum number of bookmarks |
311 | 53 | Bar = BookmarkBar(ui, uistate, get_page_func = lambda: '') | 53 | |
312 | 54 | self.assertTrue(Bar.paths == []) | 54 | # Add paths to the beginning of the bar. |
313 | 55 | self.assertTrue(Bar.paths_names == {}) | 55 | for i, path in enumerate(self.PATHS): |
314 | 56 | 56 | Bar._add_new(path, add_bookmarks_to_beginning = True) | |
315 | 57 | # Add paths to the beginning of the bar. | 57 | self.assertEqual(len(Bar.paths), i + 1) |
316 | 58 | for i, path in enumerate(PATHS): | 58 | self.assertTrue(Bar.paths == list(reversed(self.PATHS))) |
317 | 59 | Bar._add_new(path, add_bookmarks_to_beginning = True) | 59 | |
318 | 60 | self.assertTrue(len(Bar.paths) == i + 1) | 60 | # Add paths to the end of the bar. |
319 | 61 | self.assertTrue(Bar.paths == list(reversed(PATHS))) | 61 | Bar.paths = [] |
320 | 62 | 62 | for i, path in enumerate(self.PATHS): | |
321 | 63 | # Add paths to the end of the bar. | 63 | Bar._add_new(path, add_bookmarks_to_beginning = False) |
322 | 64 | Bar.paths = [] | 64 | self.assertEqual(len(Bar.paths), i + 1) |
323 | 65 | for i, path in enumerate(PATHS): | 65 | self.assertEqual(Bar.paths, list(self.PATHS)) |
324 | 66 | Bar._add_new(path, add_bookmarks_to_beginning = False) | 66 | |
325 | 67 | self.assertTrue(len(Bar.paths) == i + 1) | 67 | # Check that the same path can't be added to the bar. |
326 | 68 | self.assertTrue(Bar.paths == list(PATHS)) | 68 | Bar._add_new(self.PATHS[0]) |
327 | 69 | 69 | Bar._add_new(self.PATHS[1]) | |
328 | 70 | # Check that the same path can't be added to the bar. | 70 | self.assertEqual(Bar.paths, list(self.PATHS)) |
329 | 71 | Bar._add_new(PATHS[0]) | 71 | |
330 | 72 | Bar._add_new(PATHS[1]) | 72 | # Delete paths from the bar. |
331 | 73 | self.assertTrue(Bar.paths == list(PATHS)) | 73 | for i, button in enumerate(Bar.container.get_children()[2:]): |
332 | 74 | 74 | path = button.zim_path | |
333 | 75 | # Delete paths from the bar. | 75 | self.assertTrue(path in Bar.paths) |
334 | 76 | for i, button in enumerate(Bar.container.get_children()[2:]): | 76 | Bar.delete(button.zim_path) |
335 | 77 | path = button.zim_path | 77 | self.assertEqual(len(Bar.paths), self.LEN_PATHS - i - 1) |
336 | 78 | self.assertTrue(path in Bar.paths) | 78 | self.assertTrue(path not in Bar.paths) |
337 | 79 | Bar.delete(button.zim_path) | 79 | self.assertEqual(Bar.paths, []) |
338 | 80 | self.assertTrue(len(Bar.paths) == LEN_PATHS - i - 1) | 80 | |
339 | 81 | self.assertTrue(path not in Bar.paths) | 81 | # Delete all bookmarks from the bar. |
340 | 82 | self.assertTrue(Bar.paths == []) | 82 | Bar.delete_all() |
341 | 83 | 83 | self.assertEqual(Bar.paths, []) | |
342 | 84 | # Check short page names. | 84 | |
343 | 85 | uistate['show_full_page_name'] = False | 85 | |
344 | 86 | for path in PATHS: | 86 | def testDeletePages(self): |
345 | 87 | Bar._add_new(path) | 87 | '''Check deleting a bookmark after deleting a page in the notebook.''' |
346 | 88 | self.assertTrue(Bar.paths == list(PATHS)) | 88 | |
347 | 89 | for i, button in enumerate(Bar.container.get_children()[2:]): | 89 | notebook = tests.new_notebook() |
348 | 90 | self.assertTrue(PATHS[i] == button.zim_path) | 90 | ui = MockUI() |
349 | 91 | self.assertTrue(Path(PATHS[i]).basename == button.get_label()) | 91 | ui.notebook = notebook |
350 | 92 | uistate['show_full_page_name'] = True | 92 | self.uistate['bookmarks'] = list(self.PATHS) |
351 | 93 | 93 | ||
352 | 94 | # Delete all bookmarks from the bar. | 94 | Bar = BookmarkBar(ui, self.uistate, get_page_func = lambda: '') |
353 | 95 | Bar.delete_all() | 95 | for i, path in enumerate(self.PATHS): |
354 | 96 | self.assertTrue(Bar.paths == []) | 96 | self.assertTrue(path in Bar.paths) |
355 | 97 | 97 | notebook.delete_page(Path(path)) | |
356 | 98 | # Check restriction of max bookmarks in the bar. | 98 | self.assertTrue(path not in Bar.paths) |
357 | 99 | pagelist = set(self.index.list_pages(None)) | 99 | self.assertEqual(len(Bar.paths), self.LEN_PATHS - i - 1) |
358 | 100 | _enhanced_pagelist = set() | 100 | self.assertEqual(Bar.paths, []) |
359 | 101 | for page in pagelist: | 101 | |
360 | 102 | _enhanced_pagelist.update( set(self.index.list_pages(page)) ) | 102 | |
361 | 103 | if len(_enhanced_pagelist) > MAX_BOOKMARKS: | 103 | def testFunctions(self): |
362 | 104 | break | 104 | '''Test bookmark functions: changing, reordering, ranaming.''' |
363 | 105 | pagelist.update(_enhanced_pagelist) | 105 | |
364 | 106 | self.assertTrue(len(pagelist) > MAX_BOOKMARKS) | 106 | Bar = BookmarkBar(self.ui, self.uistate, get_page_func = lambda: '') |
365 | 107 | pagelist = list(pagelist) | 107 | Bar.max_bookmarks = 15 # set maximum number of bookmarks |
366 | 108 | for page in pagelist: | 108 | |
367 | 109 | Bar._add_new(page.name) | 109 | # Check changing a bookmark. |
368 | 110 | self.assertTrue(len(Bar.paths) == MAX_BOOKMARKS) | 110 | for i, path in enumerate(self.PATHS): |
369 | 111 | self.assertTrue(Bar.paths == [a.name for a in pagelist[:MAX_BOOKMARKS]]) | 111 | Bar._add_new(path, add_bookmarks_to_beginning = False) |
370 | 112 | Bar.delete_all() | 112 | |
371 | 113 | 113 | self.assertTrue('Test' not in Bar.paths) | |
372 | 114 | # Check 'save' option in preferences. | 114 | self.assertTrue('Books' in Bar.paths) |
373 | 115 | for i, path in enumerate(PATHS): | 115 | Bar.change_bookmark('Books', 'Books') |
374 | 116 | Bar.on_preferences_changed({'save':False, 'add_bookmarks_to_beginning':False}) | 116 | self.assertEqual(Bar.paths, list(self.PATHS)) |
375 | 117 | Bar._add_new(path) | 117 | Bar.change_bookmark('Books', 'Test') |
376 | 118 | self.assertTrue(uistate['bookmarks'] == []) | 118 | self.assertTrue('Test' in Bar.paths) |
377 | 119 | Bar.on_preferences_changed({'save':True, 'add_bookmarks_to_beginning':False}) | 119 | self.assertTrue('Books' not in Bar.paths) |
378 | 120 | self.assertTrue(uistate['bookmarks'] == list(PATHS[:i+1])) | 120 | _result = [a if a != 'Books' else 'Test' for a in self.PATHS] |
379 | 121 | self.assertTrue(uistate['bookmarks'] == list(PATHS)) | 121 | self.assertEqual(Bar.paths, _result) |
380 | 122 | 122 | ||
381 | 123 | # Check changing a bookmark. | 123 | Bar.change_bookmark('Test', 'Books') |
382 | 124 | self.assertTrue('Test' not in Bar.paths) | 124 | self.assertEqual(Bar.paths, list(self.PATHS)) |
383 | 125 | self.assertTrue('Books' in Bar.paths) | 125 | |
384 | 126 | Bar.change_bookmark('Books', 'Books') | 126 | # Check reordering bookmarks. |
385 | 127 | self.assertTrue(Bar.paths == list(PATHS)) | 127 | new_paths = ('1','2','3','4','5') |
386 | 128 | _b_paths = [a for a in Bar.paths if a != 'Books'] | 128 | |
387 | 129 | Bar.change_bookmark('Books', 'Test') | 129 | Bar.paths = list(new_paths) |
388 | 130 | self.assertTrue('Test' in Bar.paths) | 130 | Bar.move_bookmark(new_paths[2], new_paths[2], 'left') |
389 | 131 | self.assertTrue('Books' not in Bar.paths) | 131 | self.assertEqual(Bar.paths, list(new_paths)) |
390 | 132 | _e_paths = [a for a in Bar.paths if a != 'Test'] | 132 | Bar.move_bookmark(new_paths[3], new_paths[3], 'right') |
391 | 133 | self.assertTrue(_b_paths == _e_paths) | 133 | self.assertEqual(Bar.paths, list(new_paths)) |
392 | 134 | 134 | Bar.move_bookmark('3', '1', 'left') | |
393 | 135 | Bar.change_bookmark('Test', 'Books') | 135 | self.assertEqual(Bar.paths, ['3','1','2','4','5']) |
394 | 136 | self.assertTrue(Bar.paths == list(PATHS)) | 136 | Bar.move_bookmark('5', '1', 'left') |
395 | 137 | 137 | self.assertEqual(Bar.paths, ['3','5','1','2','4']) | |
396 | 138 | # Check deleting a bookmark after deleting a page in the notebook. | 138 | Bar.move_bookmark('5', '1', 'right') |
397 | 139 | self.assertTrue(len(Bar.paths) == LEN_PATHS) | 139 | self.assertEqual(Bar.paths, ['3','1','5','2','4']) |
398 | 140 | for i, path in enumerate(PATHS): | 140 | Bar.move_bookmark('3', '4', 'right') |
399 | 141 | self.assertTrue(path in Bar.paths) | 141 | self.assertEqual(Bar.paths, ['1','5','2','4','3']) |
400 | 142 | self.notebook.delete_page(Path(path)) | 142 | Bar.move_bookmark('5', '4', '-') |
401 | 143 | self.assertTrue(path not in Bar.paths) | 143 | self.assertEqual(Bar.paths, ['1','5','2','4','3']) |
402 | 144 | self.assertTrue(len(Bar.paths) == LEN_PATHS - i - 1) | 144 | |
403 | 145 | self.assertTrue(Bar.paths == []) | 145 | # Check rename_bookmark and save options. |
404 | 146 | 146 | preferences_changed = lambda save: Bar.on_preferences_changed({'save': save, | |
405 | 147 | # Check reordering bookmarks. | 147 | 'add_bookmarks_to_beginning': False, |
406 | 148 | PATHS_2 = ('1','2','3','4','5') | 148 | 'max_bookmarks': 15}) |
407 | 149 | PATHS_NAMES_2 = {PATHS_2[0]:'11', PATHS_2[1]:'22', PATHS_2[2]:'33'} | 149 | |
408 | 150 | 150 | new_path_names = {new_paths[0]:'11', new_paths[1]:'22', new_paths[2]:'33'} | |
409 | 151 | Bar.paths = list(PATHS_2) | 151 | Bar.paths = list(new_paths) |
410 | 152 | Bar.move_bookmark(PATHS_2[2], PATHS_2[2], 'left') | 152 | preferences_changed(True) |
411 | 153 | self.assertTrue(Bar.paths == list(PATHS_2)) | 153 | Bar._reload_bar() |
412 | 154 | Bar.move_bookmark(PATHS_2[3], PATHS_2[3], 'right') | 154 | |
413 | 155 | self.assertTrue(Bar.paths == list(PATHS_2)) | 155 | def rename_check(label, path, paths_names, path_names_uistate): |
414 | 156 | Bar.move_bookmark('3', '1', 'left') | 156 | self.assertEqual(button.get_label(), label) |
415 | 157 | self.assertTrue(Bar.paths == ['3','1','2','4','5']) | 157 | self.assertEqual(button.zim_path, path) |
416 | 158 | Bar.move_bookmark('5', '1', 'left') | 158 | self.assertEqual(Bar.paths_names, paths_names) |
417 | 159 | self.assertTrue(Bar.paths == ['3','5','1','2','4']) | 159 | self.assertEqual(self.uistate['bookmarks_names'], path_names_uistate) |
418 | 160 | Bar.move_bookmark('5', '1', 'right') | 160 | |
419 | 161 | self.assertTrue(Bar.paths == ['3','1','5','2','4']) | 161 | button = gtk.Button(label = new_paths[0], use_underline = False) |
420 | 162 | Bar.move_bookmark('3', '4', 'right') | 162 | button.zim_path = new_paths[0] |
421 | 163 | self.assertTrue(Bar.paths == ['1','5','2','4','3']) | 163 | rename_check(new_paths[0], new_paths[0], {}, {}) |
422 | 164 | Bar.move_bookmark('5', '4', '-') | 164 | |
423 | 165 | self.assertTrue(Bar.paths == ['1','5','2','4','3']) | 165 | Clipboard.set_text('new name') |
424 | 166 | 166 | Bar.rename_bookmark(button) | |
425 | 167 | # CHECK RENAMING | 167 | rename_check('new name', new_paths[0], {new_paths[0]:'new name'}, {new_paths[0]:'new name'}) |
426 | 168 | # Check rename_bookmark and save options. | 168 | preferences_changed(False) |
427 | 169 | Bar.paths = list(PATHS_2) | 169 | rename_check('new name', new_paths[0], {new_paths[0]:'new name'}, {}) |
428 | 170 | button = gtk.Button(label = PATHS_2[0], use_underline = False) | 170 | preferences_changed(True) |
429 | 171 | button.zim_path = PATHS_2[0] | 171 | rename_check('new name', new_paths[0], {new_paths[0]:'new name'}, {new_paths[0]:'new name'}) |
430 | 172 | Bar.on_preferences_changed({'save':True, 'add_bookmarks_to_beginning':False}) | 172 | Bar.rename_bookmark(button) |
431 | 173 | Bar._reload_bar() | 173 | rename_check(new_paths[0], new_paths[0], {}, {}) |
432 | 174 | 174 | ||
433 | 175 | def rename_check(label, path, paths_names, path_names_uistate): | 175 | # Check delete with renaming. |
434 | 176 | self.assertTrue(button.get_label() == label) | 176 | preferences_changed(True) |
435 | 177 | self.assertTrue(button.zim_path == path) | 177 | paths_names_copy = dict(new_path_names) |
436 | 178 | self.assertTrue(Bar.paths_names == paths_names) | 178 | Bar.paths_names = dict(new_path_names) |
437 | 179 | self.assertTrue(uistate['bookmarks_names'] == path_names_uistate) | 179 | for key in new_path_names: |
438 | 180 | 180 | Bar.delete(key) | |
439 | 181 | rename_check(PATHS_2[0], PATHS_2[0], {}, {}) | 181 | del paths_names_copy[key] |
440 | 182 | Clipboard.set_text('new name') | 182 | self.assertEqual(Bar.paths_names, paths_names_copy) |
441 | 183 | Bar.rename_bookmark(button) | 183 | self.assertEqual(self.uistate['bookmarks_names'], Bar.paths_names) |
442 | 184 | rename_check('new name', PATHS_2[0], {PATHS_2[0]:'new name'}, {PATHS_2[0]:'new name'}) | 184 | |
443 | 185 | Bar.on_preferences_changed({'save':False, 'add_bookmarks_to_beginning':False}) | 185 | # Check delete all with renaming. |
444 | 186 | rename_check('new name', PATHS_2[0], {PATHS_2[0]:'new name'}, {}) | 186 | Bar.paths_names = dict(new_path_names) |
445 | 187 | Bar.on_preferences_changed({'save':True, 'add_bookmarks_to_beginning':False}) | 187 | Bar.delete_all() |
446 | 188 | rename_check('new name', PATHS_2[0], {PATHS_2[0]:'new name'}, {PATHS_2[0]:'new name'}) | 188 | self.assertEqual(Bar.paths_names, {}) |
447 | 189 | Bar.rename_bookmark(button) | 189 | self.assertEqual(self.uistate['bookmarks_names'], {}) |
448 | 190 | rename_check(PATHS_2[0], PATHS_2[0], {}, {}) | 190 | |
449 | 191 | 191 | # Check change bookmark with renaming. | |
450 | 192 | # Check delete with renaming. | 192 | new_path_names = {new_paths[0]:'11', new_paths[1]:'22', new_paths[2]:'33'} |
451 | 193 | Bar.on_preferences_changed({'save':True, 'add_bookmarks_to_beginning':False}) | 193 | |
452 | 194 | paths_names_copy = dict(PATHS_NAMES_2) | 194 | Bar.paths = list(new_paths) |
453 | 195 | Bar.paths_names = dict(PATHS_NAMES_2) | 195 | Bar.paths_names = dict(new_path_names) |
454 | 196 | for key in PATHS_NAMES_2: | 196 | paths_names_copy = dict(new_path_names) |
455 | 197 | Bar.delete(key) | 197 | _name = paths_names_copy.pop(new_paths[0]) |
456 | 198 | del paths_names_copy[key] | 198 | paths_names_copy['new path'] = _name |
457 | 199 | self.assertTrue(Bar.paths_names == paths_names_copy) | 199 | Bar.change_bookmark(new_paths[0], 'new path') |
458 | 200 | self.assertTrue(uistate['bookmarks_names'] == Bar.paths_names) | 200 | self.assertEqual(Bar.paths_names, paths_names_copy) |
459 | 201 | 201 | self.assertEqual(Bar.paths, ['new path'] + list(new_paths[1:])) | |
460 | 202 | # Check delete all with renaming. | 202 | |
461 | 203 | Bar.paths_names = dict(PATHS_NAMES_2) | 203 | |
462 | 204 | Bar.delete_all() | 204 | def testPreferences(self): |
463 | 205 | self.assertTrue(Bar.paths_names == {}) | 205 | '''Check preferences: full/short page names, save option, |
464 | 206 | self.assertTrue(uistate['bookmarks_names'] == {}) | 206 | max number of bookmarks.''' |
465 | 207 | 207 | ||
466 | 208 | # Check change bookmark with renaming. | 208 | # Check short page names. |
467 | 209 | Bar.paths = list(PATHS_2) | 209 | Bar = BookmarkBar(self.ui, self.uistate, get_page_func = lambda: '') |
468 | 210 | Bar.paths_names = dict(PATHS_NAMES_2) | 210 | self.uistate['show_full_page_name'] = False |
469 | 211 | paths_names_copy = dict(PATHS_NAMES_2) | 211 | for path in self.PATHS: |
470 | 212 | paths_names_copy.pop(PATHS_2[0], None) | 212 | Bar._add_new(path) |
471 | 213 | Bar.change_bookmark(PATHS_2[0], 'new path') | 213 | self.assertEqual(Bar.paths, list(self.PATHS)) |
472 | 214 | self.assertTrue(Bar.paths_names == paths_names_copy) | 214 | for i, button in enumerate(Bar.container.get_children()[2:]): |
473 | 215 | self.assertTrue(Bar.paths == ['new path'] + list(PATHS_2[1:])) | 215 | self.assertEqual(self.PATHS[i], button.zim_path) |
474 | 216 | 216 | self.assertEqual(Path(self.PATHS[i]).basename, button.get_label()) | |
475 | 217 | # Check that paths and paths_names didn't change in the process. | 217 | |
476 | 218 | self.assertTrue(PATHS_2 == ('1','2','3','4','5')) | 218 | # Show full page names. |
477 | 219 | self.assertTrue(PATHS_NAMES_2 == {PATHS_2[0]:'11', PATHS_2[1]:'22', PATHS_2[2]:'33'}) | 219 | Bar.toggle_show_full_page_name() |
478 | 220 | 220 | self.assertEqual(Bar.paths, list(self.PATHS)) | |
479 | 221 | 221 | for i, button in enumerate(Bar.container.get_children()[2:]): | |
480 | 222 | class MockUI(tests.MockObject): | 222 | self.assertEqual(self.PATHS[i], button.zim_path) |
481 | 223 | page = None | 223 | self.assertEqual(self.PATHS[i], button.get_label()) |
482 | 224 | notebook = None | 224 | |
483 | 225 | 225 | # Check save option. | |
484 | 226 | self.uistate['bookmarks'] = list(self.PATHS) | ||
485 | 227 | self.uistate['bookmarks_names'] = dict(self.PATHS_NAMES) | ||
486 | 228 | Bar = BookmarkBar(self.ui, self.uistate, get_page_func = lambda: '') | ||
487 | 229 | self.assertEqual(Bar.paths, list(self.PATHS)) | ||
488 | 230 | self.assertEqual(Bar.paths_names, self.PATHS_NAMES) | ||
489 | 231 | |||
490 | 232 | self.uistate['bookmarks'] = [] | ||
491 | 233 | self.uistate['bookmarks_names'] = {} | ||
492 | 234 | Bar = BookmarkBar(self.ui, self.uistate, get_page_func = lambda: '') | ||
493 | 235 | self.assertEqual(Bar.paths, []) | ||
494 | 236 | self.assertEqual(Bar.paths_names, {}) | ||
495 | 237 | |||
496 | 238 | # Get pages to check max number of bookmarks. | ||
497 | 239 | pagelist = set(self.index.list_pages(None)) | ||
498 | 240 | _enhanced_pagelist = set() | ||
499 | 241 | for page in pagelist: | ||
500 | 242 | _enhanced_pagelist.update( set(self.index.list_pages(page)) ) | ||
501 | 243 | if len(_enhanced_pagelist) > 20: | ||
502 | 244 | break | ||
503 | 245 | pagelist.update(_enhanced_pagelist) | ||
504 | 246 | pagelist = [a.name for a in pagelist if a.exists()] | ||
505 | 247 | self.assertTrue(len(pagelist) > 20) | ||
506 | 248 | |||
507 | 249 | def preferences_changed(save, max_b): | ||
508 | 250 | Bar.on_preferences_changed({ | ||
509 | 251 | 'save': save, | ||
510 | 252 | 'add_bookmarks_to_beginning': False, | ||
511 | 253 | 'max_bookmarks': max_b}) | ||
512 | 254 | |||
513 | 255 | # Check that more than max bookmarks can be loaded at start. | ||
514 | 256 | self.uistate['bookmarks'] = pagelist | ||
515 | 257 | Bar = BookmarkBar(self.ui, self.uistate, get_page_func = lambda: '') | ||
516 | 258 | self.assertEqual(pagelist, Bar.paths) | ||
517 | 259 | preferences_changed(True, 5) | ||
518 | 260 | self.assertEqual(pagelist, Bar.paths) | ||
519 | 261 | self.assertEqual(pagelist, self.uistate['bookmarks']) | ||
520 | 262 | |||
521 | 263 | # Set maximum number of bookmarks. | ||
522 | 264 | self.uistate['bookmarks'] = [] | ||
523 | 265 | Bar = BookmarkBar(self.ui, self.uistate, get_page_func = lambda: '') | ||
524 | 266 | for max_bookmarks in (5, 10, 15, 20): | ||
525 | 267 | preferences_changed(False, max_bookmarks) | ||
526 | 268 | for page in pagelist: | ||
527 | 269 | Bar._add_new(page) | ||
528 | 270 | self.assertEqual(len(Bar.paths), max_bookmarks) | ||
529 | 271 | self.assertEqual(Bar.paths, pagelist[:max_bookmarks]) | ||
530 | 272 | Bar.delete_all() | ||
531 | 273 | |||
532 | 274 | # Check 'save' option in preferences. | ||
533 | 275 | for i, path in enumerate(self.PATHS): | ||
534 | 276 | preferences_changed(False, 15) | ||
535 | 277 | Bar._add_new(path) | ||
536 | 278 | self.assertEqual(self.uistate['bookmarks'], []) | ||
537 | 279 | preferences_changed(True, 15) | ||
538 | 280 | self.assertEqual(self.uistate['bookmarks'], list(self.PATHS[:i+1])) | ||
539 | 281 | self.assertEqual(self.uistate['bookmarks'], list(self.PATHS)) | ||
540 | 282 | |||
541 | 283 | |||
542 | 284 | class MockUI(tests.MockObject): | ||
543 | 285 | page = None | ||
544 | 286 | notebook = None | ||
545 | 287 | |||
546 | 226 | 288 | ||
547 | === modified file 'zim/plugins/bookmarksbar.py' | |||
548 | --- zim/plugins/bookmarksbar.py 2015-10-08 18:31:40 +0000 | |||
549 | +++ zim/plugins/bookmarksbar.py 2016-09-28 12:26:36 +0000 | |||
550 | @@ -1,6 +1,7 @@ | |||
551 | 1 | # -*- coding: utf-8 -*- | 1 | # -*- coding: utf-8 -*- |
552 | 2 | 2 | ||
554 | 3 | # Copyright 2015 Pavel_M <plprgt@gmail.com>. | 3 | # Copyright 2015-2016 Pavel_M <plprgt@gmail.com>, |
555 | 4 | # released under the GNU GPL version 3. | ||
556 | 4 | # This plugin is for Zim program by Jaap Karssenberg <jaap.karssenberg@gmail.com>. | 5 | # This plugin is for Zim program by Jaap Karssenberg <jaap.karssenberg@gmail.com>. |
557 | 5 | # | 6 | # |
558 | 6 | # This plugin uses an icon from Tango Desktop Project (http://tango.freedesktop.org/) | 7 | # This plugin uses an icon from Tango Desktop Project (http://tango.freedesktop.org/) |
559 | @@ -21,9 +22,6 @@ | |||
560 | 21 | import logging | 22 | import logging |
561 | 22 | logger = logging.getLogger('zim.plugins.bookmarksbar') | 23 | logger = logging.getLogger('zim.plugins.bookmarksbar') |
562 | 23 | 24 | ||
563 | 24 | # Constant for max number of bookmarks in the bar. | ||
564 | 25 | MAX_BOOKMARKS = 15 | ||
565 | 26 | |||
566 | 27 | # Keyboard shortcut constants. | 25 | # Keyboard shortcut constants. |
567 | 28 | BM_TOGGLE_BAR_KEY ='F4' | 26 | BM_TOGGLE_BAR_KEY ='F4' |
568 | 29 | BM_ADD_BOOKMARK_KEY ='<alt>1' | 27 | BM_ADD_BOOKMARK_KEY ='<alt>1' |
569 | @@ -40,6 +38,7 @@ | |||
570 | 40 | 38 | ||
571 | 41 | plugin_preferences = ( | 39 | plugin_preferences = ( |
572 | 42 | # key, type, label, default | 40 | # key, type, label, default |
573 | 41 | ('max_bookmarks', 'int', _('Maximum number of bookmarks'), 15, (5, 20)), # T: plugin preference | ||
574 | 43 | ('save', 'bool', _('Save bookmarks'), True), # T: preferences option | 42 | ('save', 'bool', _('Save bookmarks'), True), # T: preferences option |
575 | 44 | ('add_bookmarks_to_beginning', 'bool', _('Add new bookmarks to the beginning of the bar'), False), # T: preferences option | 43 | ('add_bookmarks_to_beginning', 'bool', _('Add new bookmarks to the beginning of the bar'), False), # T: preferences option |
576 | 45 | ) | 44 | ) |
577 | @@ -70,7 +69,7 @@ | |||
578 | 70 | def __init__(self, plugin, window): | 69 | def __init__(self, plugin, window): |
579 | 71 | WindowExtension.__init__(self, plugin, window) | 70 | WindowExtension.__init__(self, plugin, window) |
580 | 72 | self.widget = BookmarkBar(self.window.ui, self.uistate, | 71 | self.widget = BookmarkBar(self.window.ui, self.uistate, |
582 | 73 | self.window.pageview.get_page) | 72 | self.window.pageview.get_page) |
583 | 74 | self.widget.show_all() | 73 | self.widget.show_all() |
584 | 75 | 74 | ||
585 | 76 | # Add a new option to the Index popup menu. | 75 | # Add a new option to the Index popup menu. |
586 | @@ -149,10 +148,12 @@ | |||
587 | 149 | self.uistate = uistate | 148 | self.uistate = uistate |
588 | 150 | self.save_flag = False # if True save bookmarks in config | 149 | self.save_flag = False # if True save bookmarks in config |
589 | 151 | self.add_bookmarks_to_beginning = False # add new bookmarks to the end of the bar | 150 | self.add_bookmarks_to_beginning = False # add new bookmarks to the end of the bar |
590 | 151 | self.max_bookmarks = False # maximum number of bookmarks | ||
591 | 152 | self._get_page = get_page_func # function to get current page | 152 | self._get_page = get_page_func # function to get current page |
592 | 153 | 153 | ||
593 | 154 | # Create button to add new bookmarks. | 154 | # Create button to add new bookmarks. |
594 | 155 | self.plus_button = IconsButton(gtk.STOCK_ADD, gtk.STOCK_REMOVE, relief = False) | 155 | self.plus_button = IconsButton(gtk.STOCK_ADD, gtk.STOCK_REMOVE, relief = False) |
595 | 156 | self.plus_button.set_tooltip_text(_('Add bookmark/Show settings')) | ||
596 | 156 | self.plus_button.connect('clicked', lambda o: self.add_new_page()) | 157 | self.plus_button.connect('clicked', lambda o: self.add_new_page()) |
597 | 157 | self.plus_button.connect('button-release-event', self.do_plus_button_popup_menu) | 158 | self.plus_button.connect('button-release-event', self.do_plus_button_popup_menu) |
598 | 158 | self.pack_start(self.plus_button, expand = False) | 159 | self.pack_start(self.plus_button, expand = False) |
599 | @@ -164,7 +165,7 @@ | |||
600 | 164 | # Toggle between full/short page names. | 165 | # Toggle between full/short page names. |
601 | 165 | self.uistate.setdefault('show_full_page_name', False) | 166 | self.uistate.setdefault('show_full_page_name', False) |
602 | 166 | 167 | ||
604 | 167 | # Save path to use later in Cut/Paste menu. | 168 | # Save path to use later in Copy/Paste menu. |
605 | 168 | self._saved_bookmark = None | 169 | self._saved_bookmark = None |
606 | 169 | 170 | ||
607 | 170 | self.paths = [] # list of bookmarks as string objects | 171 | self.paths = [] # list of bookmarks as string objects |
608 | @@ -173,7 +174,8 @@ | |||
609 | 173 | # Add pages from config to the bar. | 174 | # Add pages from config to the bar. |
610 | 174 | for path in self.uistate['bookmarks']: | 175 | for path in self.uistate['bookmarks']: |
611 | 175 | page = self.ui.notebook.get_page(Path(path)) | 176 | page = self.ui.notebook.get_page(Path(path)) |
613 | 176 | self.add_new_page(page, reload_bar = False) | 177 | if page.exists() and (page.name not in self.paths): |
614 | 178 | self.paths.append(page.name) | ||
615 | 177 | 179 | ||
616 | 178 | self.paths_names = {} # dict of changed names of bookmarks | 180 | self.paths_names = {} # dict of changed names of bookmarks |
617 | 179 | self.uistate.setdefault('bookmarks_names', {}) | 181 | self.uistate.setdefault('bookmarks_names', {}) |
618 | @@ -189,46 +191,46 @@ | |||
619 | 189 | except: | 191 | except: |
620 | 190 | logger.error('BookmarksBar: Error while loading path_names.') | 192 | logger.error('BookmarksBar: Error while loading path_names.') |
621 | 191 | 193 | ||
623 | 192 | self._reload_bar() | 194 | # Look for new pages to mark corresponding bookmarks in the bar. |
624 | 195 | self.connectto(self.ui, 'open-page', self.on_open_page) | ||
625 | 193 | 196 | ||
626 | 194 | # Delete a bookmark if a page is deleted. | 197 | # Delete a bookmark if a page is deleted. |
627 | 195 | self.connectto(self.ui.notebook.index, 'page-deleted', | 198 | self.connectto(self.ui.notebook.index, 'page-deleted', |
628 | 196 | lambda obj, path: self.delete(path.name)) | 199 | lambda obj, path: self.delete(path.name)) |
629 | 197 | 200 | ||
631 | 198 | def add_new_page(self, page = None, reload_bar = True): | 201 | def on_open_page(self, ui, page, path): |
632 | 202 | '''If a page is present as a bookmark than select it.''' | ||
633 | 203 | pagename = page.name | ||
634 | 204 | for button in self.container.get_children()[2:]: | ||
635 | 205 | if button.zim_path == pagename: | ||
636 | 206 | button.set_active(True) | ||
637 | 207 | else: | ||
638 | 208 | button.set_active(False) | ||
639 | 209 | |||
640 | 210 | def add_new_page(self, page = None): | ||
641 | 199 | ''' | 211 | ''' |
642 | 200 | Add new page as bookmark to the bar. | 212 | Add new page as bookmark to the bar. |
643 | 201 | :param page: L{Page}, if None takes currently opened page, | 213 | :param page: L{Page}, if None takes currently opened page, |
644 | 202 | :reload_bar: if True reload the bar. | ||
645 | 203 | ''' | 214 | ''' |
646 | 204 | if not page: | 215 | if not page: |
647 | 205 | page = self._get_page() | 216 | page = self._get_page() |
648 | 206 | 217 | ||
649 | 207 | if page.exists(): | 218 | if page.exists(): |
653 | 208 | return self._add_new(page.name, self.add_bookmarks_to_beginning, reload_bar) | 219 | return self._add_new(page.name, self.add_bookmarks_to_beginning) |
654 | 209 | 220 | ||
655 | 210 | def _add_new(self, path, add_bookmarks_to_beginning = False, reload_bar = True): | 221 | def _add_new(self, path, add_bookmarks_to_beginning = False): |
656 | 211 | '''Add bookmark to the bar. | 222 | '''Add bookmark to the bar. |
657 | 212 | :param path: path as a string object | 223 | :param path: path as a string object |
658 | 213 | :param add_bookmarks_to_beginning: bool, | 224 | :param add_bookmarks_to_beginning: bool, |
659 | 214 | add new bookmarks to the beginning of the bar, | 225 | add new bookmarks to the beginning of the bar, |
660 | 215 | :reload_bar: if True reload the bar. | ||
661 | 216 | ''' | 226 | ''' |
662 | 217 | if path in self.paths: | 227 | if path in self.paths: |
663 | 218 | logger.debug('BookmarksBar: path is already in the bar.') | 228 | logger.debug('BookmarksBar: path is already in the bar.') |
673 | 219 | 229 | self.plus_button.blink() | |
665 | 220 | # Temporary change icon for plus_button to show | ||
666 | 221 | # that bookmark is already in the bar. | ||
667 | 222 | def _change_icon(): | ||
668 | 223 | '''Function to be called only once.''' | ||
669 | 224 | self.plus_button.change_state() | ||
670 | 225 | return False | ||
671 | 226 | self.plus_button.change_state() | ||
672 | 227 | gobject.timeout_add(300, _change_icon) | ||
674 | 228 | return False | 230 | return False |
675 | 229 | 231 | ||
676 | 230 | # Limit max number of bookmarks. | 232 | # Limit max number of bookmarks. |
678 | 231 | if len(self.paths) >= MAX_BOOKMARKS: | 233 | if self.max_bookmarks and (len(self.paths) >= self.max_bookmarks): |
679 | 232 | logger.debug('BookmarksBar: max number of bookmarks is achieved.') | 234 | logger.debug('BookmarksBar: max number of bookmarks is achieved.') |
680 | 233 | return False | 235 | return False |
681 | 234 | 236 | ||
682 | @@ -238,7 +240,7 @@ | |||
683 | 238 | else: | 240 | else: |
684 | 239 | self.paths.append(path) | 241 | self.paths.append(path) |
685 | 240 | 242 | ||
687 | 241 | if reload_bar: self._reload_bar() | 243 | self._reload_bar() |
688 | 242 | 244 | ||
689 | 243 | def delete(self, path): | 245 | def delete(self, path): |
690 | 244 | ''' | 246 | ''' |
691 | @@ -284,8 +286,13 @@ | |||
692 | 284 | 286 | ||
693 | 285 | if new_path and (new_path not in self.paths) and (new_path != old_path): | 287 | if new_path and (new_path not in self.paths) and (new_path != old_path): |
694 | 286 | self.paths[self.paths.index(old_path)] = new_path | 288 | self.paths[self.paths.index(old_path)] = new_path |
696 | 287 | self.paths_names.pop(old_path, None) | 289 | name = self.paths_names.pop(old_path, None) |
697 | 290 | if name: | ||
698 | 291 | self.paths_names[new_path] = name | ||
699 | 292 | |||
700 | 288 | self._reload_bar() | 293 | self._reload_bar() |
701 | 294 | else: | ||
702 | 295 | self.plus_button.blink() | ||
703 | 289 | 296 | ||
704 | 290 | def move_bookmark(self, first, second, direction): | 297 | def move_bookmark(self, first, second, direction): |
705 | 291 | ''' | 298 | ''' |
706 | @@ -350,7 +357,7 @@ | |||
707 | 350 | def do_bookmarks_popup_menu(self, button, event): | 357 | def do_bookmarks_popup_menu(self, button, event): |
708 | 351 | '''Handler for button-release-event, triggers popup menu for bookmarks.''' | 358 | '''Handler for button-release-event, triggers popup menu for bookmarks.''' |
709 | 352 | if event.button != 3: | 359 | if event.button != 3: |
711 | 353 | return False | 360 | return False |
712 | 354 | 361 | ||
713 | 355 | path = button.zim_path | 362 | path = button.zim_path |
714 | 356 | 363 | ||
715 | @@ -371,13 +378,12 @@ | |||
716 | 371 | (_('Remove'), lambda o: self.delete(path)), # T: menu item | 378 | (_('Remove'), lambda o: self.delete(path)), # T: menu item |
717 | 372 | (_('Remove All'), lambda o: self.delete_all(True)), # T: menu item | 379 | (_('Remove All'), lambda o: self.delete_all(True)), # T: menu item |
718 | 373 | ('separator', ''), | 380 | ('separator', ''), |
719 | 374 | (_('Open in New Window'), lambda o: self.ui.open_new_window(Path(path))), # T: menu item | ||
720 | 375 | ('separator', ''), | ||
721 | 376 | ('gtk-copy', lambda o: set_save_bookmark(path)), | 381 | ('gtk-copy', lambda o: set_save_bookmark(path)), |
722 | 377 | ('gtk-paste', lambda o: self.move_bookmark(self._saved_bookmark, path, direction)), | 382 | ('gtk-paste', lambda o: self.move_bookmark(self._saved_bookmark, path, direction)), |
723 | 378 | ('separator', ''), | 383 | ('separator', ''), |
724 | 384 | (_('Open in New Window'), lambda o: self.ui.open_new_window(Path(path))), # T: menu item | ||
725 | 385 | ('separator', ''), | ||
726 | 379 | (rename_button_text, lambda o: self.rename_bookmark(button)), | 386 | (rename_button_text, lambda o: self.rename_bookmark(button)), |
727 | 380 | ('separator', ''), | ||
728 | 381 | (_('Set to Current Page'), lambda o: self.change_bookmark(path)) ) # T: menu item | 387 | (_('Set to Current Page'), lambda o: self.change_bookmark(path)) ) # T: menu item |
729 | 382 | 388 | ||
730 | 383 | for name, func in main_menu_items: | 389 | for name, func in main_menu_items: |
731 | @@ -412,6 +418,10 @@ | |||
732 | 412 | self.uistate['bookmarks'] = [] | 418 | self.uistate['bookmarks'] = [] |
733 | 413 | self.uistate['bookmarks_names'] = {} | 419 | self.uistate['bookmarks_names'] = {} |
734 | 414 | 420 | ||
735 | 421 | if self.max_bookmarks != preferences['max_bookmarks']: | ||
736 | 422 | self.max_bookmarks = preferences['max_bookmarks'] | ||
737 | 423 | self._reload_bar() # to update plus_button | ||
738 | 424 | |||
739 | 415 | def _get_short_page_name(self, name): | 425 | def _get_short_page_name(self, name): |
740 | 416 | ''' | 426 | ''' |
741 | 417 | Function to return short name for the page. | 427 | Function to return short name for the page. |
742 | @@ -430,6 +440,12 @@ | |||
743 | 430 | for button in self.container.get_children()[2:]: | 440 | for button in self.container.get_children()[2:]: |
744 | 431 | self.container.remove(button) | 441 | self.container.remove(button) |
745 | 432 | 442 | ||
746 | 443 | page = self._get_page() | ||
747 | 444 | if page: | ||
748 | 445 | pagename = page.name | ||
749 | 446 | else: | ||
750 | 447 | pagename = None | ||
751 | 448 | |||
752 | 433 | for path in self.paths: | 449 | for path in self.paths: |
753 | 434 | if path in self.paths_names: | 450 | if path in self.paths_names: |
754 | 435 | name = self.paths_names[path] | 451 | name = self.paths_names[path] |
755 | @@ -437,16 +453,19 @@ | |||
756 | 437 | name = self._get_short_page_name(path) | 453 | name = self._get_short_page_name(path) |
757 | 438 | else: | 454 | else: |
758 | 439 | name = path | 455 | name = path |
760 | 440 | button = gtk.Button(label = name, use_underline = False) | 456 | button = gtk.ToggleButton(label = name, use_underline = False) |
761 | 441 | button.set_tooltip_text(path) | 457 | button.set_tooltip_text(path) |
762 | 442 | button.zim_path = path | 458 | button.zim_path = path |
763 | 459 | if path == pagename: | ||
764 | 460 | button.set_active(True) | ||
765 | 461 | |||
766 | 443 | button.connect('clicked', self.on_bookmark_clicked) | 462 | button.connect('clicked', self.on_bookmark_clicked) |
767 | 444 | button.connect('button-release-event', self.do_bookmarks_popup_menu) | 463 | button.connect('button-release-event', self.do_bookmarks_popup_menu) |
768 | 445 | button.show() | 464 | button.show() |
769 | 446 | self.container.add(button) | 465 | self.container.add(button) |
770 | 447 | 466 | ||
771 | 448 | # 'Disable' plus_button if max bookmarks is reached. | 467 | # 'Disable' plus_button if max bookmarks is reached. |
773 | 449 | if len(self.paths) >= MAX_BOOKMARKS: | 468 | if self.max_bookmarks and (len(self.paths) >= self.max_bookmarks): |
774 | 450 | self.plus_button.change_state(False) | 469 | self.plus_button.change_state(False) |
775 | 451 | else: | 470 | else: |
776 | 452 | self.plus_button.change_state(True) | 471 | self.plus_button.change_state(True) |
777 | @@ -501,3 +520,13 @@ | |||
778 | 501 | self._enabled_state = not self._enabled_state | 520 | self._enabled_state = not self._enabled_state |
779 | 502 | self.show_all() | 521 | self.show_all() |
780 | 503 | 522 | ||
781 | 523 | def blink(self): | ||
782 | 524 | '''Quickly change an icon to show | ||
783 | 525 | that bookmark can't be added/changed.''' | ||
784 | 526 | |||
785 | 527 | def change_icon(): | ||
786 | 528 | '''Function to be called only once.''' | ||
787 | 529 | self.change_state() | ||
788 | 530 | return False | ||
789 | 531 | self.change_state() | ||
790 | 532 | gobject.timeout_add(300, change_icon) |