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
33 GLADE_DIR = os.path.join(os.path.dirname(os.path.realpath(__file__)),
35 GLADE_FILENAME = "thread_window.glade"
40 def __init__(self, bbs, board, thread):
48 glade_path = os.path.join(GLADE_DIR, GLADE_FILENAME)
49 self.widget_tree = gtk.glade.XML(glade_path)
50 self.window = self.widget_tree.get_widget("thread_window")
51 self.textview = self.widget_tree.get_widget("textview")
52 self.textbuffer = self.textview.get_buffer()
53 self.enditer = self.textbuffer.get_end_iter()
54 self.boldtag = self.textbuffer.create_tag(weight=pango.WEIGHT_BOLD)
55 self.leftmargintag = self.textbuffer.create_tag()
56 self.leftmargintag.set_property("left-margin", 20)
58 sigdic = {"on_toolbutton_refresh_clicked": self.update,
59 "on_refresh_activate": self.update,
60 "on_close_activate": self.on_close_activate,
61 "on_quit_activate": self.on_quit_activate,
62 "on_thread_window_destroy": self.on_thread_window_destroy}
63 self.widget_tree.signal_autoconnect(sigdic)
67 def on_close_activate(self, widget):
70 def on_thread_window_destroy(self, widget):
73 def on_quit_activate(self, widget):
76 def http_get_dat(self, on_get_res):
77 datfile_url = misc.get_thread_dat_url(self.bbs, self.board, self.thread)
79 idx_dic = idxfile.load_idx(self.bbs, self.board, self.thread)
80 lastmod = idx_dic["lastModified"]
81 etag = idx_dic["etag"]
83 req = urllib2.Request(datfile_url)
85 req.add_header("Range", "bytes=" + str(self.size) + "-")
87 req.add_header("If-Modified-Since", lastmod)
89 req.add_header("If-None-Match", etag)
92 res = urllib2.urlopen(req)
97 maybe_incomplete = False
99 if not line.endswith("\n"):
100 maybe_incomplete = True
101 print "does not end with \\n. maybe incomplete"
104 line = res.readline()
112 if "Last-Modified" in headers:
113 lastmod = headers["Last-Modified"]
114 if "ETag" in headers:
115 etag = headers["Etag"]
119 idx_dic = {"title": self.title, "lineCount": self.num,
120 "lastModified": lastmod, "etag": etag}
121 idxfile.save_idx(self.bbs, self.board, self.thread, idx_dic)
123 def update(self, widget=None):
124 line_count = datfile.get_dat_line_count(
125 self.bbs, self.board, self.thread)
127 if line_count > self.num:
128 datfile.load_dat_partly(
129 self.bbs, self.board, self.thread,
130 self.append_rawres_to_buffer, self.num+1)
132 dat_path = misc.get_thread_dat_path(self.bbs, self.board, self.thread)
133 dat_file = file(dat_path, "a+")
135 def save_line_and_append_to_buffer(line):
136 dat_file.seek(self.size)
138 self.append_rawres_to_buffer(line)
140 self.http_get_dat(save_line_and_append_to_buffer)
144 self.title = datfile.get_title_from_dat(
145 self.bbs, self.board, self.thread)
147 self.window.set_title(self.title)
153 self.title = datfile.get_title_from_dat(
154 self.bbs, self.board, self.thread)
156 self.window.set_title(self.title)
158 datfile.load_dat(self.bbs, self.board, self.thread,
159 self.append_rawres_to_buffer)
160 self.textview.scroll_to_mark(self.textbuffer.get_insert(), 0)
163 def append_rawres_to_buffer(self, line):
164 self.size += len(line)
167 h = lambda name,mail,date,msg: self.reselems_to_buffer(
168 self.num, name, mail, date, msg)
170 datfile.split_line_to_elems(line.decode("cp932", "replace"), h)
172 def reselems_to_buffer(self, num, name, mail, date, msg):
174 self.textbuffer.insert(self.enditer, str(num) + " ")
177 p = barehtmlparser.BareHTMLParser(self.untiedata_to_buffer)
178 p.feed("<b>" + name + "</b>")
182 self.textbuffer.insert(self.enditer, "[" + mail + "]")
187 self.textbuffer.insert(self.enditer, "\n")
190 p.reset_func(self.untiedata_to_buffer_with_leftmargin)
194 self.textbuffer.insert(self.enditer, "\n\n")
196 def untiedata_to_buffer(self, data, bold, href):
198 self.textbuffer.insert_with_tags(self.enditer, data, self.boldtag)
200 self.textbuffer.insert(self.enditer, data)
202 def untiedata_to_buffer_with_leftmargin(self, data, bold, href):
204 self.textbuffer.insert_with_tags(self.enditer, data,
205 self.boldtag, self.leftmargintag)
207 self.textbuffer.insert_with_tags(self.enditer, data,