1#!/usr/local/bin/ruby
2
3# This is a part of the external Ruby Interface for Cairo-Dock
4
5# Author: Fabounet and Eduardo Mucelli Rezende Oliveira
6# E-mail: fabounet@glx-dock.org, edumucelli@gmail.com or eduardom@dcc.ufmg.br
7#
8# This program is free software: you can redistribute it and/or modify
9#    it under the terms of the GNU General Public License as published by
10#    the Free Software Foundation, either version 3 of the License, or
11#    (at your option) any later version.
12
13# This program is distributed in the hope that it will be useful,
14#    but WITHOUT ANY WARRANTY; without even the implied warranty of
15#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16#    GNU General Public License for more details.
17
18# This applet monitors the battery through acpi module. It is possbile to show a dialog message
19#    containing the baterry status, charge, and temperature by left-clicking on the icon.
20#    Also, through the configuration panel it is possible to set the icon's label,
21#	 show the % of carge as quick info, and activate an alert message to be shown when the charge is critically low.
22
23require 'rubygems'
24gem 'ruby-dbus'
25require 'dbus'
26require 'parseconfig'
27
28class CDApplet
29	attr_accessor :cConfFile, :cAppletName, :icon, :sub_icons, :config, :bus, :_cMenuIconId, :cParentAppName, :cBusPath, :cRootDataDir, :cShareDataDir
30
31	BOTTOM = 0
32	TOP    = 1
33	RIGHT  = 2
34	LEFT   = 3
35
36	DOCK    = 0
37	DESKLET = 1
38
39	EMBLEM_TOP_LEFT     = 0
40	EMBLEM_BOTTOM_RIGHT = 1
41	EMBLEM_BOTTOM_LEFT  = 2
42	EMBLEM_TOP_RIGHT    = 3
43	EMBLEM_MIDDLE       = 4
44	EMBLEM_BOTTOM       = 5
45	EMBLEM_TOP          = 6
46	EMBLEM_RIGHT        = 7
47	EMBLEM_LEFT         = 8
48	EMBLEM_PERSISTENT   = 0
49	EMBLEM_PRINT        = 9
50
51	MENU_ENTRY        = 0
52	MENU_SUB_MENU     = 1
53	MENU_SEPARATOR    = 2
54	MENU_CHECKBOX     = 3
55	MENU_RADIO_BUTTON = 4
56
57	MAIN_MENU_ID = 0
58
59	DIALOG_KEY_ENTER = -1
60	DIALOG_KEY_ESCAPE = -2
61
62	def initialize
63		self.config = {}
64		self.bus = nil
65		self.icon = nil
66		self.sub_icons = nil
67		self._cMenuIconId = nil
68		self.cAppletName = $0[2,999]
69		self.cBusPath = ARGV[1]
70		self.cConfFile = ARGV[2]
71		self.cRootDataDir = ARGV[3]
72		self.cParentAppName = ARGV[4]
73		self.cShareDataDir = Dir.getwd
74
75		self._get_config()
76		self._connect_to_dock()
77	end
78
79	def run
80		self.start()
81		loop = DBus::Main.new
82		loop << self.bus
83		loop.run
84		puts ">>> applet '#{self.cAppletName}' terminated."
85		exit
86	end
87
88	##################################
89	### callbacks on the main icon ###
90	##################################
91
92	def on_click iState
93		### action on click
94	end
95
96	def on_middle_click
97		puts ">>> on_middle_click"
98		### action on middle-click
99	end
100
101	def _on_build_menu
102		self._cMenuIconId = nil
103		self.on_build_menu()
104	end
105
106	def on_build_menu
107		### build our menu
108	end
109
110	def _on_menu_select(iNumEntry)
111		if self._cMenuIconId == nil
112			self.on_menu_select(iNumEntry)
113		else
114			self.on_menu_select_sub_icon(iNumEntry, self._cMenuIconId)
115		end
116	end
117
118	def on_menu_select(iNumEntry)
119		### action on selecting an entry of our menu """
120	end
121
122	def on_scroll(bScrollUp)
123		### action on scroll
124	end
125
126	def on_drop_data(cReceivedData)
127		### action on dropping something on our applet
128	end
129
130	def on_answer(answer)
131		### action on answering a dialog
132	end
133
134	def on_answer_dialog(button, answer)
135		### action on answering a dialog
136	end
137
138	def on_shortkey(cKey)
139		### action on pressing one of the shortkeys we bound beforehand
140	end
141
142	def on_change_focus(bIsActive)
143		### action when the window controlled by the applet takes or looses the focus
144	end
145
146	##################################
147	### callbacks on the sub-icons ###
148	##################################
149	def on_click_sub_icon(iState, cIconID)
150		### action on click on one of our sub-icons
151	end
152
153	def on_middle_click_sub_icon(cIconID)
154		### action on middle-click on one of our sub-icons
155	end
156
157	def on_scroll_sub_icon(bScrollUp, cIconID)
158		### action on scroll on one of our sub-icons
159	end
160
161	def _on_build_menu_sub_icon(cIconID)
162		self._cMenuIconId = cIconID
163		self.on_build_menu_sub_icon(cIconID)
164	end
165
166	def on_build_menu_sub_icon(cIconID)
167		### action on build menu on one of our sub-icons
168	end
169
170	def on_drop_data_sub_icon(cReceivedData, cIconID)
171		### action on drop data on one of our sub-icons
172	end
173
174	def on_menu_select_sub_icon(iNumEntry, cIconID)
175		### action on select entry in the menu on one of our sub-icons
176	end
177
178
179	###############################
180	### callbacks on the applet ###
181	###############################
182
183	def start
184		### action when the applet is started
185	end
186
187	def stop
188		### action when the applet is terminated
189	end
190
191	def _on_stop
192		self.stop()
193		exit
194	end
195
196	def reload
197		### called when our applet is reloaded (config parameters have changed)
198	end
199
200	def _on_reload bConfigHasChanged
201		if bConfigHasChanged
202			self._get_config()
203			self.reload()
204		end
205	end
206
207	def _get_config
208		keyfile = ParseConfig.new(self.cConfFile)
209		self.get_config(keyfile)
210	end
211
212	def get_config keyfile
213		### get our parameters from the key-file
214	end
215
216	def _connect_to_dock
217		# get the icon object on the bus
218		self.bus = DBus::SessionBus.instance
219		#~ applet_path = "/org/cairodock/CairoDock/#{self.cAppletName}" # path where our object is stored on the bus
220		applet_service = bus.service("org.cairodock.CairoDock")
221		begin
222			applet_object = applet_service.object(self.cBusPath)
223			applet_object.introspect
224			applet_object.default_iface = 'org.cairodock.CairoDock.applet'
225		rescue
226			puts ">>> object '#{self.cAppletName}' can't be found on the bus, exit.\nMake sure that the 'Dbus' plug-in is activated in Cairo-Dock"
227			exit
228		end
229		self.icon = applet_object
230
231		# get the sub-icons object on the bus
232		applet_sub_icons_object = applet_service.object("#{self.cBusPath}/sub_icons")
233		applet_sub_icons_object.introspect
234		applet_sub_icons_object.default_iface = 'org.cairodock.CairoDock.subapplet'
235		self.sub_icons = applet_sub_icons_object
236
237		# now connect to the signals
238		self.icon.on_signal("on_click") do |iState|
239			self.on_click iState
240		end
241		self.icon.on_signal("on_middle_click") do
242			self.on_middle_click
243		end
244		self.icon.on_signal("on_build_menu") do
245			self._on_build_menu
246		end
247		self.icon.on_signal("on_menu_select") do |iNumEntry|
248			self._on_menu_select iNumEntry
249		end
250		self.icon.on_signal("on_scroll") do |bScrollUp|
251			self.on_scroll bScrollUp
252		end
253		self.icon.on_signal("on_drop_data") do |cReceivedData|
254			self.on_drop_data cReceivedData
255		end
256		self.icon.on_signal("on_answer") do |answer|
257			self.on_answer answer
258		end
259		self.icon.on_signal("on_answer_dialog") do |button, answer|
260			self.on_answer_dialog button, answer
261		end
262		self.icon.on_signal("on_shortkey") do |cKey|
263			self.on_shortkey cKey
264		end
265		self.icon.on_signal("on_change_focus") do |bIsActive|
266			self.on_change_focus bIsActive
267		end
268
269		self.icon.on_signal("on_stop_module") do
270			self._on_stop
271		end
272
273		self.icon.on_signal("on_reload_module") do |bConfigHasChanged|
274			self._on_reload bConfigHasChanged
275		end
276
277		self.sub_icons.on_signal("on_click_sub_icon") do |iState, sub_icon_id|
278			self.on_click_sub_icon iState, sub_icon_id
279		end
280
281		self.sub_icons.on_signal("on_middle_click_sub_icon") do |sub_icon_id|
282			self.on_middle_click_sub_icon sub_icon_id
283		end
284
285		self.sub_icons.on_signal("on_scroll_sub_icon") do |bScrollUp, sub_icon_id|
286			self.on_scroll_sub_icon bScrollUp, sub_icon_id
287		end
288
289		self.sub_icons.on_signal("on_build_menu_sub_icon") do |sub_icon_id|
290			self._on_build_menu_sub_icon sub_icon_id
291		end
292
293		self.sub_icons.on_signal("on_drop_data_sub_icon") do |cReceivedData, sub_icon_id|
294			self.on_drop_data_sub_icon cReceivedData, sub_icon_id
295		end
296
297	end
298end
299