Skip to content
This repository has been archived by the owner on Nov 19, 2023. It is now read-only.

Commit

Permalink
Add StatusNotifier to ICS plugin.
Browse files Browse the repository at this point in the history
ICS connection state is now reflected in status bar messages.
  • Loading branch information
pcapriotti committed Dec 20, 2011
1 parent 7a22cd7 commit a3aa440
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 7 deletions.
5 changes: 4 additions & 1 deletion lib/mainwindow.rb
Expand Up @@ -24,6 +24,7 @@
require 'theme_prefs'
require 'view'
require 'multiview'
require 'status_bar'

require 'kaya_ui'

Expand Down Expand Up @@ -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

Expand Down
17 changes: 12 additions & 5 deletions lib/plugins/ics/ics.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion lib/plugins/ics/lib/connection.rb
Expand Up @@ -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 }
Expand Down Expand Up @@ -59,6 +59,7 @@ def start

def stop
@socket.close
fire :closed
end

def send_text(text)
Expand Down
42 changes: 42 additions & 0 deletions lib/plugins/ics/lib/status_observer.rb
@@ -0,0 +1,42 @@
# Copyright (c) 2009 Paolo Capriotti <p.capriotti@gmail.com>
#
# 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
22 changes: 22 additions & 0 deletions lib/status_bar.rb
@@ -0,0 +1,22 @@
# Copyright (c) 2009 Paolo Capriotti <p.capriotti@gmail.com>
#
# 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

0 comments on commit a3aa440

Please sign in to comment.