OSDN Git Service

Window managing auxiliary is added. It shows only same board windows. (#16374)
[fukui-no-namari/fukui-no-namari.git] / src / FukuiNoNamari / bookmark_window.py
1 # Copyright (C) 2006 by Aiwota Programmer
2 # aiwotaprog@tetteke.tk
3 #
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
18 import pygtk
19 pygtk.require("2.0")
20 import gtk
21 import gtk.glade
22 import traceback
23 import gnome
24 import os.path
25 import itertools
26 import sys
27
28 from bookmark_core import Bookmark
29 from bookmark_editwindow import BookmarkEditWindow
30 from BbsType import bbs_type_exception
31 import bookmark_list
32 import uri_opener
33 import winwrapbase
34 import session
35 import config
36
37 GLADE_FILENAME = "bookmark_window.glade"
38
39 def open():
40     winwrap = session.get_window(WinWrap.uri)
41     if winwrap:
42         # already opened
43         winwrap.window.present()
44     else:
45         winwrap = WinWrap()
46
47
48 class WinWrap(winwrapbase.WinWrapBase):
49
50     uri = "namari://bookmarks-manager"
51
52     def __init__(self):
53         self.bookmarklist = bookmark_list.bookmark_list
54         self.bookmarklist.set_view(self)
55
56         glade_path = os.path.join(config.glade_dir, GLADE_FILENAME)
57         self.widget_tree = gtk.glade.XML(glade_path)
58         self.window = self.widget_tree.get_widget("window_bookmark")
59         self.window.connect("destroy", self.on_destroy)
60         self.treeview_categories = self.widget_tree.get_widget(
61             "treeview_categories")
62         self.treeview_categories.connect(
63             "cursor-changed", self.on_treeview_categories_cursor_changed)
64         self.treeview_bookmarks = self.widget_tree.get_widget(
65             "treeview_bookmarks")
66         self.treeview_bookmarks.connect(
67             "row-activated", self.on_treeview_bookmarks_row_activated)
68         self.treeview_bookmarks.connect(
69             "cursor-changed", self.on_treeview_bookmarks_cursor_changed)
70         self.treeview_bookmarks.connect(
71             "button-press-event",
72             self.on_treeview_bookmarks_button_press_event)
73         self.statusbar = self.widget_tree.get_widget("statusbar")
74         self.statusbar.push(
75             self.statusbar.get_context_id("bookmarks"), "Ready.")
76         self.menu_bookmark = self.widget_tree.get_widget("menu_bookmark")
77
78         self.widget_tree.signal_autoconnect(self)
79
80         renderer = gtk.CellRendererText()
81
82         treeviewcolumn = gtk.TreeViewColumn("category", renderer, text=0)
83         treeviewcolumn.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED)
84         self.treeview_categories.append_column(treeviewcolumn)
85
86         treeviewcolumn = gtk.TreeViewColumn("bookmark", renderer, text=0)
87         treeviewcolumn.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED)
88         self.treeview_bookmarks.append_column(treeviewcolumn)
89
90         self.treeview_categories.set_fixed_height_mode(True)
91         self.treeview_bookmarks.set_fixed_height_mode(True)
92
93         self.treeview_categories.set_model(gtk.ListStore(str, int))
94         self.treeview_bookmarks.set_model(gtk.ListStore(str, object))
95
96         self.current_category = ""
97         self.current_category_type = 0
98
99         self.update_categories()
100
101         self.created()
102
103     def on_destroy(self, widget):
104         self.destroyed()
105
106     def get_uri(self):
107         return self.uri
108
109     def destroyed(self):
110         self.bookmarklist.set_view(None)
111         winwrapbase.WinWrapBase.destroyed(self)
112
113     def show(self):
114         self.window.deiconify()
115
116     def hide(self):
117         self.window.iconify()
118
119     def destroy(self):
120         self.window.destroy()
121
122     def on_treeview_categories_cursor_changed(self, widget):
123         selection = self.treeview_categories.get_selection()
124         model, iter = selection.get_selected()
125         if model and iter:
126             category = model.get_value(iter, 0)
127             category_type = model.get_value(iter, 1)
128             if category and category != self.current_category:
129                 self.update_bookmarks(category, category_type)
130
131     def _get_selected_bookmark(self):
132         selection = self.treeview_bookmarks.get_selection()
133         model, iter = selection.get_selected()
134         if model and iter:
135             return model.get_value(iter, 1)
136
137     def on_treeview_bookmarks_cursor_changed(self, widget):
138         bookmark = self._get_selected_bookmark()
139         if bookmark:
140             self.statusbar.pop(self.statusbar.get_context_id("bookmarks"))
141             self.statusbar.push(
142                 self.statusbar.get_context_id("bookmarks"), bookmark.uri)
143
144     def on_treeview_bookmarks_row_activated(self, widget, path, column):
145         model = self.treeview_bookmarks.get_model()
146         if model:
147             iter = model.get_iter(path)
148             if iter:
149                 self._open_bookmark(model.get_value(iter, 1))
150
151     def on_treeview_bookmarks_button_press_event(self, widget, event):
152         if event.button == 3:
153             x = int(event.x)
154             y = int(event.y)
155             time = event.time
156             pthinfo = widget.get_path_at_pos(x, y)
157             if pthinfo is not None:
158                 path, col, cellx, celly = pthinfo
159                 widget.grab_focus()
160                 widget.set_cursor(path, col, 0)
161                 self.menu_bookmark.popup(None, None, None, event.button, time)
162             return True
163
164     def on_update_bbsmenu(self, widget):
165         self.bookmarklist.update_bbsmenu()
166         self.update_categories()
167
168     def _open_bookmark(self, bookmark):
169         try:
170             uri_opener.open_uri(bookmark.uri)
171         except bbs_type_exception.BbsTypeError:
172             self._open_bookmark_with_web_browser(bookmark)
173
174     def _open_bookmark_with_web_browser(self, bookmark):
175         gnome.url_show(bookmark.uri)
176
177     def update_bookmarks(self, category, category_type):
178         if category_type == 0:
179             bookmarks = self.bookmarklist.get_bookmark_list_in_category(
180                 category)
181         else:
182             bookmarks = self.bookmarklist.get_non_category_bookmark()
183
184         model = self.treeview_bookmarks.get_model()
185         model.clear()
186
187         for bookmark in bookmarks:
188             model.append([bookmark.name, bookmark])
189         self.treeview_bookmarks.set_model(model)
190
191         self.current_category = category
192         self.current_category_type = category_type
193
194         category_model = self.treeview_categories.get_model()
195         if category_model:
196             for row in category_model:
197                 text = row.model.get_value(row.iter, 0)
198                 cat_type = row.model.get_value(row.iter, 1)
199                 if text == category and cat_type == category_type:
200                     selection = self.treeview_categories.get_selection()
201                     selection.select_iter(row.iter)
202                     self.treeview_categories.scroll_to_cell(row.path)
203                     break
204
205         self.statusbar.pop(self.statusbar.get_context_id("bookmarks"))
206
207     def update_categories(self):
208         model = self.treeview_categories.get_model()
209         model.clear()
210         categories = self.bookmarklist.get_category_list()
211         model.append(["Non Category", 1])
212         for category in categories:
213             model.append([category, 0])
214         self.treeview_categories.set_model(model)
215
216         if self.current_category:
217             self.update_bookmarks(self.current_category,
218                                   self.current_category_type)
219         else:
220             self.update_bookmarks("", 0)
221
222     def on_editbookmark_complete(self, old_bookmark, new_bookmark):
223         changed = False
224         changed = changed or old_bookmark.name != new_bookmark.name
225         changed = changed or old_bookmark.uri != new_bookmark.uri
226         changed = changed or \
227                   len(old_bookmark.categories) != len(new_bookmark.categories)
228         if not changed:
229             for cat, kat in itertools.izip(old_bookmark.categories,
230                                            new_bookmark.categories):
231                 if cat not in new_bookmark.categories:
232                     changed = True
233                     break
234                 if kat not in old_bookmark.categories:
235                     changed = True
236                     break
237         if changed:
238             old_bookmark.name = new_bookmark.name
239             old_bookmark.uri = new_bookmark.uri
240             old_bookmark.categories = new_bookmark.categories
241             self.update_categories()
242             self.bookmarklist.save()
243         else:
244             print "not changed"
245         
246     def on_newbookmark_complete(self, bookmark):
247         self.bookmarklist.add_new_bookmark(bookmark)
248         self.update_categories()
249
250     # menu handler
251
252     def on_new_activate(self, widget):
253         BookmarkEditWindow(self.on_newbookmark_complete)
254
255     def on_open_activate(self, widget):
256         bookmark = self._get_selected_bookmark()
257         if bookmark:
258             self._open_bookmark(bookmark)
259
260     def on_open_web_activate(self, widget):
261         bookmark = self._get_selected_bookmark()
262         if bookmark:
263             self._open_bookmark_with_web_browser(bookmark)
264
265     def on_properties_activate(self, widget):
266         bookmark = self._get_selected_bookmark()
267         if bookmark:
268             BookmarkEditWindow(
269                 lambda new_bookmark: \
270                 self.on_editbookmark_complete(bookmark, new_bookmark),
271                 bookmark)
272
273     def on_save_activate(self, widget):
274         self.bookmarklist.save()
275
276     def on_close_activate(self, widget):
277         self.destroy()
278
279     def on_quit_activate(self, widget):
280         session.main_quit()
281
282     def on_delete_activate(self, widget):
283         bookmark = self._get_selected_bookmark()
284         if bookmark:
285             self.bookmarklist.delete_bookmark(bookmark)
286             self.update_categories()
287
288     def on_refresh_bbsmenu_activate(self, widget):
289         self.bookmarklist.update_bbsmenu()
290         self.update_categories()