From a3aa440f7a8201dd76bf9fe27179ea692873bdfe Mon Sep 17 00:00:00 2001
From: Paolo Capriotti
Date: Tue, 20 Dec 2011 17:11:26 +0000
Subject: [PATCH] Add StatusNotifier to ICS plugin.
ICS connection state is now reflected in status bar messages.
---
lib/mainwindow.rb | 5 ++-
lib/plugins/ics/ics.rb | 17 ++++++++---
lib/plugins/ics/lib/connection.rb | 3 +-
lib/plugins/ics/lib/status_observer.rb | 42 ++++++++++++++++++++++++++
lib/status_bar.rb | 22 ++++++++++++++
5 files changed, 82 insertions(+), 7 deletions(-)
create mode 100644 lib/plugins/ics/lib/status_observer.rb
create mode 100644 lib/status_bar.rb
diff --git a/lib/mainwindow.rb b/lib/mainwindow.rb
index 07d039c..ee72f35 100644
--- a/lib/mainwindow.rb
+++ b/lib/mainwindow.rb
@@ -24,6 +24,7 @@
require 'theme_prefs'
require 'view'
require 'multiview'
+require 'status_bar'
require 'kaya_ui'
@@ -209,7 +210,9 @@ def startup(game)
console_dock.window_flags = console_dock.window_flags & ~Qt::WindowStaysOnTopHint
console_dock.show
action_collection[:toggle_console] = console_dock.toggle_view_action
-
+
+ self.status_bar = StatusBar.new(self)
+
self.central_widget = @view
end
diff --git a/lib/plugins/ics/ics.rb b/lib/plugins/ics/ics.rb
index 9455529..ae9e2b1 100644
--- a/lib/plugins/ics/ics.rb
+++ b/lib/plugins/ics/ics.rb
@@ -8,11 +8,12 @@
require 'toolkit'
require 'plugins/plugin'
require 'action_provider'
-require_bundle 'ics', 'protocol'
+require_bundle 'ics', 'config'
require_bundle 'ics', 'connection'
require_bundle 'ics', 'match_handler'
require_bundle 'ics', 'preferences'
-require_bundle 'ics', 'config'
+require_bundle 'ics', 'protocol'
+require_bundle 'ics', 'status_observer'
class ICSPlugin
include Plugin
@@ -85,15 +86,21 @@ def initialize
dialog.show
end
end
-
+
def connect_to_ics(parent)
- protocol = ICS::Protocol.new(:debug)
+ protocol = ICS::Protocol.new(false)
@connection = ICS::Connection.new('freechess.org', 23)
+
config = ICS::Config.load
protocol.add_observer ICS::AuthModule.new(@connection,
config[:username], config[:password])
protocol.add_observer ICS::StartupModule.new(@connection)
- protocol.link_to @connection
+ protocol.link_to(@connection)
+
+ status_observer = StatusObserver.new(
+ lambda {|msg| parent.status_bar.show_message(msg, 2000) },
+ lambda {|msg| parent.status_bar.show_permanent_message(msg) })
+ status_observer.link_to(@connection, protocol)
protocol.on :text do |text|
parent.console.append(text)
diff --git a/lib/plugins/ics/lib/connection.rb b/lib/plugins/ics/lib/connection.rb
index e585e45..5bee78b 100644
--- a/lib/plugins/ics/lib/connection.rb
+++ b/lib/plugins/ics/lib/connection.rb
@@ -19,7 +19,7 @@ def initialize(host, port)
super nil
@create_socket = lambda do
- puts "connecting to #{host}:#{port}"
+ fire :connecting
s = Qt::TcpSocket.new(self)
s.on(:host_found) { fire :host_found }
s.on(:connected) { fire :established }
@@ -59,6 +59,7 @@ def start
def stop
@socket.close
+ fire :closed
end
def send_text(text)
diff --git a/lib/plugins/ics/lib/status_observer.rb b/lib/plugins/ics/lib/status_observer.rb
new file mode 100644
index 0000000..b438fa3
--- /dev/null
+++ b/lib/plugins/ics/lib/status_observer.rb
@@ -0,0 +1,42 @@
+# Copyright (c) 2009 Paolo Capriotti
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+
+class StatusObserver
+ MESSAGES = {
+ :disconnected => { :permanent => '', :temporary => "Disconnected from ICS Server" },
+ :established => { :temporary => "Connection established" },
+ :connecting => { :permanent => "Connecting..." },
+ :logging_in => { :permanent => "Logging in..." },
+ :logged_in => { :temporary => "Logged in", :permanent => "Connected to ICS server" }
+ }
+
+ def initialize(set_temporary, set_permanent)
+ @set_temporary = set_temporary
+ @set_permanent = set_permanent
+ switch_to(:disconnected)
+ end
+
+ def link_to(connection, protocol)
+ connection.on(:connecting) { switch_to :connecting }
+ connection.on(:established) { switch_to :established }
+ connection.on(:closed) { switch_to :disconnected }
+ protocol.on(:login_prompt) { switch_to :logging_in }
+ protocol.observe_limited(:prompt) do
+ switch_to(:logged_in)
+ true
+ end
+ end
+
+ def switch_to(state)
+ @state = state
+ messages = MESSAGES[state]
+ if messages
+ @set_permanent[messages[:permanent]] if messages[:permanent]
+ @set_temporary[messages[:temporary]] if messages[:temporary]
+ end
+ end
+end
diff --git a/lib/status_bar.rb b/lib/status_bar.rb
new file mode 100644
index 0000000..ff774d9
--- /dev/null
+++ b/lib/status_bar.rb
@@ -0,0 +1,22 @@
+# Copyright (c) 2009 Paolo Capriotti
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+
+require 'toolkit'
+
+class StatusBar < KDE::StatusBar
+ def initialize(parent)
+ super(parent)
+
+ @label = Qt::Label.new(self)
+
+ add_widget(@label)
+ end
+
+ def show_permanent_message(msg)
+ @label.text = msg
+ end
+end