~canonical-dx-team/unity/unity.fix-ql-losing-focus

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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
// -*- Mode: C++; indent-tabs-mode: nil; tab-width: 2 -*-
/*
 * Copyright (C) 2011 Canonical Ltd
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3 as
 * published by the Free Software Foundation.
 *
 * This program 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/>.
 *
 * Authored by: Mikkel Kamstrup Erlandsen <mikkel.kamstrup@canonical.com>
 */

#include "LauncherEntryRemoteModel.h"

static void nux_object_destroy_notify (nux::Object *obj);

/**
 * Helper class implementing the remote API to control the icons in the
 * launcher. Also known as the com.canonical.Unity.LauncherEntry DBus API.
 * It enables clients to dynamically control their launcher icons by
 * adding a counter, progress indicator, or emblem to it. It also supports
 * adding a quicklist menu by means of the dbusmenu library.
 *
 * We take care to print out any client side errors or oddities in detail,
 * in order to help third party developers as much as possible when integrating
 * with Unity.
 */
LauncherEntryRemoteModel::LauncherEntryRemoteModel()
{
  GError          *error;

  _launcher_entry_dbus_signal_id = 0;

  error = NULL;
  _conn = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
  if (error)
    {
      g_warning ("Unable to connect to session bus: %s", error->message);
      g_error_free (error);
      return;
    }
  
  _entries_by_uri = g_hash_table_new_full (g_str_hash, g_str_equal, g_free,
                                           (GDestroyNotify) nux_object_destroy_notify);

  /* Listen for *all* signals on the "com.canonical.Unity.LauncherEntry"
   * interface, no matter who the sender is */
  _launcher_entry_dbus_signal_id =
      g_dbus_connection_signal_subscribe (_conn,
                                          NULL,                       // sender
                                          "com.canonical.Unity.LauncherEntry",
                                          NULL,                       // member
                                          NULL,                       // path
                                          NULL,                       // arg0
                                          G_DBUS_SIGNAL_FLAGS_NONE,
                                          &on_launcher_entry_signal_received,
                                          this,
                                          NULL);

  _dbus_name_owner_changed_signal_id =
      g_dbus_connection_signal_subscribe (_conn,
                                          "org.freedesktop.DBus",     // sender
                                          "org.freedesktop.DBus",     // interface
                                          "NameOwnerChanged",         // member
                                          "/org/freedesktop/DBus",    // path
                                          NULL,                       // arg0
                                          G_DBUS_SIGNAL_FLAGS_NONE,
                                          &on_dbus_name_owner_changed_signal_received,
                                          this,
                                          NULL);
}

LauncherEntryRemoteModel::~LauncherEntryRemoteModel()
{
  g_hash_table_destroy (_entries_by_uri);
  _entries_by_uri = NULL;

  if (_launcher_entry_dbus_signal_id && _conn)
    {
      g_dbus_connection_signal_unsubscribe (_conn,
                                            _launcher_entry_dbus_signal_id);
      _launcher_entry_dbus_signal_id = 0;
    }
  
  if (_conn)
    {
      g_object_unref (_conn);
      _conn = NULL;
    }
}

/**
 * Get a pointer to the default LauncherEntryRemoteModel singleton for this
 * process. The return value should not be freed.
 */
LauncherEntryRemoteModel*
LauncherEntryRemoteModel::GetDefault()
{
  static LauncherEntryRemoteModel *singleton = NULL;

  if (singleton == NULL)
    singleton = new LauncherEntryRemoteModel ();

  return singleton;
}

/**
 * Return the number of unique LauncherEntryRemote objects managed by the model.
 * The entries are identified by their LauncherEntryRemote::AppUri property.
 */
guint
LauncherEntryRemoteModel::Size ()
{
  return g_hash_table_size (_entries_by_uri);
}

/**
 * Return a pointer to a LauncherEntryRemote if there is one for app_uri,
 * otherwise NULL. The returned object should not be freed.
 *
 * App Uris look like application://$desktop_file_id, where desktop_file_id
 * is the base name of the .desktop file for the application including the
 * .desktop extension. Eg. application://firefox.desktop.
 */
LauncherEntryRemote*
LauncherEntryRemoteModel::LookupByUri (const gchar *app_uri)
{
  gpointer entry;

  g_return_val_if_fail (app_uri != NULL, NULL);

  entry = g_hash_table_lookup (_entries_by_uri,  app_uri);
  return static_cast<LauncherEntryRemote *> (entry);
}

/**
 * Return a pointer to a LauncherEntryRemote if there is one for desktop_id,
 * otherwise NULL. The returned object should not be freed.
 *
 * The desktop id is the base name of the .desktop file for the application
 * including the .desktop extension. Eg. firefox.desktop.
 */
LauncherEntryRemote*
LauncherEntryRemoteModel::LookupByDesktopId (const gchar *desktop_id)
{
  LauncherEntryRemote *entry;
  gchar               *app_uri;

  g_return_val_if_fail (desktop_id != NULL, NULL);

  app_uri = g_strconcat ("application://", desktop_id, NULL);
  entry = LookupByUri (app_uri);
  g_free (app_uri);

  return entry;
}

/**
 * Return a pointer to a LauncherEntryRemote if there is one for
 * desktop_file_path, otherwise NULL. The returned object should not be freed.
 */
LauncherEntryRemote*
LauncherEntryRemoteModel::LookupByDesktopFile (const gchar *desktop_file_path)
{
  LauncherEntryRemote *entry;
  gchar               *desktop_id, *app_uri;

  g_return_val_if_fail (desktop_file_path != NULL, NULL);

  desktop_id = g_path_get_basename (desktop_file_path);
  app_uri = g_strconcat ("application://", desktop_id, NULL);
  entry = LookupByUri (app_uri);
  g_free (app_uri);
  g_free (desktop_id);

  return entry;
}

