2 * This file is part of hildon-libs
4 * Copyright (C) 2005 Nokia Corporation.
6 * Contact: Luc Pionchon <luc.pionchon@nokia.com>
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.
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.
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
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>
42 #include <hildon-insert-object-dialog.h>
43 #include <hildon-widgets/hildon-caption.h>
45 #define _(String) dgettext(PACKAGE, String)
47 #define MAX_ERR_MSG 256
49 #define HILDON_INSERT_OBJECT_DIALOG_WIDTH 300
50 #define HILDON_INSERT_OBJECT_DIALOG_HEIGHT 100
52 #define PROJECT_TEMPLATE_DIR "/tmp"
53 #define RUN_AT_TERMINATION 0
56 static GtkDialogClass *parent_class;
58 struct _HildonInsertObjectDialogPrivate {
60 GtkButton *existingBtn;
67 HildonCaption *comboCap;
68 HildonCaption *entryCap;
69 HildonCaption *locationCap;
71 GnomeVFSDirectoryHandle *dir;
77 #define HILDON_INSERT_OBJECT_DIALOG_GET_PRIVATE(o) \
78 (G_TYPE_INSTANCE_GET_PRIVATE ((o), \
79 HILDON_TYPE_INSERT_OBJECT_DIALOG, \
80 HildonInsertObjectDialogPrivate) )
83 hildon_insert_object_dialog_class_init(HildonInsertObjectDialogClass *
85 static void hildon_insert_object_dialog_init(HildonInsertObjectDialog *
87 static void hildon_insert_object_dialog_finalize(GObject * obj_self);
88 static gint hildon_insert_object_dialog_key_snooper(GtkWidget * gadget,
91 static gboolean hildon_insert_object_foo(gpointer data)
94 g_message("GnomeVFS shutdown succeed!\n");
98 /* Private functions */
100 hildon_insert_object_dialog_class_init(HildonInsertObjectDialogClass *
104 GObjectClass *object_class = G_OBJECT_CLASS(class);
106 parent_class = g_type_class_peek_parent(class);
107 g_type_class_add_private(class,
108 sizeof(HildonInsertObjectDialogPrivate));
110 object_class->finalize = hildon_insert_object_dialog_finalize;
113 static void hildon_insert_object_dialog_finalize(GObject * obj_self)
115 HildonInsertObjectDialogPrivate *priv;
116 HildonInsertObjectDialog *dlg;
118 g_return_if_fail(HILDON_IS_INSERT_OBJECT_DIALOG(obj_self));
120 dlg = HILDON_INSERT_OBJECT_DIALOG(obj_self);
121 priv = HILDON_INSERT_OBJECT_DIALOG_GET_PRIVATE(dlg);
123 gtk_key_snooper_remove(priv->keysnooper);
125 if (G_OBJECT_CLASS(parent_class)->finalize)
126 G_OBJECT_CLASS(parent_class)->finalize(obj_self);
131 hildon_insert_object_dialog_init(HildonInsertObjectDialog * dialog)
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;
143 dialog->priv = HILDON_INSERT_OBJECT_DIALOG_GET_PRIVATE(dialog);
144 gtk_dialog_set_has_separator(GTK_DIALOG(dialog), FALSE);
146 gtk_window_set_title(GTK_WINDOW(dialog), _("ckdg_ti_insert_insert_objec"));
148 dialog->priv->group =
149 GTK_SIZE_GROUP(gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL));
151 dialog->priv->insertBtn =
152 GTK_BUTTON(gtk_dialog_add_button(GTK_DIALOG(dialog),
153 _("ckdg_bd_insert_new"),
156 dialog->priv->existingBtn =
157 GTK_BUTTON(gtk_dialog_add_button
158 (GTK_DIALOG(dialog), _("ckdg_bd_insert_existing"),
159 HILDON_RESPONSE_INSERT_EXISTING));
161 dialog->priv->cancelBtn =
162 GTK_BUTTON(gtk_dialog_add_button(GTK_DIALOG(dialog),
163 _("ckdg_bd_insert_cancel"),
164 GTK_RESPONSE_CANCEL));
166 dialog->priv->label = GTK_LABEL(gtk_label_new(NULL));
167 dialog->priv->locationCap =
168 HILDON_CAPTION(hildon_caption_new(dialog->priv->group,
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,
176 dialog->priv->entry = GTK_ENTRY(gtk_entry_new());
177 dialog->priv->entryCap =
178 HILDON_CAPTION(hildon_caption_new(dialog->priv->group,
180 GTK_WIDGET(dialog->priv->entry),
181 NULL, HILDON_CAPTION_OPTIONAL));
183 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox),
184 GTK_WIDGET(dialog->priv->entryCap), FALSE, FALSE,
187 dialog->priv->combo = GTK_COMBO_BOX(gtk_combo_box_new_text());
189 dialog->priv->comboCap =
190 HILDON_CAPTION(hildon_caption_new(dialog->priv->group,
192 GTK_WIDGET(dialog->priv->combo),
193 NULL, HILDON_CAPTION_OPTIONAL));
195 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox),
196 GTK_WIDGET(dialog->priv->comboCap), FALSE, FALSE,
199 dialog->priv->mimetype = (char *) GNOME_VFS_MIME_TYPE_UNKNOWN;
201 /* Initialize gnome-vfs */
202 if (gnome_vfs_initialized() == FALSE) {
203 gtk_quit_add(RUN_AT_TERMINATION, hildon_insert_object_foo, NULL);
205 if (gnome_vfs_init() == FALSE) {
206 g_error("GnomeVFS can not be run");
209 g_message("GnomeVFS init!");
211 g_message("GnomeVFS initialized!");
214 /* Read registered mime types into combobox */
215 mimelist = gnome_vfs_get_registered_mime_types();
218 if (gnome_vfs_mime_get_description((char *) (mimelist->data)) !=
220 gtk_combo_box_append_text
221 (dialog->priv->combo,
222 gnome_vfs_mime_get_description
223 ((char *) (mimelist->data)));
225 gtk_combo_box_append_text(dialog->priv->combo,
226 (char *) (mimelist->data));
228 mimelist = mimelist->next;
231 gnome_vfs_mime_registered_mime_type_list_free(mimelist);
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);
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");
248 gtk_label_set_label(dialog->priv->label, (gchar *) dir_path);
250 while (gnome_vfs_directory_read_next(dialog->priv->dir, file) ==
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);
257 combo_tree = gtk_combo_box_get_model(dialog->priv->combo);
259 gtk_tree_model_get_iter_first(combo_tree, &combo_iter);
264 gtk_tree_model_get(combo_tree, &combo_iter, 0,
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,
278 gtk_tree_model_iter_next(combo_tree, &combo_iter);
288 if (gnome_vfs_directory_close(dialog->priv->dir) != GNOME_VFS_OK)
289 g_warning("Could not close the project templates directory\n");
291 g_message("Gnome VFS dir closed \n");
295 dialog->priv->keysnooper =
296 (guint) (gtk_key_snooper_install
298 (hildon_insert_object_dialog_key_snooper),
299 GTK_WIDGET(dialog)));
301 gtk_window_resize(GTK_WINDOW(dialog),
302 HILDON_INSERT_OBJECT_DIALOG_WIDTH,
303 HILDON_INSERT_OBJECT_DIALOG_HEIGHT);
305 gtk_widget_show_all(GTK_DIALOG(dialog)->vbox);
306 gtk_widget_show_all(GTK_DIALOG(dialog)->action_area);
308 gnome_vfs_file_info_unref(file);
312 static gint hildon_insert_object_dialog_key_snooper(GtkWidget * gadget,
316 if (event->type == GDK_KEY_RELEASE) {
317 switch (event->keyval) {
320 gtk_dialog_response(GTK_DIALOG(widget),
321 HILDON_RESPONSE_INSERT_EXISTING);
323 /* case GDK_C: case GDK_c: gtk_dialog_response(
324 GTK_DIALOG(widget), GTK_RESPONSE_CANCEL ); break; */
331 /* Public functions */
332 GType hildon_insert_object_dialog_get_type(void)
334 static GType dialog_type = 0;
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),
346 (GInstanceInitFunc) hildon_insert_object_dialog_init
349 dialog_type = g_type_register_static(GTK_TYPE_DIALOG,
350 "HildonInsertObjectDialog",
358 * hildon_insert_object_dialog_new:
359 * @parent: the parent window of the dialog.
361 * Creates a new #HildonInsertObjectDialog widget.
363 * Return value: the newly created #HildonInsertObjectDialog
365 GtkWidget *hildon_insert_object_dialog_new(GtkWindow * parent)
367 GtkWidget *self = g_object_new(HILDON_TYPE_INSERT_OBJECT_DIALOG, NULL);
370 gtk_window_set_transient_for(GTK_WINDOW(self), parent);
372 return GTK_WIDGET(self);
376 * hildon_insert_object_dialog_get_name:
377 * @dialog: the dialog.
379 * Gets the text in name field.
381 * Return value: a pointer to the name string.
383 const gchar *hildon_insert_object_dialog_get_name(HildonInsertObjectDialog
387 g_return_val_if_fail(HILDON_IS_INSERT_OBJECT_DIALOG(dialog), NULL);
389 return gtk_entry_get_text(dialog->priv->entry);
393 * hildon_insert_object_dialog_get_mime_type:
394 * @dialog: the dialog.
396 * Gets the mime type selected in the combobox.
398 * Return value: a pointer to the mime type string.
401 *hildon_insert_object_dialog_get_mime_type(HildonInsertObjectDialog *
404 /* GtkTreeIter iter; gchar *return_string; GtkTreeModel *tree_model;
406 tree_model = gtk_combo_box_get_model(dialog->priv->combo);
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; } */
413 return dialog->priv->mimetype;