~ken-vandine/+junk/liblauncher-gtk3

« back to all changes in this revision

Viewing changes to launcher/launcher-application.c

  • Committer: Bazaar Package Importer
  • Author(s): Loïc Minier
  • Date: 2009-09-25 12:44:07 UTC
  • mfrom: (0.1.4 karmic) (62.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20090925124407-zc40q066a5n5i9hw
* New upstream release.
  - Fixes software-store entry not showing up; LP: #433386.
* Wrap build-deps and deps to get cleaner diffs.
* Bump up Standards-Version to 3.8.3; no change needed.
* Enhance short descriptions slightly.
* Add shlibs:Depends and misc:Depends to liblauncher-dev.
* Build-dep on libglib2.0-dev.
* Let liblauncher-dev dep on libglib2.0-dev, libgtk2.0-dev, libwnck-dev,
  libgnome-menu-dev as their .pcs are referenced in launcher.pc.
* rules: add .PHONY and use $@ in clean:.
* debian/synbols: use 0.1.7~ in versions instead of 0.1.7-0ubuntu1.
* Use dh_prep instead of dh_clean -k.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 2; tab-width: 2 -*- */
2
1
/*
3
2
 * Copyright (C) 2009 Canonical, Ltd.
4
3
 *
20
19
 
21
20
/**
22
21
 * SECTION:launcher-application 
23
 
 * @short_description: #LauncherApplication objects abstract application handling
 
22
 * @short_description: #LauncherApplication objects handle invididual application information
24
23
 * @include: launcher-application.h
25
24
 *
26
 
 * #LauncherApplication objects exist in order to acquire data about specific 
27
 
 * applications and to launch applications. it also provides a method for 
28
 
 * returning a libwnck application
 
25
 * #LauncherApplication objects exist in order to acquire data about specific applications
29
26
 */
30
27
#if HAVE_CONFIG_H
31
28
#include <config.h>
35
32
 
36
33
#include <gio/gdesktopappinfo.h>
37
34
 
38
 
#define TYPE_GS_LIST gs_list_get_type()
39
 
 
40
 
static gpointer gs_list_copy (gpointer boxed)
41
 
{
42
 
  return g_memdup (boxed, sizeof (GSList));
43
 
}
44
 
 
45
 
GType gs_list_get_type (void)
46
 
{
47
 
  static GType type = 0;
48
 
 
49
 
  if (!type) {
50
 
    type = g_boxed_type_register_static ("GSList", 
51
 
       gs_list_copy, g_free);
52
 
  }
53
 
 
54
 
  return type;
55
 
}
56
 
 
57
 
enum
58
 
{
59
 
  PROP_0,
60
 
 
61
 
  PROP_NAME,
62
 
  PROP_EXEC,
63
 
  PROP_ICON_NAME,
64
 
  PROP_COMMENT,
65
 
  PROP_DESKTOP_FILE_PATH,
66
 
  PROP_UNIQUE_STRING,
67
 
  PROP_CATEGORIES,
68
 
  PROP_RUNNING,
69
 
  PROP_FAVORITE,
70
 
  PROP_FOCUSED,
71
 
  PROP_WNCKAPPLICATIONS
72
 
};
73
 
 
74
 
enum
75
 
{
76
 
  OPENED,
77
 
  CLOSED,
78
 
 
79
 
  LAST_SIGNAL
80
 
};
81
 
 
82
 
static guint application_signals[LAST_SIGNAL] = { 0 };
83
 
 
84
 
G_DEFINE_TYPE (LauncherApplication, launcher_application, G_TYPE_OBJECT);
85
 
#define LAUNCHER_APPLICATION_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE(obj, \
86
 
LAUNCHER_TYPE_APPLICATION, LauncherApplicationPrivate))
87
 
 
88
 
struct _LauncherApplicationWindow
89
 
{
90
 
  GTimeVal timestamp;
91
 
  WnckWindow *window;
92
 
};
93
 
 
94
 
struct _LauncherApplicationPrivate
95
 
