OSDN Git Service

Fix thread title is not saved to idx file.
[fukui-no-namari/fukui-no-namari.git] / src / Hage1 / thread_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.path
23 import codecs
24 import re
25 import pango
26 import urllib2
27
28 import misc
29 import datfile
30 import barehtmlparser
31 import idxfile
32
33 GLADE_DIR = os.path.join(os.path.dirname(os.path.realpath(__file__)),
34                          "..", "data")
35 GLADE_FILENAME = "thread_window.glade"
36
37
38 class WinWrap:
39
40     def __init__(self, bbs, board, thread):
41         self.bbs = bbs
42         self.board = board
43         self.thread = thread
44         self.size = 0
45         self.num = 0
46         self.title = None
47
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)
57
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)
64
65         self.load_dat()
66
67     def on_close_activate(self, widget):
68         self.window.destroy()
69
70     def on_thread_window_destroy(self, widget):
71         -1
72
73     def on_quit_activate(self, widget):
74         gtk.main_quit()
75
76     def http_get_dat(self, on_get_res):
77         datfile_url = misc.get_thread_dat_url(self.bbs, self.board, self.thread)
78
79         idx_dic = idxfile.load_idx(self.bbs, self.board, self.thread)
80         lastmod = idx_dic["lastModified"]
81         etag = idx_dic["etag"]
82
83         req = urllib2.Request(datfile_url)
84         if self.size > 0:
85             req.add_header("Range", "bytes=" + str(self.size) + "-")
86         if lastmod:
87             req.add_header("If-Modified-Since", lastmod)
88         if etag:
89             req.add_header("If-None-Match", etag)
90         print req.headers
91
92         res = urllib2.urlopen(req)
93         headers = res.info()
94         print headers
95
96         line = res.readline()
97         maybe_incomplete = False
98         while line:
99             if not line.endswith("\n"):
100                 maybe_incomplete = True
101                 print "does not end with \\n. maybe incomplete"
102                 break
103             on_get_res(line)
104             line = res.readline()
105
106         res.close()
107
108         if maybe_incomplete:
109             lastmod = None
110             etag = None
111         else:
112             if "Last-Modified" in headers:
113                 lastmod = headers["Last-Modified"]
114             if "ETag" in headers:
115                 etag = headers["Etag"]
116
117         if self.num > 0:
118             if not self.title:
119                 self.title = datfile.get_title_from_dat(
120                     self.bbs, self.board, self.thread)
121                 if self.title:
122                     self.window.set_title(self.title)
123             # save idx
124             idx_dic = {"title": self.title, "lineCount": self.num,
125                    "lastModified": lastmod, "etag": etag}
126             idxfile.save_idx(self.bbs, self.board, self.thread, idx_dic)
127
128     def update(self, widget=None):
129         line_count = datfile.get_dat_line_count(
130             self.bbs, self.board, self.thread)
131
132         if line_count > self.num:
133             datfile.load_dat_partly(
134                 self.bbs, self.board, self.thread,
135                 self.append_rawres_to_buffer, self.num+1)
136
137         dat_path = misc.get_thread_dat_path(self.bbs, self.board, self.thread)
138         dat_file = file(dat_path, "a+")
139
140         def save_line_and_append_to_buffer(line):
141             dat_file.seek(self.size)
142             dat_file.write(line)
143             self.append_rawres_to_buffer(line)
144
145         self.http_get_dat(save_line_and_append_to_buffer)
146         dat_file.close()
147
148     def load_dat(self):
149         self.size = 0
150         self.num = 0
151
152         self.title = datfile.get_title_from_dat(
153             self.bbs, self.board, self.thread)
154         if self.title:
155             self.window.set_title(self.title)
156
157         datfile.load_dat(self.bbs, self.board, self.thread,
158                          self.append_rawres_to_buffer)
159         self.textview.scroll_to_mark(self.textbuffer.get_insert(), 0)
160
161
162     def append_rawres_to_buffer(self, line):
163         self.size += len(line)
164         self.num += 1
165
166         h = lambda name,mail,date,msg: self.reselems_to_buffer(
167             self.num, name, mail, date, msg)
168
169         datfile.split_line_to_elems(line.decode("cp932", "replace"), h)
170         
171     def reselems_to_buffer(self, num, name, mail, date, msg):
172         # number
173         self.textbuffer.insert(self.enditer, str(num) + " ")
174
175         # name
176         p = barehtmlparser.BareHTMLParser(self.untiedata_to_buffer)
177         p.feed("<b>" + name + "</b>")
178         p.close()
179
180         # mail
181         self.textbuffer.insert(self.enditer, "[" + mail + "]")
182
183         # date
184         p.feed(date)
185         p.close()
186         self.textbuffer.insert(self.enditer, "\n")
187
188         # msg
189         p.reset_func(self.untiedata_to_buffer_with_leftmargin)
190         p.feed(msg)
191         p.close()
192
193         self.textbuffer.insert(self.enditer, "\n\n")
194
195     def untiedata_to_buffer(self, data, bold, href):
196         if bold:
197             self.textbuffer.insert_with_tags(self.enditer, data, self.boldtag)
198         else:
199             self.textbuffer.insert(self.enditer, data)
200
201     def untiedata_to_buffer_with_leftmargin(self, data, bold, href):
202         if bold:
203             self.textbuffer.insert_with_tags(self.enditer, data,
204                                              self.boldtag, self.leftmargintag)
205         else:
206             self.textbuffer.insert_with_tags(self.enditer, data,
207                                              self.leftmargintag)