* get the device name, surprisingly hard...
authorDirk-Jan C. Binnema <dirk-jan.binnema@nokia.com>
Tue, 27 Feb 2007 16:19:41 +0000 (16:19 +0000)
committerDirk-Jan C. Binnema <dirk-jan.binnema@nokia.com>
Tue, 27 Feb 2007 16:19:41 +0000 (16:19 +0000)
pmo-trunk-r858

src/maemo/Makefile.am
src/maemo/modest-maemo-utils.c [new file with mode: 0644]
src/maemo/modest-maemo-utils.h [new file with mode: 0644]
src/maemo/modest-main-window.c

index 7345ae0..8f63a59 100644 (file)
@@ -53,8 +53,9 @@ libmodest_ui_la_SOURCES=              \
         modest-msg-view-window.c      \
        modest-msg-edit-window.c      \
        modest-account-assistant.c    \
-       modest-account-assistant.h
-
+       modest-account-assistant.h    \
+       modest-maemo-utils.c          \
+       modest-maemo-utils.h
 
 LDADD = \
        $(MODEST_GSTUFF_LIBS) \
diff --git a/src/maemo/modest-maemo-utils.c b/src/maemo/modest-maemo-utils.c
new file mode 100644 (file)
index 0000000..4a8af73
--- /dev/null
@@ -0,0 +1,172 @@
+/* Copyright (c) 2006, Nokia Corporation
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * * Neither the name of the Nokia Corporation nor the names of its
+ *   contributors may be used to endorse or promote products derived from
+ *   this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
+ * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
+ * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef DBUS_API_SUBJECT_TO_CHANGE
+#define DBUS_API_SUBJECT_TO_CHANGE
+#endif /*DBUS_API_SUBJECT_TO_CHANGE*/
+
+#include <dbus/dbus.h>
+#include <dbus/dbus-glib-lowlevel.h>
+#include <glib.h>
+#include <modest-runtime.h>
+
+#include "modest-maemo-utils.h"
+
+/*
+ * For getting and tracking the Bluetooth name
+ */
+#define BTNAME_SERVICE                  "org.bluez"
+#define BTNAME_REQUEST_IF               "org.bluez.Adapter"
+#define BTNAME_SIGNAL_IF                "org.bluez.Adapter"
+#define BTNAME_REQUEST_PATH             "/org/bluez/hci0"
+#define BTNAME_SIGNAL_PATH              "/org/bluez/hci0"
+
+#define BTNAME_REQ_GET                  "GetName"
+#define BTNAME_SIG_CHANGED              "NameChanged"
+
+#define BTNAME_MATCH_RULE "type='signal',interface='" BTNAME_SIGNAL_IF \
+                          "',member='" BTNAME_SIG_CHANGED "'"
+
+
+static void
+update_device_name_from_msg (DBusMessage *message)
+{
+       DBusError error;
+       DBusMessageIter iter;
+
+       dbus_error_init (&error);
+
+       if (dbus_set_error_from_message (&error, message)) {
+               g_printerr ("modest: failed to get bt name: %s\n", error.message);
+               dbus_error_free (&error);
+               modest_conf_set_string (modest_runtime_get_conf(),
+                                       MODEST_CONF_DEVICE_NAME,
+                                       MODEST_LOCAL_FOLDERS_DEFAULT_DISPLAY_NAME,
+                                       NULL);
+                                       
+       } else {
+               const gchar *device_name;
+               if (!dbus_message_iter_init (message, &iter)) {
+                       g_printerr ("modest: message did not have argument\n");
+                       return;
+               }
+
+               dbus_message_iter_get_basic (&iter, &device_name);
+               g_warning ("update device name: %s", device_name);
+               modest_conf_set_string (modest_runtime_get_conf(),
+                                       MODEST_CONF_DEVICE_NAME, device_name,
+                                       NULL);
+       }
+}
+
+
+static void
+on_device_name_received (DBusPendingCall *call, void *user_data)
+{
+       DBusMessage *message;
+       
+       g_return_if_fail (dbus_pending_call_get_completed (call));
+       
+       message = dbus_pending_call_steal_reply (call);
+       if (!message) {
+               g_printerr ("modest: no reply on device name query\n");
+               return;
+       }
+
+       update_device_name_from_msg (message);
+       dbus_message_unref (message);
+}
+
+
+static DBusHandlerResult
+handle_dbus_signal (DBusConnection *conn, DBusMessage *msg, gpointer data)
+{
+       if (dbus_message_is_signal(msg, BTNAME_SIGNAL_IF, BTNAME_SIG_CHANGED))
+               update_device_name_from_msg (msg);
+
+       return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+}
+
+
+static void
+get_device_name_from_dbus ()
+{
+       static DBusConnection *conn = NULL;
+       DBusMessage *request;
+       DBusError error;
+       DBusPendingCall *call = NULL;
+
+       g_warning ("get device name from dbus");
+       
+       dbus_error_init (&error);
+       if (!conn) {
+               conn = dbus_bus_get (DBUS_BUS_SYSTEM, &error);
+               if (!conn) {
+                       g_printerr ("modest: cannot get on the dbus: %s: %s\n",
+                                   error.name, error.message);
+                       dbus_error_free (&error);
+                       return;
+               }
+       }
+       
+       request = dbus_message_new_method_call (BTNAME_SERVICE, BTNAME_REQUEST_PATH,
+                                               BTNAME_REQUEST_IF, BTNAME_REQ_GET);
+       if (!request) {
+               /* should we free the connection? */
+               g_printerr ("modest: dbus_message_new_method_call failed\n");
+               return;
+       }
+       dbus_message_set_auto_start (request, TRUE);
+       if (dbus_connection_send_with_reply (conn, request, &call, -1)) {
+               dbus_pending_call_set_notify (call, on_device_name_received,
+                                             NULL, NULL);
+               dbus_pending_call_unref (call);
+       }
+       dbus_message_unref (request);
+       
+       dbus_connection_setup_with_g_main (conn, NULL);
+       dbus_bus_add_match (conn, BTNAME_MATCH_RULE, &error);
+       if (dbus_error_is_set(&error)) {
+               g_printerr ("modest: dbus_bus_add_match failed: %s\n", error.message);
+               dbus_error_free (&error);
+       }
+
+       if (!dbus_connection_add_filter(conn, handle_dbus_signal, NULL, NULL))
+               g_printerr ("modest: dbus_connection_add_filter failed\n");
+}
+
+
+void
+modest_maemo_utils_get_device_name (void)
+{
+       get_device_name_from_dbus ();
+}
+
+
+
diff --git a/src/maemo/modest-maemo-utils.h b/src/maemo/modest-maemo-utils.h
new file mode 100644 (file)
index 0000000..99fb67b
--- /dev/null
@@ -0,0 +1,46 @@
+/* Copyright (c) 2006, Nokia Corporation
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * * Neither the name of the Nokia Corporation nor the names of its
+ *   contributors may be used to endorse or promote products derived from
+ *   this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
+ * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
+ * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+
+#ifndef __MODEST_MAEMO_UTILS_H__
+#define __MODEST_MAEMO_UTILS_H__
+
+/**
+ * modest_maemo_utils_get_device_name
+ *
+ * get the name for this device. Note: this queries the bluetooth
+ * name over DBUS, and may block. The result will be available in
+ * MODEST_DEVICE_NAME in ModestConf; it will be updated when it
+ * changes
+ * 
+ */
+void modest_maemo_utils_get_device_name (void);
+
+
+#endif /*__MODEST_MAEMO_UTILS_H__*/
index 178d839..18ccad5 100644 (file)
@@ -33,6 +33,7 @@
 #include <gtk/gtktreeviewcolumn.h>
 #include <tny-account-store-view.h>
 #include <modest-runtime.h>
