~unity-team/unity-lens-sample/trunk

« back to all changes in this revision

Viewing changes to src/main.vala

  • Committer: Mikkel Kamstrup Erlandsen
  • Date: 2010-10-23 19:28:23 UTC
  • Revision ID: mikkel.kamstrup@gmail.com-20101023192823-gw12ovvxnd3cjp6y
Require only Valac 0.9 since maverick doesn't ship 0.10

Make code more readable and add some comments

Do some s/files/sample/ in src/Makefile.am

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 * BEGIN LICENSE
3
 
 * This file is in the public domain
4
 
 * END LICENSE
 
2
 * Copyright (C) 2010 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
 *
5
18
 */
6
19
 
 
20
using DBus;
7
21
using Config;
8
22
 
9
23
namespace Unity.SamplePlace {
10
24
 
11
 
  static Daemon? daemon = null;
12
 
  static MainLoop? mainloop = null;
13
 
 
14
25
  public static int main (string[] args)
15
26
    {
16
27
      GLib.Intl.textdomain (Config.PACKAGE);
19
30
      GLib.Intl.setlocale(GLib.LocaleCategory.ALL, "");
20
31
    
21
32
      DesktopAppInfo.set_desktop_env ("GNOME");
22
 
      
 
33
    
 
34
      var daemon = new Daemon ();
 
35
 
23
36
      /* Export the place daemon on the session bus - as everywhere else
24
37
       * these values should match those definedd in the .place file */
25
 
      Bus.own_name (BusType.SESSION, "com.canonical.Unity.SamplePlace",
26
 
                    BusNameOwnerFlags.NONE,
27
 
                    on_bus_acquired, on_name_acquired, on_name_lost);
28
 
      
29
 
      mainloop = new MainLoop ();
30
 
      mainloop.run ();
 
38
      try {
 
39
        var conn = DBus.Bus.get (DBus.BusType.SESSION);
 
40
        dynamic DBus.Object bus = conn.get_object ("org.freedesktop.DBus",
 
41
                                                   "/org/freedesktop/DBus",
 
42
                                                   "org.freedesktop.DBus");
 
43
 
 
44
        uint request_name_result = bus.request_name ("com.canonical.Unity.SamplePlace", (uint) 0);
 
45
        if (request_name_result == DBus.RequestNameReply.PRIMARY_OWNER)
 
46
          {
 
47
            debug ("Running sample place daemon.");
 
48
            new MainLoop (null, false).run();
 
49
          }
 
50
        else
 
51
          {
 
52
            print (_("Another Sample Place daemon appears to be running.\nBailing out."));
 
53
            return 1;
 
54
          }
 
55
      } catch (DBus.Error error) {
 
56
        print (_("Error connecting to session bus: %s.\nBailing out."), error.message);
 
57
        return 2;
 
58
      }
31
59
 
32
60
      return 0;
33
61
    }
34
 
  
35
 
  private static void on_bus_acquired (DBusConnection conn, string name)
36
 
  {
37
 
    debug ("Connected to session bus - checking for existing instances...");
38
 
    
39
 
    /* We need to set up our DBus objects *before* we know if we've acquired
40
 
     * the name. This is a bit unfortunate because it means we might do work
41
 
     * for no reason if another daemon is already running. See
42
 
     * https://bugzilla.gnome.org/show_bug.cgi?id=640714 */
43
 
    daemon = new Daemon ();
44
 
  }
45
 
  
46
 
  private static void on_name_acquired (DBusConnection conn, string name)
47
 
  {
48
 
    debug ("Acquired name %s. We're the main instance.\nAll system are go.",
49
 
           name);
50
 
  }
51
 
  
52
 
  private static void on_name_lost (DBusConnection conn, string name)
53
 
  {
54
 
    debug ("Another daemon is running.\nBailing out.");
55
 
    mainloop.quit ();
56
 
  }
57
 
  
58
62
} /* End namespace Unity.SamplePlace */