{
 
35
struct _LauncherApplication
 
36
{
 
37
  gint ref_count;
 
38
 
 
39
  WnckApplication *wnckapp;
 
40
 
96
41
  gchar *name;
97
 
  gchar *exec;
 
42
  gchar *match_name;
 
43
  gchar *comment;
98
44
  gchar *icon_name;
99
 
  gchar *comment;
100
 
  gchar *desktop_file_path;
101
 
  gchar *unique_string;
102
 
  GSList *categories;
103
 
  gboolean running;
104
 
  gboolean favorite;
105
 
  GSList *wnck_apps;
106
 
  gboolean focused;
107
 
  
108
 
  WnckScreen  *screen;
109
 
  GHashTable *windows;
 
45
  gchar *exec_path;
 
46
  gchar *desktop_file;
110
47
};
111
48
 
112
 
static void
113
 
on_application_closed (WnckScreen              *screen,
114
 
                       WnckApplication         *app,
115
 
                       LauncherApplication     *application);
116
 
 
117
 
static void
118
 
launcher_application_init (LauncherApplication *object)
119
 
{
120
 
  LauncherApplicationPrivate *priv;
121
 
    
122
 
  priv = object->priv = LAUNCHER_APPLICATION_GET_PRIVATE (object);
123
 
  priv->running = FALSE;
124
 
  priv->favorite = FALSE;
125
 
  
126
 
  priv->screen = wnck_screen_get_default ();
127
 
  g_signal_connect (priv->screen, "application-closed",
128
 
                    G_CALLBACK (on_application_closed), object);
129
 
 
130
 
  priv->windows = g_hash_table_new (g_direct_hash, g_direct_equal);
131
 
}
132
 
 
133
 
static void
134
 
launcher_application_finalize (GObject *object)
135
 
{
136
 
  LauncherApplication *app = LAUNCHER_APPLICATION (object);
137
 
  LauncherApplicationPrivate *priv;
138
 
  
139
 
  priv = LAUNCHER_APPLICATION_GET_PRIVATE (app);
140
 
  g_hash_table_destroy (priv->windows);
141
 
  
142
 
  G_OBJECT_CLASS (launcher_application_parent_class)->finalize (object);
143
 
  
144
 
}
145
 
 
146
 
static void
147
 
launcher_application_set_property (GObject *object, 
148
 
                                   guint prop_id, 
149
 
                                   const GValue *value, 
150
 
                                   GParamSpec *pspec)
151
 
{
152
 
  LauncherApplication *application = LAUNCHER_APPLICATION(object);
153
 
  g_return_if_fail (LAUNCHER_IS_APPLICATION (application));
154
 
  
155
 
    
156
 
  switch (prop_id)
157
 
    {
158
 
    case PROP_NAME:
159
 
      if (application->priv->name)
160
 
        g_free(application->priv->name);
161
 
      application->priv->name = g_value_dup_string(value);
162
 
      break;
163
 
    case PROP_EXEC:
164
 
      if (application->priv->exec)
165
 
        g_free(application->priv->exec);
166
 
      application->priv->exec = g_value_dup_string(value);
167
 
      break;
168
 
    case PROP_ICON_NAME:
169
 
      if (application->priv->icon_name)
170
 
        g_free(application->priv->icon_name);
171
 
      application->priv->icon_name = g_value_dup_string(value);
172
 
      break;
173
 
    case PROP_COMMENT:
174
 
      if (application->priv->comment)
175
 
        g_free(application->priv->comment);
176
 
      application->priv->comment = g_value_dup_string(value);
177
 
      break;
178
 
    case PROP_DESKTOP_FILE_PATH:
179
 
      launcher_application_set_desktop_file(application, 
180
 
                                            g_value_dup_string(value));
181
 
      break;
182
 
    case PROP_UNIQUE_STRING:
183
 
      break;
184
 
    case PROP_CATEGORIES:
185
 
      application->priv->categories = g_value_get_boxed(value);
186
 
      break;
187
 
    case PROP_RUNNING:
188
 
      application->priv->running = g_value_get_boolean(value);
189
 
      break;
190
 
    case PROP_FAVORITE:
191
 
      application->priv->favorite = g_value_get_boolean(value);
192
 
      break;
193
 
    case PROP_WNCKAPPLICATIONS:
194
 
      application->priv->wnck_apps = g_value_get_boxed(value);
195
 
      break;
196
 
    case PROP_FOCUSED:
197
 
      application->priv->focused = g_value_get_boolean (value);
198
 
      break;
199
 
    default:
200
 
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
201
 
      break;
202
 
    }
203
 
}
204
 
 
205
 
static void
206
 
launcher_application_get_property (GObject *object, 
207
 
                                   guint prop_id, 
208
 
                                   GValue *value, 
209
 
                                   GParamSpec *pspec)
210
 
{
211
 
  LauncherApplication *application = LAUNCHER_APPLICATION(object);
212
 
  LauncherApplicationPrivate *priv = application->priv;
213
 
  
214
 
  g_return_if_fail (LAUNCHER_IS_APPLICATION (application));
215
 
  
216
 
  switch (prop_id)
217
 
    {
218
 
    case PROP_NAME:
219
 
      g_value_set_string(value, priv->name);
220
 
      break;
221
 
    case PROP_EXEC:
222
 
      g_value_set_string(value, priv->exec);
223
 
      break;
224
 
    case PROP_ICON_NAME:
225
 
      g_value_set_string(value, priv->icon_name);
226
 
      break;
227
 
    case PROP_COMMENT:
228
 
      g_value_set_string(value, priv->comment);
229
 
      break;
230
 
    case PROP_DESKTOP_FILE_PATH:
231
 
      g_value_set_string(value, priv->desktop_file_path);
232
 
      break;
233
 
    case PROP_UNIQUE_STRING:
234
 
      {
235
 
        const gchar *str;
236
 
        str = launcher_application_get_unique_string(application);
237
 
        g_value_set_string(value, str);
238
 
      }
239
 
      break;
240
 
    case PROP_CATEGORIES:
241
 
      g_value_set_boxed(value, &priv->categories);
242
 
      break;
243
 
    case PROP_RUNNING:
244
 
      g_value_set_boolean(value, priv->running);
245
 
      break;
246
 
    case PROP_FAVORITE:
247
 
      g_value_set_boolean(value, priv->favorite);
248
 
      break;
249
 
    case PROP_WNCKAPPLICATIONS:
250
 
      g_value_set_object(value, priv->wnck_apps);
251
 
      break;
252
 
    case PROP_FOCUSED:
253
 
      {
254
 
        gboolean focused = FALSE;
255
 
        focused = launcher_application_get_focused (application);
256
 
        g_value_set_boolean (value, priv->focused);
257
 
      }
258
 
      break;
259
 
    default:
260
 
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
261
 
      break;
262
 
    }
263
 
}
264
 
 
265
 
static void
266
 
launcher_application_opened (LauncherApplication *self, 
267
 
                             WnckApplication *wnckapp)
268
 
{
269
 
  /* TODO: Add default signal handler implementation here */
270
 
}
271
 
 
272
 
static void
273
 
launcher_application_closed (LauncherApplication *self,
274
 
                             WnckApplication *wnckapp)
275
 
{
276
 
  /* TODO: Add default signal handler implementation here */
277
 
}
278
 
 
279
 
static void
280
 
launcher_application_class_init (LauncherApplicationClass *klass)
281
 
{
282
 
  GObjectClass* object_class = G_OBJECT_CLASS (klass);
283
 
 
284
 
  g_type_class_add_private (object_class, sizeof (LauncherApplicationPrivate));
285
 
 
286
 
  object_class->finalize = launcher_application_finalize;
287
 
  object_class->set_property = launcher_application_set_property;
288
 
  object_class->get_property = launcher_application_get_property;
289
 
 
290
 
  klass->opened = launcher_application_opened;
291
 
  klass->closed = launcher_application_closed;
292
 
 
293
 
  g_object_class_install_property (object_class,
294
 
                                   PROP_NAME,
295
 
                                   g_param_spec_string ("name",
296
 
                                                        "name",
297
 
                                                        "name of the application",
298
 
                                                        "",
299
 
                                                        G_PARAM_READABLE));
300
 
 
301
 
  g_object_class_install_property (object_class,
302
 
                                   PROP_EXEC,
303
 
                                   g_param_spec_string ("exec",
304
 
                                                        "exec",
305
 
                                                        "the exec thats called in order to launch the application",
306
 
                                                        "",
307
 
                                                        G_PARAM_READABLE));
308
 
 
309
 
  g_object_class_install_property (object_class,
310
 
                                   PROP_ICON_NAME,
311
 
                                   g_param_spec_string ("icon-name",
312
 
                                                        "icon-name",
313
 
                                                        "the standard \"icon-name\" for this application",
314
 
                                                        "",
315
 
                                                        G_PARAM_READABLE));
316
 
 
317
 
  g_object_class_install_property (object_class,
318
 
                                   PROP_COMMENT,
319
 
                                   g_param_spec_string ("comment",
320
 
                                                        "comment",
321
 
                                                        "a general comment about the application, usually helpful information",
322
 
                                                        "",
323
 
                                                        G_PARAM_READABLE));
324
 
 
325
 
  g_object_class_install_property (object_class,
326
 
                                   PROP_DESKTOP_FILE_PATH,
327
 
                                   g_param_spec_string ("desktop_file_path",
328
 
                                                        "desktop_file_path",
329
 
                                                        "the path to the desktop file accociated with this application",
330
 
                                                        "",
331
 
                                                        G_PARAM_READABLE|G_PARAM_WRITABLE));
332
 
 
333
 
  g_object_class_install_property (object_class,
334
 
                                   PROP_UNIQUE_STRING,
335
 
                                   g_param_spec_string ("unique_string",
336
 
                                                        "unique_string",
337
 
                                                        "A Unique string to identify this application",
338
 
                                                        "",
339
 
                                                        G_PARAM_READABLE));
340
 
  g_object_class_install_property (object_class,
341
 
                                   PROP_CATEGORIES,
342
 
                                   g_param_spec_boxed  ("categories",
343
 
                                                        "categories",
344
 
                                                        "a list of LauncherCategories that this application is listed inside",
345
 
                                                        TYPE_GS_LIST,
346
 
                                                        G_PARAM_READABLE));
347
 
 
348
 
  g_object_class_install_property (object_class,
349
 
                                   PROP_RUNNING,
350
 
                                   g_param_spec_boolean ("running",
351
 
                                                        "running",
352
 
                                                        "Returns TRUE if the application is running",
353
 
                                                        FALSE,
354
 
                                                        G_PARAM_READABLE|G_PARAM_WRITABLE));
355
 
 
356
 
  g_object_class_install_property (object_class,
357
 
                                   PROP_FOCUSED,
358
 
                                   g_param_spec_boolean ("focused",
359
 
                                                         "focused",
360
 
                                                         "returns TRUE if the application contains a focused window",
361
 
                                                         FALSE,
362
 
                                                         G_PARAM_READABLE|G_PARAM_WRITABLE));
363
 
 
364
 
  g_object_class_install_property (object_class,
365
 
                                   PROP_FAVORITE,
366
 
                                   g_param_spec_boolean ("favorite",
367
 
                                                        "favorite",
368
 
                                                        "returns TRUE if the application is listed as a favorite application",
369
 
                                                        FALSE,
370
 
                                                        G_PARAM_READABLE));
371
 
 
372
 
  g_object_class_install_property (object_class,
373
 
                                   PROP_WNCKAPPLICATIONS,
374
 
                                   g_param_spec_boxed  ("wnckapps",
375
 
                                                        "wnckapps",
376
 
                                                        "a list of WnckApplication objects that this Application current has",
377
 
                                                        TYPE_GS_LIST,
378
 
                                                        G_PARAM_READABLE));
379
 
 
380
 
  application_signals[OPENED] =
381
 
      g_signal_new ("opened",
382
 
                    G_OBJECT_CLASS_TYPE (klass),
383
 
                    0,
384
 
                    G_STRUCT_OFFSET (LauncherApplicationClass, opened),
385
 
                    NULL, NULL,
386
 
                    g_cclosure_marshal_VOID__POINTER,
387
 
                    G_TYPE_NONE, 1, G_TYPE_POINTER
388
 
                    );
389
 
 
390
 
  application_signals[CLOSED] =
391
 
      g_signal_new ("closed",
392
 
                    G_OBJECT_CLASS_TYPE (klass),
393
 
                    0,
394
 
                    G_STRUCT_OFFSET (LauncherApplicationClass, closed),
395
 
                    NULL, NULL,
396
 
                    g_cclosure_marshal_VOID__POINTER,
397
 
                    G_TYPE_NONE, 1, WNCK_TYPE_APPLICATION
398
 
                    );
 
49
GType
 
50
launcher_application_get_type (void)
 
51
{
 
52
  static GType _launcher_application_type = 0;
 
53
 
 
54
  if (G_UNLIKELY (_launcher_application_type == 0))
 
55
    {
 
56
      _launcher_application_type =
 
57
        g_boxed_type_register_static ("LauncherApplication",
 
58
                                      (GBoxedCopyFunc) launcher_application_copy,
 
59
                                      (GBoxedFreeFunc) launcher_application_free);
 
60
 
 
61
    }
 
62
 
 
63
  return _launcher_application_type;
399
64
}
400
65
 
401
66
/*
402
 
 * Private Methods
403
 
 */
404
 
 
405
 
 
406
 
static void
407
 
on_application_closed (WnckScreen      *screen,
408
 
                       WnckApplication *app,
409
 
                       LauncherApplication     *application)
410
 
{
411
 
  int       wnck_pid = 0;
412
 
  int       app_pid = 1;
413
 
  GSList    *a;
414
 
  WnckApplication *found_app = NULL;
415
 
  /* when *any* application closes we do a quick check to see if its this one
416
 
   * thus we need to make this check as quick and easy as possible
417
 
   */
418
 
  g_return_if_fail (LAUNCHER_IS_APPLICATION (application));
419
 
  g_return_if_fail (WNCK_IS_APPLICATION (app));
420
 
  
421
 
  /* we need to go though and check each wnckapp in this launcherapplication 
422
 
   * to see if the pid's match
423
 
   */
424
 
  wnck_pid = wnck_application_get_pid (app);
425
 
  for (a = application->priv->wnck_apps; a; a = a->next) 
426
 
    {
427
 
      WnckApplication *store_app = a->data;
428
 
      app_pid = wnck_application_get_pid (store_app);
429
 
      if (wnck_pid == app_pid) 
430
 
        {
431
 
          found_app = store_app;
432
 
          break;
433
 
        }
434
 
    }
435
 
    
436
 
  if (!found_app)
437
 
    return;
438
 
    
439
 
  // we get here then we have the wnckapplication in our store
440
 
  application->priv->wnck_apps = g_slist_remove(application->priv->wnck_apps,
441
 
                                                found_app);
442
 
  g_object_unref (found_app);
443
 
  
444
 
  // do we have any apps in our store? if so, we are running!
445
 
  if (application->priv->wnck_apps != NULL)
446
 
    {
447
 
      g_object_set (G_OBJECT (application),
448
 
                    "running", TRUE,
449
 
                    NULL);
450
 
    } else
451
 
    { 
452
 
      g_object_set (G_OBJECT (application),
453
 
                    "running", FALSE,
454
 
                    NULL);
455
 
    }   
456
 
    
457
 
  g_object_set (G_OBJECT (application),
458
 
                "focused", FALSE,
459
 
                NULL);
460
 
 
461
 
  // we are closing apprently, lets emit a signal about that :-)
462
 
  g_signal_emit (application, application_signals[CLOSED], 0, app);
463
 
}
464
 
 
465
 
 
466
 
 
467
 
/* 
468
 
 * Constructors 
469
 
 */
470
 
 
471
 
 
 
67
 * Public Methods
 
68
 */
 
69
/**
 
70
 * launcher_application_new_from_info:
 
71
 * @name: string containing application name
 
72
 * @comment: string containing application comment
 
73
 * @icon_name: string containing icon_name for application
 
74
 * @exec_path: string containing the path to the executable
 
75
 * @desktop_file: string containing the path to the desktop file for this application
 
76
 *
 
77
 * Creates a new #LauncherApplication object with the given information
 
78
 *
 
79
 * Returns: a new #LauncherApplication
 
80
 */
472
81
LauncherApplication *
473
 
launcher_application_new (void)
 
82
launcher_application_new_from_info (const gchar  *name,
 
83
                                    const gchar  *comment,
 
84
                                    const gchar  *icon_name,
 
85
                                    const gchar  *exec_path,
 
86
                                    const gchar  *desktop_file)
474
87
{
475
88
  LauncherApplication *application;
476
89
 
477
 
  application = g_object_new (LAUNCHER_TYPE_APPLICATION,
478
 
                              "running", FALSE,
479
 
                              NULL);
 
90
  application = g_slice_new0 (LauncherApplication);
 
91
 
 
92
  if (application != NULL)
 
93
    {
 
94
      application->ref_count = 1;
 
95
 
 
96
      application->name = g_strdup (name);
 
97
      application->comment = g_strdup (comment);
 
98
      application->icon_name = g_strdup (icon_name);
 
99
      application->exec_path = g_strdup (exec_path);
 
100
      application->desktop_file = g_strdup (desktop_file);
 
101
    }
 
102
 
 
103
 
480
104
  return application;
481
105
}
482
106
 
483
107
/**
484
 
 * launcher_application_new_from_wnck_app:
 
108
 * launcher_application_new_from-wnck_app:
485
109
 * @app: A #WnckApplication object
486
110
 *
487
111
 * creates a new #LauncherApplication object based on information from @app
491
115
{
492
116
  LauncherApplication *application;
493
117
 
494
 
  application = g_object_new (LAUNCHER_TYPE_APPLICATION,
495
 
                        "running", TRUE,
496
 
                        NULL);
497
 
  launcher_application_add_wnckapp (application, app);
498
 
  return application;
499
 
}
500
 
 
501
 
/**
502
 
 * launcher_application_new_from_destkop_file
503
 
 * @desktop_file: the file path to the desktop file used to build the application
504
 
 * 
505
 
 * This will create a new #LauncherApplication object using the information
506
 
 * contained within the given desktop file at @desktop_file
507
 
 * 
508
 
 * Returns: (transfer full) A new #LauncherApplication object
509
 
 */
510
 
LauncherApplication *
511
 
launcher_application_new_from_desktop_file (const gchar *desktop_file)
512
 
{
513
 
  LauncherApplication *application;
514
 
  /* we can now make our application */
515
 
  application = g_object_new (LAUNCHER_TYPE_APPLICATION,
516
 
                              "desktop_file_path", desktop_file, 
517
 
                              "running", FALSE,
518
 
                              NULL);
519
 
 
520
 
  return application;
521
 
}
522
 
 
523
 
/*
524
 
 * Public Methods
525
 
 */
526
 
 
527
 
/**
528
 
 * launcher_application_launch
529
 
 * @application: a #LauncherApplication object
530
 
 * @error: a #GError error
531
 
 * 
532
 
 * This method will attempt to launch the application using the exec string
533
 
 * associated with it
534
 
 * 
535
 
 * Returns: A truth value dictating whether the launching was successful
536
 
 */
537
 
gboolean
538
 
launcher_application_launch (LauncherApplication *application, GError **error)
 
118
  application = g_slice_new0 (LauncherApplication);
 
119
 
 
120
  if (application != NULL)
 
121
    {
 
122
      application->ref_count = 1;
 
123
 
 
124
      application->wnckapp = app;
 
125
      application->name = g_strdup (wnck_application_get_name (app));
 
126
      application->icon_name = g_strdup (wnck_application_get_icon_name (app));
 
127
    }
 
128
 
 
129
  return application;
 
130
}
 
131
 
 
132
/**
 
133
 * launcher_application_copy:
 
134
 * @app: A #LauncherApplication 
 
135
 *
 
136
 * makes a copy of @app 
 
137
 *
 
138
 * Returns: A new #LauncherApplication copy of @app
 
139
 */
 
140
LauncherApplication *
 
141
launcher_application_copy (const LauncherApplication *application)
 
142
{
 
143
  if (G_LIKELY (application != NULL))
 
144
    {
 
145
      LauncherApplication *ret = NULL;
 
146
 
 
147
      ret = g_slice_new0 (LauncherApplication);
 
148
      /*FIXME: Update for real copy */
 
149
      return ret;
 
150
    }
 
151
  return NULL;
 
152
}
 
153
 
 
154
/**
 
155
 * launcher_application_free:
 
156
 * @app: a #LauncherApplication
 
157
 *
 
158
 * free's @app and its information
 
159
 */
 
160
void
 
161
launcher_application_free (LauncherApplication *application)
 
162
{
 
163
  if (G_LIKELY (application != NULL))
 
164
    {
 
165
      g_free (application->name);
 
166
      g_free (application->match_name);
 
167
      g_free (application->comment);
 
168
      g_free (application->icon_name);
 
169
      g_free (application->exec_path);
 
170
      g_free (application->desktop_file);
 
171
 
 
172
      g_slice_free (LauncherApplication, application);
 
173
      application = NULL;
 
174
    }
 
175
}
 
176
 
 
177
/**
 
178
 * launcher_application_ref:
 
179
 * @app: a #LauncherApplication 
 
180
 *
 
181
 * increases the reference count of @app 
 
182
 *
 
183
 * Returns: the same @app or NULL
 
184
 */
 
185
LauncherApplication *
 
186
launcher_application_ref (LauncherApplication *application)
 
187
{
 
188
  g_return_val_if_fail (application, NULL);
 
189
  g_return_val_if_fail (application->ref_count > 0, NULL);
 
190
 
 
191
  g_atomic_int_inc (&application->ref_count);
 
192
 
 
193
  return application;
 
194
}
 
195
 
 
196
/**
 
197
 * launcher_application_unref:
 
198
 * @app: A #LauncherApplication 
 
199
 * 
 
200
 * unreferences the #LauncherApplication, reducing its reference count
 
201
 */
 
202
void
 
203
launcher_application_unref (LauncherApplication *application)
 
204
{
 
205
  g_return_if_fail (application);
 
206
  g_return_if_fail (application->ref_count > 0);
 
207
 
 
208
  if (G_UNLIKELY (g_atomic_int_dec_and_test (&application->ref_count)))
 
209
    {
 
210
      launcher_application_free (application);
 
211
    }
 
212
}
 
213
 
 
214
/** 
 
215
 * launcher_application_set_wnckapp:
 
216
 * @app: a #LauncherApplication
 
217
 * @wnckapp: a #WnckApplication object
 
218
 * 
 
219
 * Sets this #LauncherApplication to use the given #WnckApplication  
 
220
 */
 
221
void
 
222
launcher_application_set_wnckapp (LauncherApplication *application,
 
223
                                  WnckApplication *wnckapp)
 
224
{
 
225
  g_return_if_fail (application);
 
226
  application->wnckapp = wnckapp;
 
227
}
 
228
 
 
229
/**
 
230
 * launcher_application_get_wnckapp:
 
231
 * @app: a #LauncherApplication
 
232
 * 
 
233
 * Provides the #WnckApplication used for this #LauncherApplication object
 
234
 * 
 
235
 * Returns: a #WnckApplication or NULL
 
236
 */
 
237
WnckApplication *
 
238
launcher_application_get_wnckapp (LauncherApplication *application)
 
239
{
 
240
  g_return_val_if_fail (application, NULL);
 
241
  return application->wnckapp;
 
242
}
 
243
 
 
244
/**
 
245
 * launcher_application_get_name:
 
246
 * @app: a #LauncherApplication object
 
247
 *
 
248
 * Retrives the name of the application @app
 
249
 *
 
250
 * Returns: a string containing the name
 
251
 */
 
252
const gchar *
 
253
launcher_application_get_name (LauncherApplication *application)
 
254
{
 
255
  g_return_val_if_fail (application, NULL);
 
256
  return application->name;
 
257
}
 
258
 
 
259
/**
 
260
 * launcher_application_get_comment:
 
261
 * @app: A #LauncherApplication object
 
262
 *
 
263
 * Retrives the comment assigned to the given @app object
 
264
 *
 
265
 * Returns: A string containing the comment
 
266
 */
 
267
const gchar *
 
268
launcher_application_get_comment (LauncherApplication *application)
 
269
{
 
270
  g_return_val_if_fail (application, NULL);
 
271
  return application->comment;
 
272
}
 
273
 
 
274
const gchar *
 
275
launcher_application_get_icon_name (LauncherApplication *application)
 
276
{
 
277
  g_return_val_if_fail (application, NULL);
 
278
  return application->icon_name;
 
279
}
 
280
 
 
281
const gchar *
 
282
launcher_application_get_exec_string (LauncherApplication *application)
 
283
{
 
284
  g_return_val_if_fail (application, NULL);
 
285
  return application->exec_path;
 
286
}
 
287
 
 
288
const gchar *
 
289
launcher_application_get_desktop_file (LauncherApplication *application)
 
290
{
 
291
  g_return_val_if_fail (application, NULL);
 
292
  return application->desktop_file;
 
293
}
 
294
 
 
295
const gchar *
 
296
launcher_application_get_match_name (LauncherApplication *application)
 
297
{
 
298
  g_return_val_if_fail (application, NULL);
 
299
 
 
300
  if (!application->match_name)
 
301
    application->match_name = g_utf8_strdown (application->name, -1);
 
302
 
 
303
  return application->match_name;
 
304
}
 
305
 
 
306
/**
 
307
 * launcher_application_launch:
 
308
 * @app: A #LauncherApplication object
 
309
 * @err: A pointer to a GError object
 
310
 *
 
311
 * Launches the application contained within @app, will raise an error if there is a problem
 
312
 */
 
313
void
 
314
launcher_application_launch (LauncherApplication *app, GError **error)
539
315
{
540
316
  GDesktopAppInfo     *info;
541
 
  gchar               *icon_name;
542
 
  gchar               *desktop_file_path;
543
317
  GdkAppLaunchContext *context;
544
318
 
545
 
  g_return_val_if_fail (application, FALSE);
546
 
  g_return_val_if_fail (*error == NULL, FALSE);
547
 
  
548
 
  g_object_get(application, "desktop_file_path", &desktop_file_path, NULL);
 
319
  g_return_if_fail (app);
 
320
  g_return_if_fail (*error == NULL);
549
321
 
550
 
  info = g_desktop_app_info_new_from_filename (desktop_file_path);
 
322
  info = g_desktop_app_info_new_from_filename (app->desktop_file);
551
323
 
552
324
  if (!info)
553
325
    {
556
328
        quark = g_quark_from_static_string ("launcher_application_launch_error");
557
329
 
558
330
      *error = g_error_new (quark, 1, "Unable to load GDesktopAppInfo for %s",
559
 
                            desktop_file_path);
560
 
      return FALSE;
 
331
                            app->desktop_file);
 
332
      return;
561
333
    }
562
334
 
563
335
  context = gdk_app_launch_context_new ();
564
336
  gdk_app_launch_context_set_screen (context, gdk_screen_get_default ());
565
337
  gdk_app_launch_context_set_timestamp (context, GDK_CURRENT_TIME);
566
 
  
567
 
  g_object_get(application, "icon-name", &icon_name, NULL);
568
 
  gdk_app_launch_context_set_icon_name (context, icon_name);
 
338
  gdk_app_launch_context_set_icon_name (context, app->icon_name);
569
339
 
570
340
  g_app_info_launch ((GAppInfo *)info, NULL, (GAppLaunchContext*)context,
571
341
                     error);
572
342
 
573
343
  g_object_unref (context);
574
344
  g_object_unref (info);
575
 
 
576
 
  return TRUE;
577
 
}
578
 
 
579
 
