This is a manual merge of branch drop split view intro trunk.
[modest] / src / hildon2 / modest-signature-editor-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-signature-editor-dialog.h"
31 #include "widgets/modest-ui-constants.h"
32 #include "modest-hildon-includes.h"
33 #include "widgets/modest-validating-entry.h"
34 #include "modest-runtime.h"
35 #include <modest-account-mgr-helpers.h>
36 #include <gtk/gtkhbox.h>
37 #include <gtk/gtkvbox.h>
38 #include <gtk/gtktextview.h>
39 #include <gtk/gtklabel.h>
40 #include <hildon/hildon-pannable-area.h>
41 #include <gtk/gtkstock.h>
42 #include <glib/gi18n.h>
43 #include <modest-maemo-utils.h>
44 #include "modest-text-utils.h"
45 #include <hildon/hildon-text-view.h>
46
47 G_DEFINE_TYPE (ModestSignatureEditorDialog, modest_signature_editor_dialog, GTK_TYPE_DIALOG);
48
49 #define SIGNATURE_EDITOR_DIALOG_GET_PRIVATE(o) \
50         (G_TYPE_INSTANCE_GET_PRIVATE ((o), MODEST_TYPE_SIGNATURE_EDITOR_DIALOG, ModestSignatureEditorDialogPrivate))
51
52 typedef struct _ModestSignatureEditorDialogPrivate ModestSignatureEditorDialogPrivate;
53
54 struct _ModestSignatureEditorDialogPrivate
55 {
56         GtkWidget *checkbox_use;
57         GtkWidget *label;
58         GtkWidget *pannable;
59         GtkWidget *textview;
60 };
61
62 static void
63 modest_signature_editor_dialog_get_property (GObject *object, guint property_id,
64                                                                                                                         GValue *value, GParamSpec *pspec)
65 {
66         switch (property_id) {
67         default:
68                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
69         }
70 }
71
72 static void
73 modest_signature_editor_dialog_set_property (GObject *object, guint property_id,
74                                                                                                                         const GValue *value, GParamSpec *pspec)
75 {
76         switch (property_id) {
77         default:
78                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
79         }
80 }
81
82 static void
83 modest_signature_editor_dialog_dispose (GObject *object)
84 {
85         if (G_OBJECT_CLASS (modest_signature_editor_dialog_parent_class)->dispose)
86                 G_OBJECT_CLASS (modest_signature_editor_dialog_parent_class)->dispose (object);
87 }
88
89 static void
90 modest_signature_editor_dialog_finalize (GObject *object)
91 {
92         G_OBJECT_CLASS (modest_signature_editor_dialog_parent_class)->finalize (object);
93 }
94
95 static void
96 modest_signature_editor_dialog_class_init (ModestSignatureEditorDialogClass *klass)
97 {
98         GObjectClass *object_class = G_OBJECT_CLASS (klass);
99
100         g_type_class_add_private (klass, sizeof (ModestSignatureEditorDialogPrivate));
101
102         object_class->get_property = modest_signature_editor_dialog_get_property;
103         object_class->set_property = modest_signature_editor_dialog_set_property;
104         object_class->dispose = modest_signature_editor_dialog_dispose;
105         object_class->finalize = modest_signature_editor_dialog_finalize;
106 }
107
108 static void
109 enable_widgets (ModestSignatureEditorDialog *self)
110 {
111         ModestSignatureEditorDialogPrivate *priv = 
112                 SIGNATURE_EDITOR_DIALOG_GET_PRIVATE (self);
113                 
114         const gboolean enable = hildon_check_button_get_active (HILDON_CHECK_BUTTON (priv->checkbox_use));
115         gtk_widget_set_sensitive (priv->label, enable);
116         gtk_widget_set_sensitive (priv->pannable, enable);
117         gtk_text_view_set_editable (GTK_TEXT_VIEW (priv->textview), enable);
118 }
119
120 static void
121 on_toggle_button_changed (GtkToggleButton *togglebutton, gpointer user_data)
122 {
123         ModestSignatureEditorDialog *self = MODEST_SIGNATURE_EDITOR_DIALOG (user_data);
124         enable_widgets (self);
125 }
126
127 static void
128 modest_signature_editor_dialog_init (ModestSignatureEditorDialog *self)
129 {
130         ModestSignatureEditorDialogPrivate *priv = 
131                 SIGNATURE_EDITOR_DIALOG_GET_PRIVATE (self);
132         
133         gtk_window_set_title (GTK_WINDOW (self), _("mcen_ti_email_signatures_edit_title"));
134                 
135         GtkWidget *box = GTK_DIALOG(self)->vbox; /* gtk_vbox_new (FALSE, MODEST_MARGIN_HALF); */
136         gtk_container_set_border_width (GTK_CONTAINER (box), MODEST_MARGIN_HALF);
137
138         priv->checkbox_use = hildon_check_button_new (HILDON_SIZE_FINGER_HEIGHT);
139         gtk_button_set_label (GTK_BUTTON (priv->checkbox_use), 
140                               _("mcen_fi_email_signatures_use_signature"));
141         gtk_button_set_alignment (GTK_BUTTON (priv->checkbox_use), 0.0, 0.5);
142         gtk_box_pack_start (GTK_BOX (box), priv->checkbox_use, FALSE, FALSE, MODEST_MARGIN_HALF);
143         gtk_widget_show (priv->checkbox_use);
144         
145         g_signal_connect (G_OBJECT (priv->checkbox_use), "toggled",
146                           G_CALLBACK (on_toggle_button_changed), self);         
147         
148         priv->label = gtk_label_new (""); /* Set in modest_signature_editor_dialog_set_settings(). */
149         gtk_misc_set_alignment (GTK_MISC (priv->label), 0.0, 0.0);
150         gtk_box_pack_start (GTK_BOX (box), priv->label, FALSE, FALSE, MODEST_MARGIN_HALF);
151         gtk_widget_show (priv->label);
152         
153         priv->pannable = hildon_pannable_area_new ();
154         gtk_box_pack_start (GTK_BOX (box), priv->pannable, TRUE, TRUE, MODEST_MARGIN_HALF);
155         gtk_widget_show (priv->pannable);
156                 
157         priv->textview = hildon_text_view_new ();
158         gtk_container_add (GTK_CONTAINER (priv->pannable), priv->textview);
159         gtk_widget_show (priv->textview);
160         GtkTextBuffer *buffer = hildon_text_view_get_buffer (HILDON_TEXT_VIEW (priv->textview));
161         gtk_text_buffer_set_text (buffer, _("mcen_va_default_signature_tablet"), -1); /* Default, as per the UI spec. */
162         
163         /* Add the buttons: */
164         gtk_dialog_add_button (GTK_DIALOG (self), _HL("wdgt_bd_save"), GTK_RESPONSE_OK);
165         
166         gtk_widget_show (box);
167         gtk_widget_set_size_request (GTK_WIDGET (self), -1, 320);
168         
169         /* When this window is shown, hibernation should not be possible, 
170          * because there is no sensible way to save the state: */
171         modest_window_mgr_prevent_hibernation_while_window_is_shown (
172                 modest_runtime_get_window_mgr (), GTK_WINDOW (self)); 
173         
174 }
175
176 ModestSignatureEditorDialog*
177 modest_signature_editor_dialog_new (void)
178 {
179         return g_object_new (MODEST_TYPE_SIGNATURE_EDITOR_DIALOG, NULL);
180 }
181
182 void
183 modest_signature_editor_dialog_set_settings (
184         ModestSignatureEditorDialog *window, gboolean use_signature, const gchar* signature, 
185         const gchar* account_title)
186 {
187         ModestSignatureEditorDialogPrivate *priv = 
188                 SIGNATURE_EDITOR_DIALOG_GET_PRIVATE (window);
189
190         /* This causes a warning because of the %s in the translation, but not in the original string: */
191         gchar* label_text = g_strdup_printf (_("mcen_ia_email_signatures_edit_dlg_label"), 
192                 account_title);
193         gtk_label_set_text (GTK_LABEL (priv->label), label_text);
194         gtk_label_set_ellipsize (GTK_LABEL (priv->label),  PANGO_ELLIPSIZE_END);
195         g_free (label_text);
196         
197         hildon_check_button_set_active (HILDON_CHECK_BUTTON (priv->checkbox_use), use_signature);
198         
199         GtkTextBuffer *buffer = hildon_text_view_get_buffer (HILDON_TEXT_VIEW (priv->textview));
200         if (signature && signature[0] != '\0')
201                 gtk_text_buffer_set_text (buffer, signature, -1);
202         else
203                 gtk_text_buffer_set_text (buffer, _("mcen_va_default_signature_tablet"), -1); /* Default, as per the UI spec. */
204                 
205         enable_widgets (window);
206 }
207
208 /*
209  * The result must be freed with g_free(). */
210 gchar*
211 modest_signature_editor_dialog_get_settings (
212         ModestSignatureEditorDialog *window, gboolean* use_signature)
213 {
214         ModestSignatureEditorDialogPrivate *priv = 
215                 SIGNATURE_EDITOR_DIALOG_GET_PRIVATE (window);
216                 
217         g_assert(use_signature);
218         
219         *use_signature = hildon_check_button_get_active (HILDON_CHECK_BUTTON (priv->checkbox_use));
220                         
221         GtkTextBuffer *buffer = hildon_text_view_get_buffer (HILDON_TEXT_VIEW (priv->textview));
222         
223         GtkTextIter start, end;
224         gtk_text_buffer_get_bounds (buffer, &start, &end);
225         return gtk_text_buffer_get_text (buffer, &start, &end, TRUE);
226 }