]> git.cworth.org Git - sup/blobdiff - lib/sup/poll.rb
fix reply mode/edit field interactions
[sup] / lib / sup / poll.rb
index 9fc8b6f72b65103b056090d258b3d39d8009d1e6..822521d4da0bfe85febedf12f246449563d954e7 100644 (file)
@@ -9,6 +9,7 @@ class PollManager
 
   def initialize
     @mutex = Mutex.new
+    @thread = nil
     @last_poll = nil
     
     self.class.i_am_the_instance self
@@ -29,8 +30,8 @@ class PollManager
     [num, numi]
   end
 
-  def start_thread
-    Redwood::reporting_thread do
+  def start
+    @thread = Redwood::reporting_thread do
       while true
         sleep DELAY / 2
         poll if @last_poll.nil? || (Time.now - @last_poll) >= DELAY
@@ -38,21 +39,37 @@ class PollManager
     end
   end
 
+  def stop
+    @thread.kill if @thread
+    @thread = nil
+  end
+
   def do_poll
     total_num = total_numi = 0
     @mutex.synchronize do
-      found = {}
       Index.usual_sources.each do |source|
-        yield "Loading from #{source}... " unless source.done? || source.broken?
+#        yield "source #{source} is done? #{source.done?} (cur_offset #{source.cur_offset} >= #{source.end_offset})"
+        begin
+          yield "Loading from #{source}... " unless source.done? || source.has_errors?
+        rescue SourceError => e
+          Redwood::log "problem getting messages from #{source}: #{e.message}"
+          Redwood::report_broken_sources
+          next
+        end
+
         num = 0
         numi = 0
-        Index.add_new_messages_from source do |m, offset, source_labels, entry|
-          yield "Found message at #{offset} with labels #{m.labels * ', '}"
-          num += 1
-          numi += 1 if m.labels.include? :inbox
+        add_messages_from source do |m, offset, entry|
+          ## always preserve the labels on disk.
+          m.labels = entry[:label].split(/\s+/).map { |x| x.intern } if entry
+          yield "Found message at #{offset} with labels {#{m.labels * ', '}}"
+          unless entry
+            num += 1
+            numi += 1 if m.labels.include? :inbox
+          end
           m
         end
-        yield "Found #{num} messages, #{numi} to inbox" unless num == 0
+        yield "Found #{num} messages, #{numi} to inbox." unless num == 0
         total_num += num
         total_numi += numi
       end
@@ -63,6 +80,56 @@ class PollManager
     end
     [total_num, total_numi]
   end
+
+  ## this is the main mechanism for adding new messages to the
+  ## index. it's called both by sup-sync and by PollMode.
+  ##
+  ## for each message in the source, starting from the source's
+  ## starting offset, this methods yields the message, the source
+  ## offset, and the index entry on disk (if any). it expects the
+  ## yield to return the message (possibly altered in some way), and
+  ## then adds it (if new) or updates it (if previously seen).
+  ##
+  ## the labels of the yielded message are the default source
+  ## labels. it is likely that callers will want to replace these with
+  ## the index labels, if they exist, so that state is not lost when
+  ## e.g. a new version of a message from a mailing list comes in.
+  def add_messages_from source
+    begin
+      return if source.done? || source.has_errors?
+      
+      source.each do |offset, labels|
+        if source.has_errors?
+          Redwood::log "error loading messages from #{source}: #{source.error.message}"
+          return
+        end
+      
+        labels.each { |l| LabelManager << l }
+        labels = labels + (source.archived? ? [] : [:inbox])
+
+        begin
+          m = Message.new :source => source, :source_info => offset, :labels => labels
+          if m.source_marked_read?
+            m.remove_label :unread
+            labels.delete :unread
+          else
+            m.add_label :unread
+            labels << :unread
+          end
+
+          docid, entry = Index.load_entry_for_id m.id
+          m = yield(m, offset, entry) or next
+          Index.sync_message m, docid, entry
+          UpdateManager.relay self, :add, m unless entry
+        rescue MessageFormatError => e
+          Redwood::log "ignoring erroneous message at #{source}##{offset}: #{e.message}"
+        end
+      end
+    rescue SourceError => e
+      Redwood::log "problem getting messages from #{source}: #{e.message}"
+      Redwood::report_broken_sources
+    end
+  end
 end
 
 end