Update connect/disconnect action states.
[kaya.git] / lib / toolkits / kde.rb
blob2b15ddbb591e53bd0a5735dbddbc1c554bffbb9b
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 'toolkits/qt'
9 require 'builder'
10 begin 
11   require 'kio' 
12 rescue LoadError
13 end
15 class KDE::Dialog
16   include Layoutable
17   
18   def setGUI(gui)
19     self.caption = gui.opts[:caption]
20     widget = Qt::Widget.new(self)
21     widget.owner = self
22     widget.setGUI(gui)
23     self.main_widget = widget
24   end
25 end
27 class KDE::Application
28   # 
29   # Initialize an application.
30   # 
31   def self.init(data)
32     about = KDE::AboutData.new(
33       data[:id],
34       data[:id],
35       data[:name] || KDE::ki18n(data[:id]),
36       data[:version] || "0.1",
37       data[:description] || KDE::ki18n(""),
38       KDE::AboutData::License_GPL,
39       data[:copyright] || KDE::ki18n(""))
40     (data[:authors] || []).each do |name, email|
41       about.addAuthor(name, KDE::LocalizedString.new, email)
42     end
43     (data[:contributors] || []).each do |name, contribution|
44       about.addCredit(name, contribution)
45     end
46     about.bug_address = Qt::ByteArray.new(data[:bug_tracker] || "")
47     
48     KDE::CmdLineArgs.init(ARGV, about)
49     KDE::CmdLineOptions.new.tap do |opts|
50       (data[:options] || []).each do |args|
51         case args.size
52         when 2
53           opts.add(args[0], args[1])
54         when 3
55           opts.add(args[0], args[1], args[2])
56         end
57       end
58       KDE::CmdLineArgs.add_cmd_line_options opts
59     end
61     KDE::Application.new
62   end
63 end
65 class KDE::CmdLineArgs
66   def [](i)
67     arg(i)
68   end
69   
70   def is_set(name)
71     isSet(Qt::ByteArray.new(name))
72   end
73 end
75 class KDE::ActionCollection  
76   def []=(name, action)
77     unless action.is_a? KDE::Action
78       orig_action = action
79       action = KDE::Action.new(action.text, action.parent)
80       action.icon = orig_action.icon
81       action.checkable = orig_action.checkable
82       action.checked = orig_action.checked
83       action.on(:triggered) { orig_action.trigger }
84       orig_action.on(:changed) { action.checked = orig_action.checked }
85     end
86     add_action(name.to_s, action)
87   end
88 end
90 class KDE::XmlGuiWindow
91   def setGUI(gui)
92     KDE::with_xml_gui(gui) do |file|
93       setupGUI(KDE::XmlGuiWindow::Default, file)
94     end
95   end
96   
97   def saveGUI
98   end
99 end
101 class KDE::XMLGUIClient
102   def setGUI(gui)
103     KDE::with_xml_gui(gui) do |file|
104       setXMLFile(file)
105     end
106   end
109 module ActionHandler
110   def std_action(action, opts = {}, &blk)
111     target, slot = get_slot(opts[:slot], &blk)
112     KDE::StandardAction.method_missing(action, target, slot, action_collection)
113   end
114   
115   def get_slot(s = nil, &blk)
116     target, slot = if block_given?
117       [Qt::SignalBlockInvocation.new(action_parent, blk, 'invoke()'), SLOT('invoke()')]
118     else
119       [action_parent, SLOT(s)]
120     end
121   end
122   
123   def regular_action(name, opts, &blk)
124     KDE::Action.new(KDE::Icon.from_theme(opts[:icon]), 
125                     opts[:text], action_parent).tap do |a|
126       action_collection.add_action(name.to_s, a)  
127       a.connect(SIGNAL('triggered(bool)'), &blk)
128       a.tool_tip = opts[:tooltip] if opts[:tooltip]
129       a.shortcut = opts[:shortcut] if opts[:shortcut]
130       a.enabled = opts.fetch(:enabled, true)
131     end
132   end
133   
134   def action_parent
135     self
136   end
138   def plug_action_list(name, actions)
139     plugActionList(name.to_s, actions)
140   end
142   def unplug_action_list(name)
143     unplugActionList(name.to_s)
144   end
147 class KDE::Icon
148   def self.from_theme(name)
149     if name
150       new(name.to_s)
151     else
152       new
153     end
154   end
157 class KDE::ConfigGroup
158   def each_group
159     group_list.each do |g|
160       yield group(g)
161     end
162   end
165 module KDE
166   def self.gui(name, &blk)
167     "<!DOCTYPE kpartgui SYSTEM \"kpartgui.dtd\">\n" + 
168     GuiBuilder.new.gui({ :version => 2, :name => name }, &blk)
169   end
170   
171   def self.with_xml_gui(xml, &blk)
172     tmp = TemporaryFile.new
173     tmp.open
174     
175     ::File.open(tmp.file_name, 'w') do |f|
176       f.write(xml)
177     end
178     blk[tmp.file_name]
179   ensure
180     tmp.close
181     ::File.unlink(tmp.file_name)
182   end
183   
184   class GuiBuilder < Builder::XmlMarkup
185     def initialize
186       super
187       @action_opts = { }
188     end
190     def menu_bar(&blk)
191       MenuBar(&blk)
192     end
193     
194     def menu(name, opts = {}, &blk)
195       Menu(:name => name) do |m|
196         m.text(opts[:text]) if opts[:text]
197         blk[m] if block_given?
198       end
199     end
200     
201     def action(name, opts = {})
202       Action(opts.merge(@action_opts).merge(:name => name))
203     end
204     
205     def separator
206       self.Separator
207     end
208     
209     def tool_bar(name, opts = { }, &blk)
210       ToolBar(:name => name) do |tb|
211         tb.text(opts[:text]) if opts[:text]
212         blk[tb] if block_given?
213       end
214     end
215     
216     def action_list(name)
217       ActionList(:name => name)
218     end
219     
220     def group(name, &blk)
221       if block_given?
222         @action_opts = { :group => name }
223         blk[self]
224         @action_opts = { }
225       else
226         DefineGroup(:name => name)
227       end
228     end
229   end
230   
231   def self.active_color
232     scheme = KDE::ColorScheme.new(Qt::Palette::Active, KDE::ColorScheme::Window)
233     color = scheme.foreground(KDE::ColorScheme::PositiveText).color
234   end
235   
236   def self.std_shortcut(name)
237     code = KDE::StandardShortcut.send(name.to_s.capitalize)
238     StandardShortcut::shortcut(code)
239   end
242 class KDE::TabWidget
243   include Layoutable
246 class KDE::Process
247   def output_channel_mode=(value)
248     c = self.class.const_get("#{value.to_s.capitalize.camelize}Channel")
249     setOutputChannelMode(c)
250   end
251   
252   def self.split_args(str)
253     KDE::Shell.split_args(str)
254   end
255   
256   def run(path, args)
257     set_program(path, args)
258     start
259   end
262 def KDE.download_tempfile(url, parent)
263   result = ""
264   if KIO::NetAccess.download(url, result, parent)
265     result
266   end