File: bamf-tab.c

package info (click to toggle)
bamf 0.5.3-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 3,732 kB
  • sloc: ansic: 14,574; sh: 4,238; makefile: 423; xml: 249; python: 46
file content (374 lines) | stat: -rw-r--r-- 10,268 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
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
372
373
374
/*
 * Copyright 2010-2013 Canonical Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of either or both of the following licenses:
 *
 * 1) the GNU Lesser General Public License version 3, as published by the
 * Free Software Foundation; and/or
 * 2) the GNU Lesser General Public License version 2.1, 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 warranties of
 * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the applicable version of the GNU Lesser General Public
 * License for more details.
 *
 * You should have received a copy of both the GNU Lesser General Public
 * License version 3 and version 2.1 along with this program.  If not, see
 * <http://www.gnu.org/licenses/>
 *
 * Authored by: Jason Smith <jason.smith@canonical.com>
                Robert Carr <racarr@canonical.com>
 *              Marco Trevisan (TreviƱo) <3v1n0@ubuntu.com>
 *
 */

#include <libbamf-private/bamf-private.h>
#include "bamf-tab.h"
#include "bamf-view-private.h"

#define BAMF_TAB_GET_PRIVATE(object) (G_TYPE_INSTANCE_GET_PRIVATE (object, BAMF_TYPE_TAB, BamfTabPrivate))

enum
{
  PROP_0,
  PROP_LOCATION,
  PROP_DESKTOP_ID,
  PROP_XID,
  PROP_IS_FOREGROUND_TAB
};

struct _BamfTabPrivate
{
  BamfDBusItemTab *proxy;
};

static void bamf_tab_unset_proxy (BamfTab *self);

G_DEFINE_TYPE (BamfTab, bamf_tab, BAMF_TYPE_VIEW);

static void
on_proxy_property_change (GObject *gobject, GParamSpec *pspec, gpointer user_data)
{
  BamfTab *self = BAMF_TAB (user_data);

  if (g_object_class_find_property (G_OBJECT_GET_CLASS (self), pspec->name))
    g_object_notify (G_OBJECT (self), pspec->name);
}

static void
bamf_tab_set_path (BamfView *view, const gchar *path)
{
  BamfTab *self;
  BamfTabPrivate *priv;
  GError *error = NULL;

  self = BAMF_TAB (view);
  priv = self->priv;

  bamf_tab_unset_proxy (self);
  priv->proxy = _bamf_dbus_item_tab_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
                                                            G_DBUS_PROXY_FLAGS_NONE,
                                                            BAMF_DBUS_SERVICE_NAME,
                                                            path, CANCELLABLE (view),
                                                            &error);
  if (!G_IS_DBUS_PROXY (priv->proxy))
    {
      g_error ("Unable to get %s tab: %s", BAMF_DBUS_SERVICE_NAME, error ? error->message : "");
      g_error_free (error);
      return;
    }

  g_dbus_proxy_set_default_timeout (G_DBUS_PROXY (priv->proxy), BAMF_DBUS_DEFAULT_TIMEOUT);
  g_signal_connect (priv->proxy, "notify", G_CALLBACK (on_proxy_property_change), self);
}

static void
bamf_tab_get_property (GObject *object, guint property_id, GValue *value, GParamSpec *pspec)
{
  BamfTab *self;

  self = BAMF_TAB (object);

  if (!_bamf_view_remote_ready (BAMF_VIEW (self)))
    {
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      return;
    }

  switch (property_id)
    {
    case PROP_LOCATION:
      g_value_set_string (value, _bamf_dbus_item_tab_get_location (self->priv->proxy));
      break;
    case PROP_DESKTOP_ID:
      g_value_set_string (value, _bamf_dbus_item_tab_get_desktop_id (self->priv->proxy));
      break;
    case PROP_XID:
      g_value_set_uint64 (value, _bamf_dbus_item_tab_get_xid (self->priv->proxy));
      break;
    case PROP_IS_FOREGROUND_TAB:
      g_value_set_boolean (value, _bamf_dbus_item_tab_get_is_foreground_tab (self->priv->proxy));
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
    }
}

