import SDK release
[hildon] / hildon-widgets / hildon-insert-object-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 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include <gtk/gtkmain.h>
30 #include <gdk/gdkkeysyms.h>
31 #include <gtk/gtkdialog.h>
32 #include <gtk/gtkbutton.h>
33 #include <gtk/gtklabel.h>
34 #include <gtk/gtkentry.h>
35 #include <gtk/gtkcombobox.h>
36 #include <gtk/gtksizegroup.h>
37 #include <gtk/gtkbox.h>
38 #include <string.h>
39
40 #include <libintl.h>
41
42 #include <hildon-insert-object-dialog.h>
43 #include <hildon-widgets/hildon-caption.h>
44
45 #define _(String) dgettext(PACKAGE, String)
46
47 #define MAX_ERR_MSG 256
48
49 #define HILDON_INSERT_OBJECT_DIALOG_WIDTH 300
50 #define HILDON_INSERT_OBJECT_DIALOG_HEIGHT 100
51
52 #define PROJECT_TEMPLATE_DIR "/tmp"
53 #define RUN_AT_TERMINATION 0
54
55
56 static GtkDialogClass *parent_class;
57
58 struct _HildonInsertObjectDialogPrivate {
59     GtkButton *insertBtn;
60     GtkButton *existingBtn;
61     GtkButton *cancelBtn;
62     GtkEntry *entry;
63     GtkLabel *label;
64     GtkComboBox *combo;
65     GtkSizeGroup *group;
66
67     HildonCaption *comboCap;
68     HildonCaption *entryCap;
69     HildonCaption *locationCap;
70
71     GnomeVFSDirectoryHandle *dir;
72     char *mimetype;
73
74     guint keysnooper;
75 };
76
77 #define HILDON_INSERT_OBJECT_DIALOG_GET_PRIVATE(o)  \
78    (G_TYPE_INSTANCE_GET_PRIVATE ((o), \
79       HILDON_TYPE_INSERT_OBJECT_DIALOG, \
80       HildonInsertObjectDialogPrivate) )
81
82 static void
83 hildon_insert_object_dialog_class_init(HildonInsertObjectDialogClass *
84                                        class);
85 static void hildon_insert_object_dialog_init(HildonInsertObjectDialog *
86                                              dialog);
87 static void hildon_insert_object_dialog_finalize(GObject * obj_self);
88 static gint hildon_insert_object_dialog_key_snooper(GtkWidget * gadget,
89                                                     GdkEventKey * event,
90                                                     GtkWidget * widget);
91 static gboolean hildon_insert_object_foo(gpointer data)
92 {
93     gnome_vfs_shutdown();
94     g_message("GnomeVFS shutdown succeed!\n");
95     return FALSE;
96 }
97
98 /* Private functions */
99 static void
100 hildon_insert_object_dialog_class_init(HildonInsertObjectDialogClass *
101                                        class)
102 {
103
104     GObjectClass *object_class = G_OBJECT_CLASS(class);
105
106     parent_class = g_type_class_peek_parent(class);
107     g_type_class_add_private(class,
108                              sizeof(HildonInsertObjectDialogPrivate));
109
110     object_class->finalize = hildon_insert_object_dialog_finalize;
111 }
112
113 static void hildon_insert_object_dialog_finalize(GObject * obj_self)
114 {
115     HildonInsertObjectDialogPrivate *priv;
116     HildonInsertObjectDialog *dlg;
117
118     g_return_if_fail(HILDON_IS_INSERT_OBJECT_DIALOG(obj_self));
119
120     dlg = HILDON_INSERT_OBJECT_DIALOG(obj_self);
121     priv = HILDON_INSERT_OBJECT_DIALOG_GET_PRIVATE(dlg);
122
123     gtk_key_snooper_remove(priv->keysnooper);
124
125     if (G_OBJECT_CLASS(parent_class)->finalize)
126         G_OBJECT_CLASS(parent_class)->finalize(obj_self);
127 }
128
129
130 static void
131 hildon_insert_object_dialog_init(HildonInsertObjectDialog * dialog)
132 {
133
134     GnomeVFSResult result;
135     gchar *dir_path = "";
136     GnomeVFSFileInfo *file = gnome_vfs_file_info_new();
137     GList *mimelist = NULL;
138     GtkTreeModel *combo_tree = NULL;
139     GtkTreeIter combo_iter;
140     gboolean valid = FALSE;
141
142
143     dialog->priv = HILDON_INSERT_OBJECT_DIALOG_GET_PRIVATE(dialog);
144     gtk_dialog_set_has_separator(GTK_DIALOG(dialog), FALSE);
145
146     gtk_window_set_title(GTK_WINDOW(dialog), _("ckdg_ti_insert_insert_objec"));
147
148     dialog->priv->group =
149         GTK_SIZE_GROUP(gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL));
150
151     dialog->priv->insertBtn =
152         GTK_BUTTON(gtk_dialog_add_button(GTK_DIALOG(dialog),
153                                          _("ckdg_bd_insert_new"),
154                                          GTK_RESPONSE_OK));
155
156     dialog->priv->existingBtn =
157         GTK_BUTTON(gtk_dialog_add_button
158                    (GTK_DIALOG(dialog), _("ckdg_bd_insert_existing"),
159                     HILDON_RESPONSE_INSERT_EXISTING));
160
161     dialog->priv->cancelBtn =
162         GTK_BUTTON(gtk_dialog_add_button(GTK_DIALOG(dialog),
163                                          _("ckdg_bd_insert_cancel"),
164                                          GTK_RESPONSE_CANCEL));
165
166     dialog->priv->label = GTK_LABEL(gtk_label_new(NULL));
167     dialog->priv->locationCap =
168         HILDON_CAPTION(hildon_caption_new(dialog->priv->group,
169                                           _("Location"),
170                                           GTK_WIDGET(dialog->priv->label),
171                                           NULL, HILDON_CAPTION_OPTIONAL));
172     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox),
173                        GTK_WIDGET(dialog->priv->locationCap), FALSE, FALSE,
174                        0);
175
176     dialog->priv->entry = GTK_ENTRY(gtk_entry_new());
177     dialog->priv->entryCap =
178         HILDON_CAPTION(hildon_caption_new(dialog->priv->group,
179                                           _("Name"),
180                                           GTK_WIDGET(dialog->priv->entry),
181                                           NULL, HILDON_CAPTION_OPTIONAL));
182
183     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox),
184                        GTK_WIDGET(dialog->priv->entryCap), FALSE, FALSE,
185                        0);
186
187     dialog->priv->combo = GTK_COMBO_BOX(gtk_combo_box_new_text());
188
189     dialog->priv->comboCap =
190         HILDON_CAPTION(hildon_caption_new(dialog->priv->group,
191                                           _("Mime type"),
192                                           GTK_WIDGET(dialog->priv->combo),
193                                           NULL, HILDON_CAPTION_OPTIONAL));
194
195     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox),
196                        GTK_WIDGET(dialog->priv->comboCap), FALSE, FALSE,
197                        0);
198
199     dialog->priv->mimetype = (char *) GNOME_VFS_MIME_TYPE_UNKNOWN;
200
201     /* Initialize gnome-vfs */
202     if (gnome_vfs_initialized() == FALSE) {
203         gtk_quit_add(RUN_AT_TERMINATION, hildon_insert_object_foo, NULL);
204
205         if (gnome_vfs_init() == FALSE) {
206             g_error("GnomeVFS can not be run");
207             return;
208         } else
209             g_message("GnomeVFS init!");
210     } else
211         g_message("GnomeVFS initialized!");
212
213
214     /* Read registered mime types into combobox */
215     mimelist = gnome_vfs_get_registered_mime_types();
216
217     while (mimelist) {
218         if (gnome_vfs_mime_get_description((char *) (mimelist->data)) !=
219             NULL)
220             gtk_combo_box_append_text
221                 (dialog->priv->combo,
222                  gnome_vfs_mime_get_description
223                      ((char *) (mimelist->data)));
224         else
225             gtk_combo_box_append_text(dialog->priv->combo,
226                                       (char *) (mimelist->data));
227
228         mimelist = mimelist->next;
229     }
230
231     gnome_vfs_mime_registered_mime_type_list_free(mimelist);
232
233     /* Read directory path into label and first file into entry */
234     /* dir_path = g_strconcat( (const char *)g_get_current_dir(),
235        PROJECT_TEMPLATE_DIR, NULL); */
236     dir_path = g_strconcat(g_getenv((const char *) "PWD"),
237                            PROJECT_TEMPLATE_DIR, NULL);
238
239     result = gnome_vfs_directory_open(&(dialog->priv->dir), dir_path,
240                                       GNOME_VFS_FILE_INFO_DEFAULT |
241                                       GNOME_VFS_FILE_INFO_GET_MIME_TYPE);
242     if (result != GNOME_VFS_OK) {
243         g_warning("Could not read project templates directory\n");
244         gtk_label_set_label(dialog->priv->label,
245                             "Could not read project templates directory");
246         return;
247     } else {
248         gtk_label_set_label(dialog->priv->label, (gchar *) dir_path);
249
250         while (gnome_vfs_directory_read_next(dialog->priv->dir, file) ==
251                GNOME_VFS_OK) {
252             if (file->name != NULL
253                 && file->type == GNOME_VFS_FILE_TYPE_REGULAR) {
254                 gtk_entry_set_text(dialog->priv->entry,
255                                    (gchar *) file->name);
256
257                 combo_tree = gtk_combo_box_get_model(dialog->priv->combo);
258                 valid =
259                     gtk_tree_model_get_iter_first(combo_tree, &combo_iter);
260
261                 while (valid) {
262                     gchar *str_data;
263
264                     gtk_tree_model_get(combo_tree, &combo_iter, 0,
265                                        &str_data, -1);
266
267                     if (strcmp(str_data,
268                                gnome_vfs_mime_get_description
269                                ((char *) file->mime_type)) == 0) {
270                         dialog->priv->mimetype = (char *) file->mime_type;
271                         gtk_combo_box_set_active_iter(dialog->priv->combo,
272                                                       &combo_iter);
273                         g_free(str_data);
274                         break;
275                     }
276                     g_free(str_data);
277                     valid =
278                         gtk_tree_model_iter_next(combo_tree, &combo_iter);
279                 }
280
281                 break;
282             }
283
284         }
285     }
286
287
288     if (gnome_vfs_directory_close(dialog->priv->dir) != GNOME_VFS_OK)
289         g_warning("Could not close the project templates directory\n");
290     else
291         g_message("Gnome VFS dir closed \n");
292
293     g_free(dir_path);
294
295     dialog->priv->keysnooper =
296         (guint) (gtk_key_snooper_install
297                  ((GtkKeySnoopFunc)
298                   (hildon_insert_object_dialog_key_snooper),
299                   GTK_WIDGET(dialog)));
300
301     gtk_window_resize(GTK_WINDOW(dialog),
302                       HILDON_INSERT_OBJECT_DIALOG_WIDTH,
303                       HILDON_INSERT_OBJECT_DIALOG_HEIGHT);
304
305     gtk_widget_show_all(GTK_DIALOG(dialog)->vbox);
306     gtk_widget_show_all(GTK_DIALOG(dialog)->action_area);
307
308     gnome_vfs_file_info_unref(file);
309
310 }
311
312 static gint hildon_insert_object_dialog_key_snooper(GtkWidget * gadget,
313                                                     GdkEventKey * event,
314                                                     GtkWidget * widget)
315 {
316     if (event->type == GDK_KEY_RELEASE) {
317         switch (event->keyval) {
318         case GDK_I:
319         case GDK_i:
320             gtk_dialog_response(GTK_DIALOG(widget),
321                                 HILDON_RESPONSE_INSERT_EXISTING);
322             break;
323             /* case GDK_C: case GDK_c: gtk_dialog_response(
324                GTK_DIALOG(widget), GTK_RESPONSE_CANCEL ); break; */
325         }
326     }
327     return FALSE;
328 }
329
330
331 /* Public functions */
332 GType hildon_insert_object_dialog_get_type(void)
333 {
334     static GType dialog_type = 0;
335
336     if (!dialog_type) {
337         static const GTypeInfo dialog_info = {
338             sizeof(HildonInsertObjectDialogClass),
339             NULL,       /* base_init */
340             NULL,       /* base_finalize */
341             (GClassInitFunc) hildon_insert_object_dialog_class_init,
342             NULL,       /* class_finalize */
343             NULL,       /* class_data */
344             sizeof(HildonInsertObjectDialog),
345             0,  /* n_preallocs */
346             (GInstanceInitFunc) hildon_insert_object_dialog_init
347         };
348
349         dialog_type = g_type_register_static(GTK_TYPE_DIALOG,
350                                              "HildonInsertObjectDialog",
351                                              &dialog_info, 0);
352     }
353     return dialog_type;
354
355 }
356
357 /**
358  * hildon_insert_object_dialog_new:
359  * @parent: the parent window of the dialog.
360  *
361  * Creates a new #HildonInsertObjectDialog widget.
362  *
363  * Return value: the newly created #HildonInsertObjectDialog
364  */
365 GtkWidget *hildon_insert_object_dialog_new(GtkWindow * parent)
366 {
367     GtkWidget *self = g_object_new(HILDON_TYPE_INSERT_OBJECT_DIALOG, NULL);
368
369     if (parent)
370         gtk_window_set_transient_for(GTK_WINDOW(self), parent);
371
372     return GTK_WIDGET(self);
373 }
374
375 /**
376  * hildon_insert_object_dialog_get_name:
377  * @dialog: the dialog.
378  *
379  * Gets the text in name field.
380  *
381  * Return value: a pointer to the name string.
382  */
383 const gchar *hildon_insert_object_dialog_get_name(HildonInsertObjectDialog
384                                                   * dialog)
385 {
386
387     g_return_val_if_fail(HILDON_IS_INSERT_OBJECT_DIALOG(dialog), NULL);
388
389     return gtk_entry_get_text(dialog->priv->entry);
390 }
391
392 /**
393  * hildon_insert_object_dialog_get_mime_type:
394  * @dialog: the dialog.
395  *
396  * Gets the mime type selected in the combobox.
397  *
398  * Return value: a pointer to the mime type string.
399  */
400 const gchar
401     *hildon_insert_object_dialog_get_mime_type(HildonInsertObjectDialog *
402                                                dialog)
403 {
404     /* GtkTreeIter iter; gchar *return_string; GtkTreeModel *tree_model;
405
406        tree_model = gtk_combo_box_get_model(dialog->priv->combo);
407
408        if(gtk_combo_box_get_active_iter(dialog->priv->combo,&iter)){
409        gtk_tree_model_get (tree_model, &iter, 0, &return_string, -1);
410        return return_string; } else { return NULL; } */
411
412
413     return dialog->priv->mimetype;
414 }