Added new method to set a new label on captioned
[modest] / src / hildon2 / modest-maemo-utils.c
1 /* Copyright (c) 2006, Nokia Corporation
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  *   notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  *   notice, this list of conditions and the following disclaimer in the
12  *   documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Nokia Corporation nor the names of its
14  *   contributors may be used to endorse or promote products derived from
15  *   this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
18  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #ifndef DBUS_API_SUBJECT_TO_CHANGE
31 #define DBUS_API_SUBJECT_TO_CHANGE
32 #endif /*DBUS_API_SUBJECT_TO_CHANGE*/
33
34 #include <dbus/dbus.h>
35 #include <dbus/dbus-glib-lowlevel.h>
36 #include <glib.h>
37 #include <glib/gstdio.h>
38 #include <errno.h>
39 #include <string.h> /* for strlen */
40 #include <modest-runtime.h>
41 #include <libgnomevfs/gnome-vfs.h>
42 #include <tny-fs-stream.h>
43 #include <tny-camel-account.h>
44 #include <tny-status.h>
45 #include <tny-camel-transport-account.h>
46 #include <tny-camel-imap-store-account.h>
47 #include <tny-camel-pop-store-account.h>
48 #include "modest-hildon-includes.h"
49
50 #include <modest-defs.h>
51 #include "modest-maemo-utils.h"
52 #include "modest-text-utils.h"
53 #include "modest-platform.h"
54 #include "modest-ui-constants.h"
55
56 /*
57  * For getting and tracking the Bluetooth name
58  */
59 #define BTNAME_SERVICE                  "org.bluez"
60 #define BTNAME_REQUEST_IF               "org.bluez.Adapter"
61 #define BTNAME_SIGNAL_IF                "org.bluez.Adapter"
62 #define BTNAME_REQUEST_PATH             "/org/bluez/hci0"
63 #define BTNAME_SIGNAL_PATH              "/org/bluez/hci0"
64
65 #define BTNAME_REQ_GET                  "GetName"
66 #define BTNAME_SIG_CHANGED              "NameChanged"
67
68 #define BTNAME_MATCH_RULE "type='signal',interface='" BTNAME_SIGNAL_IF \
69                           "',member='" BTNAME_SIG_CHANGED "'"
70
71 /* Label child of a captioned */
72 #define CAPTIONED_LABEL_CHILD "captioned-label"
73
74
75 static osso_context_t *__osso_context = NULL; /* urgh global */
76
77 osso_context_t *
78 modest_maemo_utils_get_osso_context (void)
79 {
80         if (!__osso_context) 
81                 g_warning ("%s: __osso_context == NULL", __FUNCTION__);
82         
83         return __osso_context;
84 }
85
86 void
87 modest_maemo_utils_set_osso_context (osso_context_t *osso_context)
88 {
89         g_return_if_fail (osso_context);
90         __osso_context = osso_context;
91 }
92
93 static void
94 update_device_name_from_msg (DBusMessage *message)
95 {
96         DBusError error;
97         DBusMessageIter iter;
98
99         dbus_error_init (&error);
100
101         if (dbus_set_error_from_message (&error, message)) {
102                 g_printerr ("modest: failed to get bluetooth name: %s\n", error.message);
103                 dbus_error_free (&error);
104         } else {
105                 const gchar *device_name;
106                 if (!dbus_message_iter_init (message, &iter)) {
107                         g_printerr ("modest: message did not have argument\n");
108                         return;
109                 }
110                 dbus_message_iter_get_basic (&iter, &device_name);
111                 modest_conf_set_string (modest_runtime_get_conf(),
112                                         MODEST_CONF_DEVICE_NAME, device_name,
113                                         NULL);
114         }
115 }
116
117
118 static void
119 on_device_name_received (DBusPendingCall *call, void *user_data)
120 {
121         DBusMessage *message;
122         
123         g_return_if_fail (dbus_pending_call_get_completed (call));
124         
125         message = dbus_pending_call_steal_reply (call);
126         if (!message) {
127                 g_printerr ("modest: no reply on device name query\n");
128                 return;
129         }
130
131         update_device_name_from_msg (message);
132         dbus_message_unref (message);
133 }
134
135
136 static DBusHandlerResult
137 handle_dbus_signal (DBusConnection *conn, DBusMessage *msg, gpointer data)
138 {
139         if (dbus_message_is_signal(msg, BTNAME_SIGNAL_IF, BTNAME_SIG_CHANGED))
140                 update_device_name_from_msg (msg);
141
142         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
143 }
144
145
146 static void
147 get_device_name_from_dbus ()
148 {
149         static DBusConnection *conn = NULL;
150         DBusMessage *request;
151         DBusError error;
152         DBusPendingCall *call = NULL;
153         
154         dbus_error_init (&error);
155         if (!conn) {
156                 conn = dbus_bus_get (DBUS_BUS_SYSTEM, &error);
157                 if (!conn) {
158                         g_printerr ("modest: cannot get on the dbus: %s: %s\n",
159                                     error.name, error.message);
160                         dbus_error_free (&error);
161                         return;
162                 }
163         }
164         
165         request = dbus_message_new_method_call (BTNAME_SERVICE, BTNAME_REQUEST_PATH,
166                                                 BTNAME_REQUEST_IF, BTNAME_REQ_GET);
167         if (!request) {
168                 /* should we free the connection? */
169                 g_printerr ("modest: dbus_message_new_method_call failed\n");
170                 return;
171         }
172         dbus_message_set_auto_start (request, TRUE);
173         if (dbus_connection_send_with_reply (conn, request, &call, -1)) {
174                 dbus_pending_call_set_notify (call, on_device_name_received,
175                                               NULL, NULL);
176                 dbus_pending_call_unref (call);
177         }
178         dbus_message_unref (request);
179         
180         dbus_connection_setup_with_g_main (conn, NULL);
181         dbus_bus_add_match (conn, BTNAME_MATCH_RULE, &error);
182         if (dbus_error_is_set(&error)) {
183                 g_printerr ("modest: dbus_bus_add_match failed: %s\n", error.message);
184                 dbus_error_free (&error);
185         }
186
187         if (!dbus_connection_add_filter(conn, handle_dbus_signal, NULL, NULL))
188                 g_printerr ("modest: dbus_connection_add_filter failed\n");
189 }
190
191
192 void
193 modest_maemo_utils_get_device_name (void)
194 {
195         get_device_name_from_dbus ();
196 }
197
198 void
199 modest_maemo_utils_setup_images_filechooser (GtkFileChooser *chooser)
200 {
201         gchar *images_folder;
202         GtkFileFilter *file_filter;
203         GList *image_mimetypes_list;
204         GList *node;
205
206         g_return_if_fail (GTK_IS_FILE_CHOOSER (chooser));
207
208         /* Set the default folder to images folder */
209         images_folder = g_build_filename (g_getenv (MODEST_MAEMO_UTILS_MYDOCS_ENV),
210                                           MODEST_MAEMO_UTILS_DEFAULT_IMAGE_FOLDER, NULL);
211         gtk_file_chooser_set_current_folder (chooser, images_folder);
212         g_free (images_folder);
213
214         /* Set the images mime filter */
215         file_filter = gtk_file_filter_new ();
216 #ifdef MODEST_HAVE_HILDON0_WIDGETS
217         image_mimetypes_list = osso_mime_get_mime_types_for_category (OSSO_MIME_CATEGORY_IMAGES);
218 #else
219         image_mimetypes_list = hildon_mime_get_mime_types_for_category (HILDON_MIME_CATEGORY_IMAGES);
220 #endif
221         for (node = image_mimetypes_list; node != NULL; node = g_list_next (node)) {
222                 gtk_file_filter_add_mime_type (file_filter, node->data);
223         }
224         gtk_file_chooser_set_filter (chooser, file_filter);
225 #ifdef MODEST_HAVE_HILDON0_WIDGETS
226         osso_mime_types_list_free (image_mimetypes_list);
227 #else
228         hildon_mime_types_list_free (image_mimetypes_list);
229 #endif
230
231 }
232
233 void
234 modest_maemo_set_thumbable_scrollbar (GtkScrolledWindow *win, 
235                                       gboolean thumbable)
236 {
237         g_return_if_fail (GTK_IS_SCROLLED_WINDOW(win));
238 #ifdef MODEST_HAVE_HILDON1_WIDGETS              
239         hildon_helper_set_thumb_scrollbar (win, thumbable);
240 #endif /* MODEST_HAVE_HILDON1_WIDGETS */
241 }
242
243 FILE*
244 modest_maemo_open_mcc_mapping_file (void)
245 {
246         FILE* result;
247         
248         const gchar* path;
249         const gchar* path1 = MODEST_OPERATOR_WIZARD_MCC_MAPPING;
250         const gchar* path2 = MODEST_MCC_MAPPING;
251         
252         if (access(path1, R_OK) == 0) 
253                 path = path1;
254         else if (access(path2, R_OK) == 0)
255                 path = path2;
256         else {
257                 g_warning ("%s: neither '%s' nor '%s' is a readable mapping file",
258                            __FUNCTION__, path1, path2);
259                 return NULL;
260         }
261         
262         result = fopen (path, "r");
263         if (!result) {
264                 g_warning ("%s: error opening mapping file '%s': %s",
265                            __FUNCTION__, path, strerror(errno));
266                 return NULL;
267         }
268         return result;
269 }
270
271 GtkWidget *
272 modest_maemo_utils_get_manager_menubar_as_menu (GtkUIManager *manager,
273                                                 const gchar *item_name)
274 {
275         GtkWidget *new_menu;
276         GtkWidget *menubar;
277         GList *children, *iter;
278
279         menubar = gtk_ui_manager_get_widget (manager, item_name);
280         new_menu = gtk_menu_new ();
281
282         children = gtk_container_get_children (GTK_CONTAINER (menubar));
283         for (iter = children; iter != NULL; iter = g_list_next (iter)) {
284                 GtkWidget *menu;
285
286                 menu = GTK_WIDGET (iter->data);
287                 gtk_widget_reparent (menu, new_menu);
288         }
289         
290         g_list_free (children);
291
292         return new_menu;
293 }
294
295 /**
296  * modest_maemo_utils_create_captioned:
297  * @title_size_group: a #GtkSizeGroup
298  * @value_size_group: a #GtkSizeGroup
299  * @title: a string
300  * @control: a #GtkWidget
301  *
302  * this creates a widget (a #GtkHBox) with a control, and a label
303  * (@string) captioning it. It also uses the proper size groups for title
304  * and control.
305  *
306  * Returns: a widget containing the control and a proper label.
307  */
308 GtkWidget *
309 modest_maemo_utils_create_captioned    (GtkSizeGroup *title_size_group,
310                                         GtkSizeGroup *value_size_group,
311                                         const gchar *title,
312                                         gboolean use_markup,
313                                         GtkWidget *control)
314 {
315         return modest_maemo_utils_create_captioned_with_size_type (title_size_group,
316                                                                    value_size_group,
317                                                                    title,
318                                                                    use_markup,
319                                                                    control,
320                                                                    HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH);
321 }
322
323 /**
324  * modest_maemo_utils_create_captioned_with_size_type:
325  * @title_size_group: a #GtkSizeGroup
326  * @value_size_group: a #GtkSizeGroup
327  * @title: a string
328  * @control: a #GtkWidget
329  * @size_type: a #HildonSizeType
330  *
331  * this creates a widget (a #GtkHBox) with a control, and a label
332  * (@string) captioning it. It also uses the proper size groups for title
333  * and control.
334  *
335  * Returns: a widget containing the control and a proper label.
336  */
337 GtkWidget *
338 modest_maemo_utils_create_captioned_with_size_type    (GtkSizeGroup *title_size_group,
339                                                        GtkSizeGroup *value_size_group,
340                                                        const gchar *title,
341                                                        gboolean use_markup,
342                                                        GtkWidget *control,
343                                                        HildonSizeType size_type)
344 {
345         GtkWidget *label;
346         GtkWidget *box;
347   
348         if (use_markup) {
349                 label = gtk_label_new (NULL);
350                 gtk_label_set_markup (GTK_LABEL (label), title);
351         } else {
352                 label = gtk_label_new (title);
353         }
354
355         gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
356         hildon_gtk_widget_set_theme_size (label, HILDON_SIZE_FINGER_HEIGHT);
357         gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
358         gtk_widget_show (label);
359         box = gtk_hbox_new (FALSE, MODEST_MARGIN_HALF);
360         gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, MODEST_MARGIN_HALF);
361         gtk_box_pack_start (GTK_BOX (box), control, TRUE, TRUE, MODEST_MARGIN_HALF);
362         if (title_size_group)
363                 gtk_size_group_add_widget (title_size_group, label);
364         if (value_size_group)
365                 gtk_size_group_add_widget (value_size_group, control);
366
367         hildon_gtk_widget_set_theme_size (control, size_type);
368
369         g_object_set_data (G_OBJECT (box), CAPTIONED_LABEL_CHILD, label);
370
371         return box;
372 }
373
374 /**
375  * modest_maemo_utils_captioned_set_label:
376  * @captioned: a #GtkWidget built as captioned
377  * @new_label: a string
378  * @use_markup: a #gboolean
379  *
380  * set a new label for the captioned
381  */
382 void
383 modest_maemo_utils_captioned_set_label (GtkWidget *captioned,
384                                         const gchar *new_label,
385                                         gboolean use_markup)
386 {
387         GtkWidget *label;
388
389         g_return_if_fail (GTK_IS_WIDGET (captioned));
390
391         label = g_object_get_data (G_OBJECT (captioned), CAPTIONED_LABEL_CHILD);
392         g_return_if_fail (GTK_IS_LABEL (label));
393
394         if (use_markup) {
395                 gtk_label_set_markup (GTK_LABEL (label), new_label);
396         } else {
397                 gtk_label_set_text (GTK_LABEL (label), new_label);
398         }
399 }
400
401 /**
402  * modest_maemo_utils_set_hbutton_layout:
403  * @title_sizegroup: a #GtkSizeGroup, or %NULL
404  * @value_sizegroup: a #GtkSizeGroup, or %NULL
405  * @title: a string
406  * @button: a #HildonButton
407  *
408  * Configures the alignment and layout of @button. If @title_sizegroup is provided,
409  * the title will be aligned to the left using it. If @value_sizegroup is provided,
410  * the value will be aligned to the left using it. It also sets the title
411  * of the button.
412  *
413  * The alignment is left for the title and for the value.
414  */
415 void
416 modest_maemo_utils_set_hbutton_layout (GtkSizeGroup *title_sizegroup, 
417                                        GtkSizeGroup *value_sizegroup,
418                                        const gchar *title, 
419                                        GtkWidget *button)
420 {
421         hildon_button_set_title (HILDON_BUTTON (button), title);
422         if (title_sizegroup)
423                 hildon_button_add_title_size_group (HILDON_BUTTON (button), title_sizegroup);
424         if (value_sizegroup)
425                 hildon_button_add_value_size_group (HILDON_BUTTON (button), value_sizegroup);
426         hildon_button_set_alignment (HILDON_BUTTON (button), 0.0, 0.5, 1.0, 0.0);
427         hildon_button_set_title_alignment (HILDON_BUTTON (button), 0.0, 0.5);
428         hildon_button_set_value_alignment (HILDON_BUTTON (button), 0.0, 0.5);
429 }
430
431 void
432 modest_maemo_utils_set_vbutton_layout (GtkSizeGroup *sizegroup, 
433                                        const gchar *title, 
434                                        GtkWidget *button)
435 {
436         hildon_button_set_title (HILDON_BUTTON (button), title);
437         if (sizegroup) {
438                 hildon_button_add_title_size_group (HILDON_BUTTON (button), sizegroup);
439                 hildon_button_add_value_size_group (HILDON_BUTTON (button), sizegroup);
440         }
441         hildon_button_set_alignment (HILDON_BUTTON (button), 0.0, 0.5, 1.0, 0.0);
442         hildon_button_set_title_alignment (HILDON_BUTTON (button), 0.0, 0.5);
443         hildon_button_set_value_alignment (HILDON_BUTTON (button), 0.0, 0.5);
444 }
445
446 GtkWidget *
447 modest_maemo_utils_create_group_box (const gchar *label_text, GtkWidget *contents)
448 {
449         GtkWidget *label;
450         GtkWidget *box;
451
452         label = gtk_label_new (label_text);
453         gtk_widget_show (label);
454
455         box = gtk_vbox_new (FALSE, MODEST_MARGIN_HALF);
456         gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
457         gtk_box_pack_start (GTK_BOX (box), contents, TRUE, TRUE, 0);
458         gtk_widget_show (box);
459
460         return box;
461 }