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 7688e4e..2a81afa 100644 (file)
@@ -49,6 +49,8 @@ import config
 import winwrapbase
 import bookmark_list
 import bookmark_window
+import thread_view
+import thread_popup
 
 GLADE_FILENAME = "thread_window.glade"
 
@@ -73,80 +75,56 @@ 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())
             winwrap.jump_to_res(resnum)
 
+
+class HTMLParserToThreadView:
+    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.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):
+        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):
     hovering_over_link = False
     hand_cursor = gtk.gdk.Cursor(gtk.gdk.HAND2)
     regular_cursor = gtk.gdk.Cursor(gtk.gdk.XTERM)
 
-
-    def relayout(self):
-        width = self.drawingarea.allocation.width
-        sum_height = 0
-        for layout in self.pangolayout:
-            layout.set_width(width * pango.SCALE)
-            layout.posY = sum_height
-            x, y = layout.get_pixel_size()
-            sum_height += y
-        self.adjustment.upper = sum_height
-
-    def draw_viewport(self):
-        view_y = self.adjustment.get_value()
-        self.drawingarea.window.draw_rectangle(
-            self.drawingarea.style.base_gc[0],
-            True, 0, 0,
-            self.drawingarea.allocation.width,
-            self.drawingarea.allocation.height)
-
-        gc = self.drawingarea.window.new_gc()
-        for layout in self.pangolayout:
-            w, h = layout.get_pixel_size()
-            layout_top = layout.posY
-            layout_bottom = layout.posY + h
-            area_top = view_y
-            area_bottom = view_y + self.drawingarea.allocation.height
-            if layout_top <= area_bottom and layout_bottom >= area_top:
-                self.drawingarea.window.draw_layout(
-                    gc, 0, layout.posY - int(view_y), layout)
-
-    def on_drawingarea_expose_event(self, widget, event, data=None):
-        self.draw_viewport()
-
-    def on_drawingarea_size_allocate(self, widget, allocation, data=None):
-        if allocation.width != self.drawingarea.prev_width:
-            self.relayout()
-            self.drawingarea.prev_width = allocation.width
-        self.adjustment.page_size = self.drawingarea.allocation.height
-        self.adjustment.page_increment = self.drawingarea.allocation.height
-
-    def on_drawingarea_button_press_event(self, widget, event, data=None):
-        self.drawingarea.queue_draw()
-
-    def on_vscrollbar_value_changed(self, widget, data=None):
-        self.drawingarea.queue_draw()
-
-    def on_drawingarea_scroll_event(self, widget, event, data=None):
-        if event.direction == gtk.gdk.SCROLL_UP:
-            self.adjustment.value -= 66.476200804
-            if self.adjustment.value < self.adjustment.lower:
-                self.adjustment.value = self.adjustment.lower
-        if event.direction == gtk.gdk.SCROLL_DOWN:
-            self.adjustment.value += 66.476200804
-            max_value = self.adjustment.upper - self.adjustment.page_size
-            if self.adjustment.value > max_value:
-                self.adjustment.value = max_value
-
     def __init__(self, uri):
-        self.pangolayout = []
-
         from BbsType import bbs_type_judge_uri
         from BbsType import bbs_type_exception
         self.bbs_type = bbs_type_judge_uri.get_type(uri)
@@ -166,12 +144,38 @@ class WinWrap(winwrapbase.WinWrapBase):
         self.toolbar = self.widget_tree.get_widget("toolbar")
         self.toolbar.unset_style()
         self.statusbar = self.widget_tree.get_widget("statusbar")
-        self.drawingarea = self.widget_tree.get_widget("drawingarea")
-        self.vscrollbar = self.widget_tree.get_widget("vscrollbar")
-        self.adjustment = self.vscrollbar.get_adjustment()
-        self.adjustment.step_increment = 20
-
-        self.drawingarea.prev_width = 0
+        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()
 
@@ -184,20 +188,21 @@ class WinWrap(winwrapbase.WinWrapBase):
                   "on_quit_activate": self.on_quit_activate,
                   "on_show_board_activate": self.on_show_board_activate,
                   "on_delete_activate": self.on_delete_activate,
-                  "on_drawingarea_expose_event": self.on_drawingarea_expose_event,
-                  "on_drawingarea_size_allocate":
-                  self.on_drawingarea_size_allocate,
                   "on_thread_window_delete_event":
                   self.on_thread_window_delete_event,
-                  "on_drawingarea_button_press_event":
-                  self.on_drawingarea_button_press_event,
-                  "on_drawingarea_scroll_event":
-                  self.on_drawingarea_scroll_event,
-                  "on_vscrollbar_value_changed":
-                  self.on_vscrollbar_value_changed,
                   "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)
 
