OSDN Git Service

Add async loading.
authorAiwota Programmer <aiwotaprog@tetteke.tk>
Tue, 29 Aug 2006 20:34:04 +0000 (05:34 +0900)
committerAiwota Programmer <aiwotaprog@tetteke.tk>
Tue, 29 Aug 2006 20:34:04 +0000 (05:34 +0900)
src/Hage1/thread_window.py

index b251af7..bff4178 100644 (file)
@@ -26,6 +26,8 @@ import pango
 import urllib2
 import urlparse
 import gnome
+import gobject
+import threading
 
 import misc
 import datfile
@@ -67,6 +69,42 @@ def open_thread(uri, update=False):
     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:
     hovering_over_link = False
     hand_cursor = gtk.gdk.Cursor(gtk.gdk.HAND2)
@@ -88,6 +126,9 @@ class WinWrap:
         self.size = 0
         self.num = 0
         self.title = None
+        self.lock_obj = False
+        self.jump_request_num = 0
+        self.progress = False
 
         glade_path = os.path.join(GLADE_DIR, GLADE_FILENAME)
         self.widget_tree = gtk.glade.XML(glade_path)
@@ -248,80 +289,126 @@ class WinWrap:
 
         if self.num > 0:
             if not self.title:
-                self.title = datfile.get_title_from_dat(
+                title = datfile.get_title_from_dat(
                     self.bbs, self.board, self.thread)
-                if self.title:
-                    self.window.set_title(self.title)
+                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)
 
-            session.thread_idx_updated(
-                self.bbs_type.get_thread_uri(), idx_dic)
+            gobject.idle_add(session.thread_idx_updated,
+                             self.bbs_type.get_thread_uri(), idx_dic)
 
     def update(self, widget=None):
-        line_count = datfile.get_dat_line_count(
-            self.bbs, self.board, self.thread)
-
-        if line_count > self.num:
-            datfile.load_dat_partly(
-                self.bbs, self.board, self.thread,
-                self.append_rawres_to_buffer, self.num+1)
-
-
-        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
-
-        dat_path = misc.get_thread_dat_path(self.bbs, self.board, self.thread)
-        dat_file = FileWrap(dat_path)
-
-        def save_line_and_append_to_buffer(line):
-            dat_file.seek(self.size)
-            dat_file.write(line)
-            self.append_rawres_to_buffer(line)
-
-        self.http_get_dat(save_line_and_append_to_buffer)
-        dat_file.close()
+
+        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, self.board, self.thread)
+            if line_count > self.num:
+                datfile.load_dat_partly(
+                    self.bbs, self.board, self.thread,
+                    self.append_rawres_to_buffer, self.num+1)
+
+                def do_jump(num):
+                    if self.jump_request_num:
+                        if self.jump_request_num <= num:
+                            # jump if enable, otherwize jump later.
+                            num = self.jump_request_num
+                            self.jump_request_num = 0
+                            mark = self.textbuffer.get_mark(str(num))
+                            if mark:
+                                self.textview.scroll_to_mark(
+                                    mark, 0, True, 0, 0)
+                    else:
+                        self.jump_to_the_end(num)
+
+                gobject.idle_add(do_jump, self.num)
+
+        def get():
+            dat_path = misc.get_thread_dat_path(
+                self.bbs, self.board, self.thread)
+            dat_file = FileWrap(dat_path)
+
+            def save_line_and_append_to_buffer(line):
+                dat_file.seek(self.size)
+                dat_file.write(line)
+                self.append_rawres_to_buffer(line)
+
+            self.http_get_dat(save_line_and_append_to_buffer)
+            dat_file.close()
+
+            def do_jump():
+                if self.jump_request_num:
+                    num = self.jump_request_num
+                    self.jump_request_num = 0
+                    mark = self.textbuffer.get_mark(str(num))
+                    if mark:
+                        self.textview.scroll_to_mark(mark, 0, True, 0, 0)
+
+            gobject.idle_add(do_jump)
+
+        if self.lock():
+
+            def on_end():
+                self.un_lock()
+                self.progress = False
+
+            self.progress = True
+            t = ThreadInvoker(lambda : gobject.idle_add(on_end), load, get)
+            t.start()
 
     def load_dat(self):
+
         self.size = 0
         self.num = 0
+        self.jump_request_num = 0
 
-        self.title = datfile.get_title_from_dat(
-            self.bbs, self.board, self.thread)
-        if self.title:
-            self.window.set_title(self.title)
+        def load():
+            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)
 
-        self.textbuffer.create_mark("1", self.enditer, True)
+            def create_mark():
+                self.textbuffer.create_mark("1", self.enditer, True)
+            gobject.idle_add(create_mark)
 
