OSDN Git Service

disuse res queue
authorAiwota Programmer <aiwotaprog@tetteke.tk>
Tue, 11 Dec 2007 11:08:36 +0000 (20:08 +0900)
committerAiwota Programmer <aiwotaprog@tetteke.tk>
Tue, 11 Dec 2007 11:08:36 +0000 (20:08 +0900)
src/FukuiNoNamari/thread_window.py

index 20f6650..59b4517 100644 (file)
@@ -83,6 +83,39 @@ def open_thread(uri, update=False):
             resnum = int(match.group())
             winwrap.jump_to_res(resnum)
 
+
+class HTMLParserToThreadView:
+    def __init__(self, threadview, resnum):
+        self.threadview = threadview
+        self.resnum = resnum
+        self.initialize()
+
+    def initialize(self):
+        self.buf = ""
+        self.attrlist = pango.AttrList()
+
+    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()
+
+
 class WinWrap(winwrapbase.WinWrapBase):
     hovering_over_link = False
     hand_cursor = gtk.gdk.Cursor(gtk.gdk.HAND2)
@@ -372,8 +405,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:
@@ -398,15 +429,17 @@ 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
 
-        self.process_queue(self.res_queue, self.num)
-
     def reselems_to_buffer(self, num, name, mail, date, msg):
-        p = barehtmlparser.BareHTMLParser(
-            lambda d,b,h: self.res_queue.append((d,b,h,False)))
+        pipe = HTMLParserToThreadView(self.threadview, num)
+        p = barehtmlparser.BareHTMLParser(pipe.from_html_parser)
+
+        # First, create a pango layout for num,name,mail,date
+        # 'margin left' is 0
         # number
         p.feed(str(num) + " ")
 
@@ -418,75 +451,26 @@ class WinWrap(winwrapbase.WinWrapBase):
 
         # date
         p.feed(date)
-        p.feed("<br>")
+        p.flush()
+
+        pipe.to_thread_view(0)
 
+
+        # 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)))
         p.feed(msg.lstrip(" "))
 
-        p.feed("<br><br>")
+        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
 
-    def process_queue(self, queue, num):
-        text = ""
-        current_margin = False
-        attrlist = pango.AttrList()
-        for data, bold, href, margin in queue:
-            data = data.encode("utf8")
-            if current_margin != margin:
-                layout = self.threadview.create_pango_layout(text)
-                layout.set_wrap(pango.WRAP_CHAR)
-                layout.posY = 0
-                layout.resnum = num
-                layout.set_attributes(attrlist)
-                if current_margin:
-                    layout.marginleft = 20
-                else:
-                    layout.marginleft = 0
-                gobject.idle_add(self.threadview.add_layout, layout)
-
-                current_margin = margin
-                text = ""
-                attrlist = pango.AttrList()
-
-            if bold:
-                attrlist.insert(pango.AttrWeight(
-                    pango.WEIGHT_BOLD, len(text), len(text) + len(data)))
-            if href:
-                attrlist.insert(pango.AttrUnderline(
-                    pango.UNDERLINE_SINGLE, len(text), len(text) + len(data)))
-
-            text += data
-
-        if text != "":
-            layout = self.threadview.create_pango_layout(text)
-            layout.set_wrap(pango.WRAP_CHAR)
-            layout.posY = 0
-            layout.resnum = num
-            layout.set_attributes(attrlist)
-            if current_margin:
-                layout.marginleft = 20
-            else:
-                layout.marginleft = 0
-        gobject.idle_add(self.threadview.add_layout, layout)
-#             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)
-
     def jump(self, value):
         gobject.idle_add(self.threadview.jump, value)