thread safety
[hildon] / hildon-widgets / hildon-add-home-dialog.c
1 /*
2  * This file is part of hildon-libs
3  *
4  * Copyright (C) 2005 Nokia Corporation.
5  *
6  * Contact: Luc Pionchon <luc.pionchon@nokia.com>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation; either version 2.1 of
11  * the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21  * 02110-1301 USA
22  *
23  */
24
25 #include <glib.h>
26 #include <gtk/gtk.h>
27 #include <gdk/gdkkeysyms.h>
28 #include <errno.h>
29 #include <string.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <unistd.h>
33 #include <stdio.h>
34
35 #ifdef HAVE_CONFIG_H
36 #include <config.h>
37 #endif
38
39 #include <libintl.h>
40
41 #include "hildon-add-home-dialog.h"
42 #include <hildon-widgets/hildon-caption.h>
43
44 #define _(String) dgettext(PACKAGE, String)
45 #define MAX_ERR_MSG 256
46 #define HILDON_ADD_HOME_DIALOG_WIDTH 370
47 #define HILDON_ADD_HOME_DIALOG_HEIGHT 100
48 #define HILDON_MAX_TITLE_LENGTH 256
49 #define HILDON_HOME_MAX_SHORTCUT_LEN 255
50
51 #define HILDON_ADD_HOME_DIALOG_GET_PRIVATE(obj) \
52   (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
53                                 HILDON_TYPE_ADD_HOME_DIALOG, \
54                                 HildonAddHomeDialogPrivate));
55
56 typedef struct _HildonAddHomeDialogPrivate HildonAddHomeDialogPrivate;
57
58 static GtkDialogClass *parent_class;
59
60 static void
61 hildon_add_home_dialog_class_init(HildonAddHomeDialogClass * class);
62 static void hildon_add_home_dialog_init(HildonAddHomeDialog * dialog);
63
64 /* private struct */
65
66 struct _HildonAddHomeDialogPrivate {
67     GtkWidget *desc_label;
68     GtkWidget *name_entry;
69     GtkWidget *new_name_entry;
70     GtkWidget *okButton;
71     GtkWidget *cancelButton;
72     gboolean isrename;
73 };
74
75 /* Private functions */
76
77 static void
78 hildon_add_home_dialog_class_init(HildonAddHomeDialogClass * class)
79 {
80     parent_class = g_type_class_peek_parent(class);
81     g_type_class_add_private(class, sizeof(HildonAddHomeDialogPrivate));
82 }
83
84 static void hildon_add_home_dialog_init(HildonAddHomeDialog * dialog)
85 {
86     HildonAddHomeDialogPrivate *priv;
87
88     gtk_dialog_set_has_separator(GTK_DIALOG(dialog), FALSE);
89
90     priv = HILDON_ADD_HOME_DIALOG_GET_PRIVATE(dialog);
91     priv->name_entry = NULL;
92     priv->new_name_entry = NULL;
93     priv->isrename = FALSE;
94
95     priv->okButton = gtk_dialog_add_button(GTK_DIALOG(dialog),
96                                            "addtoHome_button_ok",
97                                            GTK_RESPONSE_OK);
98     priv->cancelButton = gtk_dialog_add_button(GTK_DIALOG(dialog),
99                                                "addtoHome_button_cancel",
100                                                GTK_RESPONSE_CANCEL);
101
102     gtk_window_resize(GTK_WINDOW(dialog),
103                       HILDON_ADD_HOME_DIALOG_WIDTH,
104                       HILDON_ADD_HOME_DIALOG_HEIGHT);
105
106     gtk_widget_show_all(GTK_DIALOG(dialog)->vbox);
107     gtk_widget_show_all(GTK_DIALOG(dialog)->action_area);
108 }
109
110 /* Public functions */
111
112 GType hildon_add_home_dialog_get_type(void)
113 {
114     static GType dialog_type = 0;
115
116     if (!dialog_type) {
117         static const GTypeInfo dialog_info = {
118             sizeof(HildonAddHomeDialogClass),
119             NULL,       /* base_init */
120             NULL,       /* base_finalize */
121             (GClassInitFunc) hildon_add_home_dialog_class_init,
122             NULL,       /* class_finalize */
123             NULL,       /* class_data */
124             sizeof(HildonAddHomeDialog),
125             0,  /* n_preallocs */
126             (GInstanceInitFunc) hildon_add_home_dialog_init
127         };
128
129         dialog_type = g_type_register_static(GTK_TYPE_DIALOG,
130                                              "HildonAddHomeDialog",
131                                              &dialog_info, 0);
132     }
133     return dialog_type;
134 }
135
136 /**
137  * hildon_add_home_dialog_new:
138  * @parent: parent window for the dialog
139  * @name: name to show in the entry (or label, if @new_name is not NULL)
140  * @new_name: name to show in the new name entry. If this is not NULL
141  * the widget acts as a RenameShortcutDialog.
142  *
143  * Creates a new Add to Home dialog or Rename Shortcut dialog.
144  *
145  * Returns: the new dialog.
146  */
147 GtkWidget *hildon_add_home_dialog_new(GtkWindow * parent,
148                                       const gchar * name,
149                                       const gchar * new_name)
150 {
151     HildonAddHomeDialog *dialog;
152     HildonAddHomeDialogPrivate *priv;
153     GtkWidget *caption;
154     GtkSizeGroup *size_group =
155         gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
156
157     dialog =
158         HILDON_ADD_HOME_DIALOG(g_object_new
159                                (HILDON_TYPE_ADD_HOME_DIALOG, NULL));
160     priv = HILDON_ADD_HOME_DIALOG_GET_PRIVATE(dialog);
161
162     if (parent)
163         gtk_window_set_transient_for(GTK_WINDOW(dialog), parent);
164
165     /* Set up the window title */
166     if (new_name != NULL) {
167         priv->isrename = TRUE;
168         gtk_window_set_title(GTK_WINDOW(dialog), _("ckdg_ti_rename_link"));
169     } else {
170         priv->isrename = FALSE;
171         gtk_window_set_title(GTK_WINDOW(dialog), "addtoHome_dialog_title");
172     }
173
174     /* add description text */
175     if (priv->isrename) {
176         priv->desc_label =
177             gtk_label_new(_("ckdg_ib_link_exists"));
178         gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox),
179                            priv->desc_label, FALSE, FALSE, 0);
180     }
181
182     /* add name entry (or label ) */
183     if (priv->isrename) {
184         GtkWidget *label = gtk_label_new((name) ? name : "");
185
186         priv->name_entry = gtk_hbox_new(FALSE, 0);
187         gtk_box_pack_start(GTK_BOX(priv->name_entry), label, FALSE, FALSE,
188                            0);
189     } else {
190         priv->name_entry = gtk_entry_new();
191         gtk_entry_set_text(GTK_ENTRY(priv->name_entry),
192                            (name) ? name : "");
193     }
194     caption = hildon_caption_new(size_group, "addtoHome_editor_caption",
195                                  priv->name_entry, NULL,
196                                  HILDON_CAPTION_OPTIONAL);
197     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), caption,
198                        FALSE, FALSE, 0);
199
200     /* add new name entry */
201     if (priv->isrename) {
202         priv->new_name_entry = gtk_entry_new();
203         gtk_entry_set_text(GTK_ENTRY(priv->new_name_entry), new_name);
204         caption = hildon_caption_new(size_group, _("ckdg_fi_rename_name"),
205                                      priv->new_name_entry, NULL,
206                                      HILDON_CAPTION_OPTIONAL);
207         gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), caption,
208                            FALSE, FALSE, 0);
209     }
210
211     /* highlight proper entry */
212     if (priv->isrename) {
213         gtk_widget_grab_focus(priv->new_name_entry);
214         gtk_editable_select_region(GTK_EDITABLE(priv->new_name_entry), 0,
215                                    -1);
216     } else {
217         gtk_widget_grab_focus(priv->name_entry);
218         gtk_editable_select_region(GTK_EDITABLE(priv->name_entry), 0, -1);
219     }
220
221     gtk_widget_show_all(GTK_DIALOG(dialog)->vbox);
222     gtk_widget_show_all(GTK_DIALOG(dialog)->action_area);
223
224     return GTK_WIDGET(dialog);
225 }
226
227 /**
228  * hildon_add_home_dialog_get_name:
229  * @dialog: the dialog
230  *
231  * Returns: the string the user has entered in the entry
232  */
233 const gchar *hildon_add_home_dialog_get_name(HildonAddHomeDialog * dialog)
234 {
235     HildonAddHomeDialogPrivate *priv;
236
237     g_return_val_if_fail(HILDON_IS_ADD_HOME_DIALOG(dialog), NULL);
238     priv = HILDON_ADD_HOME_DIALOG_GET_PRIVATE(dialog);
239
240     if (priv->isrename)
241         return gtk_entry_get_text(GTK_ENTRY(priv->new_name_entry));
242     else
243         return gtk_entry_get_text(GTK_ENTRY(priv->name_entry));
244 }