Back to index

bamf  0.2.120
bamf-daemon.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2011 Canonical Ltd
00003  *
00004  * This program is free software: you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License version 3 as
00006  * published by the Free Software Foundation.
00007  *
00008  * This program is distributed in the hope that it will be useful,
00009  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00010  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011  * GNU General Public License for more details.
00012  *
00013  * You should have received a copy of the GNU General Public License
00014  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00015  *
00016  * Authored by: Marco Trevisan (TreviƱo) <3v1n0@ubuntu.com>
00017  *
00018  */
00019 
00020 #include "bamf.h"
00021 
00022 #include "bamf-daemon.h"
00023 #include "bamf-matcher.h"
00024 #include "bamf-control.h"
00025 #include "bamf-indicator-source.h"
00026 
00027 G_DEFINE_TYPE (BamfDaemon, bamf_daemon, G_TYPE_OBJECT);
00028 #define BAMF_DAEMON_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE(obj, \
00029                                       BAMF_TYPE_DAEMON, BamfDaemonPrivate))
00030 
00031 static BamfDaemon *instance = NULL;
00032 
00033 struct _BamfDaemonPrivate
00034 {
00035   BamfMatcher *matcher;
00036   BamfControl *control;
00037   BamfIndicatorSource *approver;
00038   GMainLoop *loop;
00039 };
00040 
00041 gboolean
00042 bamf_daemon_is_running (BamfDaemon *self)
00043 {
00044   g_return_val_if_fail (self, FALSE);
00045 
00046   if (self->priv->loop && g_main_loop_is_running (self->priv->loop))
00047     {
00048       return TRUE;
00049     }
00050 
00051   return FALSE;
00052 }
00053 
00054 static void
00055 bamf_on_bus_acquired (GDBusConnection *connection, const gchar *name,
00056                       BamfDaemon *self)
00057 {
00058   GError *error = NULL;
00059   g_return_if_fail (BAMF_IS_DAEMON (self));
00060 
00061   g_debug ("Acquired a message bus connection");
00062 
00063   g_dbus_connection_set_exit_on_close (connection, TRUE);
00064 
00065   self->priv->matcher = bamf_matcher_get_default ();
00066   self->priv->control = bamf_control_get_default ();
00067   //self->priv->approver = bamf_indicator_source_get_default ();
00068 
00069   g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (self->priv->matcher),
00070                                     connection,
00071                                     BAMF_MATCHER_PATH,
00072                                     &error);
00073 
00074   if (error)
00075     {
00076       g_critical ("Can't register BAMF matcher at path %s: %s", BAMF_MATCHER_PATH,
00077                                                                 error->message);
00078       g_clear_error (&error);
00079     }
00080 
00081   g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (self->priv->control),
00082                                     connection,
00083                                     BAMF_CONTROL_PATH,
00084                                     &error);
00085 
00086   if (error)
00087     {
00088       g_critical ("Can't register BAMF control at path %s: %s", BAMF_CONTROL_PATH,
00089                                                                 error->message);
00090       g_clear_error (&error);
00091     }
00092 /*
00093   g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (self->priv->approver),
00094                                     connection,
00095                                     BAMF_INDICATOR_SOURCE_PATH,
00096                                     &error);
00097 
00098   if (error)
00099     {
00100       g_critical ("Can't register BAMF approver at path %s: %s",
00101                                      BAMF_INDICATOR_SOURCE_PATH, error->message);
00102       g_clear_error (&error);
00103     }
00104 */
00105 }
00106 
00107 static void
00108 bamf_on_name_acquired (GDBusConnection *connection, const gchar *name,
00109                        BamfDaemon *self)
00110 {
00111   g_debug ("Acquired the name %s", name);
00112 }
00113 
00114 static void
00115 bamf_on_name_lost (GDBusConnection *connection, const gchar *name, BamfDaemon *self)
00116 {
00117   g_critical ("Lost the name %s, another BAMF daemon is currently running", name);
00118 
00119   bamf_daemon_stop (self);
00120 }
00121 
00122 void
00123 bamf_daemon_start (BamfDaemon *self)
00124 {
00125   g_return_if_fail (BAMF_IS_DAEMON (self));
00126 
00127   if (bamf_daemon_is_running (self))
00128     return;
00129 
00130   g_bus_own_name (G_BUS_TYPE_SESSION, BAMF_DBUS_SERVICE,
00131                   G_BUS_NAME_OWNER_FLAGS_NONE,
00132                   (GBusAcquiredCallback) bamf_on_bus_acquired,
00133                   (GBusNameAcquiredCallback) bamf_on_name_acquired,
00134                   (GBusNameLostCallback) bamf_on_name_lost,
00135                   self, NULL);
00136 
00137   g_main_loop_run (self->priv->loop);
00138 }
00139 
00140 void
00141 bamf_daemon_stop (BamfDaemon *self)
00142 {
00143   g_return_if_fail (BAMF_IS_DAEMON (self));
00144 
00145   if (self->priv->matcher)
00146     {
00147       g_object_unref (self->priv->matcher);
00148       self->priv->matcher = NULL;
00149     }
00150 
00151   if (self->priv->control)
00152     {
00153       g_object_unref (self->priv->control);
00154       self->priv->control = NULL;
00155     }
00156 
00157   if (self->priv->approver)
00158     {
00159       g_object_unref (self->priv->approver);
00160       self->priv->approver = NULL;
00161     }
00162 
00163   g_main_loop_quit (self->priv->loop);
00164 }
00165 
00166 static void
00167 bamf_daemon_dispose (GObject *object)
00168 {
00169   BamfDaemon *self = BAMF_DAEMON (object);
00170 
00171   bamf_daemon_stop (self);
00172 
00173   if (self->priv->loop)
00174     {
00175       g_main_loop_unref (self->priv->loop);
00176       self->priv->loop = NULL;
00177     }
00178 
00179   G_OBJECT_CLASS (bamf_daemon_parent_class)->dispose (object);
00180 }
00181 
00182 static void
00183 bamf_daemon_finalize (GObject *object)
00184 {
00185   instance = NULL;
00186 }
00187 
00188 static void
00189 bamf_daemon_init (BamfDaemon *self)
00190 {
00191   BamfDaemonPrivate *priv;
00192   priv = self->priv = BAMF_DAEMON_GET_PRIVATE (self);
00193 
00194   priv->loop = g_main_loop_new (NULL, FALSE);
00195 }
00196 
00197 static void
00198 bamf_daemon_class_init (BamfDaemonClass * klass)
00199 {
00200   GObjectClass *object_class = G_OBJECT_CLASS (klass);
00201 
00202   object_class->dispose  = bamf_daemon_dispose;
00203   object_class->finalize = bamf_daemon_finalize;
00204 
00205   g_type_class_add_private (klass, sizeof (BamfDaemonPrivate));
00206 }
00207 
00208 BamfDaemon *
00209 bamf_daemon_get_default (void)
00210 {
00211   if (!BAMF_IS_DAEMON (instance))
00212     instance = (BamfDaemon *) g_object_new (BAMF_TYPE_DAEMON, NULL);
00213 
00214   return instance;
00215 }