Show "no subject" in select attachments dialog if subject is empty
[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         GtkFileFilter *file_filter;
111         GList *image_mimetypes_list;
112         GList *node;
113         gchar *conf_folder;
114
115         g_return_if_fail (GTK_IS_FILE_CHOOSER (chooser));
116
117         conf_folder = modest_conf_get_string (modest_runtime_get_conf (), MODEST_CONF_LATEST_INSERT_IMAGE_PATH, NULL);
118         if (conf_folder && conf_folder[0] != '\0') {
119                 gtk_file_chooser_set_current_folder_uri (chooser, conf_folder);
120         } else {
121                 gchar *images_folder;
122                 /* Set the default folder to images folder */
123                 images_folder = g_build_filename (g_getenv (MODEST_MAEMO_UTILS_MYDOCS_ENV),
124                                                   MODEST_MAEMO_UTILS_DEFAULT_IMAGE_FOLDER, NULL);
125                 gtk_file_chooser_set_current_folder (chooser, images_folder);
126                 g_free (images_folder);
127         }
128         g_free (conf_folder);
129
130         /* Set the images mime filter */
131         file_filter = gtk_file_filter_new ();
132 #ifdef MODEST_HAVE_HILDON0_WIDGETS
133         image_mimetypes_list = osso_mime_get_mime_types_for_category (OSSO_MIME_CATEGORY_IMAGES);
134 #else
135         image_mimetypes_list = hildon_mime_get_mime_types_for_category (HILDON_MIME_CATEGORY_IMAGES);
136 #endif
137         for (node = image_mimetypes_list; node != NULL; node = g_list_next (node)) {
138                 gtk_file_filter_add_mime_type (file_filter, node->data);
139         }
140         gtk_file_chooser_set_filter (chooser, file_filter);
141 #ifdef MODEST_HAVE_HILDON0_WIDGETS
142         osso_mime_types_list_free (image_mimetypes_list);
143 #else
144         hildon_mime_types_list_free (image_mimetypes_list);
145 #endif
146
147 }
148
149 void
150 modest_maemo_set_thumbable_scrollbar (GtkScrolledWindow *win, 
151                                       gboolean thumbable)
152 {
153         g_return_if_fail (GTK_IS_SCROLLED_WINDOW(win));
154 #ifdef MODEST_HAVE_HILDON1_WIDGETS              
155         hildon_helper_set_thumb_scrollbar (win, thumbable);
156 #endif /* MODEST_HAVE_HILDON1_WIDGETS */
157 }
158
159 GtkWidget *
160 modest_maemo_utils_get_manager_menubar_as_menu (GtkUIManager *manager,
161                                                 const gchar *item_name)
162 {
163         GtkWidget *new_menu;
164         GtkWidget *menubar;
165         GList *children, *iter;
166
167         menubar = gtk_ui_manager_get_widget (manager, item_name);
168         new_menu = gtk_menu_new ();
169
170         children = gtk_container_get_children (GTK_CONTAINER (menubar));
171         for (iter = children; iter != NULL; iter = g_list_next (iter)) {
172                 GtkWidget *menu;
173
174                 menu = GTK_WIDGET (iter->data);
175                 gtk_widget_reparent (menu, new_menu);
176         }
177         
178         g_list_free (children);
179
180         return new_menu;
181 }
182
183 /**
184  * modest_maemo_utils_create_captioned:
185  * @title_size_group: a #GtkSizeGroup
186  * @value_size_group: a #GtkSizeGroup
187  * @title: a string
188  * @control: a #GtkWidget
189  *
190  * this creates a widget (a #GtkHBox) with a control, and a label
191  * (@string) captioning it. It also uses the proper size groups for title
192  * and control.
193  *
194  * Returns: a widget containing the control and a proper label.
195  */
196 GtkWidget *
197 modest_maemo_utils_create_captioned    (GtkSizeGroup *title_size_group,
198                                         GtkSizeGroup *value_size_group,
199                                         const gchar *title,
200                                         gboolean use_markup,
201                                         GtkWidget *control)
202 {
203         return modest_maemo_utils_create_captioned_with_size_type (title_size_group,
204                                                                    value_size_group,
205                                                                    title,
206                                                                    use_markup,
207                                                                    control,
208                                                                    0);
209 }
210
211 /**
212  * modest_maemo_utils_create_captioned_with_size_type:
213  * @title_size_group: a #GtkSizeGroup
214  * @value_size_group: a #GtkSizeGroup
215  * @title: a string
216  * @control: a #GtkWidget
217  * @size_type: a #HildonSizeType
218  *
219  * this creates a widget (a #GtkHBox) with a control, and a label
220  * (@string) captioning it. It also uses the proper size groups for title
221  * and control.
222  *
223  * Returns: a widget containing the control and a proper label.
224  */
225 GtkWidget *
226 modest_maemo_utils_create_captioned_with_size_type    (GtkSizeGroup *title_size_group,
227                                                        GtkSizeGroup *value_size_group,
228                                                        const gchar *title,
229                                                        gboolean use_markup,
230                                                        GtkWidget *control,
231                                                        HildonSizeType size_type)
232 {
233         GtkWidget *label;
234         GtkWidget *align;
235         GtkWidget *box;
236   
237         if (use_markup) {
238                 label = gtk_label_new (NULL);
239                 gtk_label_set_markup (GTK_LABEL (label), title);
240         } else {
241                 label = gtk_label_new (title);
242         }
243         align = gtk_alignment_new (0.0, 0.0, 1.0, 1.0);
244         gtk_alignment_set_padding (GTK_ALIGNMENT (align), 0, 0, MODEST_MARGIN_DOUBLE, MODEST_MARGIN_TRIPLE);
245
246         gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
247         hildon_gtk_widget_set_theme_size (label, HILDON_SIZE_FINGER_HEIGHT);
248         gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
249         gtk_widget_show (label);
250         gtk_widget_show (align);
251         box = gtk_hbox_new (FALSE, 0);
252         gtk_container_add (GTK_CONTAINER (align), label);
253         gtk_box_pack_start (GTK_BOX (box), align, FALSE, FALSE, 0);
254         gtk_box_pack_start (GTK_BOX (box), control, TRUE, TRUE, 0);
255         if (title_size_group)
256                 gtk_size_group_add_widget (title_size_group, label);
257         if (value_size_group)
258                 gtk_size_group_add_widget (value_size_group, control);
259
260         hildon_gtk_widget_set_theme_size (control, size_type);
261
262         g_object_set_data (G_OBJECT (box), CAPTIONED_LABEL_CHILD, label);
263
264         return box;
265 }
266
267 /**
268  * modest_maemo_utils_captioned_set_label:
269  * @captioned: a #GtkWidget built as captioned
270  * @new_label: a string
271  * @use_markup: a #gboolean
272  *
273  * set a new label for the captioned
274  */
275 void
276 modest_maemo_utils_captioned_set_label (GtkWidget *captioned,
277                                         const gchar *new_label,
278                                         gboolean use_markup)
279 {
280         GtkWidget *label;
281
282         g_return_if_fail (GTK_IS_WIDGET (captioned));
283
284         label = g_object_get_data (G_OBJECT (captioned), CAPTIONED_LABEL_CHILD);
285         g_return_if_fail (GTK_IS_LABEL (label));
286
287         if (use_markup) {
288                 gtk_label_set_markup (GTK_LABEL (label), new_label);
289         } else {
290                 gtk_label_set_text (GTK_LABEL (label), new_label);
291         }
292 }
293
294 /**
295  * modest_maemo_utils_set_hbutton_layout:
296  * @title_sizegroup: a #GtkSizeGroup, or %NULL
297  * @value_sizegroup: a #GtkSizeGroup, or %NULL
298  * @title: a string
299  * @button: a #HildonButton
300  *
301  * Configures the alignment and layout of @button. If @title_sizegroup is provided,
302  * the title will be aligned to the left using it. If @value_sizegroup is provided,
303  * the value will be aligned to the left using it. It also sets the title
304  * of the button.
305  *
306  * The alignment is left for the title and for the value.
307  */
308 void
309 modest_maemo_utils_set_hbutton_layout (GtkSizeGroup *title_sizegroup, 
310                                        GtkSizeGroup *value_sizegroup,
311                                        const gchar *title, 
312                                        GtkWidget *button)
313 {
314         hildon_button_set_title (HILDON_BUTTON (button), title);
315         if (title_sizegroup)
316                 hildon_button_add_title_size_group (HILDON_BUTTON (button), title_sizegroup);
317         if (value_sizegroup)
318                 hildon_button_add_value_size_group (HILDON_BUTTON (button), value_sizegroup);
319         hildon_button_set_alignment (HILDON_BUTTON (button), 0.0, 0.5, 1.0, 0.0);
320         hildon_button_set_title_alignment (HILDON_BUTTON (button), 0.0, 0.5);
321         hildon_button_set_value_alignment (HILDON_BUTTON (button), 0.0, 0.5);
322 }
323
324 void
325 modest_maemo_utils_set_vbutton_layout (GtkSizeGroup *sizegroup, 
326                                        const gchar *title, 
327                                        GtkWidget *button)
328 {
329         hildon_button_set_title (HILDON_BUTTON (button), title);
330         if (sizegroup) {
331                 hildon_button_add_title_size_group (HILDON_BUTTON (button), sizegroup);
332                 hildon_button_add_value_size_group (HILDON_BUTTON (button), sizegroup);
333         }
334         hildon_button_set_alignment (HILDON_BUTTON (button), 0.0, 0.5, 1.0, 0.0);
335         hildon_button_set_title_alignment (HILDON_BUTTON (button), 0.0, 0.5);
336         hildon_button_set_value_alignment (HILDON_BUTTON (button), 0.0, 0.5);
337 }
338
339 GtkWidget *
340 modest_maemo_utils_create_group_box (const gchar *label_text, GtkWidget *contents)
341 {
342         GtkWidget *label;
343         GtkWidget *box;
344
345         label = gtk_label_new (label_text);
346         gtk_widget_show (label);
347
348         box = gtk_vbox_new (FALSE, MODEST_MARGIN_HALF);
349         gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
350         gtk_box_pack_start (GTK_BOX (box), contents, TRUE, TRUE, 0);
351         gtk_widget_show (box);
352
353         return box;
354 }
355
356 static gboolean match_all (TnyList *list, GObject *item, gpointer match_data)
357 {
358         return TRUE;
359 }
360
361 gboolean
362 modest_maemo_utils_select_attachments (GtkWindow *window, TnyList *att_list, gboolean include_msgs)
363 {
364         GtkTreeModel *model;
365         TnyIterator *iterator;
366         GtkWidget *selector;
367         GtkCellRenderer *renderer;
368         GtkWidget *dialog;
369         gint response;
370         gboolean result = TRUE;
371         gint attachments_added = 0;
372
373         model = GTK_TREE_MODEL (gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_OBJECT));
374         for (iterator = tny_list_create_iterator (att_list);
375              !tny_iterator_is_done (iterator);
376              tny_iterator_next (iterator)) {
377                 GtkTreeIter iter;
378                 TnyMimePart *part;
379                 gchar *label;
380                 gchar *filename = NULL;
381
382                 part = (TnyMimePart *) tny_iterator_get_current (iterator);
383
384                 /* Ignore purged attachments and messages if ignore is
385                    set to TRUE */
386                 if (!(tny_mime_part_is_purged (part) ||
387                       (TNY_IS_MSG (part) && !include_msgs))) {
388
389                         if (TNY_IS_MSG (part)) {
390                                 TnyHeader *header = tny_msg_get_header (TNY_MSG (part));
391                                 filename = tny_header_dup_subject (header);
392                                 if ((filename == NULL) || (filename[0] == '\0')) {
393                                         g_free (filename);
394                                         filename = g_strdup (_("mail_va_no_subject"));
395                                 }
396                                 g_object_unref (header);
397                         } else {
398                                 filename = g_strdup (tny_mime_part_get_filename (part));
399                         }
400                         label = g_strconcat (filename, NULL);
401                         gtk_list_store_append (GTK_LIST_STORE (model), &iter);
402                         gtk_list_store_set (GTK_LIST_STORE (model), &iter, 0, label, 1, part, -1);
403                         attachments_added ++;
404                         g_free (label);
405                         g_object_unref (part);
406                 }
407         }
408
409         selector = GTK_WIDGET (hildon_touch_selector_new ());
410         renderer = gtk_cell_renderer_text_new ();
411         hildon_touch_selector_append_column (HILDON_TOUCH_SELECTOR (selector), model, renderer,
412                                              "text", 0, NULL);
413         hildon_touch_selector_set_column_selection_mode (HILDON_TOUCH_SELECTOR (selector), 
414                                                          HILDON_TOUCH_SELECTOR_SELECTION_MODE_MULTIPLE);
415
416         dialog = hildon_picker_dialog_new (window);
417         gtk_window_set_title (GTK_WINDOW (dialog), (attachments_added > 1)?
418                               _("mcen_ti_select_attachments_title"):_("mcen_ti_select_attachment_title"));
419         hildon_picker_dialog_set_selector (HILDON_PICKER_DIALOG (dialog), HILDON_TOUCH_SELECTOR (selector));
420         hildon_picker_dialog_set_done_label (HILDON_PICKER_DIALOG (dialog), _HL("wdgt_bd_done"));
421
422         response = gtk_dialog_run (GTK_DIALOG (dialog));
423
424         if (response == GTK_RESPONSE_OK) {
425                 GList *selected_rows, *node;
426
427                 tny_list_remove_matches (att_list, match_all, NULL);
428                 selected_rows = hildon_touch_selector_get_selected_rows (HILDON_TOUCH_SELECTOR (selector), 0);
429                 for (node = selected_rows; node != NULL; node = g_list_next (node)) {
430                         GtkTreePath *path;
431                         GObject *selected;
432                         GtkTreeIter iter;
433
434                         path = (GtkTreePath *) node->data;
435                         gtk_tree_model_get_iter (model, &iter, path);
436                         gtk_tree_model_get (model, &iter, 1, &selected, -1);
437                         tny_list_append (att_list, selected);
438                 }
439                 if (tny_list_get_length (att_list) == 0)
440                         result = FALSE;
441         } else {
442                 result = FALSE;
443         }
444
445         gtk_widget_destroy (dialog);
446
447         g_object_unref (model);
448
449         return result;
450 }