Add StatusNotifier to ICS plugin.
[kaya.git] / lib / plugins / ics / lib / status_observer.rb
blobb438fa30f4f68b8471fce9c468f1b96f72b093bf
1 # Copyright (c) 2009 Paolo Capriotti <p.capriotti@gmail.com>
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
8 class StatusObserver
9   MESSAGES = {
10     :disconnected => { :permanent => '', :temporary => "Disconnected from ICS Server" },
11     :established => { :temporary => "Connection established" },
12     :connecting => { :permanent => "Connecting..." },
13     :logging_in => { :permanent => "Logging in..." },
14     :logged_in => { :temporary => "Logged in", :permanent => "Connected to ICS server" }
15   }
17   def initialize(set_temporary, set_permanent)
18     @set_temporary = set_temporary
19     @set_permanent = set_permanent
20     switch_to(:disconnected)
21   end
23   def link_to(connection, protocol)
24     connection.on(:connecting) { switch_to :connecting }
25     connection.on(:established) { switch_to :established }
26     connection.on(:closed) { switch_to :disconnected }
27     protocol.on(:login_prompt) { switch_to :logging_in }
28     protocol.observe_limited(:prompt) do
29       switch_to(:logged_in)
30       true
31     end
32   end
34   def switch_to(state)
35     @state = state
36     messages = MESSAGES[state]
37     if messages
38       @set_permanent[messages[:permanent]] if messages[:permanent]
39       @set_temporary[messages[:temporary]] if messages[:temporary]
40     end
41   end
42 end