2
* Copyright (C) 2011 Canonical Ltd
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.
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.
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/>.
16
* Authored by Mikkel Kamstrup Erlandsen <mikkel.kamstrup@canonical.com>
17
* Alex Launi <alex.launi@canonical.com>
24
namespace Unity.MusicLens {
26
static Application? app = null;
27
static Daemon? daemon = null;
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)
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",
40
new Variant ("(s)", name),
41
new VariantType ("(b)"),
42
DBusCallFlags.NO_AUTO_START,
44
result.get ("(b)", out has_owner);
47
warning ("Unable to decide whether '%s' is running: %s", name, e.message);
53
public static int main (string[] args)
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, "");
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"))
69
print ("Another instance of the Unity Music Daemon " +
70
"already appears to be running.\nBailing out.\n");
74
/* Now register our DBus objects *before* acquiring the name!
75
* See above for reasons */
76
daemon = new Daemon ();
78
/* Use GApplication directly for single instance app functionality */
79
app = new Application ("com.canonical.Unity.Lens.Music",
80
ApplicationFlags.IS_SERVICE);
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);
90
if (app.get_is_remote ())
92
print ("Another instance of the Unity Music Daemon " +
93
"already appears to be running.\nBailing out.\n");
97
/* Hold()ing the app makes sure the GApplication doesn't exit */