/**
580
 
 * launcher_application_get_unique_string
581
 
 * @app: a #LauncherApplication
582
 
 * 
583
 
 * Provides a unique string that can be used to identify this application
584
 
 *
585
 
 * Returns: a unique string
586
 
 */
587
 
const gchar *
588
 
launcher_application_get_unique_string (LauncherApplication *application)
589
 
{
590
 
  g_return_val_if_fail (application, NULL);
591
 
  if (!application->priv->unique_string)
592
 
    application->priv->unique_string = g_utf8_strdown (application->priv->name, 
593
 
                                                       -1);
594
 
                                                       
595
 
  return application->priv->unique_string;
596
 
}
597
 
 
598
 
/**
599
 
 * launcher_application_get_wnckapp:
600
 
 * @app: a #LauncherApplication
601
 
 * 
602
 
 * Provides a list of #WnckApplications associated with this @app
603
 
 * 
604
 
 * Returns: (transfer none): a #GSList containing #WnckApplications 
605
 
 */
606
 
GSList *
607
 
launcher_application_get_wnckapps (LauncherApplication *application)
608
 
{
609
 
  g_return_val_if_fail (application, NULL);
610
 
  
611
 
  return application->priv->wnck_apps;
612
 
}
613
 
 
614
 
/**
615
 
 * launcher_application_add_wnckapp
616
 
 * @application: a #LauncherApplication object
617
 
 * @wnck_app: a #WnckApplication object
618
 
 * 
619
 
 * This method will add @wnck_app to @application and associate it with
620
 
 * the various signals @application has.
621
 
 */ 
