OSDN Git Service

Add bookmark.
[fukui-no-namari/fukui-no-namari.git] / src / FukuiNoNamari / board_window.py
index f1132bf..42097be 100644 (file)
@@ -34,6 +34,8 @@ import config
 import session
 import winwrapbase
 from misc import ThreadInvoker
+import bookmark_list
+import bookmark_window
 
 GLADE_DIR = os.path.join(os.path.dirname(os.path.realpath(__file__)),
                          "..", "data")
@@ -60,17 +62,13 @@ class WinWrap(winwrapbase.WinWrapBase, board_data.BoardData):
 
         self.bbs_type = bbs_type_judge_uri.get_type(uri)
         board_data.BoardData.__init__(self, self.bbs_type)
-        self.bbs = self.bbs_type.bbs_type
-        self.host = self.bbs_type.host
-        self.board = self.bbs_type.board
-        self.uri = self.bbs_type.uri
         
         glade_path = os.path.join(GLADE_DIR, GLADE_FILENAME)
         self.widget_tree = gtk.glade.XML(glade_path)
 
         self.window = self.widget_tree.get_widget("board_window")
 
-        self.window.set_title(self.uri)
+        self.window.set_title(self.bbs_type.uri)
 
         self.treeview = self.widget_tree.get_widget("treeview")
         self.treeview.set_model(ThreadListModel())
@@ -79,6 +77,8 @@ class WinWrap(winwrapbase.WinWrapBase, board_data.BoardData):
         self.toolbar = self.widget_tree.get_widget("toolbar")
         self.toolbar.unset_style()
         self.statusbar = self.widget_tree.get_widget("appbar")
+        self.filterbar = self.widget_tree.get_widget("bonobodockitem_filterbar")
+        self.entry_filterbar = self.widget_tree.get_widget("entry_filterbar")
 
         renderer = gtk.CellRendererText()
 
@@ -119,6 +119,16 @@ class WinWrap(winwrapbase.WinWrapBase, board_data.BoardData):
                   "on_statusbar_activate": self.on_statusbar_activate,
                   "on_board_window_delete_event":
                   self.on_board_window_delete_event,
+                  "on_entry_filterbar_activate": self.on_entry_filterbar_activate,
+                  "on_filter_activate": self.on_filter_activate,
+                  "on_toolbutton_filterbar_close_clicked":
+                  self.on_toolbutton_filterbar_close_clicked,
+                  "on_button_filterbar_clear_clicked":
+                  self.on_button_filterbar_clear_clicked,
+                  "on_delete_activate": self.on_delete_activate,
+                  "on_manage_bookmarks_activate": \
+                  self.on_manage_bookmarks_activate,
+                  "on_add_bookmark_activate": self.on_add_bookmark_activate,
                   "on_popup_menu_open_activate": self.on_open_thread}
         self.widget_tree.signal_autoconnect(sigdic)
 
@@ -149,6 +159,39 @@ class WinWrap(winwrapbase.WinWrapBase, board_data.BoardData):
         else:
             self.statusbar.show()
 
+    def on_filter_activate(self, widget):
+        self.filterbar.show()
+        self.entry_filterbar.grab_focus()
+
+    def on_entry_filterbar_activate(self, widget):
+        text = widget.get_text()
+
+        def func(model, item):
+            try:
+                item["title"].index(text)
+            except ValueError:
+                return False
+            else:
+                return True
+
+        model = self.treeview.get_model()
+        if model:
+            if text:
+                model.set_filter_func(func)
+            else:
+                model.set_filter_func(None)
+            model.refilter()
+
+    def on_toolbutton_filterbar_close_clicked(self, widget):
+        self.filterbar.hide()
+
+    def on_button_filterbar_clear_clicked(self, widget):
+        self.entry_filterbar.set_text("")
+        model = self.treeview.get_model()
+        if model:
+            model.set_filter_func(None)
+            model.refilter()
+
     def updated_thread_highlight(self, column, cell, model, iter):
 
         def is_updated_thread():
@@ -243,6 +286,39 @@ class WinWrap(winwrapbase.WinWrapBase, board_data.BoardData):
         bbs_type_for_thread = self.bbs_type.clone_with_thread(thread)
         uri_opener.open_uri(bbs_type_for_thread.get_thread_uri(), update)
 
+    def on_add_bookmark_activate(self, widget):
+        bookmark_list.bookmark_list.add_bookmark_with_edit(
+            uri=self.bbs_type.uri)
+
+    def on_manage_bookmarks_activate(self, widget):
+        bookmark_window.open()
+
+    def on_delete_activate(self, widget):
+        selection = self.treeview.get_selection()
+        model, iter = selection.get_selected()
+        if not iter:
+            return
+        thread = model.get_value(
+            iter, ThreadListModel.column_names.index("id"))
+        
+        bbs_type_for_thread = self.bbs_type.clone_with_thread(thread)
+
+        dat_path = misc.get_thread_dat_path(bbs_type_for_thread)
+        try:
+            os.remove(dat_path)
+        except OSError:
+            traceback.print_exc()
+        idx_path = misc.get_thread_idx_path(bbs_type_for_thread)
+        try:
+            os.remove(idx_path)
+        except OSError:
+            traceback.print_exc()
+        states_path = misc.get_thread_states_path(bbs_type_for_thread)
+        try:
+            os.remove(states_path)
+        except OSError:
+            traceback.print_exc()
+
     def on_treeview_button_press_event(self, widget, event):
         if event.button == 3:
             x = int(event.x)
