OSDN Git Service

Destroy popupmenu and treeview columns.
[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 destroy(self):
114         self.window.destroy()
115
116     def on_treeview_categories_cursor_changed(self, widget):
117         selection = self.treeview_categories.get_selection()
118         model, iter = selection.get_selected()
119         if model and iter:
120             category = model.get_value(iter, 0)
121             category_type = model.get_value(iter, 1)
122             if category and category != self.current_category:
123                 self.update_bookmarks(category, category_type)
124
125     def _get_selected_bookmark(self):
126         selection = self.treeview_bookmarks.get_selection()
127         model, iter = selection.get_selected()
128         if model and iter:
129             return model.get_value(iter, 1)
130
131     def on_treeview_bookmarks_cursor_changed(self, widget):
132         bookmark = self._get_selected_bookmark()
133         if bookmark:
134             self.statusbar.pop(self.statusbar.get_context_id("bookmarks"))
135             self.statusbar.push(
136                 self.statusbar.get_context_id("bookmarks"), bookmark.uri)
137
138     def on_treeview_bookmarks_row_activated(self, widget, path, column):
139         model = self.treeview_bookmarks.get_model()
140         if model:
141             iter = model.get_iter(path)
142             if iter:
143                 self._open_bookmark(model.get_value(iter, 1))
144
145     def on_treeview_bookmarks_button_press_event(self, widget, event):
146         if event.button == 3:
147             x = int(event.x)
148             y = int(event.y)
149             time = event.time
150             pthinfo = widget.get_path_at_pos(x, y)
151             if pthinfo is not None:
152                 path, col, cellx, celly = pthinfo
153                 widget.grab_focus()
154                 widget.set_cursor(path, col, 0)
155                 self.menu_bookmark.popup(None, None, None, event.button, time)
156             return True
157
158     def on_update_bbsmenu(self, widget):
159         self.bookmarklist.update_bbsmenu()
160         self.update_categories()
161
162     def _open_bookmark(self, bookmark):
163         try:
164             uri_opener.open_uri(bookmark.uri)
165         except bbs_type_exception.BbsTypeError:
166             self._open_bookmark_with_web_browser(bookmark)
167
168     def _open_bookmark_with_web_browser(self, bookmark):
169         gnome.url_show(bookmark.uri)
170
171     def update_bookmarks(self, category, category_type):
172         if category_type == 0:
173             bookmarks = self.bookmarklist.get_bookmark_list_in_category(
174                 category)
175         else:
176             bookmarks = self.bookmarklist.get_non_category_bookmark()
177
178         model = self.treeview_bookmarks.get_model()
179         model.clear()
180
181         for bookmark in bookmarks:
182             model.append([bookmark.name, bookmark])
183         self.treeview_bookmarks.set_model(model)
184
185         self.current_category = category
186         self.current_category_type = category_type
187
188         category_model = self.treeview_categories.get_model()
189         if category_model:
190             for row in category_model:
191                 text = row.model.get_value(row.iter, 0)
192                 cat_type = row.model.get_value(row.iter, 1)
193                 if text == category and cat_type == category_type:
194                     selection = self.treeview_categories.get_selection()
195                     selection.select_iter(row.iter)
196                     self.treeview_categories.scroll_to_cell(row.path)
197                     break
198
199         self.statusbar.pop(self.statusbar.get_context_id("bookmarks"))
200
201     def update_categories(self):
202         model = self.treeview_categories.get_model()
203         model.clear()
204         categories = self.bookmarklist.get_category_list()
205         model.append(["Non Category", 1])
206         for category in categories:
207             model.append([category, 0])
208         self.treeview_categories.set_model(model)
209
210         if self.current_category:
211             self.update_bookmarks(self.current_category,
212                                   self.current_category_type)
213         else:
214             self.update_bookmarks("", 0)
215
216     def on_editbookmark_complete(self, old_bookmark, new_bookmark):
217         changed = False
218         changed = changed or old_bookmark.name != new_bookmark.name
219         changed = changed or old_bookmark.uri != new_bookmark.uri
220         changed = changed or \
221                   len(old_bookmark.categories) != len(new_bookmark.categories)
222         if not changed:
223             for cat, kat in itertools.izip(old_bookmark.categories,
224                                            new_bookmark.categories):
225                 if cat not in new_bookmark.categories:
226                     changed = True
227                     break
228                 if kat not in old_bookmark.categories:
229                     changed = True
230                     break
231         if changed:
232             old_bookmark.name = new_bookmark.name
233             old_bookmark.uri = new_bookmark.uri
234             old_bookmark.categories = new_bookmark.categories
235             self.update_categories()
236             self.bookmarklist.save()
237         else:
238             print "not changed"
239         
240     def on_newbookmark_complete(self, bookmark):
241         self.bookmarklist.add_new_bookmark(bookmark)
242         self.update_categories()
243
244     # menu handler
245
246     def on_new_activate(self, widget):
247         BookmarkEditWindow(self.on_newbookmark_complete)
248
249     def on_open_activate(self, widget):
250         bookmark = self._get_selected_bookmark()
251         if bookmark:
252             self._open_bookmark(bookmark)
253
254     def on_open_web_activate(self, widget):
255         bookmark = self._get_selected_bookmark()
256         if bookmark:
257             self._open_bookmark_with_web_browser(bookmark)
258
259     def on_properties_activate(self, widget):
260         bookmark = self._get_selected_bookmark()
261         if bookmark:
262             BookmarkEditWindow(
263                 lambda new_bookmark: \
264                 self.on_editbookmark_complete(bookmark, new_bookmark),
265                 bookmark)
266
267     def on_save_activate(self, widget):
268         self.bookmarklist.save()
269
270     def on_close_activate(self, widget):
271         self.destroy()
272
273     def on_quit_activate(self, widget):
274         session.main_quit()
275
276     def on_delete_activate(self, widget):
277         bookmark = self._get_selected_bookmark()
278         if bookmark:
279             self.bookmarklist.delete_bookmark(bookmark)
280             self.update_categories()
281
282     def on_refresh_bbsmenu_activate(self, widget):
283         self.bookmarklist.update_bbsmenu()
284         self.update_categories()