Translate status bar messages.
[kaya.git] / lib / plugins / ics / ics.rb
blobae9e2b178a75025d76d2b0384fd0fa84f84982b1
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
64     
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') do |parent|
73       if @connection
74         @connection.stop
75         @connection = nil
76         if @console_obs
77           parent.console.delete_observer(@console_obs)
78           @console_obs = nil
79         end
80       end
81     end
82     action(:configure_ics,
83            :icon => 'network-workgroup',
84            :text => KDE.i18n("Configure &ICS...")) do |parent|
85       dialog = ICS::Preferences.new(parent)
86       dialog.show
87     end
88   end
90   def connect_to_ics(parent)
91     protocol = ICS::Protocol.new(false)
92     @connection = ICS::Connection.new('freechess.org', 23)
94     config = ICS::Config.load
95     protocol.add_observer ICS::AuthModule.new(@connection, 
96       config[:username], config[:password])
97     protocol.add_observer ICS::StartupModule.new(@connection)
98     protocol.link_to(@connection)
100     status_observer = StatusObserver.new(
101       lambda {|msg| parent.status_bar.show_message(msg, 2000) },
102       lambda {|msg| parent.status_bar.show_permanent_message(msg) })
103     status_observer.link_to(@connection, protocol)
105     protocol.on :text do |text|
106       parent.console.append(text)
107     end
109     @console_obs = parent.console.observe :input do |text|
110       if @connection
111         @connection.send_text text
112       end
113     end
115     # create an ICS view
116     @view = ICSView.new(parent.view)
117     @handler = ICS::MatchHandler.new(@view, protocol)
118     @connection.start
119   end