File: Client.vala

package info (click to toggle)
plank 0.11.89-6
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 5,660 kB
  • sloc: sh: 4,959; makefile: 1,076; cpp: 404; xml: 33; ansic: 31; python: 22; sed: 16
file content (338 lines) | stat: -rw-r--r-- 8,711 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
//
//  Copyright (C) 2015 Rico Tzschichholz
//
//  This file is part of Plank.
//
//  Plank is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  Plank is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
//

namespace Plank
{
	/**
	 * Connects to a running instance of plank via DBus and
	 * provides remote interface to a currently runnning dock.
	 */
	public class DBusClient : GLib.Object
	{
		static DBusClient? instance;
		
		/**
		 * Get the singleton instance of {@link Plank.DBusClient}
		 */
		public static unowned DBusClient get_instance ()
		{
			if (instance == null)
				instance = new DBusClient ();
			
			return instance;
		}
		
		/**
		 * If the proxy interfaces for the dock are ready to be used
		 * or were changed on runtime this signal will be emitted.
		 */
		public signal void proxy_changed ();
		
		/**
		 * Whether the client is in an operatable state and connected to
		 * a running dock
		 */
		public bool is_connected {
			get {
				return (items_proxy != null);
			}
		}
		
		DBusConnection? connection = null;
		string? client_object_path;
		
		string? dock_bus_owner;
		string? dock_bus_name;
		string? dock_object_path;
		
		uint dbus_dock_ping_id = 0;
		uint dbus_name_owner_changed_signal_id = 0;
		
		DBusItemsIface? items_proxy = null;
		int items_count = int.MIN;
		string[]? persistent_apps_list = null;
		string[]? transient_apps_list = null;
		
		DBusClient ()
		{
			Object ();
		}
		
		construct
		{
			unowned Application? application = GLib.Application.get_default ();
			string? object_path = null;
			
			if (application != null) {
				connection = application.get_dbus_connection ();
				object_path = application.get_dbus_object_path ();
			}
			
			if (connection == null || object_path == null) {
				critical ("Initializing client failed");
				return;
			}
			
			try {
				// Listen for "Ping" signals coming from docks
				dbus_dock_ping_id = connection.signal_subscribe (null, Plank.DBUS_DOCK_INTERFACE_NAME,
					Plank.DBUS_PING_NAME, null, null, DBusSignalFlags.NONE, (DBusSignalCallback) handle_dock_ping);
			} catch (Error e) {
				warning ("Could not subscribe for dock signal (%s)", e.message);
			}
			
			dbus_name_owner_changed_signal_id = connection.signal_subscribe ("org.freedesktop.DBus", "org.freedesktop.DBus",
				"NameOwnerChanged", "/org/freedesktop/DBus", null, DBusSignalFlags.NONE, (DBusSignalCallback) handle_name_owner_changed);
			
			client_object_path = (owned) object_path;
			
			try {
				// Broadcast to inform running docks
				connection.emit_signal (null, client_object_path, Plank.DBUS_CLIENT_INTERFACE_NAME, Plank.DBUS_PING_NAME, null);
			} catch (Error e) {
				warning ("Could not ping running docks (%s)", e.message);
			}
		}
		
		~DBusClient ()
		{
			if (connection != null) {
				if (dbus_dock_ping_id > 0)
					connection.signal_unsubscribe (dbus_dock_ping_id);
				if (dbus_name_owner_changed_signal_id > 0)
					connection.signal_unsubscribe (dbus_name_owner_changed_signal_id);
			}
		}
		
		[CCode (instance_pos = -1)]
		void handle_dock_ping (DBusConnection connection, string sender_name, string object_path,
			string interface_name, string signal_name, Variant parameters)
		{
			if (dock_bus_name == null && dock_bus_name != sender_name)
				connect_proxies (connection, sender_name, object_path);
		}
		
		[CCode (instance_pos = -1)]
		void handle_name_owner_changed (DBusConnection connection, string sender_name, string object_path,
			string interface_name, string signal_name, Variant parameters)
		{
			string name, before, after;
			parameters.get ("(sss)", out name, out before, out after);
			
			if (dock_bus_owner != null && dock_bus_owner == after)
				return;
			
			if (name != null && name != "" && name != dock_bus_name)
				return;
			
			if (after == null || after == "") {
				disconnect_proxies ();
				return;
			}
			
			connect_proxies (connection, name, object_path);
		}
		
