OSDN Git Service

Add HTTP header output.
[fukui-no-namari/fukui-no-namari.git] / src / FukuiNoNamari / thread_window.py
index 527a6b7..ad7e392 100644 (file)
@@ -29,18 +29,21 @@ import gnome
 import gobject
 import threading
 import gconf
+import traceback
 
 import misc
+from misc import FileWrap, ThreadInvoker
 import datfile
 import barehtmlparser
 import idxfile
 import session
 import board_window
 import uri_opener
-from http_sub import HTTPRedirectHandler302
+from http_sub import HTTPRedirectHandler302, HTTPDebugHandler
 from BbsType import bbs_type_judge_uri
 from BbsType import bbs_type_exception
 import config
+import winwrapbase
 
 GLADE_DIR = os.path.join(os.path.dirname(os.path.realpath(__file__)),
                          "..", "data")
@@ -64,50 +67,13 @@ def open_thread(uri, update=False):
             winwrap.load(update)
     else:
         winwrap = WinWrap(bbs_type.uri)  # pass original uri
-        session.window_created(uri, winwrap)
         winwrap.load(update)
 
     # jump to the res if necessary.
     winwrap.jump_to_res(bbs_type.uri)
 
 
-class ThreadInvoker(threading.Thread):
-    def __init__(self, on_end, *methods):
-        super(ThreadInvoker, self).__init__()
-        self.on_end = on_end
-        self.methods = methods
-    def run(self):
-        try:
-            for m in self.methods:
-                m()
-        finally:
-            self.on_end()
-
-
-class FileWrap:
-    def __init__(self, path):
-        self._file = None
-        self._path = path
-    def __del__(self):
-        self.close()
-    def seek(self, size):
-        self.file().seek(size)
-    def write(self, data):
-        self.file().write(data)
-    def close(self):
-        if self._file:
-            self._file.close()
-            self._file = None
-    def file(self):
-        if not self._file:
-            basedir = os.path.dirname(self._path)
-            if not os.path.isdir(basedir):
-                os.makedirs(basedir)
-            self._file = file(self._path, "a+")
-        return self._file
-
-
-class WinWrap:
+class WinWrap(winwrapbase.WinWrapBase):
     hovering_over_link = False
     hand_cursor = gtk.gdk.Cursor(gtk.gdk.HAND2)
     regular_cursor = gtk.gdk.Cursor(gtk.gdk.XTERM)
@@ -139,6 +105,7 @@ class WinWrap:
         self.toolbar.unset_style()
         self.statusbar = self.widget_tree.get_widget("appbar")
         self.textview = self.widget_tree.get_widget("textview")
+        self.textview.drag_dest_unset()
         self.textbuffer = self.textview.get_buffer()
         self.enditer = self.textbuffer.get_end_iter()
         self.boldtag = self.textbuffer.create_tag(weight=pango.WEIGHT_BOLD)
@@ -163,23 +130,17 @@ class WinWrap:
                               self.on_motion_notify_event)
         self.textview.connect("visibility-notify-event",
                               self.on_visibility_notify_event)
+        self.restore()
+        self.window.show()
 
-        self.gconf_client = gconf.client_get_default()
-        self.gconf_key_base = "/apps/" + config.APPNAME.lower() + \
-                              "/thread_states/"
-
-        width = self.gconf_client.get_int(
-            self.gconf_key_base + "window_width")
-        height = self.gconf_client.get_int(
-            self.gconf_key_base + "window_height")
-        self.window.set_default_size(width, height)
+        self.created()
 
-        if not self.gconf_client.get_bool(self.gconf_key_base + "toolbar"):
-            self.toolbar.parent.hide()
-        if not self.gconf_client.get_bool(self.gconf_key_base + "statusbar"):
-            self.statusbar.hide()
+    def destroy(self):
+        self.save()
+        self.window.destroy()
 
-        self.window.show()
+    def get_uri(self):
+        return self.bbs_type.get_thread_uri()
 
     def on_compose_clicked(self, widget):
         import submit_window
