Update connect/disconnect action states.
[kaya.git] / lib / plugins / ics / ics.rb
blobafb6e6ec5c318a01c33e80d6fa68ef38c4d77242
1 # Copyright (c) 2009 Paolo Capriotti <p.capriotti@gmail.com>
2
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 require 'toolkit'
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'
18 class ICSPlugin
19   include Plugin
20   include ActionProvider
21   
22   class ICSView
23     def initialize(view)
24       @view = view
25     end
26     
27     def main
28       if not @main or @main.closed?
29         @main = create(:name => 'ICS')
30       end
31       @main
32     end
33     
34     def create(opts = { })
35       @view.create(opts)
36     end
37     
38     def activate(user, name)
39       @view.activate(user, name)
40     end
41   end
42   
43   plugin :name => 'ICS Plugin',
44          :interface => :action_provider
45     
46   attr_reader :gui
47   
48   def initialize
49     @gui = KDE::gui(:icsplugin) do |g|
50       g.menu_bar do |mb|
51         mb.menu(:game) do |m|
52           m.action :connect, :group => :file_extensions
53           m.action :disconnect, :group => :file_extensions
54         end
55         mb.menu(:settings) do |m|
56           m.action :configure_ics
57         end
58       end
59       g.tool_bar(:ics_toolbar, :text => KDE::i18n("&ICS Toolbar")) do |tb|
60         tb.action :connect
61         tb.action :disconnect
62       end
63     end
65     action(:connect,
66            :text => KDE.i18n("&Connect to ICS"),
67            :icon => 'network-connect') do |parent|
68       connect_to_ics(parent)
69     end
70     action(:disconnect,
71            :text => KDE.i18n("&Disconnect from ICS"),
72            :icon => 'network-disconnect',
73            :enabled => false) do |parent|
74       disconnect_from_ics(parent)
75     end
76     action(:configure_ics,
77            :icon => 'network-workgroup',
78            :text => KDE.i18n("Configure &ICS...")) do |parent|
79       dialog = ICS::Preferences.new(parent)
80       dialog.show
81     end
82   end
84   def disconnect_from_ics(parent)
85     if @connection
86       @connection.stop
87       @connection = nil
88       if @console_obs
89         parent.console.delete_observer(@console_obs)
90         @console_obs = nil
91       end
93       # update action states
94       parent.action("connect").enabled = true
95       parent.action("disconnect").enabled = false
96     end
97   end
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)
116     end
118     @console_obs = parent.console.observe :input do |text|
119       if @connection
120         @connection.send_text text
121       end
122     end
124     # create an ICS view
125     @view = ICSView.new(parent.view)
126     @handler = ICS::MatchHandler.new(@view, protocol)
127     @connection.start
129     # update action states
130     parent.action("connect").enabled = false
131     parent.action("disconnect").enabled = true
132   end