Back to index

bamf  0.2.120
bamf-control.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2010-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: Jason Smith <jason.smith@canonical.com>
00017  *              Marco Trevisan (TreviƱo) <3v1n0@ubuntu.com>
00018  *
00019  */
00020 
00021 
00022 #include "bamf-tab-source.h"
00023 #include "bamf-matcher.h"
00024 #include "bamf-control.h"
00025 #include "bamf-indicator-source.h"
00026 #include "bamf-daemon.h"
00027 
00028 G_DEFINE_TYPE (BamfControl, bamf_control, BAMF_DBUS_TYPE_CONTROL_SKELETON);
00029 #define BAMF_CONTROL_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE(obj, \
00030 BAMF_TYPE_CONTROL, BamfControlPrivate))
00031 
00032 struct _BamfControlPrivate
00033 {
00034   GList *sources;
00035 };
00036 
00037 static void 
00038 bamf_control_on_launched_callback (GDBusConnection *connection,
00039                                    const gchar *sender_name,
00040                                    const gchar *object_path,
00041                                    const gchar *interface_name,
00042                                    const gchar *signal_name,
00043                                    GVariant *parameters,
00044                                    gpointer user_data)
00045 {
00046   const gchar *desktop_file;
00047   gint64 pid;
00048 
00049   g_variant_get_child (parameters, 0, "^&ay", &desktop_file);
00050   g_variant_get_child (parameters, 2, "x", &pid);
00051 
00052   bamf_matcher_register_desktop_file_for_pid (bamf_matcher_get_default (),
00053                                               desktop_file, pid);
00054 }
00055 
00056 static void
00057 bamf_control_constructed (GObject *object)
00058 {
00059   GDBusConnection *gbus;
00060 
00061   if (G_OBJECT_CLASS (bamf_control_parent_class)->constructed)
00062     G_OBJECT_CLASS (bamf_control_parent_class)->constructed (object);
00063 
00064   gbus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
00065   g_dbus_connection_signal_subscribe  (gbus,
00066                                        NULL,
00067                                        "org.gtk.gio.DesktopAppInfo",
00068                                        "Launched",
00069                                        "/org/gtk/gio/DesktopAppInfo",
00070                                        NULL,
00071                                        0,
00072                                        bamf_control_on_launched_callback,
00073                                        BAMF_CONTROL (object),
00074                                        NULL);
00075 }
00076 
00077 static gboolean
00078 on_dbus_handle_quit (BamfDBusControl *interface,
00079                      GDBusMethodInvocation *invocation,
00080                      BamfControl *self)
00081 {
00082   bamf_control_quit (self);
00083   g_dbus_method_invocation_return_value (invocation, NULL);
00084 
00085   return TRUE;
00086 }
00087 
00088 static gboolean
00089 on_dbus_handle_set_approver_behavior (BamfDBusControl *interface,
00090                                       GDBusMethodInvocation *invocation,
00091                                       gint behavior,
00092                                       BamfControl *self)
00093 {
00094   bamf_control_set_approver_behavior (self, behavior);
00095   g_dbus_method_invocation_return_value (invocation, NULL);
00096 
00097   return TRUE;
00098 }
00099 
00100 static gboolean
00101 on_dbus_handle_om_nom_nom_desktop_file (BamfDBusControl *interface,
00102                                         GDBusMethodInvocation *invocation,
00103                                         const gchar *desktop_file,
00104                                         BamfControl *self)
00105 {
00106   bamf_control_insert_desktop_file (self, desktop_file);
00107   g_dbus_method_invocation_return_value (invocation, NULL);
00108 
00109   return TRUE;
00110 }
00111 
00112 static gboolean
00113 on_dbus_handle_register_tab_provider (BamfDBusControl *interface,
00114                                       GDBusMethodInvocation *invocation,
00115                                       const gchar *tab_path,
00116                                       BamfControl *self)
00117 {
00118   const gchar *sender = g_dbus_method_invocation_get_sender (invocation);
00119   bamf_control_register_tab_provider (self, sender, tab_path);
00120   g_dbus_method_invocation_return_value (invocation, NULL);
00121 
00122   return TRUE;
00123 }
00124 
00125 static gboolean
00126 on_dbus_handle_register_application_for_pid (BamfDBusControl *interface,
00127                                              GDBusMethodInvocation *invocation,
00128                                              const gchar *application,
00129                                              guint pid,
00130                                              BamfControl *self)
00131 {
00132   bamf_control_register_application_for_pid (self, application, pid);
00133   g_dbus_method_invocation_return_value (invocation, NULL);
00134 
00135   return TRUE;
00136 }
00137 
00138 static void
00139 bamf_control_init (BamfControl * self)
00140 {
00141   self->priv = BAMF_CONTROL_GET_PRIVATE (self);
00142   self->priv->sources = NULL;
00143 
00144   /* Registering signal callbacks to reply to dbus method calls */
00145   g_signal_connect (self, "handle-quit",
00146                     G_CALLBACK (on_dbus_handle_quit), self);
00147 
00148   g_signal_connect (self, "handle-set-approver-behavior",
00149                     G_CALLBACK (on_dbus_handle_set_approver_behavior), self);
00150 
00151   g_signal_connect (self, "handle-om-nom-nom-desktop-file",
00152                     G_CALLBACK (on_dbus_handle_om_nom_nom_desktop_file), self);
00153 
00154   g_signal_connect (self, "handle-register-tab-provider",
00155                     G_CALLBACK (on_dbus_handle_register_tab_provider), self);
00156 
00157   g_signal_connect (self, "handle-register-application-for-pid",
00158                     G_CALLBACK (on_dbus_handle_register_application_for_pid), self);
00159 }
00160 
00161 static void
00162 bamf_control_finalize (GObject *object)
00163 {
00164   BamfControl *self = BAMF_CONTROL (object);
00165   g_list_free_full (self->priv->sources, g_object_unref);
00166   self->priv->sources = NULL;
00167 }
00168 
00169 static void
00170 bamf_control_class_init (BamfControlClass * klass)
00171 {
00172   GObjectClass *obj_class = G_OBJECT_CLASS (klass);
00173 
00174   obj_class->constructed = bamf_control_constructed;
00175   obj_class->finalize = bamf_control_finalize;
00176 
00177   g_type_class_add_private (klass, sizeof (BamfControlPrivate));
00178 }
00179 
00180 void
00181 bamf_control_set_approver_behavior (BamfControl *control,
00182                                     gint32 behavior)
00183 {
00184   BamfIndicatorSource *indicator_source = bamf_indicator_source_get_default ();
00185   bamf_indicator_source_set_behavior (indicator_source, behavior);
00186 }
00187 
00188 void
00189 bamf_control_register_application_for_pid (BamfControl *control,
00190                                            const char *application,
00191                                            gint32 pid)
00192 {
00193   BamfMatcher *matcher = bamf_matcher_get_default ();
00194   bamf_matcher_register_desktop_file_for_pid (matcher, application, pid);
00195 }
00196 
00197 void
00198 bamf_control_insert_desktop_file (BamfControl *control,
00199                                   const char *path)
00200 {
00201   BamfMatcher *matcher = bamf_matcher_get_default ();
00202   bamf_matcher_load_desktop_file (matcher, path);
00203 }
00204 
00205 void
00206 bamf_control_register_tab_provider (BamfControl *control,
00207                                     const char *sender,
00208                                     const char *path)
00209 {
00210   BamfTabSource *source;
00211 
00212   if (!path || !sender)
00213     {
00214       return;
00215     }
00216 
00217   source = bamf_tab_source_new (g_strdup (sender), g_strdup (path));
00218 
00219   if (BAMF_IS_TAB_SOURCE (source))
00220     {
00221       control->priv->sources = g_list_prepend (control->priv->sources, source);
00222     }
00223 }
00224 
00225 static gboolean
00226 bamf_control_on_quit (BamfControl *control)
00227 {
00228   BamfDaemon *daemon = bamf_daemon_get_default ();
00229   bamf_daemon_stop (daemon);
00230   return FALSE;
00231 }
00232 
00233 void
00234 bamf_control_quit (BamfControl *control)
00235 {
00236   g_idle_add ((GSourceFunc) bamf_control_on_quit, control);
00237 }
00238 
00239 BamfControl *
00240 bamf_control_get_default (void)
00241 {
00242   static BamfControl *control;
00243 
00244   if (!BAMF_IS_CONTROL (control))
00245     {
00246       control = (BamfControl *) g_object_new (BAMF_TYPE_CONTROL, NULL);
00247     }
00248 
00249   return control;
00250 }