OSDN Git Service

ThreadView is redrawn after HTTP GET due to prevent white drawingarea.
[fukui-no-namari/fukui-no-namari.git] / src / FukuiNoNamari / thread_window.py
index 59b4517..2a81afa 100644 (file)
@@ -50,6 +50,7 @@ import winwrapbase
 import bookmark_list
 import bookmark_window
 import thread_view
+import thread_popup
 
 GLADE_FILENAME = "thread_window.glade"
 
@@ -74,10 +75,10 @@ def open_thread(uri, update=False):
         winwrap.load(update)
 
     # jump to the res if necessary.
-    strict_uri = winwrap.bbs_type.get_thread_uri()
-    if (winwrap.bbs_type.uri != strict_uri and
-        winwrap.bbs_type.uri.startswith(strict_uri)):
-        resnum = winwrap.bbs_type.uri[len(strict_uri):]
+    strict_uri = bbs_type.get_thread_uri()
+    if (bbs_type.uri != strict_uri and
+        bbs_type.uri.startswith(strict_uri)):
+        resnum = bbs_type.uri[len(strict_uri):]
         match = re.match("\d+", resnum)
         if match:
             resnum = int(match.group())
@@ -85,35 +86,37 @@ def open_thread(uri, update=False):
 
 
 class HTMLParserToThreadView:
-    def __init__(self, threadview, resnum):
+    def __init__(self, threadview, resnum, left_margin):
         self.threadview = threadview
         self.resnum = resnum
+        self.left_margin = left_margin
         self.initialize()
 
+    def set_left_margin(self, left_margin):
+        self.left_margin = left_margin
+
     def initialize(self):
-        self.buf = ""
-        self.attrlist = pango.AttrList()
+        self.layout = None
+
+    def on_new_line(self):
+        self.to_thread_view()
+        self.layout = self.threadview.create_res_layout(
+            self.left_margin, self.resnum)
 
     def from_html_parser(self, data, bold, href):
-        data = data.encode("utf8")
-        start = len(self.buf)
-        end = start + len(data)
-        self.buf += data
-        if bold:
-            attr = pango.AttrWeight(pango.WEIGHT_BOLD, start, end)
-            self.attrlist.insert(attr)
-        if href:
-            attr = pango.AttrUnderline(pango.UNDERLINE_SINGLE, start, end)
-            self.attrlist.insert(attr)
-
-    def to_thread_view(self, marginleft):
-        layout = self.threadview.create_pango_layout(self.buf)
-        layout.posY = 0
-        layout.resnum = self.resnum
-        layout.marginleft = marginleft
-        layout.set_attributes(self.attrlist)
-        gobject.idle_add(self.threadview.add_layout, layout)
-        self.initialize()
+        if self.layout == None:
+            self.layout = self.threadview.create_res_layout(
+                self.left_margin, self.resnum)
+
+        self.layout.add_text(data, bold, href)
+
+    def to_thread_view(self):
+        if self.layout is not None:
+            # gobject.idle_add(self.threadview.add_layout, self.layout)
+            gtk.gdk.threads_enter()
+            self.threadview.add_layout(self.layout)
+            gtk.gdk.threads_leave()
+            self.initialize()
 
 
 class WinWrap(winwrapbase.WinWrapBase):
@@ -144,8 +147,35 @@ class WinWrap(winwrapbase.WinWrapBase):
         self.vbox = self.widget_tree.get_widget("vbox")
 
         self.threadview = thread_view.ThreadView()
+        self.threadpopup = thread_popup.ThreadPopup(self.bbs_type)
+        self.threadpopup.push_thread_view(self.threadview)
         self.vbox.pack_start(self.threadview)
         self.vbox.reorder_child(self.threadview, 2)