+#include <string.h>
 
 #include <widgets/modest-main-window.h>
 #include <widgets/modest-msg-edit-window.h>
@@ -44,6 +45,7 @@
 #include "modest-account-mgr.h"
 #include "modest-conf.h"
 
+#include "modest-maemo-utils.h"
 #include "modest-tny-platform-factory.h"
 #include "modest-tny-msg.h"
 #include "modest-mail-operation.h"
@@ -132,6 +134,37 @@ modest_main_window_class_init (ModestMainWindowClass *klass)
        g_type_class_add_private (gobject_class, sizeof(ModestMainWindowPrivate));
 }
 
+
+static void
+on_key_changed (ModestConf* conf, const gchar *key, ModestConfEvent event, ModestMainWindow *win)
+{
+       TnyAccount *account;
+       
+       if (!key || strcmp (key, MODEST_CONF_DEVICE_NAME) != 0)
+               return; /* wrong key */
+       
+       /* ok, the device name changed; thus, we have to update the
+        * local folder account name*/
+       account =
+               modest_tny_account_store_get_tny_account_by_account (modest_runtime_get_account_store(),
+                                                                    MODEST_LOCAL_FOLDERS_ACCOUNT_ID,
+                                                                    TNY_ACCOUNT_TYPE_STORE);
+       if (!account) {
+               g_printerr ("modest: could not get account\n");
+               return;
+       }
+
+       if (event == MODEST_CONF_EVENT_KEY_UNSET) 
+               tny_account_set_name (account, MODEST_LOCAL_FOLDERS_DEFAULT_DISPLAY_NAME);
+       else {
+               gchar *device_name = modest_conf_get_string (modest_runtime_get_conf(),
+                                                            MODEST_CONF_DEVICE_NAME, NULL);
+               tny_account_set_name (account, device_name);
+               g_free (device_name);
+       }
+       g_object_unref (G_OBJECT(account));     
+}
+
 static void
 modest_main_window_init (ModestMainWindow *obj)
 {
@@ -157,7 +190,6 @@ modest_main_window_finalize (GObject *obj)
        G_OBJECT_CLASS(parent_class)->finalize (obj);
 }
 
