~ricmm/+junk/unity-lens_music-sc

« back to all changes in this revision

Viewing changes to src/main.vala

  • Committer: Ricardo Mendoza
  • Date: 2012-09-05 14:20:15 UTC
  • Revision ID: ricardo.mendoza@canonical.com-20120905142015-prem6hiyfshwgm8q
Initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2011 Canonical Ltd
 
3
 *
 
4
 * This program is free software: you can redistribute it and/or modify
 
5
 * it under the terms of the GNU General Public License version 3 as
 
6
 * published by the Free Software Foundation.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful,
 
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
 * GNU General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU General Public License
 
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
 *
 
16
 * Authored by Mikkel Kamstrup Erlandsen <mikkel.kamstrup@canonical.com>
 
17
 *             Alex Launi <alex.launi@canonical.com>
 
18
 *
 
19
 */
 
20
 
 
21
using GLib;
 
22
using Config;
 
23
 
 
24
namespace Unity.MusicLens {
 
25
 
 
26
  static Application? app = null;
 
27
  static Daemon? daemon = null;
 
28
 
 
29
  /* Check if a given well known DBus is owned.
 
30
   * WARNING: This does sync IO!  */
 
31
  public static bool dbus_name_has_owner (string name)
 
32
  {
 
33
    try {
 
34
      bool has_owner;
 
35
      DBusConnection bus = Bus.get_sync (BusType.SESSION);
 
36
      Variant result = bus.call_sync ("org.freedesktop.DBus",
 
37
                                      "/org/freedesktop/dbus",
 
38
                                      "org.freedesktop.DBus",
 
39
                                      "NameHasOwner",
 
40
                                      new Variant ("(s)", name),
 
41
                                      new VariantType ("(b)"),
 
42
                                      DBusCallFlags.NO_AUTO_START,
 
43
                                      -1);
 
44
      result.get ("(b)", out has_owner);
 
45
      return has_owner;
 
46
    } catch (Error e) {
 
47
      warning ("Unable to decide whether '%s' is running: %s", name, e.message);
 
48
    }
 
49
    
 
50
    return false;
 
51
  }
 
52
 
 
53
  public static int main (string[] args)
 
54
  {
 
55
    /* Sort up locale to get translations but also sorting and
 
56
     * punctuation right */
 
57
    GLib.Intl.textdomain (Config.PACKAGE);
 
58
    GLib.Intl.bindtextdomain (Config.PACKAGE, Config.LOCALEDIR);
 
59
    GLib.Intl.bind_textdomain_codeset (Config.PACKAGE, "UTF-8");
 
60
    GLib.Intl.setlocale(GLib.LocaleCategory.ALL, "");
 
61
  
 
62
    /* Workaround for https://bugzilla.gnome.org/show_bug.cgi?id=640714 
 
63
     * GApplication.register() call owns our DBus name in a sync manner
 
64
     * making it race against GDBus' worker thread to export our
 
65
     * objects on the bus before/after owning our name and receiving
 
66
     * method calls on our objects (which may not yet be up!)*/
 
67
    if (dbus_name_has_owner ("com.canonical.Unity.Lens.Music"))
 
68
      {
 
69
        print ("Another instance of the Unity Music Daemon " +
 
70
               "already appears to be running.\nBailing out.\n");
 
71
        return 2;
 
72
      }
 
73
    
 
74
    /* Now register our DBus objects *before* acquiring the name!
 
75
     * See above for reasons */
 
76
    daemon = new Daemon ();
 
77
  
 
78
    /* Use GApplication directly for single instance app functionality */
 
79
    app = new Application ("com.canonical.Unity.Lens.Music",
 
80
                           ApplicationFlags.IS_SERVICE);
 
81
    try {
 
82
      app.register ();
 
83
    } catch (Error e) {
 
84
      /* FIXME: We get this error if another daemon is already running,
 
85
       * but it uses a generic error so we can't detect this reliably... */
 
86
      print ("Failed to start music daemon: %s\n", e.message);
 
87
      return 1;
 
88
    }
 
89
    
 
90
    if (app.get_is_remote ())
 
91
      {
 
92
        print ("Another instance of the Unity Music Daemon " +
 
93
               "already appears to be running.\nBailing out.\n");
 
94
        return 2;
 
95
      }
 
96
    
 
97
    /* Hold()ing the app makes sure the GApplication doesn't exit */    
 
98
    app.hold();
 
99
    return app.run ();
 
100
  }
 
101
 
 
102
} /* namespace */