* src/widgets/modest-details-dialog.c:
[modest] / src / widgets / modest-details-dialog.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 #include "modest-details-dialog.h"
31
32 #include <glib/gi18n.h>
33 #include <gdk/gdkkeysyms.h>
34 #include <gtk/gtkscrolledwindow.h>
35 #include <gtk/gtktable.h>
36 #include <gtk/gtkstock.h>
37 #include <gtk/gtklabel.h>
38 #include <tny-msg.h>
39 #include <tny-header.h>
40 #include <tny-header-view.h>
41 #include <tny-folder-store.h>
42 #include <modest-tny-folder.h>
43 #include <modest-tny-account.h>
44 #include <modest-text-utils.h>
45 #include <string.h> /* for strlen */
46
47 static void    modest_details_dialog_set_header_default          (ModestDetailsDialog *self,
48                                                                   TnyHeader *header);
49
50 static void    modest_details_dialog_set_folder_default          (ModestDetailsDialog *self,
51                                                                   TnyFolder *foler);
52
53 static void    modest_details_dialog_create_container_default    (ModestDetailsDialog *self);
54
55 static void    modest_details_dialog_add_data_default            (ModestDetailsDialog *self,
56                                                                   const gchar *label,
57                                                                   const gchar *value);
58
59
60 G_DEFINE_TYPE (ModestDetailsDialog, 
61                modest_details_dialog, 
62                GTK_TYPE_DIALOG);
63
64 #define MODEST_DETAILS_DIALOG_GET_PRIVATE(o) \
65         (G_TYPE_INSTANCE_GET_PRIVATE ((o), MODEST_TYPE_DETAILS_DIALOG, ModestDetailsDialogPrivate))
66
67
68 typedef struct _ModestDetailsDialogPrivate ModestDetailsDialogPrivate;
69
70 struct _ModestDetailsDialogPrivate
71 {
72         GtkWidget *props_table;
73 };
74
75 static void
76 modest_details_dialog_finalize (GObject *object)
77 {
78         G_OBJECT_CLASS (modest_details_dialog_parent_class)->finalize (object);
79 }
80
81 static void
82 modest_details_dialog_class_init (ModestDetailsDialogClass *klass)
83 {
84         GObjectClass *object_class = G_OBJECT_CLASS (klass);
85
86         g_type_class_add_private (klass, sizeof (ModestDetailsDialogPrivate));
87         object_class->finalize = modest_details_dialog_finalize;
88
89         klass->create_container_func = modest_details_dialog_create_container_default;
90         klass->add_data_func = modest_details_dialog_add_data_default;
91         klass->set_header_func = modest_details_dialog_set_header_default;
92         klass->set_folder_func = modest_details_dialog_set_folder_default;
93 }
94
95 static void
96 modest_details_dialog_init (ModestDetailsDialog *self)
97 {
98         MODEST_DETAILS_DIALOG_GET_CLASS (self)->create_container_func (self);
99 }
100
101 GtkWidget*
102 modest_details_dialog_new_with_header (GtkWindow *parent, 
103                                        TnyHeader *header)
104 {
105         ModestDetailsDialog *dialog;
106
107         g_return_val_if_fail (GTK_IS_WINDOW (parent), NULL);
108         g_return_val_if_fail (TNY_IS_HEADER (header), NULL);
109
110         dialog = (ModestDetailsDialog *) (g_object_new (MODEST_TYPE_DETAILS_DIALOG, 
111                                                         "transient-for", parent, 
112                                                         NULL));
113
114         MODEST_DETAILS_DIALOG_GET_CLASS (dialog)->set_header_func (dialog, header);
115
116         return GTK_WIDGET (dialog);
117 }
118
119 GtkWidget* 
120 modest_details_dialog_new_with_folder  (GtkWindow *parent, 
121                                         TnyFolder *folder)
122 {
123         ModestDetailsDialog *dialog;
124
125         g_return_val_if_fail (GTK_IS_WINDOW (parent), NULL);
126         g_return_val_if_fail (TNY_IS_FOLDER (folder), NULL);
127
128         dialog = (ModestDetailsDialog *) (g_object_new (MODEST_TYPE_DETAILS_DIALOG, 
129                                                         "transient-for", parent, 
130                                                         NULL));
131
132         MODEST_DETAILS_DIALOG_GET_CLASS (dialog)->set_folder_func (dialog, folder);
133
134         return GTK_WIDGET (dialog);
135 }
136
137 void
138 modest_details_dialog_add_data (ModestDetailsDialog *self,
139                                 const gchar *label,
140                                 const gchar *value)
141 {
142         MODEST_DETAILS_DIALOG_GET_CLASS (self)->add_data_func (self, label, value);
143 }
144
145 static void
146 modest_details_dialog_add_data_default (ModestDetailsDialog *self,
147                                         const gchar *label,
148                                         const gchar *value)
149 {
150         ModestDetailsDialogPrivate *priv;
151         guint n_rows = 0;
152         GtkWidget *label_w, *value_w;
153
154         priv = MODEST_DETAILS_DIALOG_GET_PRIVATE (self);
155
156         g_object_get (G_OBJECT (priv->props_table), "n-rows", &n_rows,NULL);
157
158         /* Create label */
159         label_w = gtk_label_new (label);
160         gtk_misc_set_alignment (GTK_MISC (label_w), 1.0, 0.0);
161         gtk_label_set_justify (GTK_LABEL (label_w), GTK_JUSTIFY_RIGHT);
162
163         /* Create value */
164         value_w = gtk_label_new (value);
165         gtk_label_set_line_wrap (GTK_LABEL (value_w), TRUE);
166         gtk_label_set_line_wrap_mode (GTK_LABEL (value_w), PANGO_WRAP_WORD_CHAR);
167         gtk_misc_set_alignment (GTK_MISC (value_w), 0.0, 0.0);
168         gtk_label_set_justify (GTK_LABEL (value_w), GTK_JUSTIFY_LEFT);
169
170         /* Attach label and value */
171         gtk_table_attach (GTK_TABLE (priv->props_table), 
172                           label_w, 0, 1, 
173                           n_rows, n_rows + 1, 
174                           GTK_SHRINK|GTK_FILL, 
175                           GTK_SHRINK|GTK_FILL, 
176                           0, 0);
177         gtk_table_attach (GTK_TABLE (priv->props_table), 
178                           value_w, 1, 2, 
179                           n_rows, n_rows + 1, 
180                           GTK_EXPAND|GTK_FILL, 
181                           GTK_SHRINK|GTK_FILL, 
182                           0, 0);
183 }
184
185
186 static void
187 modest_details_dialog_set_header_default (ModestDetailsDialog *self,
188                                           TnyHeader *header)
189 {
190         gchar *from, *subject, *to, *cc;
191         time_t received, sent;
192         guint size;
193         gchar *size_s;
194         TnyFolder *folder;
195         TnyFolderType folder_type;
196 #define DATE_TIME_BUFFER_SIZE 128
197         gchar date_time_buffer [DATE_TIME_BUFFER_SIZE];
198         
199         /* Set window title & Add close button */
200         gtk_window_set_title (GTK_WINDOW (self), _("mcen_ti_message_properties"));
201         gtk_dialog_add_button (GTK_DIALOG (self), _("mcen_bd_close"), GTK_RESPONSE_CLOSE);
202
203         folder = tny_header_get_folder (header);
204         folder_type = modest_tny_folder_guess_folder_type (folder);
205         g_object_unref (folder);
206
207         g_return_if_fail (folder_type != TNY_FOLDER_TYPE_INVALID);
208         
209         /* Get header data */
210         from = g_strdup (tny_header_get_from (header));
211         to = g_strdup (tny_header_get_to (header));
212         subject = g_strdup (tny_header_get_subject (header));
213         cc = g_strdup (tny_header_get_cc (header));
214         received = tny_header_get_date_received (header);
215         sent = tny_header_get_date_sent (header);
216         size = tny_header_get_message_size (header);
217
218         if (from == NULL)
219                 from = g_strdup ("");
220         if (to == NULL)
221                 to = g_strdup ("");
222         if (subject == NULL)
223                 subject = g_strdup ("");
224         if (cc == NULL)
225                 cc = g_strdup ("");
226
227         /* Add from and subject for all folders */
228         modest_details_dialog_add_data (self, _("mcen_fi_message_properties_from"), from);
229         modest_details_dialog_add_data (self, _("mcen_fi_message_properties_subject"), subject);
230
231
232         /* for inbox, user-created folders and archive: Received */
233         if (received && (folder_type != TNY_FOLDER_TYPE_SENT) &&
234             (folder_type != TNY_FOLDER_TYPE_DRAFTS) &&
235             (folder_type != TNY_FOLDER_TYPE_OUTBOX)) {
236                 
237                 modest_text_utils_strftime (date_time_buffer, DATE_TIME_BUFFER_SIZE, "%x %X",
238                                             received);
239                 modest_details_dialog_add_data (self, _("mcen_fi_message_properties_received"),
240                                                 date_time_buffer);
241         }
242
243         /* for outbox, drafts: Modified: (_created) */
244         if ((folder_type == TNY_FOLDER_TYPE_DRAFTS) ||
245             (folder_type == TNY_FOLDER_TYPE_OUTBOX) ||
246             (folder_type == TNY_FOLDER_TYPE_SENT)) {
247                 modest_text_utils_strftime (date_time_buffer, DATE_TIME_BUFFER_SIZE, "%x %X",
248                                             received);
249                 modest_details_dialog_add_data (self, _("mcen_fi_message_properties_created"),
250                                                 date_time_buffer);
251         }
252
253         /* for everyting except outbox, drafts: Sent */
254         if (sent && (folder_type != TNY_FOLDER_TYPE_DRAFTS)&&
255             (folder_type != TNY_FOLDER_TYPE_OUTBOX)) {
256                 
257                 modest_text_utils_strftime (date_time_buffer, DATE_TIME_BUFFER_SIZE, "%x %X",
258                                             sent);
259                 modest_details_dialog_add_data (self, _("mcen_fi_message_properties_sent"),
260                                                 date_time_buffer);
261         }
262         
263         /* Set To and CC */
264         modest_details_dialog_add_data (self, _("mcen_fi_message_properties_to"), to);
265
266         /* only show cc when it's there */
267         if (cc && strlen(cc) > 0)
268                 modest_details_dialog_add_data (self, _("mcen_fi_message_properties_cc"), cc);
269
270         /* Set size */
271         if (size <= 0)
272                 size_s = g_strdup (_("mcen_va_message_properties_size_noinfo"));
273         else
274                 size_s = modest_text_utils_get_display_size (size);
275         modest_details_dialog_add_data (self, _("mcen_fi_message_properties_size"), size_s);
276         g_free (size_s);
277
278         /* Frees */
279         g_free (to);
280         g_free (from);
281         g_free (subject);
282         g_free (cc);
283 }
284
285 static void
286 modest_details_dialog_set_folder_default (ModestDetailsDialog *self,
287                                           TnyFolder *folder)
288 {
289         gchar *count_s, *size_s, *name = NULL;
290         gchar *tmp = NULL;
291         gint size, count;
292
293         g_return_if_fail (folder && TNY_IS_FOLDER (folder));
294         g_return_if_fail (modest_tny_folder_guess_folder_type (folder)
295                           != TNY_FOLDER_TYPE_INVALID);
296         
297         /* Set window title */
298         gtk_window_set_title (GTK_WINDOW (self), _("mcen_ti_folder_properties"));
299         gtk_dialog_add_button (GTK_DIALOG (self), _("mcen_bd_close"), GTK_RESPONSE_CLOSE);
300
301         /* Get data. We use our function because it's recursive */
302         if (TNY_IS_FOLDER (folder)) {
303                 count = tny_folder_get_all_count (TNY_FOLDER (folder));
304                 size = tny_folder_get_local_size (TNY_FOLDER (folder));
305         } else {
306                 count = modest_tny_folder_store_get_message_count (TNY_FOLDER_STORE (folder));
307                 size = modest_tny_folder_store_get_local_size (TNY_FOLDER_STORE (folder));
308         }
309
310         /* Format count and size */
311         count_s = g_strdup_printf ("%d", count);
312         if (size <= 0)
313                 size_s = g_strdup (_("mcen_va_message_properties_size_noinfo"));
314         else
315                 size_s = modest_text_utils_get_display_size (size);
316
317         /* Different names for the local folders */
318         if (modest_tny_folder_is_local_folder (folder)) {
319                 gint type = modest_tny_folder_get_local_or_mmc_folder_type (folder);
320                 if (type != TNY_FOLDER_TYPE_UNKNOWN)
321                         name = g_strdup(modest_local_folder_info_get_type_display_name (type));
322         } 
323
324         if (!name)      
325                 name = g_strdup (tny_folder_get_name (folder));
326
327         tmp = g_strconcat (_("mcen_fi_folder_properties_foldername"), ":", NULL);
328         modest_details_dialog_add_data (self, tmp, name);
329         g_free (tmp);
330
331         tmp = g_strconcat (_("mcen_fi_folder_properties_messages"), ":", NULL);
332         modest_details_dialog_add_data (self, tmp, count_s);
333         g_free (tmp);
334
335         tmp = g_strconcat (_("mcen_fi_folder_properties_size"), ":", NULL);
336         modest_details_dialog_add_data (self, tmp, size_s);
337         g_free (tmp);
338
339         /* Frees */
340         g_free (name);
341         g_free (size_s);
342         g_free (count_s);
343 }
344
345 static gboolean
346 on_key_press_event (GtkWindow *window, GdkEventKey *event, gpointer userdata)
347 {
348         GtkWidget *focused;
349
350         focused = gtk_window_get_focus (window);
351         if (GTK_IS_SCROLLED_WINDOW (focused)) {
352                 GtkAdjustment *vadj;
353                 gboolean return_value;
354
355                 vadj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (focused));
356                 switch (event->keyval) {
357                 case GDK_Up:
358                         if (vadj->value > 0.0) {
359                                 g_signal_emit_by_name (G_OBJECT (focused), "scroll-child", GTK_SCROLL_STEP_UP, FALSE, 
360                                                        &return_value);
361                                 return TRUE;
362                         }
363                         break;
364                 case GDK_Down:
365                         if (vadj->value < vadj->upper - vadj->page_size) {
366                                 g_signal_emit_by_name (G_OBJECT (focused), "scroll-child", GTK_SCROLL_STEP_DOWN, FALSE, 
367                                                        &return_value);
368                                 return TRUE;
369                         }
370                         break;
371                 }
372         }
373
374         return FALSE;
375 }
376
377 static void
378 modest_details_dialog_create_container_default (ModestDetailsDialog *self)
379 {
380         ModestDetailsDialogPrivate *priv;
381         GtkWidget *scrollbar;
382
383         priv = MODEST_DETAILS_DIALOG_GET_PRIVATE (self);
384         scrollbar = gtk_scrolled_window_new (NULL, NULL);
385
386         gtk_window_set_default_size (GTK_WINDOW (self), 400, 220);
387
388         priv->props_table = gtk_table_new (0, 2, FALSE);
389         gtk_table_set_col_spacings (GTK_TABLE (priv->props_table), 12);
390         gtk_table_set_row_spacings (GTK_TABLE (priv->props_table), 1);
391         gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (scrollbar), priv->props_table);
392         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrollbar), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
393         gtk_container_set_focus_vadjustment (GTK_CONTAINER (priv->props_table), 
394                                              gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (scrollbar)));
395         gtk_container_add (GTK_CONTAINER (GTK_DIALOG (self)->vbox), scrollbar);
396
397         gtk_dialog_set_has_separator (GTK_DIALOG (self), FALSE);
398
399         g_signal_connect (self, "key-press-event", G_CALLBACK (on_key_press_event), self);
400 }