622
 
void
623
 
launcher_application_add_wnckapp (LauncherApplication *application, 
624
 
                                  WnckApplication *wnck_app) 
625
 
{
626
 
  GSList *a;
627
 
  gint pid = 0;
628
 
  g_return_if_fail (application);
629
 
  g_return_if_fail (wnck_app);
630
 
  
631
 
  /* first i guess we need to check to make sure that
632
 
   * this WnckApplication does not already exist in the list
633
 
   * of wnckapplications
634
 
   */
635
 
  pid = wnck_application_get_pid (wnck_app);
636
 
  for (a = application->priv->wnck_apps; a; a = a->next)
637
 
    {
638
 
      WnckApplication *app = a->data;
639
 
      if (wnck_application_get_pid (app) == pid)
640
 
        return;
641
 
    }
642
 
 
643
 
  application->priv->wnck_apps = g_slist_append(application->priv->wnck_apps, 
644
 
                                                wnck_app);
645
 
  g_object_ref(wnck_app);
646
 
  
647
 
  // we emit the opened signal here
648
 
  g_signal_emit (application, application_signals[OPENED], 0, wnck_app);
649
 
  
650
 
  g_object_set (G_OBJECT (application),
651
 
                "running", TRUE,
652
 
                NULL);
653
 
 
654
 
}
655
 
  
656
 