+        self.window.set_focus(self.threadview.drawingarea)
+
+        self.threadview.connect("uri-clicked-event", self.on_threadview_uri_clicked)
+        self.threadpopup.connect(
+            "uri-clicked-event",
+            lambda widget, threadview, uri: self.on_threadview_uri_clicked(threadview, uri))
+
+        self.statusbar_context_id = self.statusbar.get_context_id(
+            "Thread Window Status")
+        self.statusbar.push(self.statusbar_context_id, "OK.")
+
+        self.threadview.popupmenu = self.widget_tree.get_widget(
+            "popup_threadview_menu")
+        self.threadview.menu_openuri = self.widget_tree.get_widget(
+            "popup_threadview_menu_openuri")
+        self.threadview.menu_copylinkaddress = self.widget_tree.get_widget(
+            "popup_threadview_menu_copylinkaddress")
+        self.threadview.menu_separator_link = self.widget_tree.get_widget(
+            "popup_threadview_menu_separator_link")
+        self.threadview.menu_copyselection = self.widget_tree.get_widget(
+            "popup_threadview_menu_copyselection")
+        self.threadview.menu_openasuri = self.widget_tree.get_widget(
+            "popup_threadview_menu_openasuri")
+        self.threadview.menu_separator_selection = self.widget_tree.get_widget(
+            "popup_threadview_menu_separator_selection")
 
         self.initialize_buffer()
 
@@ -163,6 +193,16 @@ class WinWrap(winwrapbase.WinWrapBase):
                   "on_add_bookmark_activate": self.on_add_bookmark_activate,
                   "on_manage_bookmarks_activate": \
                   self.on_manage_bookmarks_activate,
+                  "on_popup_threadview_menu_openuri_activate":
+                  self.on_popup_threadview_menu_openuri_activate,
+                  "on_popup_threadview_menu_copylinkaddress_activate":
+                  self.on_popup_threadview_menu_copylinkaddress_activate,
+                  "on_popup_threadview_menu_copyselection_activate":
+                  self.on_popup_threadview_menu_copyselection_activate,
+                  "on_popup_threadview_menu_openasuri_activate":
+                  self.on_popup_threadview_menu_openasuri_activate,
+                  "on_popup_threadview_menu_refresh_activate":
+                  self.on_popup_threadview_menu_refresh_activate,
                   "on_thread_window_destroy": self.on_thread_window_destroy}
         self.widget_tree.signal_autoconnect(sigdic)
 
@@ -172,13 +212,6 @@ class WinWrap(winwrapbase.WinWrapBase):
         self.created()
 
     def initialize_buffer(self):
-        self.textbuffer = gtk.TextBuffer()
-
-        self.enditer = self.textbuffer.get_end_iter()
-        self.boldtag = self.textbuffer.create_tag(weight=pango.WEIGHT_BOLD)
-        self.leftmargintag = self.textbuffer.create_tag()
-        self.leftmargintag.set_property("left-margin", 20)
-
         self.threadview.initialize_buffer()
 
     def destroy(self):
@@ -244,6 +277,41 @@ class WinWrap(winwrapbase.WinWrapBase):
         except OSError:
             traceback.print_exc()
 
+    def on_threadview_uri_clicked(self, widget, uri):
+
+        if not uri.startswith("http://"):
+            # maybe a relative uri.
+            uri = urlparse.urljoin(self.bbs_type.get_uri_base(), uri)
+
+        try:
+            uri_opener.open_uri(uri)
+        except bbs_type_exception.BbsTypeError:
+            # not supported, show with the web browser.
+            gnome.url_show(uri)
+            
+    def on_popup_threadview_menu_openuri_activate(self, widget):
+        self.on_threadview_uri_clicked(widget.uri)
+
+    def on_popup_threadview_menu_copylinkaddress_activate(self, widget):
+        clip = gtk.Clipboard()
+        clip.set_text(widget.uri, len(widget.uri))
+
+    def on_popup_threadview_menu_copyselection_activate(self, widget):
+        text = self.threadview.get_selected_text()
+        if text and len(text) > 0:
+            clip = gtk.Clipboard()
+            text = text.encode("utf8")
+            clip.set_text(text, len(text))
+
+    def on_popup_threadview_menu_openasuri_activate(self, widget):
+        text = self.threadview.get_selected_text()
+        if not text.startswith("http://"):
+            text = "http://" + text
+        self.on_threadview_uri_clicked(text)
+
+    def on_popup_threadview_menu_refresh_activate(self, widget):
+        self.update(widget)
+
     def http_get_dat(self, on_get_res):
         datfile_url = self.bbs_type.get_dat_uri()
 
