bugfix: safety against breaking the AI if you press undo while it's thinking
[kaya.git] / lib / plugins / ics / lib / status_observer.rb
blob662fa65791db8c220a136c13215edd23ced7031f
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 => KDE.i18n("Disconnected from ICS Server") },
11     :established => { :temporary => KDE.i18n("Connection established") },
12     :connecting => { :permanent => KDE.i18n("Connecting...") },
13     :logging_in => { :permanent => KDE.i18n("Logging in...") },
14     :logged_in => { :temporary => KDE.i18n("Logged in"), :permanent => KDE.i18n("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