/**
657
 
 * launcher_application_get_name:
658
 
 * @application: a #LauncherApplication object
659
 
 *
660
 
 * Retrives the name of the application @application
661
 
 *
662
 
 * Returns: a string containing the name
663
 
 */
664
 
const gchar *
665
 
launcher_application_get_name (LauncherApplication *application)
666
 
{
667
 
  g_return_val_if_fail (application, NULL);
668
 
  
669
 
  return application->priv->name;
670
 
}
671
 
 
672
 
/**
673
 
 * launcher_application_get_comment:
674
 
 * @application: A #LauncherApplication object
675
 
 *
676
 
 * Retrives the comment assigned to the given @application object
677
 
 *
678
 
 * Returns: A string containing the comment
679
 
 */
680
 
const gchar *
681
 
launcher_application_get_comment (LauncherApplication *application)
682
 
{
683
 
  g_return_val_if_fail (application, NULL);
684
 
  
685
 
  return application->priv->comment;
686
 
}
687
 
 
688
 
/**
689
 
 * launcher_application_get_icon_name
690
 
 * @application: A #LauncherApplication object
691
 
 * 
692
 
 * provides an icon-name associated with the application
693
 
 * 
694
 
 * Returns: an icon-name string
695
 
 */
696
 
const gchar *
697
 
