Back to index

bamf  0.2.120
bamf-control.c
Go to the documentation of this file.
00001 /*
00002  * Copyright 2010 Canonical Ltd.
00003  *
00004  * This program is free software: you can redistribute it and/or modify it
00005  * under the terms of either or both of the following licenses:
00006  *
00007  * 1) the GNU Lesser General Public License version 3, as published by the
00008  * Free Software Foundation; and/or
00009  * 2) the GNU Lesser General Public License version 2.1, as published by
00010  * the Free Software Foundation.
00011  *
00012  * This program is distributed in the hope that it will be useful, but
00013  * WITHOUT ANY WARRANTY; without even the implied warranties of
00014  * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
00015  * PURPOSE.  See the applicable version of the GNU Lesser General Public
00016  * License for more details.
00017  *
00018  * You should have received a copy of both the GNU Lesser General Public
00019  * License version 3 and version 2.1 along with this program.  If not, see
00020  * <http://www.gnu.org/licenses/>
00021  *
00022  * Authored by: Jason Smith <jason.smith@canonical.com>
00023  *              Neil Jagdish Patel <neil.patel@canonical.com>
00024  *
00025  */
00033 #if HAVE_CONFIG_H
00034 #include <config.h>
00035 #endif
00036 
00037 #include "bamf-control.h"
00038 
00039 #include <dbus/dbus.h>
00040 #include <dbus/dbus-glib.h>
00041 
00042 G_DEFINE_TYPE (BamfControl, bamf_control, G_TYPE_OBJECT);
00043 
00044 #define BAMF_CONTROL_GET_PRIVATE(o) \
00045   (G_TYPE_INSTANCE_GET_PRIVATE ((o), BAMF_TYPE_CONTROL, BamfControlPrivate))
00046 
00047 struct _BamfControlPrivate
00048 {
00049   DBusGConnection *connection;
00050   DBusGProxy      *proxy;
00051 };
00052 
00053 /* Globals */
00054 static BamfControl * default_control = NULL;
00055 
00056 /* Forwards */
00057 
00058 /*
00059  * GObject stuff
00060  */
00061 
00062 static void
00063 bamf_control_dispose (GObject *object)
00064 {
00065   BamfControl *self = BAMF_CONTROL (object);
00066 
00067   if (self->priv->proxy)
00068     {
00069       g_object_unref (self->priv->proxy);
00070       self->priv->proxy = NULL;
00071     }
00072 
00073   if (G_OBJECT_CLASS (bamf_control_parent_class)->dispose)
00074       G_OBJECT_CLASS (bamf_control_parent_class)->dispose (object);
00075 }
00076 
00077 static void
00078 bamf_control_class_init (BamfControlClass *klass)
00079 {
00080   GObjectClass *obj_class = G_OBJECT_CLASS (klass);
00081 
00082   g_type_class_add_private (obj_class, sizeof (BamfControlPrivate));
00083   obj_class->dispose = bamf_control_dispose;
00084 }
00085 
00086 
00087 static void
00088 bamf_control_init (BamfControl *self)
00089 {
00090   BamfControlPrivate *priv;
00091   GError           *error = NULL;
00092 
00093   priv = self->priv = BAMF_CONTROL_GET_PRIVATE (self);
00094 
00095   priv->connection = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
00096   if (priv->connection == NULL)
00097     {
00098       g_warning ("Failed to open connection to bus: %s",
00099                error != NULL ? error->message : "Unknown");
00100       if (error)
00101         g_error_free (error);
00102       return;
00103     }
00104 
00105   priv->proxy = dbus_g_proxy_new_for_name (priv->connection,
00106                                            "org.ayatana.bamf",
00107                                            "/org/ayatana/bamf/control",
00108                                            "org.ayatana.bamf.control");
00109   if (priv->proxy == NULL)
00110     {
00111       g_error ("Unable to get org.bamf.Control control");
00112     }
00113 }
00114 
00115 BamfControl *
00116 bamf_control_get_default (void)
00117 {
00118   if (BAMF_IS_CONTROL (default_control))
00119     return g_object_ref (default_control);
00120 
00121   return (default_control = g_object_new (BAMF_TYPE_CONTROL, NULL));
00122 }
00123 
00124 void
00125 bamf_control_set_approver_behavior (BamfControl *control,
00126                                     gint32       behavior)
00127 {
00128   BamfControlPrivate *priv;
00129   GError *error = NULL;
00130 
00131   g_return_if_fail (BAMF_IS_CONTROL (control));
00132   priv = control->priv;
00133 
00134   if (!dbus_g_proxy_call (priv->proxy,
00135                           "SetApproverBehavior",
00136                           &error,
00137                           G_TYPE_INT, behavior,
00138                           G_TYPE_INVALID,
00139                           G_TYPE_INVALID))
00140     {
00141       g_warning ("Failed to register application: %s", error->message);
00142       g_error_free (error);
00143     }
00144 }
00145 
00146 void
00147 bamf_control_insert_desktop_file (BamfControl *control,
00148                                    const gchar *desktop_file)
00149 {
00150   BamfControlPrivate *priv;
00151   GError *error = NULL;
00152 
00153   g_return_if_fail (BAMF_IS_CONTROL (control));
00154   priv = control->priv;
00155 
00156   if (!dbus_g_proxy_call (priv->proxy,
00157                           "OmNomNomDesktopFile",
00158                           &error,
00159                           G_TYPE_STRING, desktop_file,
00160                           G_TYPE_INVALID,
00161                           G_TYPE_INVALID))
00162     {
00163       g_warning ("Failed to insert desktop file: %s", error->message);
00164       g_error_free (error);
00165     }
00166 }
00167 
00168 void
00169 bamf_control_register_application_for_pid (BamfControl  *control,
00170                                            const gchar  *application,
00171                                            gint32        pid)
00172 {
00173   BamfControlPrivate *priv;
00174   GError *error = NULL;
00175 
00176   g_return_if_fail (BAMF_IS_CONTROL (control));
00177   priv = control->priv;
00178 
00179   if (!dbus_g_proxy_call (priv->proxy,
00180                           "RegisterApplicationForPid",
00181                           &error,
00182                           G_TYPE_STRING, application,
00183                           G_TYPE_UINT, pid,
00184                           G_TYPE_INVALID,
00185                           G_TYPE_INVALID))
00186     {
00187       g_warning ("Failed to register application: %s", error->message);
00188       g_error_free (error);
00189     }
00190 }
00191 
00192 void
00193 bamf_control_register_tab_provider (BamfControl *control,
00194                                     const char  *path)
00195 {
00196 }