-        datfile.load_dat(self.bbs, self.board, self.thread,
-                         self.append_rawres_to_buffer)
-        if self.num > 0:
-            # jump to the last res.
-            mark = self.textbuffer.get_mark(str(self.num+1))
-            if mark:
-                self.textview.scroll_to_mark(mark, 0)
+            datfile.load_dat(self.bbs, self.board, self.thread,
+                             self.append_rawres_to_buffer)
+        def jump():
 
+            def do_jump(num):
+                if self.jump_request_num:
+                    num = self.jump_request_num
+                    self.jump_request_num = 0
+                    mark = self.textbuffer.get_mark(str(num))
+                    if mark:
+                        self.textview.scroll_to_mark(mark, 0, True, 0, 0)
+                else:
+                    self.jump_to_the_end(num)
+
+            gobject.idle_add(do_jump, self.num)
+
+        if self.lock():
+
+            def on_end():
+                self.un_lock()
+                self.progress = False
+
+            self.progress = True
+            t = ThreadInvoker(lambda : gobject.idle_add(on_end), load, jump)
+            t.start()
 
     def append_rawres_to_buffer(self, line):
         self.size += len(line)
@@ -330,13 +417,20 @@ class WinWrap:
         h = lambda name,mail,date,msg: self.reselems_to_buffer(
             self.num, name, mail, date, msg)
 
+        self.res_queue = []
         datfile.split_line_to_elems(line.decode("cp932", "replace"), h)
 
-        # for next res
-        self.textbuffer.create_mark(str(self.num+1), self.enditer, True)
+        def process_res_queue(res_queue, num):
+            self.process_queue(res_queue)
+            # 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):
-        p = barehtmlparser.BareHTMLParser(self.untiedata_to_buffer)
+        p = barehtmlparser.BareHTMLParser(
+            lambda d,b,h: self.res_queue.append((d,b,h,False)))
         # number
         p.feed(str(num) + " ")
 
@@ -351,7 +445,7 @@ class WinWrap:
         p.feed("<br>")
 
         # msg
-        p.reset_func(self.untiedata_to_buffer_with_leftmargin)
+        p.reset_func(lambda d,b,h: self.res_queue.append((d,b,h,True)))
         p.feed(msg.lstrip(" "))
 
         p.feed("<br><br>")
@@ -362,38 +456,38 @@ class WinWrap:
         tag.set_data("href", href)
         return tag
 
-    def untiedata_to_buffer(self, data, bold, href):
-        if bold:
+    def process_queue(self, queue):
+        for data, bold, href, margin in queue:
+            taglist = []
+            if bold:
+                taglist.append(self.boldtag)
             if href:
-                self.textbuffer.insert_with_tags(
-                    self.enditer, data, self.boldtag, self.href_tag(href))
-            else:                
-                self.textbuffer.insert_with_tags(
-                    self.enditer, data, self.boldtag)
-        else:
-            if href:
-                self.textbuffer.insert_with_tags(
-                    self.enditer, data, self.href_tag(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 untiedata_to_buffer_with_leftmargin(self, data, bold, href):
-        if bold:
-            if href:
-                self.textbuffer.insert_with_tags(
-                    self.enditer, data, self.boldtag,
-                    self.leftmargintag,self.href_tag(href))
-            else:
-                self.textbuffer.insert_with_tags(
-                    self.enditer, data, self.boldtag, self.leftmargintag)
+    def jump_to_the_end(self, num):
+        mark = self.textbuffer.get_mark(str(num+1))
+        if mark:
+            self.textview.scroll_to_mark(mark, 0)
+
+    def lock(self):
+        if self.lock_obj:
+            print "locked, try later."
+            return False
         else:
-            if href:
-                self.textbuffer.insert_with_tags(
-                    self.enditer, data,
-                    self.leftmargintag, self.href_tag(href))
-            else:
-                self.textbuffer.insert_with_tags(
-                    self.enditer, data, self.leftmargintag)
+            print "get lock"
+            self.lock_obj = True
+            return True
+
+    def un_lock(self):
+        self.lock_obj = False
+        print "unlock"
 
     def jump_to_res(self, uri):
         strict_uri = self.bbs_type.get_thread_uri()
@@ -405,6 +499,9 @@ class WinWrap:
                 mark = self.textbuffer.get_mark(resnum)
                 if mark:
                     self.textview.scroll_to_mark(mark, 0, True, 0, 0)
+                elif self.progress:
+                    # try later.
+                    self.jump_request_num = int(resnum)
 
     def load(self, update=False):
         dat_path = misc.get_thread_dat_path(