-
 GtkWidget*
 modest_main_window_get_child_widget (ModestMainWindow *self,
                                     ModestWidgetType widget_type)
@@ -341,6 +373,19 @@ connect_signals (ModestMainWindow *self)
        /* window */
        g_signal_connect (G_OBJECT(self), "destroy", G_CALLBACK(on_destroy), NULL);
        g_signal_connect (G_OBJECT(self), "delete-event", G_CALLBACK(on_delete_event), self);
+
+       
+       /* modest_maemo_utils_get_device_name will probably change
+        * MODEST_CONF_DEVICE_NAME. If that happens, we update the local folders
+        * account name in the callback
+        */
+       g_signal_connect (G_OBJECT(modest_runtime_get_conf()), "key_changed",
+                         G_CALLBACK(on_key_changed), self);
+
+       
+       g_signal_connect (G_OBJECT(self), "delete-event", G_CALLBACK(on_delete_event), self);
+
+
 }
 
 
@@ -351,6 +396,7 @@ sync_accounts_cb (ModestMainWindow *win)
        return FALSE;
 }
 
+
        
 ModestWindow*
 modest_main_window_new (void)
@@ -381,7 +427,8 @@ modest_main_window_new (void)
        g_object_unref (action_group);
 
        /* Load the UI definition */
-       gtk_ui_manager_add_ui_from_file (parent_priv->ui_manager, MODEST_UIDIR "modest-main-window-ui.xml", &error);
+       gtk_ui_manager_add_ui_from_file (parent_priv->ui_manager,
+                                        MODEST_UIDIR "modest-main-window-ui.xml", &error);
        if (error != NULL) {
                g_warning ("Could not merge modest-ui.xml: %s", error->message);
                g_error_free (error);
@@ -407,8 +454,9 @@ modest_main_window_new (void)
        priv->folder_view = MODEST_FOLDER_VIEW(modest_folder_view_new (query));
        if (!priv->folder_view)
                g_printerr ("modest: cannot instantiate folder view\n");        
-       g_object_unref (G_OBJECT (query));
-       
+       g_object_unref (G_OBJECT (query));      
+       modest_maemo_utils_get_device_name ();
+
        /* header view */
        priv->header_view  =
                MODEST_HEADER_VIEW(modest_header_view_new (NULL, MODEST_HEADER_VIEW_STYLE_DETAILS));
@@ -436,9 +484,6 @@ modest_main_window_new (void)
        gtk_window_set_icon_from_file (GTK_WINDOW(self), MODEST_APP_ICON, NULL);
        gtk_widget_show_all (main_vbox);
 
-       g_signal_connect (G_OBJECT(self), "delete-event",
-                         G_CALLBACK(on_delete_event), self);
-
        /* should we hide the toolbar? */
        if (!modest_conf_get_bool (modest_runtime_get_conf (), MODEST_CONF_SHOW_TOOLBAR, NULL))
                gtk_widget_hide (parent_priv->toolbar);
@@ -451,6 +496,12 @@ modest_main_window_new (void)
                                                  TNY_ACCOUNT_STORE (modest_runtime_get_account_store ()));
        g_idle_add ((GSourceFunc)sync_accounts_cb, self);
        /* do send & receive when we are idle */
-
+       
        return MODEST_WINDOW(self);
 }
+
+
+
+
+
+