* Get the device name from the system instead of from bluetooth. Seems that could...
[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 <sys/utsname.h>
35 #include <dbus/dbus.h>
36 #include <dbus/dbus-glib-lowlevel.h>
37 #include <glib.h>
38 #include <glib/gstdio.h>
39 #include <errno.h>
40 #include <string.h> /* for strlen */
41 #include <modest-runtime.h>
42 #include <libgnomevfs/gnome-vfs.h>
43 #include <tny-fs-stream.h>
44 #include <tny-camel-account.h>
45 #include <tny-status.h>
46 #include <tny-camel-transport-account.h>
47 #include <tny-camel-imap-store-account.h>
48 #include <tny-camel-pop-store-account.h>
49 #include "modest-hildon-includes.h"
50
51 #include <modest-defs.h>
52 #include "modest-maemo-utils.h"
53 #include "modest-text-utils.h"
54 #include "modest-platform.h"
55 #include "modest-ui-constants.h"
56 #include <hildon/hildon-picker-dialog.h>
57
58 /*
59  * For getting and tracking the Bluetooth name
60  */
61 #define BTNAME_SERVICE                  "org.bluez"
62 #define BTNAME_REQUEST_IF               "org.bluez.Adapter"
63 #define BTNAME_SIGNAL_IF                "org.bluez.Adapter"
64 #define BTNAME_REQUEST_PATH             "/org/bluez/hci0"
65 #define BTNAME_SIGNAL_PATH              "/org/bluez/hci0"
66
67 #define BTNAME_REQ_GET                  "GetName"
68 #define BTNAME_SIG_CHANGED              "NameChanged"
69
70 #define BTNAME_MATCH_RULE "type='signal',interface='" BTNAME_SIGNAL_IF \
71                           "',member='" BTNAME_SIG_CHANGED "'"
72
73 /* Label child of a captioned */
74 #define CAPTIONED_LABEL_CHILD "captioned-label"
75
76
77 static osso_context_t *__osso_context = NULL; /* urgh global */
78
79 osso_context_t *
80 modest_maemo_utils_get_osso_context (void)
81 {
82         if (!__osso_context) 
83                 g_warning ("%s: __osso_context == NULL", __FUNCTION__);
84
85         return __osso_context;
86 }
87
88 void
89 modest_maemo_utils_set_osso_context (osso_context_t *osso_context)
90 {
91         g_return_if_fail (osso_context);
92         __osso_context = osso_context;
93 }
94
95 void
96 modest_maemo_utils_get_device_name (void)
97 {
98         struct utsname name;
99
100         if (uname (&name) == 0) {
101                 modest_conf_set_string (modest_runtime_get_conf(),
102                                         MODEST_CONF_DEVICE_NAME, name.nodename,
103                                         NULL);
104         }
105 }
106
107 void
108 modest_maemo_utils_setup_images_filechooser (GtkFileChooser *chooser)
109 {
110         gchar *images_folder;
111         GtkFileFilter *file_filter;
112         GList *image_mimetypes_list;
113         GList *node;
114
115         g_return_if_fail (GTK_IS_FILE_CHOOSER (chooser));
116
117         /* Set the default folder to images folder */
118         images_folder = g_build_filename (g_getenv (MODEST_MAEMO_UTILS_MYDOCS_ENV),
119                                           MODEST_MAEMO_UTILS_DEFAULT_IMAGE_FOLDER, NULL);
120         gtk_file_chooser_set_current_folder (chooser, images_folder);
121         g_free (images_folder);
122
123         /* Set the images mime filter */
124         file_filter = gtk_file_filter_new ();
125 #ifdef MODEST_HAVE_HILDON0_WIDGETS
126         image_mimetypes_list = osso_mime_get_mime_types_for_category (OSSO_MIME_CATEGORY_IMAGES);
127 #else
128         image_mimetypes_list = hildon_mime_get_mime_types_for_category (HILDON_MIME_CATEGORY_IMAGES);
129 #endif
130         for (node = image_mimetypes_list; node != NULL; node = g_list_next (node)) {
131                 gtk_file_filter_add_mime_type (file_filter, node->data);
132         }
133         gtk_file_chooser_set_filter (chooser, file_filter);
134 #ifdef MODEST_HAVE_HILDON0_WIDGETS
135         osso_mime_types_list_free (image_mimetypes_list);
136 #else
137         hildon_mime_types_list_free (image_mimetypes_list);
138 #endif
139
140 }
141
142 void
143 modest_maemo_set_thumbable_scrollbar (GtkScrolledWindow *win, 
144                                       gboolean thumbable)
145 {
146         g_return_if_fail (GTK_IS_SCROLLED_WINDOW(win));
147 #ifdef MODEST_HAVE_HILDON1_WIDGETS              
148         hildon_helper_set_thumb_scrollbar (win, thumbable);
149 #endif /* MODEST_HAVE_HILDON1_WIDGETS */
150 }
151
152 FILE*
153 modest_maemo_open_mcc_mapping_file (void)
154 {
155         FILE* result;
156         
157         const gchar* path;
158         const gchar* path1 = MODEST_OPERATOR_WIZARD_MCC_MAPPING;
159         const gchar* path2 = MODEST_MCC_MAPPING;
160         
161         if (access(path1, R_OK) == 0) 
162                 path = path1;
163         else if (access(path2, R_OK) == 0)
164                 path = path2;
165         else {
166                 g_warning ("%s: neither '%s' nor '%s' is a readable mapping file",
167                            __FUNCTION__, path1, path2);
168                 return NULL;
169         }
170         
171         result = fopen (path, "r");
172         if (!result) {
173                 g_warning ("%s: error opening mapping file '%s': %s",
174                            __FUNCTION__, path, strerror(errno));
175                 return NULL;
176         }
177         return result;
178 }
179
180 GtkWidget *
181 modest_maemo_utils_get_manager_menubar_as_menu (GtkUIManager *manager,
182                                                 const gchar *item_name)
183 {
184         GtkWidget *new_menu;
185         GtkWidget *menubar;
186         GList *children, *iter;
187
188         menubar = gtk_ui_manager_get_widget (manager, item_name);
189         new_menu = gtk_menu_new ();
190
191         children = gtk_container_get_children (GTK_CONTAINER (menubar));
192         for (iter = children; iter != NULL; iter = g_list_next (iter)) {
193                 GtkWidget *menu;
194
195                 menu = GTK_WIDGET (iter->data);
196                 gtk_widget_reparent (menu, new_menu);
197         }
198         
199         g_list_free (children);
200
201         return new_menu;
202 }
203
204 /**
205  * modest_maemo_utils_create_captioned:
206  * @title_size_group: a #GtkSizeGroup
207  * @value_size_group: a #GtkSizeGroup
208  * @title: a string
209  * @control: a #GtkWidget
210  *
211  * this creates a widget (a #GtkHBox) with a control, and a label
212  * (@string) captioning it. It also uses the proper size groups for title
213  * and control.
214  *
215  * Returns: a widget containing the control and a proper label.
216  */
217 GtkWidget *
218 modest_maemo_utils_create_captioned    (GtkSizeGroup *title_size_group,
219                                         GtkSizeGroup *value_size_group,
220                                         const gchar *title,
221                                         gboolean use_markup,
222                                         GtkWidget *control)
223 {
224         return modest_maemo_utils_create_captioned_with_size_type (title_size_group,
225                                                                    value_size_group,
226                                                                    title,
227                                                                    use_markup,
228                                                                    control,
229                                                                    HILDON_SIZE_FINGER_HEIGHT | HILDON_SIZE_AUTO_WIDTH);
230 }
231
232 /**
233  * modest_maemo_utils_create_captioned_with_size_type:
234  * @title_size_group: a #GtkSizeGroup
235  * @value_size_group: a #GtkSizeGroup
236  * @title: a string
237  * @control: a #GtkWidget
238  * @size_type: a #HildonSizeType
239  *
240  * this creates a widget (a #GtkHBox) with a control, and a label
241  * (@string) captioning it. It also uses the proper size groups for title
242  * and control.
243  *
244  * Returns: a widget containing the control and a proper label.
245  */
246 GtkWidget *
247 modest_maemo_utils_create_captioned_with_size_type    (GtkSizeGroup *title_size_group,
248                                                        GtkSizeGroup *value_size_group,
249                                                        const gchar *title,
250                                                        gboolean use_markup,
251                                                        GtkWidget *control,
252                                                        HildonSizeType size_type)
253 {
254         GtkWidget *label;
255         GtkWidget *box;
256   
257         if (use_markup) {
258                 label = gtk_label_new (NULL);
259                 gtk_label_set_markup (GTK_LABEL (label), title);
260         } else {
261                 label = gtk_label_new (title);
262         }
263
264         gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
265         hildon_gtk_widget_set_theme_size (label, HILDON_SIZE_FINGER_HEIGHT);
266         gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
267         gtk_widget_show (label);
268         box = gtk_hbox_new (FALSE, MODEST_MARGIN_HALF);
269         gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, MODEST_MARGIN_HALF);
270         gtk_box_pack_start (GTK_BOX (box), control, TRUE, TRUE, MODEST_MARGIN_HALF);
271         if (title_size_group)
272                 gtk_size_group_add_widget (title_size_group, label);
273         if (value_size_group)
274                 gtk_size_group_add_widget (value_size_group, control);
275
276         hildon_gtk_widget_set_theme_size (control, size_type);
277
278         g_object_set_data (G_OBJECT (box), CAPTIONED_LABEL_CHILD, label);
279
280         return box;
281 }
282
283 /**
284  * modest_maemo_utils_captioned_set_label:
285  * @captioned: a #GtkWidget built as captioned
286  * @new_label: a string
287  * @use_markup: a #gboolean
288  *
289  * set a new label for the captioned
290  */
291 void
292 modest_maemo_utils_captioned_set_label (GtkWidget *captioned,
293                                         const gchar *new_label,
294                                         gboolean use_markup)
295 {
296         GtkWidget *label;
297
298         g_return_if_fail (GTK_IS_WIDGET (captioned));
299
300         label = g_object_get_data (G_OBJECT (captioned), CAPTIONED_LABEL_CHILD);
301         g_return_if_fail (GTK_IS_LABEL (label));
302
303         if (use_markup) {
304                 gtk_label_set_markup (GTK_LABEL (label), new_label);
305         } else {
306                 gtk_label_set_text (GTK_LABEL (label), new_label);
307         }
308 }
309
310 /**
311  * modest_maemo_utils_set_hbutton_layout:
312  * @title_sizegroup: a #GtkSizeGroup, or %NULL
313  * @value_sizegroup: a #GtkSizeGroup, or %NULL
314  * @title: a string
315  * @button: a #HildonButton
316  *
317  * Configures the alignment and layout of @button. If @title_sizegroup is provided,
318  * the title will be aligned to the left using it. If @value_sizegroup is provided,
319  * the value will be aligned to the left using it. It also sets the title
320  * of the button.
321  *
322  * The alignment is left for the title and for the value.
323  */
324 void
325 modest_maemo_utils_set_hbutton_layout (GtkSizeGroup *title_sizegroup, 
326                                        GtkSizeGroup *value_sizegroup,
327                                        const gchar *title, 
328                                        GtkWidget *button)
329 {
330         hildon_button_set_title (HILDON_BUTTON (button), title);
331         if (title_sizegroup)
332                 hildon_button_add_title_size_group (HILDON_BUTTON (button), title_sizegroup);
333         if (value_sizegroup)
334                 hildon_button_add_value_size_group (HILDON_BUTTON (button), value_sizegroup);
335         hildon_button_set_alignment (HILDON_BUTTON (button), 0.0, 0.5, 1.0, 0.0);
336         hildon_button_set_title_alignment (HILDON_BUTTON (button), 0.0, 0.5);
337         hildon_button_set_value_alignment (HILDON_BUTTON (button), 0.0, 0.5);
338 }
339
340 void
341 modest_maemo_utils_set_vbutton_layout (GtkSizeGroup *sizegroup, 
342                                        const gchar *title, 
343                                        GtkWidget *button)
344 {
345         hildon_button_set_title (HILDON_BUTTON (button), title);
346         if (sizegroup) {
347                 hildon_button_add_title_size_group (HILDON_BUTTON (button), sizegroup);
348                 hildon_button_add_value_size_group (HILDON_BUTTON (button), sizegroup);
349         }
350         hildon_button_set_alignment (HILDON_BUTTON (button), 0.0, 0.5, 1.0, 0.0);
351         hildon_button_set_title_alignment (HILDON_BUTTON (button), 0.0, 0.5);
352         hildon_button_set_value_alignment (HILDON_BUTTON (button), 0.0, 0.5);
353 }
354
355 GtkWidget *
356 modest_maemo_utils_create_group_box (const gchar *label_text, GtkWidget *contents)
357 {
358         GtkWidget *label;
359         GtkWidget *box;
360
361         label = gtk_label_new (label_text);
362         gtk_widget_show (label);
363
364         box = gtk_vbox_new (FALSE, MODEST_MARGIN_HALF);
365         gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
366         gtk_box_pack_start (GTK_BOX (box), contents, TRUE, TRUE, 0);
367         gtk_widget_show (box);
368
369         return box;
370 }
371
372 static gboolean match_all (TnyList *list, GObject *item, gpointer match_data)
373 {
374         return TRUE;
375 }
376
377 gboolean
378 modest_maemo_utils_select_attachments (GtkWindow *window, TnyList *att_list)
379 {
380         GtkTreeModel *model;
381         TnyIterator *iterator;
382         GtkWidget *selector;
383         GtkCellRenderer *renderer;
384         GtkWidget *dialog;
385         gint response;
386         gboolean result = TRUE;
387
388         model = GTK_TREE_MODEL (gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_OBJECT));
389         for (iterator = tny_list_create_iterator (att_list);
390              !tny_iterator_is_done (iterator);
391              tny_iterator_next (iterator)) {
392                 GtkTreeIter iter;
393                 TnyMimePart *part;
394
395                 part = (TnyMimePart *) tny_iterator_get_current (iterator);
396
397                 /* Embbeded messages are not offered to be saved */
398                 if (!TNY_IS_MSG (part)) {
399                         gchar *label;
400                         gchar *filename = NULL;
401
402                         filename = g_strdup (tny_mime_part_get_filename (part));
403                         label = g_strconcat (filename, NULL);
404                         gtk_list_store_append (GTK_LIST_STORE (model), &iter);
405                         gtk_list_store_set (GTK_LIST_STORE (model), &iter, 0, label, 1, part, -1);
406                         g_free (label);
407                         g_object_unref (part);
408                 }
409         }
410
411         selector = GTK_WIDGET (hildon_touch_selector_new ());
412         renderer = gtk_cell_renderer_text_new ();
413         hildon_touch_selector_append_column (HILDON_TOUCH_SELECTOR (selector), model, renderer,
414                                              "text", 0, NULL);
415         hildon_touch_selector_set_column_selection_mode (HILDON_TOUCH_SELECTOR (selector), 
416                                                          HILDON_TOUCH_SELECTOR_SELECTION_MODE_MULTIPLE);
417
418         dialog = hildon_picker_dialog_new (window);
419         gtk_window_set_title (GTK_WINDOW (dialog), _("mcen_ti_select_attachment_title"));
420         hildon_picker_dialog_set_selector (HILDON_PICKER_DIALOG (dialog), HILDON_TOUCH_SELECTOR (selector));
421         hildon_picker_dialog_set_done_label (HILDON_PICKER_DIALOG (dialog), _HL("wdgt_bd_done"));
422
423         response = gtk_dialog_run (GTK_DIALOG (dialog));
424
425         if (response == GTK_RESPONSE_OK) {
426                 GList *selected_rows, *node;
427
428                 tny_list_remove_matches (att_list, match_all, NULL);
429                 selected_rows = hildon_touch_selector_get_selected_rows (HILDON_TOUCH_SELECTOR (selector), 0);
430                 for (node = selected_rows; node != NULL; node = g_list_next (node)) {
431                         GtkTreePath *path;
432                         GObject *selected;
433                         GtkTreeIter iter;
434
435                         path = (GtkTreePath *) node->data;
436                         gtk_tree_model_get_iter (model, &iter, path);
437                         gtk_tree_model_get (model, &iter, 1, &selected, -1);
438                         tny_list_append (att_list, selected);
439                 }
440                 if (tny_list_get_length (att_list) == 0)
441                         result = FALSE;
442         } else {
443                 result = FALSE;
444         }
445
446         gtk_widget_destroy (dialog);
447
448         g_object_unref (model);
449
450         return result;
451 }