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.
9 require 'plugins/plugin'
10 require 'action_provider'
11 require_bundle 'ics', 'config'
12 require_bundle 'ics', 'connection'
13 require_bundle 'ics', 'match_handler'
14 require_bundle 'ics', 'preferences'
15 require_bundle 'ics', 'protocol'
16 require_bundle 'ics', 'status_observer'
20 include ActionProvider
28 if not @main or @main.closed?
29 @main = create(:name => 'ICS')
34 def create(opts = { })
38 def activate(user, name)
39 @view.activate(user, name)
43 plugin :name => 'ICS Plugin',
44 :interface => :action_provider
49 @gui = KDE::gui(:icsplugin) do |g|
52 m.action :connect, :group => :file_extensions
53 m.action :disconnect, :group => :file_extensions
55 mb.menu(:settings) do |m|
56 m.action :configure_ics
59 g.tool_bar(:ics_toolbar, :text => KDE::i18n("&ICS Toolbar")) do |tb|
66 :text => KDE.i18n("&Connect to ICS"),
67 :icon => 'network-connect') do |parent|
68 connect_to_ics(parent)
71 :text => KDE.i18n("&Disconnect from ICS"),
72 :icon => 'network-disconnect',
73 :enabled => false) do |parent|
74 disconnect_from_ics(parent)
76 action(:configure_ics,
77 :icon => 'network-workgroup',
78 :text => KDE.i18n("Configure &ICS...")) do |parent|
79 dialog = ICS::Preferences.new(parent)
84 def disconnect_from_ics(parent)
89 parent.console.delete_observer(@console_obs)
93 # update action states
94 parent.action("connect").enabled = true
95 parent.action("disconnect").enabled = false
99 def connect_to_ics(parent)
100 protocol = ICS::Protocol.new(false)
101 @connection = ICS::Connection.new('freechess.org', 23)
103 config = ICS::Config.load
104 protocol.add_observer ICS::AuthModule.new(@connection,
105 config[:username], config[:password])
106 protocol.add_observer ICS::StartupModule.new(@connection)
107 protocol.link_to(@connection)
109 status_observer = StatusObserver.new(
110 lambda {|msg| parent.status_bar.show_message(msg, 2000) },
111 lambda {|msg| parent.status_bar.show_permanent_message(msg) })
112 status_observer.link_to(@connection, protocol)
114 protocol.on :text do |text|
115 parent.console.append(text)
118 @console_obs = parent.console.observe :input do |text|
120 @connection.send_text text
125 @view = ICSView.new(parent.view)
126 @handler = ICS::MatchHandler.new(@view, protocol)
129 # update action states
130 parent.action("connect").enabled = false
131 parent.action("disconnect").enabled = true