]> git.cworth.org Git - sup/blob - lib/sup/poll.rb
fix reply mode/edit field interactions
[sup] / lib / sup / poll.rb
1 require 'thread'
2
3 module Redwood
4
5 class PollManager
6   include Singleton
7
8   DELAY = 300
9
10   def initialize
11     @mutex = Mutex.new
12     @thread = nil
13     @last_poll = nil
14     
15     self.class.i_am_the_instance self
16   end
17
18   def buffer
19     BufferManager.spawn_unless_exists("<poll for new messages>", :hidden => true) { PollMode.new }
20   end
21
22   def poll
23     BufferManager.flash "Polling for new messages..."
24     num, numi = buffer.mode.poll
25     if num > 0
26       BufferManager.flash "Loaded #{num} new messages, #{numi} to inbox." 
27     else
28       BufferManager.flash "No new messages." 
29     end
30     [num, numi]
31   end
32
33   def start
34     @thread = Redwood::reporting_thread do
35       while true
36         sleep DELAY / 2
37         poll if @last_poll.nil? || (Time.now - @last_poll) >= DELAY
38       end
39     end
40   end
41
42   def stop
43     @thread.kill if @thread
44     @thread = nil
45   end
46
47   def do_poll
48     total_num = total_numi = 0
49     @mutex.synchronize do
50       Index.usual_sources.each do |source|
51 #        yield "source #{source} is done? #{source.done?} (cur_offset #{source.cur_offset} >= #{source.end_offset})"
52         begin
53           yield "Loading from #{source}... " unless source.done? || source.has_errors?
54         rescue SourceError => e
55           Redwood::log "problem getting messages from #{source}: #{e.message}"
56           Redwood::report_broken_sources
57           next
58         end
59
60         num = 0
61         numi = 0
62         add_messages_from source do |m, offset, entry|
63           ## always preserve the labels on disk.
64           m.labels = entry[:label].split(/\s+/).map { |x| x.intern } if entry
65           yield "Found message at #{offset} with labels {#{m.labels * ', '}}"
66           unless entry
67             num += 1
68             numi += 1 if m.labels.include? :inbox
69           end
70           m
71         end
72         yield "Found #{num} messages, #{numi} to inbox." unless num == 0
73         total_num += num
74         total_numi += numi
75       end
76
77       yield "Done polling; loaded #{total_num} new messages total"
78       @last_poll = Time.now
79       @polling = false
80     end
81     [total_num, total_numi]
82   end
83
84   ## this is the main mechanism for adding new messages to the
85   ## index. it's called both by sup-sync and by PollMode.
86   ##
87   ## for each message in the source, starting from the source's
88   ## starting offset, this methods yields the message, the source
89   ## offset, and the index entry on disk (if any). it expects the
90   ## yield to return the message (possibly altered in some way), and
91   ## then adds it (if new) or updates it (if previously seen).
92   ##
93   ## the labels of the yielded message are the default source
94   ## labels. it is likely that callers will want to replace these with
95   ## the index labels, if they exist, so that state is not lost when
96   ## e.g. a new version of a message from a mailing list comes in.
97   def add_messages_from source
98     begin
99       return if source.done? || source.has_errors?
100       
101       source.each do |offset, labels|
102         if source.has_errors?
103           Redwood::log "error loading messages from #{source}: #{source.error.message}"
104           return
105         end
106       
107         labels.each { |l| LabelManager << l }
108         labels = labels + (source.archived? ? [] : [:inbox])
109
110         begin
111           m = Message.new :source => source, :source_info => offset, :labels => labels
112           if m.source_marked_read?
113             m.remove_label :unread
114             labels.delete :unread
115           else
116             m.add_label :unread
117             labels << :unread
118           end
119
120           docid, entry = Index.load_entry_for_id m.id
121           m = yield(m, offset, entry) or next
122           Index.sync_message m, docid, entry
123           UpdateManager.relay self, :add, m unless entry
124         rescue MessageFormatError => e
125           Redwood::log "ignoring erroneous message at #{source}##{offset}: #{e.message}"
126         end
127       end
128     rescue SourceError => e
129       Redwood::log "problem getting messages from #{source}: #{e.message}"
130       Redwood::report_broken_sources
131     end
132   end
133 end
134
135 end