launcher_application_get_icon_name (LauncherApplication *application)
698
 
{
699
 
  g_return_val_if_fail (application, NULL);
700
 
  
701
 
  return application->priv->icon_name;
702
 
}
703
 
 
704
 
/**
705
 
 * launcher_application_get_exec_string
706
 
 * @application: A #LauncherApplication
707
 
 * 
708
 
 * provides the executionable string for the application
709
 
 * 
710
 
 * Returns: a string representing the excutionable string
711
 
 */
712
 
const gchar *
713
 
launcher_application_get_exec_string (LauncherApplication *application)
714
 
{
715
 
  g_return_val_if_fail (application, NULL);
716
 
  
717
 
  return application->priv->exec;
718
 
}
719
 
 
720
 
/**
721
 
 * launcher_application_get_desktop_file
722
 
 * @application: A #LauncherApplication
723
 
 * 
724
 
 * provides the desktop file path for the application
725
 
 * 
726
 
 * Returns: a string that contains the path to the desktop file for this application
727
 
 */
728
 
const gchar *
729
 
launcher_application_get_desktop_file (LauncherApplication *application)
730
 
{
731
 
  g_return_val_if_fail (application, NULL);
732
 
  
733
 
  return application->priv->desktop_file_path;
734
 
}
735
 
 
736
 
