src/give_statusbar_plugin.c

Go to the documentation of this file.
00001 /* Give - Statusbar plugin for easy drag & drop data sending via Bluetooth.
00002  * Copyright (C) 2008 Dénes Mátételki
00003  *
00004  * This library is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU Lesser General Public
00006  * License as published by the Free Software Foundation; either
00007  * version 2 of the License, or (at your option) any later version.
00008  *
00009  * This library is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * Lesser General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU Lesser General Public
00015  * License along with this library; if not, write to the Free
00016  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00017  */
00018 
00026 #include "config.h"
00027 
00028 #include "give_statusbar_plugin.h"
00029 #include "give_drag_and_drop.h"
00030 
00031 // localization
00032 #include <locale.h>
00033 #include <libintl.h>
00034 #define _(String) gettext(String)
00035 
00036 #include <dbus/dbus-glib.h>
00037 #include <gconf/gconf-client.h>
00038 #include <hildon/hildon-banner.h>
00039 
00040 typedef struct _GiveStatusbarPluginPrivate GiveStatusbarPluginPrivate;
00041 
00042 #define GIVE_STATUSBAR_PLUGIN_GET_PRIVATE(obj) \
00043                         (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
00044                         GIVE_TYPE_STATUSBAR_PLUGIN, \
00045                         GiveStatusbarPluginPrivate));
00046 
00047 
00048 #define BT_DIR "/system/osso/connectivity/BT"
00049 
00050 #define BT_KEY "/system/osso/connectivity/BT/enabled"
00051 
00053 #define LOG_FILE "/tmp/give_log.txt"
00054 
00056 struct _GiveStatusbarPluginPrivate
00057 {
00058         GtkWidget *button;
00061         GtkWidget *image;
00064         give_drag_and_drop *dnd;
00066         GConfClient *gc_client;
00068         guint notify_uid;
00070         guint state;
00072         gint pand_pid;
00074 };
00075 
00076 HD_DEFINE_PLUGIN (GiveStatusbarPlugin,
00077         give_statusbar_plugin, STATUSBAR_TYPE_ITEM);
00078 
00079 
00081 enum bt_state {
00082         BT_IS_OFF,
00083         BT_IS_ON,
00084         BT_PAND_IS_ACTIVE
00085 };
00086 
00088 enum menu_items {
00089         NOTE_ITEM,
00090         FILE_ITEM,
00091         APPOINTMENT_ITEM,
00092         CONTACT_ITEM,
00093         EMAIL_ITEM,
00094         // NETACCESS_ITEM  it has it's own callback
00095         ABOUT_ITEM
00096 };
00097 
00098 // Private funcions
00103 static void give_statusbar_plugin_class_init (GiveStatusbarPluginClass *klass);
00104 static void give_statusbar_plugin_init (GiveStatusbarPlugin *self);
00105 static void give_statusbar_plugin_finalize (GObject *self);
00106 
00107 static void give_statusbar_plugin_popup_place (GtkMenu *menu, gint *x, gint *y,
00108                         gboolean *push_in, gpointer user_data);
00109 static void give_statusbar_plugin_update_status (GiveStatusbarPlugin *self);
00110 static void give_statusbar_plugin_clicked (GtkButton *button, 
00111                         GiveStatusbarPlugin *self);
00112 static void give_statusbar_plugin_item_clicked (GtkWidget *menu_item, 
00113                         guint item);
00114 static void give_statusbar_plugin_about_dialog(void);
00115 static void give_statusbar_plugin_log_handler (const gchar *log_domain, 
00116                         GLogLevelFlags log_level, const gchar *message, 
00117                         gpointer user_data);
00118 static void give_statusbar_plugin_bt_changed (GConfClient *gc_client, 
00119                         guint cnxn_id, GConfEntry *entry, gpointer data);
00120 static void give_statusbar_plugin_pand_dialog(GtkMenuItem *menuitem,
00121                         GiveStatusbarPlugin *self);
00122 static void give_statusbar_plugin_start_pand(GtkMenuItem *menuitem,
00123                         GiveStatusbarPlugin *self);
00124 static void give_statusbar_plugin_stop_pand(GiveStatusbarPlugin *self);
00125 
00126 
00137 static void
00138 give_statusbar_plugin_class_init (GiveStatusbarPluginClass *klass)
00139 {
00140         g_message(__PRETTY_FUNCTION__);
00141 
00142         GObjectClass *object_class;
00143         object_class = G_OBJECT_CLASS (klass);
00144 
00145         object_class->finalize = give_statusbar_plugin_finalize;
00146 
00147         g_type_class_add_private (klass, sizeof (GiveStatusbarPluginPrivate));
00148 }
00149 
00150 
00160 static void
00161 give_statusbar_plugin_init (GiveStatusbarPlugin *self)
00162 {
00163         setlocale(LC_ALL, "");
00164         bindtextdomain(GETTEXT_PACKAGE, LOCALEDIR);
00165         bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
00166         textdomain(GETTEXT_PACKAGE);
00167 
00168 
00169         g_log_set_handler ("", G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL
00170                         | G_LOG_FLAG_RECURSION,
00171                         give_statusbar_plugin_log_handler, NULL);
00172 
00173         g_message(__PRETTY_FUNCTION__);
00174 
00175         GiveStatusbarPluginPrivate *priv;
00176         GError *error = NULL;
00177 
00178         g_assert (self);
00179         g_assert (GIVE_IS_STATUSBAR_PLUGIN (self));
00180 
00181         priv = GIVE_STATUSBAR_PLUGIN_GET_PRIVATE (self);
00182         g_assert (priv);
00183 
00184         // setup the plugin button
00185         priv->button = gtk_button_new ();
00186         priv->image = gtk_image_new_from_file (ICONDIR_40 "/give-40.png");
00187         gtk_button_set_image (GTK_BUTTON (priv->button), priv->image);
00188 
00189         g_signal_connect (priv->button, "clicked",
00190                         G_CALLBACK (give_statusbar_plugin_clicked), self);
00191 
00192         gtk_container_add (GTK_CONTAINER (self), priv->button);
00193         gtk_widget_show_all(priv->button);
00194 
00195 
00196         // new drag and drop object
00197         priv->dnd = give_drag_and_drop_new(priv->button);
00198 
00199         // listen to bluetooth state
00200         priv->gc_client = gconf_client_get_default ();
00201         if (!priv->gc_client){
00202                 g_critical("%s: Error gconf client get default",
00203                                 __PRETTY_FUNCTION__);
00204                 g_assert(priv->gc_client);
00205         }
00206 
00207         gconf_client_add_dir (priv->gc_client,BT_DIR,
00208                                 GCONF_CLIENT_PRELOAD_NONE,&error);
00209         if (error){
00210                 g_warning ("%s: Error gconf client add dir: %s",
00211                         __PRETTY_FUNCTION__,error->message);
00212                 g_error_free(error);
00213         }
00214 
00215         priv->notify_uid = gconf_client_notify_add(priv->gc_client, BT_KEY,
00216                                 give_statusbar_plugin_bt_changed, 
00217                                 (gpointer)self, NULL, &error);
00218         if (error) {
00219                 g_warning("%s: Error gconf client notify add: %s",
00220                          __PRETTY_FUNCTION__,error->message);
00221                 g_error_free(error);
00222         }
00223 
00224         priv->state = BT_IS_OFF;
00225         priv->pand_pid = 0;
00226 
00227         // get bluetooth state
00228         give_statusbar_plugin_bt_changed (priv->gc_client, 0, NULL, self);
00229 }
00230 
00231 
00239 static void
00240 give_statusbar_plugin_finalize (GObject *self)
00241 {
00242         g_message(__PRETTY_FUNCTION__);
00243 
00244         GiveStatusbarPluginPrivate *priv;
00245         GError *error = NULL;
00246 
00247         g_assert (self);
00248         g_assert (GIVE_IS_STATUSBAR_PLUGIN (self));
00249 
00250         priv = GIVE_STATUSBAR_PLUGIN_GET_PRIVATE (self);
00251         g_assert (priv);
00252 
00253         // free stuff
00254 
00255         g_object_unref(priv->dnd);
00256 
00257         gconf_client_remove_dir (priv->gc_client, BT_DIR, &error);
00258         if (error){
00259                 g_warning ("%s: Error unsubscribing from gconf dir: %s",
00260                         __PRETTY_FUNCTION__,error->message);
00261                 g_error_free(error);
00262         }
00263 
00264         gconf_client_notify_remove (priv->gc_client,priv->notify_uid);
00265         g_object_unref (priv->gc_client);
00266 
00267 }
00268 
00281 static void
00282 give_statusbar_plugin_popup_place (     GtkMenu         *menu,
00283                                         gint            *x,
00284                                         gint            *y,
00285                                         gboolean        *push_in,
00286                                         gpointer        user_data)
00287 {
00288         g_message(__PRETTY_FUNCTION__);
00289 
00290         GtkRequisition req;
00291         GtkWidget *btn = GTK_WIDGET(user_data);
00292         gint sw;
00293 
00294         g_assert(menu);
00295         g_assert(GTK_IS_MENU(menu));
00296 
00297        (void)push_in;
00298 
00299         gtk_widget_size_request(GTK_WIDGET(menu), &req);
00300         sw = gdk_screen_get_width(gtk_widget_get_screen(btn));
00301         gdk_window_get_position(btn->window, x, y);
00302         
00303         *y += btn->allocation.y + btn->allocation.height;
00304         *x += btn->allocation.x;
00305         
00306         if (*x + req.width > sw) {
00307                 *x -= req.width - btn->allocation.width;
00308         }
00309 }
00310 
00315 static void
00316 give_statusbar_plugin_update_status (GiveStatusbarPlugin *self)
00317 {
00318         GiveStatusbarPluginPrivate *priv;
00319 
00320         g_assert (self);
00321         g_assert (GIVE_IS_STATUSBAR_PLUGIN (self));
00322 
00323         priv = GIVE_STATUSBAR_PLUGIN_GET_PRIVATE (self);
00324         g_assert (priv);
00325 
00326         switch ( priv->state ) {
00327                 case ( BT_IS_OFF ) :
00328                         give_drag_and_drop_have_bt(priv->dnd,FALSE);
00329         
00330                         gtk_image_set_from_file (GTK_IMAGE (priv->image),
00331                                 ICONDIR_40 "/give-no_bt-40.png");
00332 
00333                         break;
00334                 
00335                 case ( BT_IS_ON ) :
00336                         give_drag_and_drop_have_bt(priv->dnd,TRUE);
00337         
00338                         gtk_image_set_from_file (GTK_IMAGE (priv->image),
00339                                 ICONDIR_40 "/give-40.png");
00340 
00341                         break;
00342 
00343                 case ( BT_PAND_IS_ACTIVE ) :
00344                         gtk_image_set_from_file (GTK_IMAGE (priv->image),
00345                                 ICONDIR_40 "/give-pand-40.png");
00346 
00347                         break;
00348 
00349                 default:
00350                         g_assert(0);
00351         }
00352 }
00353 
00354 
00360 static void
00361 give_statusbar_plugin_clicked (GtkButton *button, GiveStatusbarPlugin *self)
00362 {
00363         g_message(__PRETTY_FUNCTION__);
00364 
00365         GiveStatusbarPluginPrivate *priv;
00366         GtkWidget *item_note, *note_icon;
00367         GtkWidget *item_file, *file_icon;
00368         GtkWidget *item_appointment, *appointment_icon;
00369         GtkWidget *item_contact, *contact_icon;
00370         GtkWidget *item_email, *email_icon;
00371         GtkWidget *item_netaccess, *netaccess_icon;
00372         GtkWidget *item_about, *about_icon;
00373 
00374         GtkWidget *item_separator0, *item_separator1;
00375         GtkWidget *main_menu;
00376 
00377         g_assert (button);
00378         g_assert (GTK_IS_BUTTON(button));
00379         g_assert (self);
00380         g_assert (GIVE_IS_STATUSBAR_PLUGIN (self));
00381 
00382         priv = GIVE_STATUSBAR_PLUGIN_GET_PRIVATE (self);
00383         g_assert (priv);
00384 
00385         // menu entries
00386         item_note = gtk_image_menu_item_new_with_mnemonic("Give _Text");
00387         note_icon = gtk_image_new_from_file (ICONDIR_26
00388                                         "/qgn_list_messagin_editor.png");
00389         gtk_image_menu_item_set_image ( GTK_IMAGE_MENU_ITEM (item_note),
00390                                         note_icon);
00391 
00392         item_file = gtk_image_menu_item_new_with_mnemonic(_("Give _File"));
00393         file_icon = gtk_image_new_from_file (
00394                                         ICONDIR_26"/qgn_list_filemanager.png");
00395         gtk_image_menu_item_set_image ( GTK_IMAGE_MENU_ITEM (item_file),
00396                                         file_icon);
00397 
00398         item_appointment = gtk_image_menu_item_new_with_mnemonic(
00399                                         _("Give _Event"));
00400         appointment_icon = gtk_image_new_from_file (
00401                                         ICONDIR_26"/qgn_widg_datedit_pr.png");
00402         gtk_image_menu_item_set_image ( GTK_IMAGE_MENU_ITEM (item_appointment),
00403                                         appointment_icon);
00404 
00405         item_contact = gtk_image_menu_item_new_with_mnemonic(
00406                                         _("Give _Contact"));
00407         contact_icon = gtk_image_new_from_file (ICONDIR_26
00408                                         "/qgn_addr_icon_user_group.png");
00409         gtk_image_menu_item_set_image ( GTK_IMAGE_MENU_ITEM (item_contact),
00410                                         contact_icon);
00411 
00412 
00413         item_email = gtk_image_menu_item_new_with_mnemonic(_("Give _Email"));
00414         email_icon = gtk_image_new_from_file (
00415                                         ICONDIR_26"/qgn_list_gene_mail.png");
00416         gtk_image_menu_item_set_image ( GTK_IMAGE_MENU_ITEM (item_email),
00417                                         email_icon);
00418 
00419         if ( priv->state == BT_PAND_IS_ACTIVE )
00420                 item_netaccess = gtk_image_menu_item_new_with_mnemonic(
00421                                         _("Stop _Internet Access"));
00422         else
00423                 item_netaccess = gtk_image_menu_item_new_with_mnemonic(
00424                                         _("Give _Internet Access"));
00425         
00426         netaccess_icon = gtk_image_new_from_file (ICONDIR_26
00427                                 "/qgn_list_connectivity_iaptype_wlan.png");
00428         gtk_image_menu_item_set_image ( GTK_IMAGE_MENU_ITEM (item_netaccess),
00429                                         netaccess_icon);
00430 
00431         item_about = gtk_image_menu_item_new_with_mnemonic(
00432                                         _("_About"));
00433         about_icon = gtk_image_new_from_file (
00434                                         ICONDIR_26"/qgn_note_infoprint.png");
00435         gtk_image_menu_item_set_image ( GTK_IMAGE_MENU_ITEM (item_about),
00436                                         about_icon);
00437 
00438         // separators
00439         item_separator0 = gtk_separator_menu_item_new();
00440         item_separator1 = gtk_separator_menu_item_new();
00441 
00442         // the menu
00443         main_menu = gtk_menu_new();
00444         gtk_menu_shell_append(GTK_MENU_SHELL(main_menu), item_note);
00445         gtk_menu_shell_append(GTK_MENU_SHELL(main_menu), item_file);
00446         gtk_menu_shell_append(GTK_MENU_SHELL(main_menu), item_appointment);
00447         gtk_menu_shell_append(GTK_MENU_SHELL(main_menu), item_contact);
00448         gtk_menu_shell_append(GTK_MENU_SHELL(main_menu), item_email);
00449         gtk_menu_shell_append(GTK_MENU_SHELL(main_menu), item_separator0);
00450         gtk_menu_shell_append(GTK_MENU_SHELL(main_menu), item_netaccess);
00451         gtk_menu_shell_append(GTK_MENU_SHELL(main_menu), item_separator1);
00452         gtk_menu_shell_append(GTK_MENU_SHELL(main_menu), item_about);
00453 
00454         g_signal_connect (item_note, "activate",
00455                                 G_CALLBACK (give_statusbar_plugin_item_clicked),
00456                                 GINT_TO_POINTER(NOTE_ITEM));
00457         g_signal_connect (item_file, "activate",
00458                                 G_CALLBACK (give_statusbar_plugin_item_clicked),
00459                                 GINT_TO_POINTER(FILE_ITEM));
00460 
00461         g_signal_connect (item_appointment, "activate",
00462                                 G_CALLBACK (give_statusbar_plugin_item_clicked),
00463                                 GINT_TO_POINTER(APPOINTMENT_ITEM));
00464         g_signal_connect (item_contact, "activate",
00465                                 G_CALLBACK (give_statusbar_plugin_item_clicked),
00466                                 GINT_TO_POINTER(CONTACT_ITEM));
00467         g_signal_connect (item_email, "activate",
00468                                 G_CALLBACK (give_statusbar_plugin_item_clicked),
00469                                 GINT_TO_POINTER(EMAIL_ITEM));
00470         g_signal_connect (item_netaccess, "activate",
00471                                 G_CALLBACK (give_statusbar_plugin_pand_dialog),
00472                                 self);
00473         g_signal_connect (item_about, "activate",
00474                                 G_CALLBACK (give_statusbar_plugin_item_clicked),
00475                                 GINT_TO_POINTER(ABOUT_ITEM));
00476 
00477         gtk_widget_show_all(GTK_WIDGET(main_menu));
00478         gtk_menu_popup(GTK_MENU(main_menu),NULL,NULL,
00479                         give_statusbar_plugin_popup_place,button,1,
00480                         gtk_get_current_event_time());
00481 
00482 }
00483 
00495 static void
00496 give_statusbar_plugin_item_clicked      (GtkWidget      *menu_item,
00497                                         guint           item)
00498 {
00499         g_message(__PRETTY_FUNCTION__);
00500 
00501         gchar *argv_notes[] =           { "/usr/bin/osso_notes", NULL };
00502         gchar *argv_filemanager[] =     { "/usr/bin/ossofilemanager", NULL };
00503         gchar *argv_dates[] =           { "/usr/bin/dates", NULL };
00504         gchar *argv_contact[] =         { "/usr/bin/osso-addressbook", NULL };
00505         gchar *argv_email[] =           { "/usr/bin/ossoemail", NULL };
00506 
00507         g_assert (menu_item);
00508         g_assert (GTK_IS_IMAGE_MENU_ITEM (menu_item));
00509 
00510         switch (item){
00511                 case (NOTE_ITEM):
00512 
00513                         if ( !g_spawn_async(    NULL, argv_notes, NULL, 0,
00514                                                 NULL, NULL, NULL, NULL)){
00515                                 hildon_banner_show_information (menu_item,NULL,
00516                                         _("Couldn't invoke notes"));
00517                         }
00518                         break;
00519 
00520                 case (FILE_ITEM):
00521                         if ( !g_spawn_async(    NULL, argv_filemanager, NULL, 0,
00522                                                 NULL, NULL, NULL, NULL)){
00523                                 hildon_banner_show_information (menu_item,NULL,
00524                                         _("Couldn't invoke filemanager"));
00525                         }
00526                         break;
00527 
00528                 case (APPOINTMENT_ITEM):
00529                         if ( !g_spawn_async(    NULL, argv_dates, NULL, 0,
00530                                                 NULL, NULL, NULL, NULL)){
00531                                 hildon_banner_show_information (menu_item,NULL,
00532                                         _("Couldn't invoke dates"));
00533                         }
00534                         break;          
00535                 case (CONTACT_ITEM):
00536                         if ( !g_spawn_async(    NULL, argv_contact, NULL, 0,
00537                                                 NULL, NULL, NULL, NULL)){
00538                                 hildon_banner_show_information (menu_item,NULL,
00539                                         _("Couldn't invoke addressbook"));
00540                         }
00541                         break;
00542 
00543                 case (EMAIL_ITEM):
00544                         if ( !g_spawn_async(    NULL, argv_email, NULL, 0,
00545                                                 NULL, NULL, NULL, NULL)){
00546                                 hildon_banner_show_information (menu_item,NULL,
00547                                         _("Couldn't invoke email"));
00548                         }
00549                         break;
00550 
00551                 case (ABOUT_ITEM):
00552 
00553                         give_statusbar_plugin_about_dialog();
00554                         break;
00555 
00556                 default:
00557                         g_assert(0);
00558                         break;
00559         }
00560 }
00561 
00563 static void
00564 give_statusbar_plugin_about_dialog(void)
00565 {
00566         g_message(__PRETTY_FUNCTION__);
00567 
00568         const gchar *comments = _("Statusbar-plugin for easy data-exchange. "
00569         "Drag 'n Drop data to the statusbar-icon, and send it via Bluetooth."
00570         "The following applications are supported yet: "
00571         "osso-notes, osso-filemanager, dates compiled with dnd, "
00572         "osso-addressbook.") /*, modest with dnd hack*/;
00573 
00574         gtk_show_about_dialog (NULL,
00575                 "name", PACKAGE, "version", VERSION,
00576                 "comments", comments,
00577 //              "copyright", "\302\251 2008 Dénes Mátételki "
00578 //                                              "<denes.matetelki@gmail.com>",
00579 //              "website", "http://give.matetelki.com",
00580                 NULL);
00581 }
00582 
00594 static void
00595 give_statusbar_plugin_log_handler       (const gchar    *log_domain,
00596                                         GLogLevelFlags  log_level,
00597                                         const gchar     *message, 
00598                                         gpointer        user_data)
00599 {
00600 #ifdef LOG_TO_CONSOLE
00601 
00602         switch ( log_level ) {
00603                 case G_LOG_LEVEL_DEBUG :
00604                         printf("\t\033[1;36m%s\033[0;39m   : %s\n",
00605                                 "GIVE - debug",message);
00606                         break;
00607                 case G_LOG_LEVEL_MESSAGE :
00608                         printf("\t\033[1;32m%s\033[0;39m : %s\n",
00609                                 "GIVE - message",message);
00610                         break;
00611                 case G_LOG_LEVEL_WARNING :
00612                         printf("\t\033[1;33m%s\033[0;39m : %s\n",
00613                                 "GIVE - warning",message);
00614                         break;
00615                 case G_LOG_LEVEL_CRITICAL :
00616                         printf("\t\033[1;31m%s\033[0;39m: %s\n",
00617                                 "GIVE - critical",message);
00618 //                      g_assert(0);
00619                         break;
00620                 default:
00621                         printf("\t\033[1;36m%s\033[0;39m   : %s\n",
00622                                 "GIVE - other",message);
00623         }
00624 #endif
00625 
00626 #ifdef LOG_TO_FILE
00627 
00628         FILE *fp = fopen(LOG_FILE,"a");
00629         g_assert (fp);
00630 
00631         GTimeVal time;
00632         g_get_current_time(&time);
00633 
00634         switch ( log_level ) {
00635                 case G_LOG_LEVEL_DEBUG :
00636                         fprintf(fp,"GIVE - debug    %s : %s\n",
00637                                 g_time_val_to_iso8601(&time),message);
00638                         break;
00639                 case G_LOG_LEVEL_MESSAGE :
00640                         fprintf(fp,"GIVE - message  %s : %s\n",
00641                                 g_time_val_to_iso8601(&time),message);
00642                         break;
00643                 case G_LOG_LEVEL_WARNING :
00644                         fprintf(fp,"GIVE - warning  %s : %s\n",
00645                                 g_time_val_to_iso8601(&time),message);
00646                         break;
00647                 case G_LOG_LEVEL_CRITICAL :
00648                         fprintf(fp,"GIVE - critical %s : %s\n",
00649                                 g_time_val_to_iso8601(&time),message);
00650                         fclose(fp);
00651 //                      g_assert(0);
00652                         break;
00653                 default:
00654 
00655                         fprintf(fp,"GIVE - other %s : %s\n",
00656                                 g_time_val_to_iso8601(&time),message);
00657                         fclose(fp);
00658         }
00659         fclose(fp);
00660 
00661 #endif
00662 
00663 }
00664 
00674 static void
00675 give_statusbar_plugin_bt_changed        (GConfClient    *gc_client,
00676                                         guint           cnxn_id,
00677                                         GConfEntry      *entry,
00678                                         gpointer        data)
00679 {
00680         g_message(__PRETTY_FUNCTION__);
00681 
00682         GiveStatusbarPlugin *self;
00683         GiveStatusbarPluginPrivate *priv;
00684         GError *error = NULL;
00685         GConfValue* key;
00686         gboolean retval;
00687 
00688         g_assert(gc_client);
00689         g_assert(data);
00690 
00691         self = (GiveStatusbarPlugin *)data;
00692         g_assert (self);
00693         g_assert (GIVE_IS_STATUSBAR_PLUGIN (self));
00694 
00695         priv = GIVE_STATUSBAR_PLUGIN_GET_PRIVATE (self);
00696         g_assert (priv);
00697 
00698         key = gconf_client_get(gc_client,BT_KEY,&error);
00699 
00700         if (error){
00701                 g_warning ("%s: Error gconf client get bool: %s",
00702                         __PRETTY_FUNCTION__,error->message);
00703                 g_error_free(error);
00704 
00705         } else {
00706                 if ( key == NULL ) {
00707                         g_warning("%s: gconf key: '%s' is unset\n",
00708                         __PRETTY_FUNCTION__, BT_KEY);
00709 
00710                         priv->state = BT_IS_OFF;
00711                         g_debug ("%s: gconf key unset, handled it as FALSE",
00712                                 __PRETTY_FUNCTION__);
00713                 } else {
00714                         retval = gconf_value_get_bool (key);
00715 
00716                         if ( retval == TRUE ){
00717                                 priv->state = BT_IS_ON;
00718                                 g_debug ("%s: BT state is ON",
00719                                         __PRETTY_FUNCTION__);
00720                         } else {
00721                                 priv->state = BT_IS_OFF;
00722                                 g_debug ("%s: BT state is OFF",
00723                                         __PRETTY_FUNCTION__);
00724 
00725                                 if ( priv->pand_pid != 0 )
00726                                         give_statusbar_plugin_stop_pand(self);
00727 
00728                         }
00729                 }
00730         }
00731 
00732         give_statusbar_plugin_update_status (self);
00733 }
00734 
00735 
00745 static void 
00746 give_statusbar_plugin_pand_dialog       (GtkMenuItem            *menuitem,
00747                                         GiveStatusbarPlugin     *self)
00748 {
00749         g_message(__PRETTY_FUNCTION__);
00750 
00751         GtkWidget *dialog;
00752         GtkWidget *label = NULL;
00753         GtkWidget *vbox;
00754         guint result;
00755         GiveStatusbarPluginPrivate *priv;
00756 
00757         g_assert (menuitem);
00758         g_assert (GTK_IS_IMAGE_MENU_ITEM (menuitem));
00759 
00760         g_assert (self);
00761         g_assert (GIVE_IS_STATUSBAR_PLUGIN (self));
00762 
00763         priv = GIVE_STATUSBAR_PLUGIN_GET_PRIVATE (self);
00764         g_assert (priv);
00765 
00766         // if BT is off, then invoke 'turn-on BT' dialog
00767         if ( priv->state ==  BT_IS_OFF )
00768                 if ( !give_drag_and_drop_turn_bt_on_dialog(priv->dnd) )
00769                         return;
00770 
00771         dialog = gtk_dialog_new_with_buttons (
00772                                         "Give Internet Access",
00773                                         NULL,
00774                                         GTK_DIALOG_MODAL,
00775                                         GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
00776                                         GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
00777                                         NULL);
00778 
00779         if ( priv->state == BT_PAND_IS_ACTIVE )
00780                 label = gtk_label_new (_("Stop internet sharing?"));
00781         else
00782                 label = gtk_label_new(
00783                                 _("Start internet sharing\nvia Bluetooth?"));
00784 
00785         vbox = gtk_vbox_new(FALSE, 5);
00786         gtk_container_add (GTK_CONTAINER (vbox), label);
00787         gtk_container_add (GTK_CONTAINER (GTK_DIALOG(dialog)->vbox), vbox);
00788         gtk_widget_show_all (dialog);
00789 
00790         result = gtk_dialog_run ( GTK_DIALOG ( dialog));
00791         switch (result){
00792                 case GTK_RESPONSE_ACCEPT:
00793                         if ( priv->state == BT_PAND_IS_ACTIVE ) {
00794                                 give_statusbar_plugin_stop_pand(self);
00795                         } else {
00796                                 give_statusbar_plugin_start_pand(menuitem,self);
00797                         }
00798                         break;
00799                 case GTK_RESPONSE_REJECT:
00800                         // do nothing
00801                         break;
00802                 default:
00803                         g_assert(0);
00804         }
00805 
00806         gtk_widget_destroy (dialog);
00807 }
00808 
00817 static void
00818 give_statusbar_plugin_start_pand        (GtkMenuItem            *menuitem,
00819                                         GiveStatusbarPlugin     *self)
00820 {
00821         g_message(__PRETTY_FUNCTION__);
00822 
00823         GiveStatusbarPluginPrivate *priv;
00824 
00825         g_assert (self);
00826         g_assert (GIVE_IS_STATUSBAR_PLUGIN (self));
00827 
00828         priv = GIVE_STATUSBAR_PLUGIN_GET_PRIVATE (self);
00829         g_assert (priv);
00830 
00831         gchar *argv_start_pand[] = { "/usr/bin/pand", "-s", "-r", "NAP", NULL };
00832 
00833         if ( !g_spawn_async(    NULL, argv_start_pand, NULL, 0,
00834                                 NULL, NULL, &(priv->pand_pid), NULL)){
00835 
00836                 hildon_banner_show_information (GTK_WIDGET(menuitem), NULL,
00837                                         _("Couldn't start Bluetooth Net"));
00838 
00839                 g_debug ("%s - %s",__PRETTY_FUNCTION__, 
00840                                 "Couldn't start Bluetooth Net");
00841 
00842         } else {
00843 
00844                 priv->state = BT_PAND_IS_ACTIVE;
00845 
00846                 hildon_banner_show_information (GTK_WIDGET(menuitem), NULL,
00847                                                 _("Bluetooth Net started"));
00848 
00849                 g_debug ("%s - %s",__PRETTY_FUNCTION__, 
00850                                 "Bluetooth Net started");
00851 
00852                 give_statusbar_plugin_update_status(self);
00853         }
00854 
00855 }
00856 
00862 static void
00863 give_statusbar_plugin_stop_pand         (GiveStatusbarPlugin *self)
00864 {
00865         g_message(__PRETTY_FUNCTION__);
00866 
00867         GiveStatusbarPluginPrivate *priv;
00868         gchar pid_buffer [10];
00869 
00870         g_assert (self);
00871         g_assert (GIVE_IS_STATUSBAR_PLUGIN (self));
00872 
00873         priv = GIVE_STATUSBAR_PLUGIN_GET_PRIVATE (self);
00874         g_assert (priv);
00875 
00876         if ( priv->pand_pid == 0 ) return;
00877 
00878         sprintf(pid_buffer,"%d", priv->pand_pid);
00879         gchar *argv_stop_pand[] = { "kill", pid_buffer, NULL };
00880 
00881         if ( !g_spawn_async(    NULL, argv_stop_pand, NULL, 0,
00882                                 NULL, NULL, NULL, NULL)){
00883 
00884                 hildon_banner_show_information (NULL,NULL,
00885                                         _("Couldn't stop Bluetooth Net"));
00886                 g_debug ("%s - %s",__PRETTY_FUNCTION__, 
00887                                 "Couldn't stop Bluetooth Net");
00888         } else {
00889 
00890                 hildon_banner_show_information (NULL,NULL,
00891                                         "Bluetooth Net stopped");
00892 
00893                 g_debug ("%s - %s",__PRETTY_FUNCTION__, 
00894                                 _("Bluetooth Net stopped"));
00895 
00896                 priv->pand_pid = 0;
00897                 priv->state = BT_IS_ON;
00898 
00899                 give_statusbar_plugin_update_status(self);
00900         }
00901 }

Generated on Sat May 17 16:27:32 2008 for Give by  doxygen 1.5.3