@@ -207,14 +212,7 @@ 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.pangolayout = []
+        self.threadview.initialize_buffer()
 
     def destroy(self):
         self.save()
@@ -279,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()
 
@@ -295,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:
@@ -342,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
@@ -379,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():
@@ -440,8 +490,6 @@ class WinWrap(winwrapbase.WinWrapBase):
                 self.title = title
                 gobject.idle_add(self.window.set_title, title)
 
-        self.res_queue = []
-
         line = line.decode(self.bbs_type.encoding, "replace")
         m = self.bbs_type.dat_reg.match(line)
         if m:
@@ -466,21 +514,18 @@ class WinWrap(winwrapbase.WinWrapBase):
                     date += " ID:" + id
             self.reselems_to_buffer(num, name, mail, date, msg)
         else:
-            self.res_queue.append((str(self.num)+"\n", False, None, False))
-            self.res_queue.append((line, False, None, True))
+            self.reselems_to_buffer(
+                str(self.num), "Invalid Name", "Invalid Mail",
+                "Invalid Date", line)
             print "maybe syntax error.", self.num, line
 
-        def process_res_queue(res_queue, num):
-            self.process_queue(res_queue, num)
-            # for next res
-            #self.textbuffer.create_mark(str(num+1), self.enditer, True)
-
-        gobject.idle_add(
-            process_res_queue, self.res_queue, self.num)
-
     def reselems_to_buffer(self, num, name, mail, date, msg):
+        pipe = HTMLParserToThreadView(self.threadview, num, 0)
         p = barehtmlparser.BareHTMLParser(
-            lambda d,b,h: self.res_queue.append((d,b,h,False)))
+            pipe.from_html_parser, pipe.on_new_line)
+
+        # First, create a pango layout for num,name,mail,date
+        # 'margin left' is 0
         # number
         p.feed(str(num) + " ")
 
@@ -492,63 +537,30 @@ class WinWrap(winwrapbase.WinWrapBase):
 
         # date
         p.feed(date)
-        p.feed("<br>")
+        p.flush()
+
+        pipe.to_thread_view()
 
+
+        # Second, create a pango layout for message
+        # 'margin left' is 20
         # msg
-        p.reset_func(lambda d,b,h: self.res_queue.append((d,b,h,True)))
+        pipe.set_left_margin(20)
         p.feed(msg.lstrip(" "))
 
-        p.feed("<br><br>")
+        p.feed("<br>")
         p.close()
 
-    def href_tag(self, href):
-        tag = self.textbuffer.create_tag(underline=pango.UNDERLINE_SINGLE)
-        tag.set_data("href", href)
-        return tag
-
-    def process_queue(self, queue, num):
-        text = ""
-        for data, bold, href, margin in queue:
-            text += data
-        layout = self.drawingarea.create_pango_layout(text)
-        layout.set_wrap(pango.WRAP_CHAR)
-        layout.posY = 0
-        layout.resnum = num
-        self.pangolayout.append(layout)
-        self.relayout()
-        self.drawingarea.queue_draw()
-#             taglist = []
-#             if bold:
-#                 taglist.append(self.boldtag)
-#             if href:
-#                 taglist.append(self.href_tag(href))
-#             if margin:
-#                 taglist.append(self.leftmargintag)
-#
-#            if taglist:
-#                self.textbuffer.insert_with_tags(self.enditer, data, *taglist)
-#            else:
-#                self.textbuffer.insert(self.enditer, data)
+        pipe.to_thread_view()
 
     def jump(self, value):
-        def j():
-            if value > self.adjustment.upper - self.adjustment.page_size:
-                self.jump_to_the_end()
-            else:
-                self.adjustment.set_value(value)
-        gobject.idle_add(j)
+        gobject.idle_add(self.threadview.jump, value)
 
     def jump_to_layout(self, layout):
-        gobject.idle_add(lambda : self.jump(layout.posY))
+        gobject.idle_add(self.threadview.jump_to_layout, layout)
         
     def jump_to_the_end(self):
-        def j():
-            self.adjustment.set_value(
-                self.adjustment.upper - self.adjustment.page_size)
-        gobject.idle_add(j)
-#        mark = self.textbuffer.get_mark(str(num+1))
-#        if mark:
-#            self.textview.scroll_to_mark(mark, 0)
+        gobject.idle_add(self.threadview.jump_to_the_end)
 
     def lock(self):
         if self.lock_obj:
@@ -564,10 +576,8 @@ class WinWrap(winwrapbase.WinWrapBase):
         print "unlock"
 
     def jump_to_res(self, resnum):
-        for layout in self.pangolayout:
-            if layout.resnum == resnum:
-                self.jump_to_layout(layout)
-                return
+        if self.threadview.jump_to_res(resnum):
+            return
         self.jump_request_num = resnum
 
     def load(self, update=False):