/**
737
 
 * launcher_application_set_desktop_file_path
738
 
 * @application: A #LauncherApplication
739
 
 * @desktop_file: A string describing the path to a desktop file
740
 
 * 
741
 
 * sets the desktop file for this application and will also regenerate its 
742
 
 * information
743
 
 * 
744
 
 */
745
 
void
746
 
launcher_application_set_desktop_file (LauncherApplication *application,
747
 
                                       gchar *desktop_file)
748
 
{
749
 
  GKeyFile *desktop_keyfile;
750
 
  GError    *error = NULL;
751
 
  gchar     *name = NULL;
752
 
  gchar     *exec = NULL;
753
 
  gchar     *comment = NULL;
754
 
  gchar     *icon_name = NULL;
755
 
  
756
 
  g_return_if_fail (application);
757
 
  g_return_if_fail (desktop_file);
758
 
 
759
 
  desktop_keyfile = g_key_file_new ();
760
 
  g_key_file_load_from_file (desktop_keyfile, desktop_file, 0, &error);
761
 
 
762
 
  if (error) 
763
 
    {
764
 
      g_warning ("launcher-application.c: Unable to read from desktop file %s: %s", 
765
 
                desktop_file,
766
 
                error->message);
767
 
      g_error_free (error);
768
 
      return;
769
 
    }
770
 
 
771
 
  name = g_key_file_get_locale_string (desktop_keyfile,
772
 
                                      G_KEY_FILE_DESKTOP_GROUP,
773
 
                                      G_KEY_FILE_DESKTOP_KEY_NAME,
774
 
                                      NULL,
775
 
                                      &error);
776
 
  if (error) 
777
 
    {
778
 
      g_warning ("Unable to read from desktop file %s: %s", 
779
 
                desktop_file, 
780
 
                error->message);
781
 
      g_error_free (error);
782
 
      name = "";
783
 
    }
784
 
 
785
 
 
786
 
  icon_name = g_key_file_get_string (desktop_keyfile,
787
 
                                     G_KEY_FILE_DESKTOP_GROUP,
788
 
                                     G_KEY_FILE_DESKTOP_KEY_ICON,
789
 
                                     NULL);
790
 
 
791
 
  exec = g_key_file_get_string (desktop_keyfile,
792
 
                                G_KEY_FILE_DESKTOP_GROUP,
793
 
                                G_KEY_FILE_DESKTOP_KEY_EXEC,
794
 
                                NULL);
795
 
  
796
 
  comment = g_key_file_get_locale_string (desktop_keyfile,
797
 
                                          G_KEY_FILE_DESKTOP_GROUP,
798
 
                                          G_KEY_FILE_DESKTOP_KEY_COMMENT,
799
 
                                          NULL,
800
 
                                          &error);
801
 
 
802
 
  if (error) 
803
 
  {
804
 
    g_warning ("Unable to read comment from desktop file %s: %s", 
805
 
              desktop_file, 
806
 
              error->message);
807
 
    g_error_free (error);
808
 
    comment = "";
809
 
  }
810
 
  
811
 
  application->priv->name = name;
812
 
  application->priv->icon_name = icon_name;
813
 
  application->priv->exec = exec;
814
 
  application->priv->comment = comment;
815
 
  application->priv->desktop_file_path = desktop_file;
816
 
 
817
 
 
818
 
}
819
 
 
820
 
 
821
 
/**
822
 
 * launcher_application_get_running
823
 
 * @application: A #LauncherApplication
824
 
 * 
825
 
 * Method to tell if the @application is currently running or not
826
 
 * 
827
 
 * Returns: A Truth value
828
 
 */
829
 
gboolean
830
 
launcher_application_get_running (LauncherApplication *application) 
831
 
{
832
 
  g_return_val_if_fail (application, FALSE);
833
 
  
834
 
  return application->priv->running;
835
 
}
836
 
 
837
 
/**
838
 
 * launcher_application_get_running
839
 
 * @application: A #LauncherApplication
840
 
 * 
841
 
 * Method to tell if the @application is a favorite or not
842
 
 * 
843
 
 * Returns: A Truth value
844
 
 */
845
 
gboolean
846
 
launcher_application_get_favorite (LauncherApplication *application)
847
 
{
848
 
  g_return_val_if_fail (application, FALSE);
849
 
  
850
 
  return application->priv->favorite;
851
 
}
852
 
 
853
 
/**
854
 
 * launcher_application_get_categories
855
 
 * @application: A #LauncherApplication
856
 
 * 
857
 
 * provides the categories that @application is associated with
858
 
 * 
859
 
 * Returns: (transfer none): A GSList * of categories
860
 
 */
861
 
GSList *
862
 
launcher_application_get_categories (LauncherApplication *application)
863
 
{
864
 
  g_return_val_if_fail(application, NULL);
865
 
  
866
 
  return application->priv->categories;
867
 
}
868
 
 
869
 