@@ -260,19 +328,38 @@ class WinWrap(winwrapbase.WinWrapBase):
         if etag:
             req.add_header("If-None-Match", etag)
 
+        def push():
+            self.statusbar.pop(self.statusbar_context_id)
+            self.statusbar.push(self.statusbar_context_id, "GET...")
+        gobject.idle_add(push)
+
         req = self.bbs_type.set_extra_dat_request(req, self)
 
         opener = urllib2.build_opener(HTTPRedirectHandler302, HTTPDebugHandler)
         try:
             res = opener.open(req)
         except urllib2.HTTPError, e:
-            pass
-#             gobject.idle_add(
-#                 lambda x: self.statusbar.push(0, x), "%d %s" % (e.code, e.msg))
+            def push(code, msg):
+                message = "%d %s" % (code, msg)
+                self.statusbar.pop(self.statusbar_context_id)
+                self.statusbar.push(self.statusbar_context_id, message)
+            gobject.idle_add(push, e.code, e.msg)
         else:
             headers = res.info()
-#             gobject.idle_add(
-#                 lambda x: self.statusbar.push(0, x), "%d %s" % (res.code, res.msg))
+
+            if "Last-Modified" in headers:
+                la = headers["Last-Modified"]
+                def push(code, msg, lastm):
+                    message = "%d %s [%s]" % (code, msg, lastm)
+                    self.statusbar.pop(self.statusbar_context_id)
+                    self.statusbar.push(self.statusbar_context_id, message)
+                gobject.idle_add(push, res.code, res.msg, la)
+            else:
+                def push(code, msg):
+                    message = "%d %s" % (code, msg)
+                    self.statusbar.pop(self.statusbar_context_id)
+                    self.statusbar.push(self.statusbar_context_id, message)
+                gobject.idle_add(push, res.code, res.msg)
 
             maybe_incomplete = False
             for line in res:
@@ -307,11 +394,6 @@ class WinWrap(winwrapbase.WinWrapBase):
         self.jump_request_num = 0
 
         def load():
-            if self.num == 0:
-                def create_mark():
-                    self.textbuffer.create_mark("1", self.enditer, True)
-                gobject.idle_add(create_mark)
-
             line_count = datfile.get_dat_line_count(self.bbs_type)
             if line_count < self.num:
                 self.num = 0
@@ -344,6 +426,9 @@ class WinWrap(winwrapbase.WinWrapBase):
                 self.append_rawres_to_buffer(line)
 
             self.http_get_dat(save_line_and_append_to_buffer)
+            gtk.gdk.threads_enter()
+            self.threadview.redraw()
+            gtk.gdk.threads_leave()
             dat_file.close()
 
             def do_jump():
@@ -435,8 +520,9 @@ class WinWrap(winwrapbase.WinWrapBase):
             print "maybe syntax error.", self.num, line
 
     def reselems_to_buffer(self, num, name, mail, date, msg):
-        pipe = HTMLParserToThreadView(self.threadview, num)
-        p = barehtmlparser.BareHTMLParser(pipe.from_html_parser)
+        pipe = HTMLParserToThreadView(self.threadview, num, 0)
+        p = barehtmlparser.BareHTMLParser(
+            pipe.from_html_parser, pipe.on_new_line)
 
         # First, create a pango layout for num,name,mail,date
         # 'margin left' is 0
@@ -453,23 +539,19 @@ class WinWrap(winwrapbase.WinWrapBase):
         p.feed(date)
         p.flush()
 
-        pipe.to_thread_view(0)
+        pipe.to_thread_view()
 
 
         # Second, create a pango layout for message
         # 'margin left' is 20
         # msg
+        pipe.set_left_margin(20)
         p.feed(msg.lstrip(" "))
 
         p.feed("<br>")
         p.close()
 
-        pipe.to_thread_view(20)
-
-    def href_tag(self, href):
-        tag = self.textbuffer.create_tag(underline=pango.UNDERLINE_SINGLE)
-        tag.set_data("href", href)
-        return tag
+        pipe.to_thread_view()
 
     def jump(self, value):
         gobject.idle_add(self.threadview.jump, value)