static void
bamf_tab_unset_proxy (BamfTab *self)
{
  if (!G_IS_DBUS_PROXY (self->priv->proxy))
    return;

  g_signal_handlers_disconnect_by_data (self->priv->proxy, self);

  g_object_unref (self->priv->proxy);
  self->priv->proxy = NULL;
}

static void
bamf_tab_dispose (GObject *object)
{
  BamfTab *self;

  self = BAMF_TAB (object);

  bamf_tab_unset_proxy (self);

  if (G_OBJECT_CLASS (bamf_tab_parent_class)->dispose)
    G_OBJECT_CLASS (bamf_tab_parent_class)->dispose (object);
}

static void
bamf_tab_class_init (BamfTabClass *klass)
{
  GParamSpec *pspec;
  GObjectClass *obj_class = G_OBJECT_CLASS (klass);
  BamfViewClass *view_class = BAMF_VIEW_CLASS (klass);

  obj_class->dispose = bamf_tab_dispose;
  obj_class->get_property = bamf_tab_get_property;

  view_class->set_path = bamf_tab_set_path;

  pspec = g_param_spec_string("location", "Location", "The Current location of the remote Tab",
                              NULL, G_PARAM_READABLE);
  g_object_class_install_property (obj_class, PROP_LOCATION, pspec);

  pspec = g_param_spec_string("desktop-id", "Desktop Name", "The Desktop ID assosciated with the application hosted in the remote Tab",
                              NULL, G_PARAM_READABLE);
  g_object_class_install_property (obj_class, PROP_DESKTOP_ID, pspec);

  pspec = g_param_spec_uint64("xid", "xid", "XID for the toplevel window containing the remote Tab",
                              0, G_MAXUINT64, 0, G_PARAM_READABLE);
  g_object_class_install_property (obj_class, PROP_XID, pspec);

  pspec = g_param_spec_boolean("is-foreground-tab", "Foreground tab", "Whether the tab is the foreground tab in it's toplevel container",
                               FALSE, G_PARAM_READABLE);
  g_object_class_install_property (obj_class, PROP_IS_FOREGROUND_TAB, pspec);

  g_type_class_add_private (obj_class, sizeof(BamfTabPrivate));
}

static void
bamf_tab_init (BamfTab *self)
{
  self->priv = BAMF_TAB_GET_PRIVATE (self);
}

BamfTab *
bamf_tab_new (const gchar *path)
{
  BamfTab *self;

  self = g_object_new (BAMF_TYPE_TAB, NULL);
  _bamf_view_set_path (BAMF_VIEW (self), path);

  return self;
}

/**
 * bamf_tab_raise:
 * @self: A #BamfTab.
 *
 * Selects the @self tab in the parent window.
 *
 * Returns: %TRUE if success, %FALSE otherwise.
 */
gboolean
bamf_tab_raise (BamfTab *self)
{
  GError *error = NULL;

  g_return_val_if_fail (BAMF_IS_TAB (self), FALSE);

  if (!_bamf_view_remote_ready (BAMF_VIEW (self)))
    return FALSE;

  if (!_bamf_dbus_item_tab_call_raise_sync (self->priv->proxy, CANCELLABLE (self), &error))
    {
      g_warning ("Failed to invoke Raise method: %s", error ? error->message : "");
      g_error_free (error);

      return FALSE;
    }

  return TRUE;
}

/**
 * bamf_tab_close:
 * @self: A #BamfTab.
 *
 * Closes the selected @self tab.
 *
 * Returns: %TRUE if success, %FALSE otherwise.
 */
gboolean
bamf_tab_close (BamfTab *self)
{
  GError *error;

  g_return_val_if_fail (BAMF_IS_TAB (self), FALSE);

  if (!_bamf_view_remote_ready (BAMF_VIEW (self)))
    return FALSE;

  error = NULL;

  if (!_bamf_dbus_item_tab_call_close_sync (self->priv->proxy, CANCELLABLE (self), &error))
    {
      g_warning ("Failed to invoke Close method: %s", error->message);
      g_error_free (error);

      return FALSE;
    }

  return TRUE;
}