@@ -289,8 +365,8 @@ class WinWrap(winwrapbase.WinWrapBase, board_data.BoardData):
 
         # nothing to do if thread_uri does not belong to this board.
         bbs_type = bbs_type_judge_uri.get_type(thread_uri)
-        if bbs_type.bbs_type != self.bbs \
-           or bbs_type.board != self.board or not bbs_type.is_thread():
+        if not bbs_type.is_thread() \
+               or not bbs_type.is_same_board(self.bbs_type):
             return
 
         thread = bbs_type.thread
@@ -318,8 +394,7 @@ class WinWrap(winwrapbase.WinWrapBase, board_data.BoardData):
             self.merge_remote_subjecttxt(datalist)
             gobject.idle_add(self.update_datastore, datalist)
 
-        sbj_path = misc.get_board_subjecttxt_path(
-            self.bbs_type.bbs_type, self.bbs_type.board)
+        sbj_path = misc.get_board_subjecttxt_path(self.bbs_type)
         sbj_exists = os.path.exists(sbj_path)
 
         if update or not sbj_exists:
@@ -331,8 +406,7 @@ class WinWrap(winwrapbase.WinWrapBase, board_data.BoardData):
 
     def save(self):
         try:
-            states_path = misc.get_board_states_path(
-                self.bbs_type.bbs_type, self.bbs_type.board)
+            states_path = misc.get_board_states_path(self.bbs_type)
             dirname = os.path.dirname(states_path)
 
             # save only if board dir exists.
@@ -340,6 +414,7 @@ class WinWrap(winwrapbase.WinWrapBase, board_data.BoardData):
                 window_width, window_height = self.window.get_size()
                 toolbar_visible = self.toolbar.parent.get_property("visible")
                 statusbar_visible = self.statusbar.get_property("visible")
+                filterbar_visible = self.filterbar.get_property("visible")
 
                 columns = self.treeview.get_columns()
                 order = ""
@@ -368,6 +443,7 @@ class WinWrap(winwrapbase.WinWrapBase, board_data.BoardData):
                 f.write("window_height=" + str(window_height) + "\n")
                 f.write("toolbar_visible=" + str(toolbar_visible) + "\n")
                 f.write("statusbar_visible=" + str(statusbar_visible) + "\n")
+                f.write("filterbar_visible=" + str(filterbar_visible) + "\n")
 
                 f.close()
         except:
@@ -379,16 +455,17 @@ class WinWrap(winwrapbase.WinWrapBase, board_data.BoardData):
             window_width = 600
             toolbar_visible = True
             statusbar_visible = True
+            filterbar_visible = False
 
             try:
                 key_base = config.gconf_app_key_base() + "/board_states"
                 gconf_client = gconf.client_get_default()
                 width = gconf_client.get_int(key_base + "/window_width")
                 height = gconf_client.get_int(key_base + "/window_height")
-                toolbar_visible = gconf_client.get_bool(
-                    key_base + "/toolbar")
-                statusbar_visible = gconf_client.get_bool(
-                    key_base + "/statusbar")
+                toolbar_visible = gconf_client.get_bool(key_base + "/toolbar")
+                statusbar_visible = gconf_client.get_bool(key_base + "/statusbar")
+                filterbar_visible = gconf_client.get_bool(key_base + "/filterbar")
+
                 if width != 0:
                     window_width = width
                 if height != 0:
@@ -396,8 +473,7 @@ class WinWrap(winwrapbase.WinWrapBase, board_data.BoardData):
             except:
                 traceback.print_exc()
 
-            states_path = misc.get_board_states_path(
-                self.bbs_type.bbs_type, self.bbs_type.board)
+            states_path = misc.get_board_states_path(self.bbs_type)
             if os.path.exists(states_path):
                 sort_column_name = "num"
                 sort_reverse = False
@@ -453,6 +529,9 @@ class WinWrap(winwrapbase.WinWrapBase, board_data.BoardData):
                     elif line.startswith("statusbar_visible="):
                         sbar = line[len("statusbar_visible="):].rstrip("\n")
                         statusbar_visible = sbar == "True"
+                    elif line.startswith("filterbar_visible="):
+                        fbar = line[len("filterbar_visible="):].rstrip("\n")
+                        filterbar_visible = fbar == "True"
 
                 self.treeview.get_model().sort(
                     sort_column_name, True, sort_reverse)
@@ -460,8 +539,13 @@ class WinWrap(winwrapbase.WinWrapBase, board_data.BoardData):
             self.window.set_default_size(window_width, window_height)
 
             if not toolbar_visible:
-                gobject.idle_add(self.toolbar.parent.hide)
+                gobject.idle_add(self.toolbar.parent.hide,
+                                 priority=gobject.PRIORITY_HIGH)
             if not statusbar_visible:
-                gobject.idle_add(self.statusbar.hide)
+                gobject.idle_add(self.statusbar.hide,
+                                 priority=gobject.PRIORITY_HIGH)
+            if not filterbar_visible:
+                gobject.idle_add(self.filterbar.hide,
+                                 priority=gobject.PRIORITY_HIGH)
         except:
             traceback.print_exc()