@@ -188,18 +149,14 @@ class WinWrap:
     def on_toolbar_activate(self, widget):
         if self.toolbar.parent.get_property("visible"):
             self.toolbar.parent.hide()
-            self.gconf_client.set_bool(self.gconf_key_base + "toolbar", False)
         else:
             self.toolbar.parent.show()
-            self.gconf_client.set_bool(self.gconf_key_base + "toolbar", True)
 
     def on_statusbar_activate(self, widget):
         if self.statusbar.get_property("visible"):
             self.statusbar.hide()
-            self.gconf_client.set_bool(self.gconf_key_base+"statusbar", False)
         else:
             self.statusbar.show()
-            self.gconf_client.set_bool(self.gconf_key_base + "statusbar", True)
 
     def on_event_after(self, widget, event):
         if event.type != gtk.gdk.BUTTON_RELEASE:
@@ -276,17 +233,14 @@ class WinWrap:
                 self.regular_cursor)
 
     def on_close_activate(self, widget):
-        self.window.destroy()
+        self.destroy()
 
     def on_thread_window_delete_event(self, widget, event):
-        w, h = widget.get_size()
-        self.gconf_client.set_int(self.gconf_key_base + "window_width", w)
-        self.gconf_client.set_int(self.gconf_key_base + "window_height", h)
-
+        self.save()
         return False
         
     def on_thread_window_destroy(self, widget):
-        -1
+        self.destroyed()
 
     def on_quit_activate(self, widget):
         session.main_quit()
@@ -308,48 +262,53 @@ class WinWrap:
             req.add_header("If-Modified-Since", lastmod)
         if etag:
             req.add_header("If-None-Match", etag)
-        print req.headers
-
-        opener = urllib2.build_opener(HTTPRedirectHandler302)
-        res = opener.open(req)
-        headers = res.info()
-        print headers
-
-        line = res.readline()
-        maybe_incomplete = False
-        while line:
-            if not line.endswith("\n"):
-                maybe_incomplete = True
-                print "does not end with \\n. maybe incomplete"
-                break
-            on_get_res(line)
-            line = res.readline()
 
-        res.close()
-
-        if maybe_incomplete:
-            lastmod = None
-            etag = None
+        opener = urllib2.build_opener(HTTPRedirectHandler302, HTTPDebugHandler)
+        try:
+            res = opener.open(req)
+        except urllib2.HTTPError, e:
+            gobject.idle_add(
+                self.statusbar.set_status, "%d %s" % (e.code, e.msg))
         else:
-            if "Last-Modified" in headers:
-                lastmod = headers["Last-Modified"]
-            if "ETag" in headers:
-                etag = headers["Etag"]
-
-        if self.num > 0:
-            if not self.title:
-                title = datfile.get_title_from_dat(
-                    self.bbs, self.board, self.thread)
-                if title:
-                    self.title = title
-                    gobject.idle_add(self.window.set_title, title)
-            # save idx
-            idx_dic = {"title": self.title, "lineCount": self.num,
-                   "lastModified": lastmod, "etag": etag}
-            idxfile.save_idx(self.bbs, self.board, self.thread, idx_dic)
-
-            gobject.idle_add(session.thread_idx_updated,
-                             self.bbs_type.get_thread_uri(), idx_dic)
+            headers = res.info()
+            gobject.idle_add(
+                self.statusbar.set_status, "%d %s" % (res.code, res.msg))
+
+            line = res.readline()
+            maybe_incomplete = False
+            while line:
+                if not line.endswith("\n"):
+                    maybe_incomplete = True
+                    print "does not end with \\n. maybe incomplete"
+                    break
+                on_get_res(line)
+                line = res.readline()
+
+            res.close()
+
+            if maybe_incomplete:
+                lastmod = None
+                etag = None
+            else:
+                if "Last-Modified" in headers:
+                    lastmod = headers["Last-Modified"]
+                if "ETag" in headers:
+                    etag = headers["Etag"]
+
+            if self.num > 0:
+                if not self.title:
+                    title = datfile.get_title_from_dat(
+                        self.bbs, self.board, self.thread)
+                    if title:
+                        self.title = title
+                        gobject.idle_add(self.window.set_title, title)
+                # save idx
+                idx_dic = {"title": self.title, "lineCount": self.num,
+                       "lastModified": lastmod, "etag": etag}
+                idxfile.save_idx(self.bbs, self.board, self.thread, idx_dic)
+
+                gobject.idle_add(session.thread_idx_updated,
+                                 self.bbs_type.get_thread_uri(), idx_dic)
 
     def update(self, widget=None):
 