/**
870
 
 * launcher_application_get_focused
871
 
 * @application: A #LauncherApplication
872
 
 *
873
 
 * provides a method that will return true if any of this @applicaiton's windows
874
 
 * are currently "focused"
875
 
 *
876
 
 * Returns: a gboolean value
877
 
 */
878
 
gboolean 
879
 
launcher_application_get_focused (LauncherApplication *application)
880
 
{
881
 
  g_return_val_if_fail (application, FALSE);
882
 
  
883
 
  return application->priv->focused;
884
 
}
885
 
 
886
 
/**
887
 
 * launcher_application_set_focused
888
 
 * @application: A #LauncherApplication
889
 
 * @window: A #WnckWindow
890
 
 * 
891
 
 * provides a mechanism to set @application as focused by providing it the 
892
 
 * focused @window (or null), this does not focus said @window - passing null
893
 
 * will not use the window tracking feature
894
 
 */
895
 
 
896
 
void
897
 
launcher_application_set_focused (LauncherApplication *application, 
898
 
                                  WnckWindow *window)
899
 
{
900
 
  g_return_if_fail (application);
901
 
  LauncherApplicationWindow *founditem;
902
 
  GTimeVal timeval;
903
 
 
904
 
  g_get_current_time (&timeval);
905
 
  
906
 
  if (window == NULL)
907
 
    {
908
 
      g_object_set (G_OBJECT (application),
909
 
                    "focused", TRUE,
910
 
                    NULL);
911
 
      return;
912
 
    }
913
 
 
914
 
   founditem = g_hash_table_lookup (application->priv->windows,
915
 
                                    window);
916
 
 
917
 
   if (founditem)
918
 
     {
919
 
       founditem->timestamp.tv_sec = timeval.tv_sec;
920
 
       founditem->timestamp.tv_usec = timeval.tv_usec;
921
 
     }
922
 
   else 
923
 
     {
924
 
       founditem = g_slice_new (LauncherApplicationWindow);
925
 
       founditem->window = window;
926
 
       founditem->timestamp.tv_sec = timeval.tv_sec;
927
 
       founditem->timestamp.tv_usec = timeval.tv_usec;
928
 
       g_hash_table_insert (application->priv->windows, window, founditem);
929
 
     }
930
 
 
931
 
   g_object_set (G_OBJECT (application),
932
 
                 "focused", TRUE,
933
 
                 NULL);
934
 
}
935
 
 
936
 
WnckWindow *
937
 
get_latest_window(LauncherApplication *app)
938
 
{
939
 
  // attempts to find the last focused wnckwindow
940
 
  WnckWindow *founditem = NULL;
941
 
  GTimeVal best_time;
942
 
  GHashTableIter iter;
943
 
  gpointer key, value;
944
 
 
945
 
  best_time.tv_sec = 0;
946
 
  best_time.tv_usec = 0;
947
 
 
948
 
  g_hash_table_iter_init (&iter, app->priv->windows);
949
 
  while (g_hash_table_iter_next (&iter, &key, &value))
950
 
    {
951
 
      LauncherApplicationWindow *appwin = (LauncherApplicationWindow *)value;
952
 
      if (WNCK_IS_WINDOW (appwin->window))
953
 
        {
954
 
          WnckWindowType type = wnck_window_get_window_type (appwin->window);
955
 
          
956
 
          if (type == WNCK_WINDOW_DESKTOP ||
957
 
              type == WNCK_WINDOW_DOCK ||
958
 
              type == WNCK_WINDOW_TOOLBAR ||
959
 
              type == WNCK_WINDOW_MENU ||
960
 
              type == WNCK_WINDOW_SPLASHSCREEN)
961
 
            {
962
 
              continue;
963
 
            }
964
 
          if (appwin->timestamp.tv_sec > best_time.tv_sec)
965
 
            {
966
 
              if (appwin->timestamp.tv_usec > best_time.tv_usec)
967
 
                {
968
 
                  best_time.tv_sec = appwin->timestamp.tv_sec;
969
 
                  best_time.tv_usec = appwin->timestamp.tv_usec;
970
 
                  founditem = appwin->window;
971
 
                }
972
 
            }
973
 
        }
974
 
      else 
975
 
        {
976
 
          // our window no longer exists, remove it from the table
977
 
          g_hash_table_remove (app->priv->windows, key);
978
 
        }
979
 
    }
980
 
 
981
 
  return founditem;
982
 
}
983
 
 
984
 
 
985
 
/**
986
 
 * launcher_application_show
987
 
 * @application: a #LauncherApplication
988
 
 *
989
 
 * this method will focus the latest un-minimized window this @application has
990
 
 * all this @application's windows are minimized then this method will 
991
 
 * unminimize them all
992
 
 */
993
 
void
994
 
launcher_application_show (LauncherApplication *application)
995
 
{
996
 
  g_return_if_fail (application);
997
 
  g_return_if_fail (application->priv->wnck_apps);
998
 
 
999
 
  /* FIXME
1000
 
   * for now i am just grabbing the first wnckwindow we find thats not 
1001
 
   * minimized, really we need to track what windows are focused so we know
1002
 
   * the last focused window
1003
 
   */
1004
 
 
1005
 
  GSList *a;
1006
 
  GList *w;
1007
 
  WnckWindow *found_window = NULL;
1008
 
 
1009
 
  // get the last focused window
1010
 
  found_window = get_latest_window (application);
1011
 
 
1012
 
  if (found_window)
1013
 
      /* just show this window */
1014
 
      wnck_window_activate (found_window, gtk_get_current_event_time ());
1015
 
  else
1016
 
    {
1017
 
      /* either there were no windows or all windows were minimized, so 
1018
 
       * unminimize them
1019
 
       */
1020
 
      for (a = application->priv->wnck_apps; a; a = a->next)
1021
 
        {
1022
 
          WnckApplication *app = a->data;
1023
 
          
1024
 
          for (w = wnck_application_get_windows (app); w; w = w->next)
1025
 
            {
1026
 
              WnckWindow *window = w->data;
1027
 
              wnck_window_activate (window, gtk_get_current_event_time ());
1028
 
            }
1029
 
        }
1030
 
    }
1031
 
}
1032
 
  
 
345
}