OSDN Git Service

1f45821fa80fc6474b8b92a1a92df366070c5f0f
[fukui-no-namari/fukui-no-namari.git] / src / Hage1 / board_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 os
23 import time
24 import gobject
25
26 import board_data
27 import thread_window
28 import misc
29
30 GLADE_DIR = os.path.join(os.path.dirname(os.path.realpath(__file__)),
31                          "..", "data")
32 GLADE_FILENAME = "board_window.glade"
33
34
35 class WinWrap:
36
37     def __init__(self, bbs, board):
38         self.bbs = bbs
39         self.board = board
40
41         glade_path = os.path.join(GLADE_DIR, GLADE_FILENAME)
42         self.mainwin = gtk.glade.XML(glade_path)
43
44         self.treeview = self.mainwin.get_widget("treeview")
45         self.treeview.set_model(ThreadListModel())
46         self.treeview.set_rules_hint(True)
47
48         self.popupmenu = self.mainwin.get_widget("popup_menu")
49
50         renderer = gtk.CellRendererText()
51
52         self.treeviewcolumn = {}
53         for i in range(1, len(ThreadListModel.column_names)):
54             column_name = ThreadListModel.column_names[i]
55             self.treeviewcolumn[column_name] = gtk.TreeViewColumn(
56                 column_name, renderer)
57             self.treeviewcolumn[column_name].set_resizable(True)
58             self.treeviewcolumn[column_name].set_reorderable(True)
59             self.treeviewcolumn[column_name].set_clickable(True)
60             self.treeviewcolumn[column_name].set_cell_data_func(
61                 renderer, self.on_cell_data, column_name)
62             self.treeviewcolumn[column_name].connect(
63                 "clicked", self.on_column_clicked, column_name)
64             self.treeview.append_column(self.treeviewcolumn[column_name])
65
66         self.treeviewcolumn["lastModified"].set_cell_data_func(
67             renderer, self.on_data_lastmodified)
68
69         sigdic = {"on_board_window_destroy": self.on_board_window_destroy,
70                   "on_quit_activate": self.on_quit_activate,
71                   "on_load_local_activate": self.on_load_local_activate,
72                   "on_get_remote_activate": self.on_get_remote_activate,
73                   "on_treeview_row_activated":
74                   lambda w,p,v: self.on_open_thread(w),
75                   "on_treeview_button_press_event":
76                   self.on_treeview_button_press_event,
77                   "on_popup_menu_open_activate": self.on_open_thread}
78         self.mainwin.signal_autoconnect(sigdic)
79
80     def updated_thread_highlight(self, column, cell, model, iter):
81
82         def is_updated_thread():
83             res = model.get_value(
84                 iter, ThreadListModel.column_names.index("res"))
85             linecount = model.get_value(
86                 iter, ThreadListModel.column_names.index("lineCount"))
87             return res != 0 and linecount != 0 and res > linecount
88
89         if is_updated_thread():
90             cell.set_property("weight", 800)
91         else:
92             cell.set_property("weight", 400)
93
94     def on_cell_data(self, column, cell, model, iter, column_name):
95         self.updated_thread_highlight(column, cell, model, iter)
96         column_num = ThreadListModel.column_names.index(column_name)
97         value = model.get_value(iter, column_num)
98         if model.get_column_type(column_num) == gobject.TYPE_INT:
99             if value == 0:
100                 cell.set_property("text", "")
101             else:
102                 cell.set_property("text", str(value))
103         else:
104             cell.set_property("text", value)
105
106     def on_data_lastmodified(self, column, cell, model, iter, user_data=None):
107         self.updated_thread_highlight(column, cell, model, iter)
108         lastmod = model.get_value(
109             iter, ThreadListModel.column_names.index("lastModified"))
110         if lastmod == 0:
111             cell.set_property("text", "")
112         else:
113             cell.set_property("text", time.strftime(
114                 "%Y/%m/%d(%a) %H:%M:%S", time.localtime(lastmod)))
115
116     def on_board_window_destroy(self, widget):
117         gtk.main_quit()
118
119     def on_quit_activate(self, widget):
120         gtk.main_quit()
121
122     def on_load_local_activate(self, widget):
123         t = board_data.LoadLocal(self.bbs, self.board, self.update_datastore)
124         t.start()
125
126     def on_get_remote_activate(self, widget):
127         t = board_data.GetRemote(self.bbs, self.board, self.update_datastore)
128         t.start()
129
130     def on_column_clicked(self, treeviewcolumn, column_name):
131         model = self.treeview.get_model()
132         if model:
133             model.sort(column_name)
134             self.reset_sort_indicator()
135
136     def reset_sort_indicator(self):
137         model = self.treeview.get_model()
138         if model:
139             sort_column_name, sort_reverse = model.get_sort()
140             for name,column in self.treeviewcolumn.iteritems():
141                 column.set_sort_indicator(False)
142             if sort_column_name != "num" or sort_reverse:
143                 self.treeviewcolumn[sort_column_name].set_sort_indicator(True)
144                 if sort_reverse:
145                     self.treeviewcolumn[sort_column_name].set_sort_order(
146                         gtk.SORT_DESCENDING)
147                 else:
148                     self.treeviewcolumn[sort_column_name].set_sort_order(
149                         gtk.SORT_ASCENDING)
150         
151     def on_open_thread(self, widget):
152         treeselection = self.treeview.get_selection()
153         model, iter = treeselection.get_selected()
154         if not iter:
155             return
156
157         thread = model.get_value(iter, ThreadListModel.column_names.index("id"))
158         title = model.get_value(
159             iter, ThreadListModel.column_names.index("title"))
160         print thread + ':"' + title + '"', "activated"
161
162         thread_window.WinWrap(self.bbs, self.board, thread)
163
164     def on_treeview_button_press_event(self, widget, event):
165         if event.button == 3:
166             x = int(event.x)
167             y = int(event.y)
168             time = event.time
169             pthinfo = widget.get_path_at_pos(x, y)
170             if pthinfo is not None:
171                 path, col, cellx, celly = pthinfo
172                 widget.grab_focus()
173                 widget.set_cursor(path, col, 0)
174                 self.popupmenu.popup(None, None, None, event.button, time)
175             return 1
176
177     def update_datastore(self, datalist, lastmod):
178         print "reflesh datastore"
179
180         try:
181             lastmod = misc.httpdate_to_secs(lastmod)
182         except:
183             lastmod = 0
184
185         time_start = time.time()
186         list_list = []
187         for id, dic in datalist.iteritems():
188             dic["id"] = id
189
190             # average
191             if lastmod == 0 or dic["num"] == 0:
192                 dic["average"] = 0
193             else:
194                 res = dic["res"]
195                 start = int(id)
196                 dic["average"] = int(res * 60 * 60 * 24 / (lastmod - start))
197
198             # lastModified
199             httpdate = dic["lastModified"]
200             try:
201                 secs = misc.httpdate_to_secs(httpdate)
202                 dic["lastModified"] = secs
203             except ValueError:
204                 dic["lastModified"] = 0
205             
206             list_list.append(dic)
207
208         model = self.treeview.get_model()
209         model.set_list(list_list)
210         self.reset_sort_indicator()
211
212         print "end"
213         time_end = time.time()
214         print time_end - time_start
215
216
217 class ThreadListModel(gtk.GenericTreeModel):
218     column_types = (str, int, str, int, int, int, int)
219     column_names = ["id", "num", "title", "res", "lineCount",
220                     "lastModified", "average"]
221
222     def __init__(self, list=None, sort_column_name="num", sort_reverse=False,
223                  filter_func=None):
224         gtk.GenericTreeModel.__init__(self)
225         if list:
226             self.original_list = list[:]
227         else:
228             self.original_list = []
229
230         self.sort_column_name = sort_column_name
231         self.sort_reverse = sort_reverse
232         self.filter_func = filter_func
233
234         self.do_filter()
235         self.do_sort(self.sort_column_name, self.sort_reverse)
236
237     def refilter(self):
238         before_size = len(self.list)
239         self.do_filter()
240         after_size = len(self.list)
241         self.do_sort(self.sort_column_name, self.sort_reverse)
242         self.list_modified(before_size, after_size)
243         
244     def set_list(self, newlist):
245         self.original_list = newlist[:]
246         self.refilter()
247
248     def set_filter_func(func):
249         self.filter_func = func
250
251     def build_order_dict(self):
252         # key: thread id, value: index in self.list
253         self.order_dict = dict(
254             [(item["id"], index) for index, item in enumerate(self.list)])
255
256     def list_modified(self, before_size, after_size):
257         if before_size > after_size:
258             print "remove", before_size-after_size, "threads"
259             for i in range(before_size - after_size):
260                 self.row_deleted(after_size)
261         elif after_size > before_size:
262             print "insert", after_size-before_size, "threads"
263             for i in range(before_size, after_size):
264                 self.row_inserted(i, self.get_iter(i))
265         elif after_size != 0:
266             self.row_changed(0, self.get_iter(0))
267
268     def do_filter(self):
269         if not self.filter_func:
270             self.list = self.original_list[:]
271             return
272
273         self.list = []
274         for item in self.original_list:
275             if self.filter_func(self, item):
276                 self.list.append(item)
277
278     def compare(self, v1, v2, reverse):
279         if not reverse:
280             if v1 == 0 and v2 == 0:
281                 return 0
282             elif v1 == 0:
283                 return 1
284             elif v2 == 0:
285                 return -1
286         return v1 - v2
287
288     def get_sort(self):
289         return self.sort_column_name, self.sort_reverse
290
291     def do_sort(self, column_name, reverse):
292         if self.column_types[self.column_names.index(column_name)] == str:
293             self.list.sort(None, lambda dic: dic[column_name], reverse)
294         else:
295             h = lambda x, y: self.compare(x, y, reverse)
296             self.list.sort(h, lambda dic: dic[column_name], reverse)
297         self.build_order_dict()
298
299     def sort(self, column_name):
300         old_order_dict = self.order_dict
301
302         if column_name == self.sort_column_name:
303             # sort reverse
304             self.sort_reverse = not self.sort_reverse
305         else:
306             self.sort_column_name = column_name
307             self.sort_reverse = False
308
309         self.do_sort(self.sort_column_name, self.sort_reverse)
310
311         neworder = [old_order_dict[item["id"]] for item in self.list]
312         self.rows_reordered(None, None, neworder)
313
314     def on_get_flags(self):
315         return gtk.TREE_MODEL_LIST_ONLY
316
317     def on_get_n_columns(self):
318         return len(self.column_types)
319
320     def on_get_column_type(self, n):
321         return self.column_types[n]
322
323     def on_get_iter(self, path):
324         try:
325             return self.list[path[0]]
326         except IndexError:
327             return None
328
329     def on_get_path(self, rowref):
330         return self.order_dict[rowref["id"]]
331
332     def on_get_value(self, rowref, column):
333         return rowref[self.column_names[column]]
334
335     def on_iter_next(self, rowref):
336         try:
337             i = self.order_dict[rowref["id"]] + 1
338             return self.list[i]
339         except IndexError:
340             return None
341
342     def on_iter_children(self, rowref):
343         if rowref:
344             return None
345         return self.list[0]
346
347     def on_iter_has_child(self, rowref):
348         return False
349
350     def on_iter_n_children(self, rowref):
351         if rowref:
352             return 0
353         return len(self.list)
354
355     def on_iter_nth_child(self, rowref, n):
356         if rowref:
357             return None
358         try:
359             return self.list[n]
360         except IndexError:
361             return None
362
363     def on_iter_parent(self, child):
364         return None