@@ -561,3 +520,95 @@ class WinWrap:
             self.update()
         else:
             self.load_dat()
+
+    def save(self):
+        try:
+            states_path = misc.get_thread_states_path(
+                self.bbs_type.bbs_type, self.bbs_type.board,
+                self.bbs_type.thread)
+            dat_path = misc.get_thread_dat_path(
+                self.bbs_type.bbs_type, self.bbs_type.board,
+                self.bbs_type.thread)
+
+            # save only if dat file exists.
+            if os.path.exists(dat_path):
+                window_width, window_height = self.window.get_size()
+                toolbar_visible = self.toolbar.parent.get_property("visible")
+                statusbar_visible = self.statusbar.get_property("visible")
+
+                dirname = os.path.dirname(states_path)
+                if not os.path.isdir(dirname):
+                    os.makedirs(dirname)
+
+                f = file(states_path, "w")
+
+                f.write("window_width=" + str(window_width) + "\n")
+                f.write("window_height=" + str(window_height) + "\n")
+                f.write("toolbar_visible=" + str(toolbar_visible) + "\n")
+                f.write("statusbar_visible=" + str(statusbar_visible) + "\n")
+
+                f.close()
+        except:
+            traceback.print_exc()
+
+    def restore(self):
+        try:
+            window_height = 600
+            window_width = 600
+            toolbar_visible = True
+            statusbar_visible = True
+
+            try:
+                key_base = config.gconf_app_key_base() + "/thread_states"
+                gconf_client = gconf.client_get_default()
+                width = gconf_client.get_int(key_base + "/window_width")
+                height = gconf_client.get_int(key_base + "/window_height")
+                toolbar_visible = gconf_client.get_bool(
+                    key_base + "/toolbar")
+                statusbar_visible = gconf_client.get_bool(
+                    key_base + "/statusbar")
+                if width != 0:
+                    window_width = width
+                if height != 0:
+                    window_height = height
+            except:
+                traceback.print_exc()
+
+            states_path = misc.get_thread_states_path(
+                self.bbs_type.bbs_type, self.bbs_type.board,
+                self.bbs_type.thread)
+            if os.path.exists(states_path):
+                for line in file(states_path):
+                    if line.startswith("window_height="):
+                        height = window_height
+                        try:
+                            height = int(
+                                line[len("window_height="):].rstrip("\n"))
+                        except:
+                            pass
+                        else:
+                            window_height = height
+                    elif line.startswith("window_width="):
+                        width = window_width
+                        try:
+                            width = int(
+                                line[len("window_width="):].rstrip("\n"))
+                        except:
+                            pass
+                        else:
+                            window_width = width
+                    elif line.startswith("toolbar_visible="):
+                        tbar = line[len("toolbar_visible="):].rstrip("\n")
+                        toolbar_visible = tbar == "True"
+                    elif line.startswith("statusbar_visible="):
+                        sbar = line[len("statusbar_visible="):].rstrip("\n")
+                        statusbar_visible = sbar == "True"
+
+            self.window.set_default_size(window_width, window_height)
+
+            if not toolbar_visible:
+                gobject.idle_add(self.toolbar.parent.hide)
+            if not statusbar_visible:
+                gobject.idle_add(self.statusbar.hide)
+        except:
+            traceback.print_exc()