Back to index

bamf  0.2.120
bamf-tab.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 #include "bamf-tab.h"
00022 #include "bamf-tab-source.h"
00023 
00024 #define BAMF_TAB_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE(obj, \
00025 BAMF_TYPE_TAB, BamfTabPrivate))
00026 
00027 static void bamf_tab_dbus_iface_init (BamfDBusItemTabIface *iface);
00028 G_DEFINE_TYPE_WITH_CODE (BamfTab, bamf_tab, BAMF_TYPE_VIEW,
00029                          G_IMPLEMENT_INTERFACE (BAMF_DBUS_ITEM_TYPE_TAB,
00030                                                 bamf_tab_dbus_iface_init));
00031 
00032 enum
00033 {
00034   PROP_0,
00035 
00036   PROP_ID,
00037   PROP_SOURCE,
00038 };
00039 
00040 struct _BamfTabPrivate
00041 {
00042   BamfDBusItemTab *dbus_iface;
00043   char *id;
00044   char *uri;
00045   BamfTabSource *source;
00046 };
00047 
00048 char *
00049 bamf_tab_current_uri (BamfTab *self)
00050 {
00051   g_return_val_if_fail (BAMF_IS_TAB (self), NULL);
00052 
00053   return g_strdup (self->priv->uri);
00054 }
00055 
00056 void
00057 bamf_tab_show (BamfTab *tab)
00058 {
00059   g_return_if_fail (BAMF_IS_TAB (tab));
00060 
00061   bamf_tab_source_show_tab (tab->priv->source, tab->priv->id);
00062 }
00063 
00064 guint32 bamf_tab_parent_xid (BamfTab *tab)
00065 {
00066   g_return_val_if_fail (BAMF_IS_TAB (tab), 0);
00067 
00068   return bamf_tab_source_get_tab_xid (tab->priv->source, tab->priv->id);
00069 }
00070 
00071 gchar *
00072 bamf_tab_get_preview (BamfTab *tab)
00073 {
00074   return bamf_tab_source_get_tab_preview (tab->priv->source, tab->priv->id);
00075 }
00076 
00077 static const char *
00078 bamf_tab_get_view_type (BamfView *view)
00079 {
00080   return "tab";
00081 }
00082 
00083 static void
00084 on_tab_source_uri_changed (BamfTabSource *source, char *id,
00085                            char *old_uri, char *new_uri, BamfTab *self)
00086 {
00087   if (g_strcmp0 (id, self->priv->id) != 0)
00088     return;
00089 
00090   g_free (self->priv->uri);
00091   self->priv->uri = g_strdup (new_uri);
00092 
00093   g_signal_emit_by_name (self, "uri-changed", old_uri, new_uri);
00094 }
00095 
00096 static void
00097 on_uri_changed (BamfTab *self, const gchar *old_uri, const gchar *new_uri, gpointer _not_used)
00098 {
00099   g_return_if_fail (BAMF_IS_TAB (self));
00100   g_signal_emit_by_name (self->priv->dbus_iface, "uri-changed", old_uri, new_uri);
00101 }
00102 
00103 static gboolean
00104 on_dbus_handle_show_tab (BamfDBusItemTab *interface,
00105                          GDBusMethodInvocation *invocation,
00106                          BamfTab *self)
00107 {
00108   bamf_tab_show (self);
00109   g_dbus_method_invocation_return_value (invocation, NULL);
00110 
00111   return TRUE;
00112 }
00113 
00114 static gboolean
00115 on_dbus_handle_parent_xid (BamfDBusItemTab *interface,
00116                            GDBusMethodInvocation *invocation,
00117                            BamfTab *self)
00118 {
00119   guint32 parent_xid = bamf_tab_parent_xid (self);
00120   g_dbus_method_invocation_return_value (invocation,
00121                                          g_variant_new ("(u)", parent_xid));
00122 
00123   return TRUE;
00124 }
00125 
00126 static gboolean
00127 on_dbus_handle_current_uri (BamfDBusItemTab *interface,
00128                             GDBusMethodInvocation *invocation,
00129                             BamfTab *self)
00130 {
00131   char *current_uri = self->priv->uri ? self->priv->uri : "";
00132   g_dbus_method_invocation_return_value (invocation,
00133                                          g_variant_new ("(s)", current_uri));
00134 
00135   return TRUE;
00136 }
00137 
00138 static gboolean
00139 on_dbus_handle_preview (BamfDBusItemTab *interface,
00140                         GDBusMethodInvocation *invocation,
00141                         BamfTab *self)
00142 {
00143   gchar *preview = bamf_tab_get_preview (self);
00144 
00145   if (preview)
00146     {
00147       bamf_dbus_item_tab_complete_preview (interface, invocation, preview);
00148       g_free (preview);
00149     }
00150   else
00151     {
00152       g_dbus_method_invocation_return_value (invocation,
00153                                              g_variant_new ("(ay)", NULL));
00154     }
00155 
00156   return TRUE;
00157 }
00158 
00159 static void
00160 bamf_tab_set_property (GObject *object, guint property_id, const GValue *value, GParamSpec *pspec)
00161 {
00162   BamfTab *self;
00163 
00164   self = BAMF_TAB (object);
00165 
00166   switch (property_id)
00167     {
00168       case PROP_ID:
00169         self->priv->id = g_value_dup_string (value);
00170         break;
00171 
00172       case PROP_SOURCE:
00173         self->priv->source = g_value_get_object (value);
00174         break;
00175 
00176       default:
00177         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
00178     }
00179 }
00180 
00181 static void
00182 bamf_tab_get_property (GObject *object, guint property_id, GValue *value, GParamSpec *pspec)
00183 {
00184   BamfTab *self;
00185 
00186   self = BAMF_TAB (object);
00187 
00188   switch (property_id)
00189     {
00190       case PROP_ID:
00191         g_value_set_string (value, self->priv->id);
00192         break;
00193 
00194       case PROP_SOURCE:
00195         g_value_set_object (value, self->priv->source);
00196         break;
00197 
00198       default:
00199         G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
00200     }
00201 }
00202 
00203 static void
00204 bamf_tab_constructed (GObject *object)
00205 {
00206   BamfTab *self;
00207 
00208   if (G_OBJECT_CLASS (bamf_tab_parent_class)->constructed)
00209     G_OBJECT_CLASS (bamf_tab_parent_class)->constructed (object);
00210 
00211   self = BAMF_TAB (object);
00212 
00213   g_signal_connect (self->priv->source, "remote-tab-uri-changed",
00214                     G_CALLBACK (on_tab_source_uri_changed), self);
00215 }
00216 
00217 static void
00218 bamf_tab_dispose (GObject *object)
00219 {
00220   BamfTab *self = BAMF_TAB (object);
00221 
00222   if (self->priv->id)
00223     {
00224       g_free (self->priv->id);
00225       self->priv->id = NULL;
00226     }
00227 
00228   if (self->priv->uri)
00229     {
00230       g_free (self->priv->uri);
00231       self->priv->uri = NULL;
00232     }
00233 
00234   G_OBJECT_CLASS (bamf_tab_parent_class)->dispose (object);
00235 }
00236 
00237 static void
00238 bamf_tab_finalize (GObject *object)
00239 {
00240   BamfTab *self = BAMF_TAB (object);
00241 
00242   g_object_unref (self->priv->dbus_iface);
00243 
00244   G_OBJECT_CLASS (bamf_tab_parent_class)->finalize (object);
00245 }
00246 
00247 static void
00248 bamf_tab_init (BamfTab * self)
00249 {
00250   self->priv = BAMF_TAB_GET_PRIVATE (self);
00251 
00252   /* Initializing the dbus interface */
00253   self->priv->dbus_iface = bamf_dbus_item_tab_skeleton_new ();
00254 
00255   /* We need to connect to the object own signals to redirect them to the dbus
00256    * interface                                                                */
00257   g_signal_connect (self, "uri-changed", G_CALLBACK (on_uri_changed), NULL);
00258 
00259   /* Registering signal callbacks to reply to dbus method calls */
00260   g_signal_connect (self->priv->dbus_iface, "handle-show-tab",
00261                     G_CALLBACK (on_dbus_handle_show_tab), self);
00262 
00263   g_signal_connect (self->priv->dbus_iface, "handle-parent-xid",
00264                     G_CALLBACK (on_dbus_handle_parent_xid), self);
00265 
00266   g_signal_connect (self->priv->dbus_iface, "handle-current-uri",
00267                     G_CALLBACK (on_dbus_handle_current_uri), self);
00268 
00269   g_signal_connect (self->priv->dbus_iface, "handle-preview",
00270                     G_CALLBACK (on_dbus_handle_preview), self);
00271 
00272   /* Setting the interface for the dbus object */
00273   bamf_dbus_item_object_skeleton_set_tab (BAMF_DBUS_ITEM_OBJECT_SKELETON (self),
00274                                           self->priv->dbus_iface);
00275 }
00276 
00277 static void bamf_tab_dbus_iface_init (BamfDBusItemTabIface *iface)
00278 {
00279 }
00280 
00281 static void
00282 bamf_tab_class_init (BamfTabClass * klass)
00283 {
00284   GParamSpec *pspec;
00285   GObjectClass *object_class = G_OBJECT_CLASS (klass);
00286   BamfViewClass *view_class = BAMF_VIEW_CLASS (klass);
00287 
00288   object_class->constructed  = bamf_tab_constructed;
00289   object_class->get_property = bamf_tab_get_property;
00290   object_class->set_property = bamf_tab_set_property;
00291   object_class->dispose      = bamf_tab_dispose;
00292   object_class->finalize     = bamf_tab_finalize;
00293   view_class->view_type      = bamf_tab_get_view_type;
00294 
00295   pspec = g_param_spec_string ("id", "id", "id", NULL, G_PARAM_READWRITE | G_PARAM_CONSTRUCT);
00296   g_object_class_install_property (object_class, PROP_ID, pspec);
00297 
00298   pspec = g_param_spec_object ("source", "source", "source", BAMF_TYPE_TAB_SOURCE, G_PARAM_READWRITE | G_PARAM_CONSTRUCT);
00299   g_object_class_install_property (object_class, PROP_SOURCE, pspec);
00300 
00301   g_type_class_add_private (klass, sizeof (BamfTabPrivate));
00302 }
00303 
00304 BamfTab *
00305 bamf_tab_new (BamfTabSource *source, const char *id)
00306 {
00307   BamfTab *self;
00308   self = (BamfTab *) g_object_new (BAMF_TYPE_TAB,
00309                                    "source", source,
00310                                    "id", id,
00311                                    NULL);
00312 
00313   return self;
00314 }