~unity-team/bamf/trunk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
 * Copyright (C) 2011 Canonical Ltd
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Authored by: Marco Trevisan (TreviƱo) <3v1n0@ubuntu.com>
 *
 */

#include "bamf-daemon.h"
#include "bamf-matcher.h"
#include "bamf-control.h"

G_DEFINE_TYPE (BamfDaemon, bamf_daemon, G_TYPE_OBJECT);
#define BAMF_DAEMON_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE(obj, \
                                      BAMF_TYPE_DAEMON, BamfDaemonPrivate))

static BamfDaemon *instance = NULL;

struct _BamfDaemonPrivate
{
  BamfMatcher *matcher;
  BamfControl *control;
  GMainLoop *loop;
};

gboolean
bamf_daemon_is_running (BamfDaemon *self)
{
  g_return_val_if_fail (self, FALSE);

  if (self->priv->loop && g_main_loop_is_running (self->priv->loop))
    {
      return TRUE;
    }

  return FALSE;
}

static void
bamf_on_bus_acquired (GDBusConnection *connection, const gchar *name,
                      BamfDaemon *self)
{
  GError *error = NULL;
  g_return_if_fail (BAMF_IS_DAEMON (self));

  g_debug ("Acquired a message bus connection");

  g_dbus_connection_set_exit_on_close (connection, TRUE);

  self->priv->matcher = bamf_matcher_get_default ();
  self->priv->control = bamf_control_get_default ();

  g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (self->priv->matcher),
                                    connection,
                                    BAMF_DBUS_MATCHER_PATH,
                                    &error);

  if (error)
    {
      g_critical ("Can't register BAMF matcher at path %s: %s", BAMF_DBUS_MATCHER_PATH,
                                                                error->message);
      g_clear_error (&error);
    }

  g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (self->priv->control),
                                    connection,
                                    BAMF_DBUS_CONTROL_PATH,
                                    &error);

  if (error)
    {
      g_critical ("Can't register BAMF control at path %s: %s", BAMF_DBUS_CONTROL_PATH,
                                                                error->message);
      g_clear_error (&error);
    }
}

static void
bamf_on_name_acquired (GDBusConnection *connection, const gchar *name,
                       BamfDaemon *self)
{
  g_debug ("Acquired the name %s", name);
}

static void
bamf_on_name_lost (GDBusConnection *connection, const gchar *name, BamfDaemon *self)
{
  g_critical ("Lost the name %s, another BAMF daemon is currently running", name);

  bamf_daemon_stop (self);
}

void
bamf_daemon_start (BamfDaemon *self)
{
  g_return_if_fail (BAMF_IS_DAEMON (self));

  if (bamf_daemon_is_running (self))
    return;

  g_bus_own_name (G_BUS_TYPE_SESSION, BAMF_DBUS_SERVICE_NAME,
                  G_BUS_NAME_OWNER_FLAGS_NONE,
                  (GBusAcquiredCallback) bamf_on_bus_acquired,
                  (GBusNameAcquiredCallback) bamf_on_name_acquired,
                  (GBusNameLostCallback) bamf_on_name_lost,
                  self, NULL);

  g_main_loop_run (self->priv->loop);
}

void
bamf_daemon_stop (BamfDaemon *self)
{
  g_return_if_fail (BAMF_IS_DAEMON (self));

  if (self->priv->matcher)
    {
      g_object_unref (self->priv->matcher);
      self->priv->matcher = NULL;
    }

  if (self->priv->control)
    {
      g_object_unref (self->priv->control);
      self->priv->control = NULL;
    }

  g_main_loop_quit (self->priv->loop);
}

static void
bamf_daemon_dispose (GObject *object)
{
  BamfDaemon *self = BAMF_DAEMON (object);

  bamf_daemon_stop (self);

  if (self->priv->loop)
    {
      g_main_loop_unref (self->priv->loop);
      self->priv->loop = NULL;
    }

  G_OBJECT_CLASS (bamf_daemon_parent_class)->dispose (object);
}

static void
bamf_daemon_finalize (GObject *object)
{
  instance = NULL;
}

static void
bamf_daemon_init (BamfDaemon *self)
{
  BamfDaemonPrivate *priv;
  priv = self->priv = BAMF_DAEMON_GET_PRIVATE (self);

  priv->loop = g_main_loop_new (NULL, FALSE);
}

static void
bamf_daemon_class_init (BamfDaemonClass * klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->dispose  = bamf_daemon_dispose;
  object_class->finalize = bamf_daemon_finalize;

  g_type_class_add_private (klass, sizeof (BamfDaemonPrivate));
}

BamfDaemon *
bamf_daemon_get_default (void)
{
  if (!BAMF_IS_DAEMON (instance))
    instance = (BamfDaemon *) g_object_new (BAMF_TYPE_DAEMON, NULL);

  return instance;
}