~njpatel/gwibber/libgwibber-o-client-njpatel

« back to all changes in this revision

Viewing changes to lens/src/main.vala

  • Committer: Neil Jagdish Patel
  • Date: 2011-06-19 12:48:27 UTC
  • mfrom: (159.2.27 client-gtk3)
  • Revision ID: neil.patel@canonical.com-20110619124827-4agvjitrl6e38q2p
Merge in kens work as well as add some updates. Should have done the merge and my work separately but I forgot to commit after the merge :)

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 it
 
5
 * under the terms of the GNU General Public License version 3, as published
 
6
 * by the Free Software Foundation.
 
7
 
 
8
 * This program is distributed in the hope that it will be useful, but
 
9
 * WITHOUT ANY WARRANTY; without even the implied warranties of
 
10
 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 
11
 * PURPOSE.  See the GNU General Public License for more details.
 
12
 
 
13
 * You should have received a copy of the GNU General Public License along
 
14
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
 *
 
16
 * Authored by Ken VanDine <ken.vandine@canonical.com>
 
17
 */
 
18
 
 
19
using Config;
 
20
 
 
21
namespace Gwibber {
 
22
 
 
23
  static Application? app = null;
 
24
  static Daemon? daemon = null;
 
25
 
 
26
  public static bool dbus_name_has_owner (string name)
 
27
  {
 
28
    try {
 
29
      bool has_owner;
 
30
      DBusConnection bus = Bus.get_sync (BusType.SESSION);
 
31
      Variant result = bus.call_sync ("org.freedesktop.DBus",
 
32
                                      "/org/freedesktop/dbus",
 
33
                                      "org.freedesktop.DBus",
 
34
                                      "NameHasOwner",
 
35
                                      new Variant ("(s)", name),
 
36
                                      new VariantType ("(b)"),
 
37
                                      DBusCallFlags.NO_AUTO_START,
 
38
                                      -1);
 
39
      result.get ("(b)", out has_owner);
 
40
      return has_owner;
 
41
    } catch (IOError e) {
 
42
      warning ("Unable to decide whether '%s' is running: %s", name, e.message);
 
43
    }
 
44
    
 
45
    return false;
 
46
  }
 
47
 
 
48
  public static int main (string[] args)
 
49
    {
 
50
      GLib.Intl.textdomain (Config.PACKAGE);
 
51
      GLib.Intl.bindtextdomain (Config.PACKAGE, Config.LOCALE_DIR);
 
52
      GLib.Intl.bind_textdomain_codeset (Config.PACKAGE, "UTF-8");
 
53
      GLib.Intl.setlocale(GLib.LocaleCategory.ALL, "");
 
54
    
 
55
      DesktopAppInfo.set_desktop_env ("GNOME");
 
56
      
 
57
      /* Export the place daemon on the session bus - as everywhere else
 
58
       * these values should match those definedd in the .place file */
 
59
 
 
60
      if (dbus_name_has_owner ("com.canonical.Unity.Gwibber"))
 
61
      {
 
62
        print ("Another instance of the Unity Gwibber Daemon " +
 
63
               "already appears to be running.\nBailing out.\n");
 
64
        return 2;
 
65
      }
 
66
 
 
67
      daemon = new Daemon ();
 
68
      /* Use GApplication directly for single instance app functionality */
 
69
      app = new Application ("com.canonical.Unity.Gwibber",
 
70
                             ApplicationFlags.IS_SERVICE);
 
71
      try {
 
72
        app.register ();
 
73
      } catch (Error e) {
 
74
        /* FIXME: We get this error if another daemon is already running,
 
75
         * but it uses a generic error so we can't detect this reliably... */
 
76
        print ("Failed to start applications daemon: %s\n", e.message);
 
77
        return 1;
 
78
      }
 
79
    
 
80
      if (app.get_is_remote ())
 
81
      {
 
82
        print ("Another instance of the Unity Applications Daemon " +
 
83
               "already appears to be running.\nBailing out.\n");
 
84
        return 2;
 
85
      }
 
86
    
 
87
      /* Hold()ing the app makes sure the GApplication doesn't exit */
 
88
      app.hold();
 
89
      return app.run ();
 
90
    }
 
91
} /* end Gwibber namespace */