typedef struct _BamfTabPreviewRequestData
{
  BamfTab *self;
  BamfTabPreviewReadyCallback callback;
  gpointer user_data;
} BamfTabPreviewRequestData;

static void
on_preview_ready (GObject *source_object, GAsyncResult *res, gpointer user_data)
{
  BamfTab *self;
  BamfTabPreviewRequestData *data;
  gchar *preview_data = NULL;
  GError *error = NULL;

  data = user_data;
  self = data->self;

  if (_bamf_dbus_item_tab_call_request_preview_finish (self->priv->proxy, &preview_data, res, &error))
    {
      data->callback (self, preview_data, data->user_data);
      g_free (preview_data);
    }
  else
    {
      data->callback (self, NULL, data->user_data);

      g_warning ("Error requesting BamfTab preview: %s", error ? error->message : "");
      g_error_free (error);
    }

  g_free (data);
}

/**
 * bamf_tab_request_preview:
 * @self: a #BamfTab
 * @callback: (closure) (scope async): a callback function to call when the result is ready
 * @user_data: (closure) (allow-none): data to be sent to the callback.
 */
void
bamf_tab_request_preview (BamfTab *self, BamfTabPreviewReadyCallback callback, gpointer user_data)
{
  BamfTabPreviewRequestData *data;

  g_return_if_fail (BAMF_IS_TAB (self));
  g_return_if_fail (callback != NULL);

  data = g_malloc (sizeof (BamfTabPreviewRequestData));
  data->self = self;
  data->callback = callback;
  data->user_data = user_data;

  _bamf_dbus_item_tab_call_request_preview (self->priv->proxy, NULL,
                                            on_preview_ready, data);
}

const gchar *
bamf_tab_get_location (BamfTab *self)
{
  g_return_val_if_fail (BAMF_IS_TAB (self), NULL);

  if (BAMF_TAB_GET_CLASS (self)->get_location)
    return BAMF_TAB_GET_CLASS (self)->get_location (self);

  return _bamf_dbus_item_tab_get_location (self->priv->proxy);
}

/**
 * bamf_tab_get_desktop_name:
 * @self: A #BamfTab.
 *
 * Returns the desktop file for the tab.
 *
 * Returns: (transfer none): The tab desktop id or %NULL if not set or available. Do not free the returned value, it belongs to @self.
 */
const gchar *
bamf_tab_get_desktop_name (BamfTab *self)
{
  g_return_val_if_fail (BAMF_IS_TAB (self), NULL);

  if (BAMF_TAB_GET_CLASS (self)->get_desktop_name)
    return BAMF_TAB_GET_CLASS (self)->get_desktop_name (self);

  return _bamf_dbus_item_tab_get_desktop_id (self->priv->proxy);
}

/**
 * bamf_tab_get_xid:
 * @self: A #BamfTab.
 *
 * The desktop file for the tab.
 *
 * Returns: The tab parent window XID id or 0 if not set or available.
 */
guint64
bamf_tab_get_xid (BamfTab *self)
{
  g_return_val_if_fail (BAMF_IS_TAB (self), 0);

  if (BAMF_TAB_GET_CLASS (self)->get_xid)
    return BAMF_TAB_GET_CLASS (self)->get_xid (self);

  return _bamf_dbus_item_tab_get_xid (self->priv->proxy);
}

/**
 * bamf_tab_get_is_foreground_tab:
 * @self: A #BamfTab.
 *
 * Returns: %TRUE if the tab is the active one on parent window XID, %FALSE otherwise.
 */
gboolean
bamf_tab_get_is_foreground_tab (BamfTab *self)
{
  g_return_val_if_fail (BAMF_IS_TAB (self), FALSE);

  if (BAMF_TAB_GET_CLASS (self)->get_is_foreground_tab)
    return BAMF_TAB_GET_CLASS (self)->get_is_foreground_tab (self);

  return _bamf_dbus_item_tab_get_is_foreground_tab (self->priv->proxy);
}