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