OSDN Git Service

Set rules_hint value in glade xml
[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 from threadlistmodel import ThreadListModel
30
31 GLADE_DIR = os.path.join(os.path.dirname(os.path.realpath(__file__)),
32                          "..", "data")
33 GLADE_FILENAME = "board_window.glade"
34
35
36 class WinWrap:
37
38     def __init__(self, bbs, board):
39         self.bbs = bbs
40         self.board = board
41
42         glade_path = os.path.join(GLADE_DIR, GLADE_FILENAME)
43         self.widget_tree = gtk.glade.XML(glade_path)
44
45         self.treeview = self.widget_tree.get_widget("treeview")
46         self.treeview.set_model(ThreadListModel())
47
48         self.popupmenu = self.widget_tree.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.widget_tree.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                 # avoid the Last-Modified time of subject.txt and
197                 # the build time of thread is equal (zero division)
198                 dur = lastmod - start
199                 if dur == 0:
200                     dic["average"] = 999999
201                 else:
202                     dic["average"] = int(res * 60 * 60 * 24 / dur)
203
204             # lastModified
205             httpdate = dic["lastModified"]
206             try:
207                 secs = misc.httpdate_to_secs(httpdate)
208                 dic["lastModified"] = secs
209             except ValueError:
210                 dic["lastModified"] = 0
211             
212             list_list.append(dic)
213
214         model = self.treeview.get_model()
215         model.set_list(list_list)
216
217         # redraw visible area after set list to model
218         self.treeview.queue_draw()
219
220         self.reset_sort_indicator()
221
222         print "end"
223         time_end = time.time()
224         print time_end - time_start