OSDN Git Service

Do not display 0
[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_rules_hint(True)
46
47         self.popupmenu = self.mainwin.get_widget("popup_menu")
48
49         renderer = gtk.CellRendererText()
50
51         self.treeviewcolumn = {}
52         for i in range(1, len(ThreadListModel.column_names)):
53             column_name = ThreadListModel.column_names[i]
54             self.treeviewcolumn[column_name] = gtk.TreeViewColumn(
55                 column_name, renderer)
56             self.treeviewcolumn[column_name].set_resizable(True)
57             self.treeviewcolumn[column_name].set_reorderable(True)
58             self.treeviewcolumn[column_name].set_clickable(True)
59             self.treeviewcolumn[column_name].set_cell_data_func(
60                 renderer, self.on_cell_data, column_name)
61             self.treeviewcolumn[column_name].connect(
62                 "clicked", self.on_column_clicked, column_name)
63             self.treeview.append_column(self.treeviewcolumn[column_name])
64
65         self.treeviewcolumn["lastModified"].set_cell_data_func(
66             renderer, self.on_data_lastmodified)
67
68         sigdic = {"on_board_window_destroy": self.on_board_window_destroy,
69                   "on_quit_activate": self.on_quit_activate,
70                   "on_load_local_activate": self.on_load_local_activate,
71                   "on_get_remote_activate": self.on_get_remote_activate,
72                   "on_treeview_row_activated":
73                   lambda w,p,v: self.on_open_thread(w),
74                   "on_treeview_button_press_event":
75                   self.on_treeview_button_press_event,
76                   "on_popup_menu_open_activate": self.on_open_thread}
77         self.mainwin.signal_autoconnect(sigdic)
78
79     def on_cell_data(self, column, cell, model, iter, column_name):
80         column_num = ThreadListModel.column_names.index(column_name)
81         value = model.get_value(iter, column_num)
82         if model.get_column_type(column_num) == gobject.TYPE_INT:
83             if value == 0:
84                 cell.set_property("text", "")
85             else:
86                 cell.set_property("text", str(value))
87         else:
88             cell.set_property("text", value)
89
90     def on_data_lastmodified(self, column, cell, model, iter, user_data=None):
91         lastmod = model.get_value(
92             iter, ThreadListModel.column_names.index("lastModified"))
93         if lastmod == 0:
94             cell.set_property("text", "")
95         else:
96             cell.set_property("text", time.strftime(
97                 "%Y/%m/%d(%a) %H:%M:%S", time.localtime(lastmod)))
98
99     def on_board_window_destroy(self, widget):
100         gtk.main_quit()
101
102     def on_quit_activate(self, widget):
103         gtk.main_quit()
104
105     def on_load_local_activate(self, widget):
106         t = board_data.LoadLocal(self.bbs, self.board, self.update_datastore)
107         t.start()
108
109     def on_get_remote_activate(self, widget):
110         t = board_data.GetRemote(self.bbs, self.board, self.update_datastore)
111         t.start()
112
113     def on_column_clicked(self, treeviewcolumn, column_name):
114         model = self.treeview.get_model()
115         if model:
116             model.sort(column_name)
117             self.reset_sort_indicator()
118
119     def reset_sort_indicator(self):
120         model = self.treeview.get_model()
121         if model:
122             sort_column_name, sort_reverse = model.get_sort()
123             for name,column in self.treeviewcolumn.iteritems():
124                 column.set_sort_indicator(False)
125             if sort_column_name != "num" or sort_reverse:
126                 self.treeviewcolumn[sort_column_name].set_sort_indicator(True)
127                 if sort_reverse:
128                     self.treeviewcolumn[sort_column_name].set_sort_order(
129                         gtk.SORT_DESCENDING)
130                 else:
131                     self.treeviewcolumn[sort_column_name].set_sort_order(
132                         gtk.SORT_ASCENDING)
133         
134     def on_open_thread(self, widget):
135         treeselection = self.treeview.get_selection()
136         model, iter = treeselection.get_selected()
137         if not iter:
138             return
139
140         thread = model.get_value(iter, ThreadListModel.column_names.index("id"))
141         title = model.get_value(
142             iter, ThreadListModel.column_names.index("title"))
143         print thread + ':"' + title + '"', "activated"
144
145         thread_window.WinWrap(self.bbs, self.board, thread)
146
147     def on_treeview_button_press_event(self, widget, event):
148         if event.button == 3:
149             x = int(event.x)
150             y = int(event.y)
151             time = event.time
152             pthinfo = widget.get_path_at_pos(x, y)
153             if pthinfo is not None:
154                 path, col, cellx, celly = pthinfo
155                 widget.grab_focus()
156                 widget.set_cursor(path, col, 0)
157                 self.popupmenu.popup(None, None, None, event.button, time)
158             return 1
159
160     def update_datastore(self, datalist, lastmod):
161         print "reflesh datastore"
162
163         try:
164             lastmod = misc.httpdate_to_secs(lastmod)
165         except:
166             lastmod = 0
167
168         time_start = time.time()
169         list_list = []
170         for id, dic in datalist.iteritems():
171             dic["id"] = id
172
173             # average
174             if lastmod == 0 or dic["num"] == 0:
175                 dic["average"] = 0
176             else:
177                 res = dic["res"]
178                 start = int(id)
179                 dic["average"] = int(res * 60 * 60 * 24 / (lastmod - start))
180
181             # lastModified
182             httpdate = dic["lastModified"]
183             try:
184                 secs = misc.httpdate_to_secs(httpdate)
185                 dic["lastModified"] = secs
186             except ValueError:
187                 dic["lastModified"] = 0
188             
189             list_list.append(dic)
190
191         model = self.treeview.get_model()
192         if model:
193             sort_column_name, sort_reverse = model.get_sort()
194             model = ThreadListModel(list_list, sort_column_name, sort_reverse)
195         else:
196             model = ThreadListModel(list_list)
197         self.treeview.set_model(model)
198
199         self.reset_sort_indicator()
200
201         print "end"
202         time_end = time.time()
203         print time_end - time_start
204
205
206 class ThreadListModel(gtk.GenericTreeModel):
207     column_types = (str, int, str, int, int, int, int)
208     column_names = ["id", "num", "title", "res", "lineCount",
209                     "lastModified", "average"]
210
211     def __init__(self, list, sort_column_name="num", sort_reverse=False):
212         gtk.GenericTreeModel.__init__(self)
213         self.list = list
214         self.sort_column_name = sort_column_name
215         self.sort_reverse = sort_reverse
216         self.do_sort(self.sort_column_name, self.sort_reverse)
217
218     def build_order_dict(self):
219         # key: thread id, value: index in self.list
220         self.order_dict = dict(
221             [(item["id"], index) for index, item in enumerate(self.list)])
222  
223     def compare(self, v1, v2, reverse):
224         if not reverse:
225             if v1 == 0 and v2 == 0:
226                 return 0
227             elif v1 == 0:
228                 return 1
229             elif v2 == 0:
230                 return -1
231         return v1 - v2
232
233     def get_sort(self):
234         return self.sort_column_name, self.sort_reverse
235
236     def do_sort(self, column_name, reverse):
237         if self.column_types[self.column_names.index(column_name)] == str:
238             self.list.sort(None, lambda dic: dic[column_name], reverse)
239         else:
240             h = lambda x, y: self.compare(x, y, reverse)
241             self.list.sort(h, lambda dic: dic[column_name], reverse)
242         self.build_order_dict()
243
244     def sort(self, column_name):
245         old_order_dict = self.order_dict
246
247         if column_name == self.sort_column_name:
248             # sort reverse
249             self.sort_reverse = not self.sort_reverse
250         else:
251             self.sort_column_name = column_name
252             self.sort_reverse = False
253
254         self.do_sort(self.sort_column_name, self.sort_reverse)
255
256         neworder = [old_order_dict[item["id"]] for item in self.list]
257         self.rows_reordered(None, None, neworder)
258
259     def on_get_flags(self):
260         return gtk.TREE_MODEL_LIST_ONLY
261
262     def on_get_n_columns(self):
263         return len(self.column_types)
264
265     def on_get_column_type(self, n):
266         return self.column_types[n]
267
268     def on_get_iter(self, path):
269         return self.list[path[0]]
270
271     def on_get_path(self, rowref):
272         return self.order_dict[rowref["id"]]
273
274     def on_get_value(self, rowref, column):
275         return rowref[self.column_names[column]]
276
277     def on_iter_next(self, rowref):
278         try:
279             i = self.order_dict[rowref["id"]] + 1
280             return self.list[i]
281         except IndexError:
282             return None
283
284     def on_iter_children(self, rowref):
285         if rowref:
286             return None
287         return self.list[0]
288
289     def on_iter_has_child(self, rowref):
290         return False
291
292     def on_iter_n_children(self, rowref):
293         if rowref:
294             return 0
295         return len(self.list)
296
297     def on_iter_nth_child(self, rowref, n):
298         if rowref:
299             return None
300         try:
301             return self.list[n]
302         except IndexError:
303             return None
304
305     def on_iter_parent(self, child):
306         return None