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
29 from threadlistmodel import ThreadListModel
35 GLADE_DIR = os.path.join(os.path.dirname(os.path.realpath(__file__)),
37 GLADE_FILENAME = "board_window.glade"
39 def open_board(bbs, board):
41 raise ValueError, "parameter must not be empty"
43 key = "/" + bbs + "/" + board
44 window = windowlist.get_window(key)
50 win_wrap = WinWrap(bbs, board)
51 widget = win_wrap.widget_tree.get_widget("board_window")
52 windowlist.window_created(key, widget)
57 def __init__(self, bbs, board):
61 glade_path = os.path.join(GLADE_DIR, GLADE_FILENAME)
62 self.widget_tree = gtk.glade.XML(glade_path)
64 self.window = self.widget_tree.get_widget("board_window")
66 title = brdlist.get(bbs, board, "name")
68 self.window.set_title(title)
70 self.treeview = self.widget_tree.get_widget("treeview")
71 self.treeview.set_model(ThreadListModel())
73 self.popupmenu = self.widget_tree.get_widget("popup_menu")
75 renderer = gtk.CellRendererText()
77 self.treeviewcolumn = {}
78 for i in range(1, len(ThreadListModel.column_names)):
79 column_name = ThreadListModel.column_names[i]
80 self.treeviewcolumn[column_name] = gtk.TreeViewColumn(
81 column_name, renderer)
82 self.treeviewcolumn[column_name].set_resizable(True)
83 self.treeviewcolumn[column_name].set_reorderable(True)
84 self.treeviewcolumn[column_name].set_clickable(True)
85 self.treeviewcolumn[column_name].set_cell_data_func(
86 renderer, self.on_cell_data, column_name)
87 self.treeviewcolumn[column_name].connect(
88 "clicked", self.on_column_clicked, column_name)
89 self.treeview.append_column(self.treeviewcolumn[column_name])
91 self.treeviewcolumn["lastModified"].set_cell_data_func(
92 renderer, self.on_data_lastmodified)
94 sigdic = {"on_board_window_destroy": self.on_board_window_destroy,
95 "on_quit_activate": self.on_quit_activate,
96 "on_load_local_activate": self.on_load_local_activate,
97 "on_get_remote_activate": self.on_get_remote_activate,
98 "on_treeview_row_activated":
99 lambda w,p,v: self.on_open_thread(w),
100 "on_treeview_button_press_event":
101 self.on_treeview_button_press_event,
103 self.on_close_activate,
104 "on_show_board_list_activate":
105 self.on_show_board_list_activate,
106 "on_popup_menu_open_activate": self.on_open_thread}
107 self.widget_tree.signal_autoconnect(sigdic)
109 t = board_data.LoadLocal(self.bbs, self.board, self.update_datastore)
112 def updated_thread_highlight(self, column, cell, model, iter):
114 def is_updated_thread():
115 res = model.get_value(
116 iter, ThreadListModel.column_names.index("res"))
117 linecount = model.get_value(
118 iter, ThreadListModel.column_names.index("lineCount"))
119 return res != 0 and linecount != 0 and res > linecount
121 if is_updated_thread():
122 cell.set_property("weight", 800)
124 cell.set_property("weight", 400)
126 def on_cell_data(self, column, cell, model, iter, column_name):
127 self.updated_thread_highlight(column, cell, model, iter)
128 column_num = ThreadListModel.column_names.index(column_name)
129 value = model.get_value(iter, column_num)
130 if model.get_column_type(column_num) == gobject.TYPE_INT:
132 cell.set_property("text", "")
134 cell.set_property("text", str(value))
136 cell.set_property("text", value)
138 def on_data_lastmodified(self, column, cell, model, iter, user_data=None):
139 self.updated_thread_highlight(column, cell, model, iter)
140 lastmod = model.get_value(
141 iter, ThreadListModel.column_names.index("lastModified"))
143 cell.set_property("text", "")
145 cell.set_property("text", time.strftime(
146 "%Y/%m/%d(%a) %H:%M:%S", time.localtime(lastmod)))
148 def on_board_window_destroy(self, widget):
151 def on_quit_activate(self, widget):
154 def on_close_activate(self, widget):
155 self.window.destroy()
157 def on_show_board_list_activate(self, widget):
158 brdlist_window.open_brdlist(self.bbs)
160 def on_load_local_activate(self, widget):
161 t = board_data.LoadLocal(self.bbs, self.board, self.update_datastore)
164 def on_get_remote_activate(self, widget):
165 t = board_data.GetRemote(self.bbs, self.board, self.update_datastore)
168 def on_column_clicked(self, treeviewcolumn, column_name):
169 model = self.treeview.get_model()
171 model.sort(column_name)
172 self.reset_sort_indicator()
174 def reset_sort_indicator(self):
175 model = self.treeview.get_model()
177 sort_column_name, sort_reverse = model.get_sort()
178 for name,column in self.treeviewcolumn.iteritems():
179 column.set_sort_indicator(False)
180 if sort_column_name != "num" or sort_reverse:
181 self.treeviewcolumn[sort_column_name].set_sort_indicator(True)
183 self.treeviewcolumn[sort_column_name].set_sort_order(
186 self.treeviewcolumn[sort_column_name].set_sort_order(
189 def on_open_thread(self, widget):
190 treeselection = self.treeview.get_selection()
191 model, iter = treeselection.get_selected()
195 thread = model.get_value(iter, ThreadListModel.column_names.index("id"))
196 title = model.get_value(
197 iter, ThreadListModel.column_names.index("title"))
198 print thread + ':"' + title + '"', "activated"
200 thread_window.open_thread(self.bbs, self.board, thread)
202 def on_treeview_button_press_event(self, widget, event):
203 if event.button == 3:
207 pthinfo = widget.get_path_at_pos(x, y)
208 if pthinfo is not None:
209 path, col, cellx, celly = pthinfo
211 widget.set_cursor(path, col, 0)
212 self.popupmenu.popup(None, None, None, event.button, time)
215 def update_datastore(self, datalist, lastmod):
216 print "reflesh datastore"
219 lastmod = misc.httpdate_to_secs(lastmod)
223 time_start = time.time()
225 for id, dic in datalist.iteritems():
229 if lastmod == 0 or dic["num"] == 0:
234 # avoid the Last-Modified time of subject.txt and
235 # the build time of thread is equal (zero division)
236 dur = lastmod - start
238 dic["average"] = 999999
240 dic["average"] = int(res * 60 * 60 * 24 / dur)
243 httpdate = dic["lastModified"]
245 secs = misc.httpdate_to_secs(httpdate)
246 dic["lastModified"] = secs
248 dic["lastModified"] = 0
250 list_list.append(dic)
252 model = self.treeview.get_model()
253 model.set_list(list_list)
255 # redraw visible area after set list to model
256 self.treeview.queue_draw()
258 self.reset_sort_indicator()
261 time_end = time.time()
262 print time_end - time_start