		void connect_proxies (DBusConnection connection, string sender_name, string object_path)
		{	
			debug ("Connecting and create proxies for '%s' (%s)", sender_name, object_path);
			
			try {
				items_proxy = connection.get_proxy_sync<Plank.DBusItemsIface> (sender_name, object_path, DBusProxyFlags.NONE);
				items_proxy.changed.connect (invalidate_items_cache);
				dock_bus_owner = ((DBusProxy) items_proxy).get_name_owner ();
				dock_bus_name = sender_name;
				dock_object_path = object_path;
			} catch (Error e) {
				dock_bus_owner = null;
				dock_bus_name = null;
				dock_object_path = null;
				
				items_proxy = null;
				critical ("Failed to create items proxy for '%s' (%s)", sender_name, object_path);
			}
			
			proxy_changed ();
		}
		
		void disconnect_proxies ()
		{
			debug ("Disconnecting from '%s' (%s)", dock_bus_name, dock_object_path);
			
			dock_bus_owner = null;
			dock_bus_name = null;
			dock_object_path = null;
			
			items_proxy.changed.disconnect (invalidate_items_cache);
			items_proxy = null;
		}
		
		
		void invalidate_items_cache ()
		{
			items_count = int.MIN;
			persistent_apps_list = null;
			transient_apps_list = null;
		}
		
		/**
		 * Add a new item for the given uri to the dock
		 *
		 * @param uri an URI
		 * @return whether it was successfully added
		 */
		public bool add_item (string uri)
		{
			if (items_proxy == null) {
				warning ("No proxy connected");
				return false;
			}
			
			try {
				return items_proxy.add (uri);
			} catch (Error e) {
				warning (e.message);
				return false;
			}
		}
		
		/**
		 * Remove an existing item for the given uri from the dock
		 *
		 * @param uri an URI
		 * @return whether it was successfully removed
		 */
		public bool remove_item (string uri)
		{
			if (items_proxy == null) {
				warning ("No proxy connected");
				return false;
			}
			
			try {
				return items_proxy.remove (uri);
			} catch (Error e) {
				warning (e.message);
				return false;
			}
		}
		
		/**
		 * Returns the number of currently visible items on the dock
		 *
		 * @return the item-count
		 */
		public int get_items_count ()
		{
			if (items_proxy == null) {
				warning ("No proxy connected");
				return -1;
			}
			
			try {
				if (items_count == int.MIN)
					items_count = items_proxy.get_count ();
			} catch (Error e) {
				warning (e.message);
				return -1;
			}
			
			return items_count;
		}
		
		/**
		 * Returns an array of uris of the persistent applications on the dock
		 *
		 * @return the array of uris
		 */
		public unowned string[]? get_persistent_applications ()
		{
			if (items_proxy == null) {
				warning ("No proxy connected");
				return null;
			}
			
			if (persistent_apps_list != null)
				return persistent_apps_list;
			
			try {
				if (persistent_apps_list == null)
					persistent_apps_list = items_proxy.get_persistent_applications ();
				return persistent_apps_list;
			} catch (Error e) {
				warning (e.message);
			}
			
			return null;
		}
		
		/**
		 * Returns an array of uris of the transient applications on the dock
		 *
		 * @return the array of uris
		 */
		public unowned string[]? get_transient_applications ()
		{
			if (items_proxy == null) {
				warning ("No proxy connected");
				return null;
			}
			
			if (transient_apps_list != null)
				return transient_apps_list;
			
			try {
				if (transient_apps_list == null)
					transient_apps_list = items_proxy.get_transient_applications ();
				return transient_apps_list;
			} catch (Error e) {
				warning (e.message);
			}
			
			return null;
		}

		/**
		 * Gets the x,y coords with the dock's position to display a hover window for an item.
		 *
		 * @param uri an URI
		 * @param x the resulting x position
		 * @param y the resulting y position
		 * @param dock_position the position of the dock
		 * @return whether it was successfully retrieved
		 */
		public bool get_hover_position (string uri, out int x, out int y, out Gtk.PositionType dock_position)
		{
			if (items_proxy == null) {
				warning ("No proxy connected");
				x = y = -1;
				dock_position = 0;
				return false;
			}
			
			try {
				return items_proxy.get_hover_position (uri, out x, out y, out dock_position);
			} catch (Error e) {
				warning (e.message);
			}
			
			return false;
		}
	}
}