/**
 * Get a list of all application URIs which have registered with the launcher
 * API. The returned GList should be freed with g_list_free(), but the URIs
 * should not be changed or freed.
 */
GList*
LauncherEntryRemoteModel::GetUris ()
{
  return (GList*) g_hash_table_get_keys (_entries_by_uri);
}

/**
 * Add or update a remote launcher entry.
 *
 * If 'entry' has a floating reference it will be consumed.
 */
void
LauncherEntryRemoteModel::AddEntry (LauncherEntryRemote *entry)
{
  LauncherEntryRemote *existing_entry;

  g_return_if_fail (entry != NULL);

  entry->SinkReference ();

  existing_entry = LookupByUri (entry->AppUri ());
  if (existing_entry != NULL)
    {
      existing_entry->Update (entry);
      entry->UnReference ();
    }
  else
    {
      /* The ref on entry will be removed by the hash table itself */
      g_hash_table_insert (_entries_by_uri, g_strdup (entry->AppUri ()), entry);
      entry_added.emit (entry);
    }
}

/**
 * Add or update a remote launcher entry.
 *
 * If 'entry' has a floating reference it will be consumed.
 */
void
LauncherEntryRemoteModel::RemoveEntry (LauncherEntryRemote *entry)
{
  g_return_if_fail (entry != NULL);

  entry->Reference ();

  if (g_hash_table_remove (_entries_by_uri, entry->AppUri ()))
    entry_removed.emit (entry);
  
  entry->UnReference ();
}

/**
 * Handle an incoming Update() signal from DBus
 */
void
LauncherEntryRemoteModel::HandleUpdateRequest (const gchar *sender_name,
                                               GVariant    *parameters)
{
  LauncherEntryRemote      *entry;
  gchar                    *app_uri;
  GVariantIter             *prop_iter;

  g_return_if_fail (sender_name != NULL);
  g_return_if_fail (parameters != NULL);

  if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(sa{sv})")))
    {
      g_warning ("Received 'com.canonical.Unity.LauncherEntry.Update' with"
                 " illegal payload signature '%s'. Expected '(sa{sv})'.",
                 g_variant_get_type_string (parameters));
      return;
    }

  if (sender_name == NULL)
    {
      g_critical ("Received 'com.canonical.Unity.LauncherEntry.Update' from"
                  " an undefined sender. This may happen if you are trying "
                  "to run Unity on a p2p DBus connection.");
      return;
    }

  g_variant_get (parameters, "(sa{sv})", &app_uri, &prop_iter);
  entry = LookupByUri (app_uri);

  if (entry)
    {
      /* It's important that we update the DBus name first since it might
       * unset the quicklist if it changes */
      entry->SetDBusName (sender_name);
      entry->Update (prop_iter);
    }
  else
    {
      entry = new LauncherEntryRemote (sender_name, parameters);
      AddEntry (entry); // consumes floating ref on entry
    }

  g_variant_iter_free (prop_iter);
  g_free (app_uri);
}

void
LauncherEntryRemoteModel::on_launcher_entry_signal_received (GDBusConnection *connection,
                                   const gchar     *sender_name,
                                   const gchar     *object_path,
                                   const gchar     *interface_name,
                                   const gchar     *signal_name,
                                   GVariant        *parameters,
                                   gpointer         user_data)
{
  LauncherEntryRemoteModel *self;

  self = static_cast<LauncherEntryRemoteModel *> (user_data);

  if (parameters == NULL)
    {
      g_warning ("Received DBus signal '%s.%s' with empty payload from %s",
                 interface_name, signal_name, sender_name);
      return;
    }

  if (g_strcmp0 (signal_name, "Update") == 0)
    {
      self->HandleUpdateRequest (sender_name, parameters);
    }
  else
    {
      g_warning ("Unknown signal '%s.%s' from %s",
                 interface_name, signal_name, sender_name);
    }
}

void 
LauncherEntryRemoteModel::on_dbus_name_owner_changed_signal_received (GDBusConnection *connection,
                                            const gchar *sender_name,
                                            const gchar *object_path,
                                            const gchar *interface_name,
                                            const gchar *signal_name,
                                            GVariant *parameters,
                                            gpointer user_data)
{
  LauncherEntryRemoteModel *self;
  char *name, *before, *after;
  
  self = static_cast<LauncherEntryRemoteModel *> (user_data);
  
  if (parameters == NULL)
    return;
  
  g_variant_get (parameters, "(sss)", &name, &before, &after);
  
  if (!after[0])
  {
    // Name gone, find and destroy LauncherEntryRemote
    GHashTableIter iter;
    GList *remove = NULL, *l;
    gpointer key, value;

    g_hash_table_iter_init (&iter, self->_entries_by_uri);
    while (g_hash_table_iter_next (&iter, &key, &value)) 
    {
      /* do something with key and value */
      LauncherEntryRemote *entry = static_cast<LauncherEntryRemote *> (value);
      if (g_str_equal (entry->DBusName (), name))
        remove = g_list_prepend (remove, entry);
    }
    
    for (l = remove; l; l = l->next)
      self->RemoveEntry (static_cast<LauncherEntryRemote *> (l->data));
    
    g_list_free (remove);
  }
}

static void
nux_object_destroy_notify (nux::Object *obj)
{
  if (G_LIKELY (obj != NULL))
    obj->UnReference();
}