1 # Copyright (C) 2006 by Aiwota Programmer
2 # aiwotaprog@tetteke.tk
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.
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.
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
31 from threadlistmodel import ThreadListModel
32 from BbsType import bbs_type_judge_uri
36 from misc import ThreadInvoker
38 GLADE_DIR = os.path.join(os.path.dirname(os.path.realpath(__file__)),
40 GLADE_FILENAME = "board_window.glade"
42 def open_board(uri, update=False):
44 raise ValueError, "parameter must not be empty"
46 winwrap = session.get_window(uri)
49 winwrap.window.present()
53 winwrap = WinWrap(uri)
57 class WinWrap(winwrapbase.WinWrapBase, board_data.BoardData):
59 def __init__(self, uri):
61 self.bbs_type = bbs_type_judge_uri.get_type(uri)
62 board_data.BoardData.__init__(self, self.bbs_type)
63 self.bbs = self.bbs_type.bbs_type
64 self.host = self.bbs_type.host
65 self.board = self.bbs_type.board
66 self.uri = self.bbs_type.uri
68 glade_path = os.path.join(GLADE_DIR, GLADE_FILENAME)
69 self.widget_tree = gtk.glade.XML(glade_path)
71 self.window = self.widget_tree.get_widget("board_window")
73 self.window.set_title(self.uri)
75 self.treeview = self.widget_tree.get_widget("treeview")
76 self.treeview.set_model(ThreadListModel())
78 self.popupmenu = self.widget_tree.get_widget("popup_menu")
79 self.toolbar = self.widget_tree.get_widget("toolbar")
80 self.toolbar.unset_style()
81 self.statusbar = self.widget_tree.get_widget("appbar")
82 self.filterbar = self.widget_tree.get_widget("bonobodockitem_filterbar")
83 self.entry_filterbar = self.widget_tree.get_widget("entry_filterbar")
85 renderer = gtk.CellRendererText()
87 self.treeviewcolumn = {}
88 for i in range(1, len(ThreadListModel.column_names)):
89 column_name = ThreadListModel.column_names[i]
90 self.treeviewcolumn[column_name] = gtk.TreeViewColumn(
91 column_name, renderer)
92 self.treeviewcolumn[column_name].set_resizable(True)
93 self.treeviewcolumn[column_name].set_reorderable(True)
94 self.treeviewcolumn[column_name].set_clickable(True)
95 self.treeviewcolumn[column_name].set_cell_data_func(
96 renderer, self.on_cell_data, column_name)
97 self.treeviewcolumn[column_name].connect(
98 "clicked", self.on_column_clicked, column_name)
99 self.treeviewcolumn[column_name].set_sizing(
100 gtk.TREE_VIEW_COLUMN_FIXED)
101 self.treeviewcolumn[column_name].set_min_width(20)
102 self.treeviewcolumn[column_name].set_fixed_width(
103 ThreadListModel.column_width[i])
104 self.treeview.append_column(self.treeviewcolumn[column_name])
106 self.treeviewcolumn["lastModified"].set_cell_data_func(
107 renderer, self.on_data_lastmodified)
109 self.treeview.set_fixed_height_mode(True)
111 sigdic = {"on_board_window_destroy": self.on_board_window_destroy,
112 "on_quit_activate": self.on_quit_activate,
113 "on_refresh_activate": self.on_refresh_activate,
114 "on_treeview_row_activated":
115 lambda w,p,v: self.on_open_thread(w),
116 "on_treeview_button_press_event":
117 self.on_treeview_button_press_event,
119 self.on_close_activate,
120 "on_toolbar_activate": self.on_toolbar_activate,
121 "on_statusbar_activate": self.on_statusbar_activate,
122 "on_board_window_delete_event":
123 self.on_board_window_delete_event,
124 "on_entry_filterbar_activate": self.on_entry_filterbar_activate,
125 "on_filter_activate": self.on_filter_activate,
126 "on_toolbutton_filterbar_close_clicked":
127 self.on_toolbutton_filterbar_close_clicked,
128 "on_popup_menu_open_activate": self.on_open_thread}
129 self.widget_tree.signal_autoconnect(sigdic)
136 def set_status(self, text):
137 self.statusbar.set_status(text)
141 self.window.destroy()
144 return self.bbs_type.get_uri_base()
146 def on_toolbar_activate(self, widget):
147 if self.toolbar.parent.get_property("visible"):
148 self.toolbar.parent.hide()
150 self.toolbar.parent.show()
152 def on_statusbar_activate(self, widget):
153 if self.statusbar.get_property("visible"):
154 self.statusbar.hide()
156 self.statusbar.show()
158 def on_filter_activate(self, widget):
159 self.filterbar.show()
160 self.entry_filterbar.grab_focus()
162 def on_entry_filterbar_activate(self, widget):
163 text = widget.get_text()
165 def func(model, item):
167 item["title"].index(text)
173 model = self.treeview.get_model()
176 model.set_filter_func(func)
178 model.set_filter_func(None)
181 def on_toolbutton_filterbar_close_clicked(self, widget):
182 self.filterbar.hide()
184 def updated_thread_highlight(self, column, cell, model, iter):
186 def is_updated_thread():
187 res = model.get_value(
188 iter, ThreadListModel.column_names.index("res"))
189 linecount = model.get_value(
190 iter, ThreadListModel.column_names.index("lineCount"))
191 return res != 0 and linecount != 0 and res > linecount
193 if is_updated_thread():
194 cell.set_property("weight", 800)
196 cell.set_property("weight", 400)
198 def on_cell_data(self, column, cell, model, iter, column_name):
199 self.updated_thread_highlight(column, cell, model, iter)
200 column_num = ThreadListModel.column_names.index(column_name)
201 value = model.get_value(iter, column_num)
202 if model.get_column_type(column_num) \
203 in (gobject.TYPE_INT, gobject.TYPE_DOUBLE):
205 cell.set_property("text", "")
207 cell.set_property("text", str(value))
209 cell.set_property("text", value)
211 def on_data_lastmodified(self, column, cell, model, iter, user_data=None):
212 self.updated_thread_highlight(column, cell, model, iter)
213 lastmod = model.get_value(
214 iter, ThreadListModel.column_names.index("lastModified"))
216 cell.set_property("text", "")
218 cell.set_property("text", time.strftime(
219 "%Y/%m/%d(%a) %H:%M:%S", time.localtime(lastmod)))
221 def on_board_window_delete_event(self, widget, event):
225 def on_board_window_destroy(self, widget):
228 def on_quit_activate(self, widget):
231 def on_close_activate(self, widget):
234 def on_refresh_activate(self, widget):
237 def on_column_clicked(self, treeviewcolumn, column_name):
238 model = self.treeview.get_model()
240 model.sort(column_name)
241 self.reset_sort_indicator()
243 def reset_sort_indicator(self):
244 model = self.treeview.get_model()
246 sort_column_name, sort_reverse = model.get_sort()
247 for name,column in self.treeviewcolumn.iteritems():
248 column.set_sort_indicator(False)
249 if sort_column_name != "num" or sort_reverse:
250 self.treeviewcolumn[sort_column_name].set_sort_indicator(True)
252 self.treeviewcolumn[sort_column_name].set_sort_order(
255 self.treeviewcolumn[sort_column_name].set_sort_order(
258 def on_open_thread(self, widget):
259 treeselection = self.treeview.get_selection()
260 model, iter = treeselection.get_selected()
264 thread = model.get_value(iter, ThreadListModel.column_names.index("id"))
265 title = model.get_value(
266 iter, ThreadListModel.column_names.index("title"))
267 print thread + ':"' + title + '"', "activated"
269 res = model.get_value(iter, ThreadListModel.column_names.index("res"))
270 lineCount = model.get_value(
271 iter, ThreadListModel.column_names.index("lineCount"))
273 update = res > lineCount
275 bbs_type_for_thread = self.bbs_type.clone_with_thread(thread)
276 uri_opener.open_uri(bbs_type_for_thread.get_thread_uri(), update)
278 def on_treeview_button_press_event(self, widget, event):
279 if event.button == 3:
283 pthinfo = widget.get_path_at_pos(x, y)
284 if pthinfo is not None:
285 path, col, cellx, celly = pthinfo
287 widget.set_cursor(path, col, 0)
288 self.popupmenu.popup(None, None, None, event.button, time)
291 def update_datastore(self, datalist):
292 print "reflesh datastore"
295 for id, dic in datalist.iteritems():
299 httpdate = dic["lastModified"]
301 secs = misc.httpdate_to_secs(httpdate)
302 dic["lastModified"] = secs
304 dic["lastModified"] = 0
306 list_list.append(dic)
308 model = self.treeview.get_model()
309 model.set_list(list_list)
311 # redraw visible area after set list to model
312 self.treeview.queue_draw()
314 self.reset_sort_indicator()
318 def on_thread_idx_updated(self, thread_uri, idx_dic):
319 if not thread_uri or not idx_dic:
322 # nothing to do if thread_uri does not belong to this board.
323 bbs_type = bbs_type_judge_uri.get_type(thread_uri)
324 if bbs_type.bbs_type != self.bbs \
325 or bbs_type.board != self.board or not bbs_type.is_thread():
328 thread = bbs_type.thread
330 model = self.treeview.get_model()
332 idx_dic["id"] = thread
334 idx_dic["lastModified"] = misc.httpdate_to_secs(
335 idx_dic["lastModified"])
337 idx_dic["lastModified"] = 0
338 model.modify_row(idx_dic)
340 def load(self, update=False):
343 datalist = self.load_idxfiles()
344 self.merge_local_subjecttxt(datalist)
345 gobject.idle_add(self.update_datastore, datalist)
348 print "start get subject.txt"
349 datalist = self.load_idxfiles()
350 self.merge_remote_subjecttxt(datalist)
351 gobject.idle_add(self.update_datastore, datalist)
353 sbj_path = misc.get_board_subjecttxt_path(self.bbs_type)
354 sbj_exists = os.path.exists(sbj_path)
356 if update or not sbj_exists:
357 t = ThreadInvoker(lambda *args: -1, get_remote)
360 t = ThreadInvoker(lambda *args: -1, load_local)
365 states_path = misc.get_board_states_path(self.bbs_type)
366 dirname = os.path.dirname(states_path)
368 # save only if board dir exists.
369 if os.path.exists(dirname):
370 window_width, window_height = self.window.get_size()
371 toolbar_visible = self.toolbar.parent.get_property("visible")
372 statusbar_visible = self.statusbar.get_property("visible")
373 filterbar_visible = self.filterbar.get_property("visible")
375 columns = self.treeview.get_columns()
378 for column in columns:
379 for name, kolumn in self.treeviewcolumn.iteritems():
386 width += str(column.get_width())
388 sort_column_name, sort_reverse = \
389 self.treeview.get_model().get_sort()
390 sort_reverse = str(sort_reverse)
392 f = file(states_path, "w")
394 f.write("columns=" + order + "\n")
395 f.write("widths=" + width + "\n")
396 f.write("sort_column=" + sort_column_name + "\n")
397 f.write("sort_reverse=" + sort_reverse + "\n")
398 f.write("window_width=" + str(window_width) + "\n")
399 f.write("window_height=" + str(window_height) + "\n")
400 f.write("toolbar_visible=" + str(toolbar_visible) + "\n")
401 f.write("statusbar_visible=" + str(statusbar_visible) + "\n")
402 f.write("filterbar_visible=" + str(filterbar_visible) + "\n")
406 traceback.print_exc()
412 toolbar_visible = True
413 statusbar_visible = True
414 filterbar_visible = False
417 key_base = config.gconf_app_key_base() + "/board_states"
418 gconf_client = gconf.client_get_default()
419 width = gconf_client.get_int(key_base + "/window_width")
420 height = gconf_client.get_int(key_base + "/window_height")
421 toolbar_visible = gconf_client.get_bool(key_base + "/toolbar")
422 statusbar_visible = gconf_client.get_bool(key_base + "/statusbar")
423 filterbar_visible = gconf_client.get_bool(key_base + "/filterbar")
428 window_height = height
430 traceback.print_exc()
432 states_path = misc.get_board_states_path(self.bbs_type)
433 if os.path.exists(states_path):
434 sort_column_name = "num"
436 for line in file(states_path):
437 if line.startswith("columns="):
438 line = line[len("columns="):].rstrip("\n")
440 for name in line.split(","):
441 if name in self.treeviewcolumn:
442 column = self.treeviewcolumn[name]
443 self.treeview.move_column_after(
446 elif line.startswith("widths="):
447 line = line[len("widths="):].rstrip("\n")
448 columns = self.treeview.get_columns()
449 for i, width in enumerate(line.split(",")):
456 columns[i].set_fixed_width(width)
457 elif line.startswith("sort_column="):
458 kolumn_name = line[len("sort_column="):].rstrip("\n")
459 if kolumn_name in ThreadListModel.column_names:
460 sort_column_name = kolumn_name
461 elif line.startswith("sort_reverse="):
462 reverse = line[len("sort_reverse="):].rstrip("\n")
463 sort_reverse = reverse == "True"
464 elif line.startswith("window_height="):
465 height = window_height
468 line[len("window_height="):].rstrip("\n"))
472 window_height = height
473 elif line.startswith("window_width="):
477 line[len("window_width="):].rstrip("\n"))
482 elif line.startswith("toolbar_visible="):
483 tbar = line[len("toolbar_visible="):].rstrip("\n")
484 toolbar_visible = tbar == "True"
485 elif line.startswith("statusbar_visible="):
486 sbar = line[len("statusbar_visible="):].rstrip("\n")
487 statusbar_visible = sbar == "True"
488 elif line.startswith("filterbar_visible="):
489 fbar = line[len("filterbar_visible="):].rstrip("\n")
490 filterbar_visible = fbar == "True"
492 self.treeview.get_model().sort(
493 sort_column_name, True, sort_reverse)
495 self.window.set_default_size(window_width, window_height)
497 if not toolbar_visible:
498 gobject.idle_add(self.toolbar.parent.hide,
499 priority=gobject.PRIORITY_HIGH)
500 if not statusbar_visible:
501 gobject.idle_add(self.statusbar.hide,
502 priority=gobject.PRIORITY_HIGH)
503 if not filterbar_visible:
504 gobject.idle_add(self.filterbar.hide,
505 priority=gobject.PRIORITY_HIGH)
507 traceback.print_exc()