1 /*
2  * import-format-dialog.c -- provides a UI to ask for users to resolve
3  *                           ambiguities.
4  *
5  * Created by:	Derek Atkins <derek@ihtfp.com>
6  * Copyright (c) 2003 Derek Atkins <warlord@MIT.EDU>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 2 of
11  * the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, contact:
20  *
21  * Free Software Foundation           Voice:  +1-617-542-5942
22  * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652
23  * Boston, MA  02110-1301,  USA       gnu@gnu.org
24  */
25 
26 #ifdef HAVE_CONFIG_H
27 #include <config.h>
28 #endif
29 
30 #include <gtk/gtk.h>
31 #include <glib/gi18n.h>
32 #include "dialog-utils.h"
33 #include "import-parse.h"
34 #include "gnc-ui-util.h"
35 
36 #define MAX_CHOICES 6
37 
38 static void
option_changed_cb(GtkWidget * widget,gpointer index_p)39 option_changed_cb (GtkWidget *widget, gpointer index_p)
40 {
41     gint *my_index = index_p;
42     *my_index = gtk_combo_box_get_active(GTK_COMBO_BOX(widget));
43 }
44 
45 
46 static GncImportFormat
add_menu_and_run_dialog(GtkWidget * dialog,GtkWidget * menu_box,GncImportFormat fmt)47 add_menu_and_run_dialog(GtkWidget *dialog, GtkWidget *menu_box, GncImportFormat fmt)
48 {
49     GtkComboBox  *combo;
50     GtkListStore *store;
51     GtkTreeIter iter;
52     GtkCellRenderer *cell;
53     gint index = 0, count = 0;
54     gint *index_p = &index;
55     GncImportFormat formats[MAX_CHOICES];
56 
57     store = gtk_list_store_new(1, G_TYPE_STRING);
58 
59     if (fmt & GNCIF_NUM_PERIOD)
60     {
61         gtk_list_store_append (store, &iter);
62         gtk_list_store_set (store, &iter, 0, _("Period: 123,456.78"), -1);
63         formats[count] = GNCIF_NUM_PERIOD;
64         count++;
65     }
66 
67     if (fmt & GNCIF_NUM_COMMA)
68     {
69         gtk_list_store_append (store, &iter);
70         gtk_list_store_set (store, &iter, 0, _("Comma: 123.456,78"), -1);
71         formats[count] = GNCIF_NUM_COMMA;
72         count++;
73     }
74 
75     if (fmt & GNCIF_DATE_MDY)
76     {
77         gtk_list_store_append (store, &iter);
78         gtk_list_store_set (store, &iter, 0, _("m/d/y"), -1);
79         formats[count] = GNCIF_DATE_MDY;
80         count++;
81     }
82 
83     if (fmt & GNCIF_DATE_DMY)
84     {
85         gtk_list_store_append (store, &iter);
86         gtk_list_store_set (store, &iter, 0, _("d/m/y"), -1);
87         formats[count] = GNCIF_DATE_DMY;
88         count++;
89     }
90 
91     if (fmt & GNCIF_DATE_YMD)
92     {
93         gtk_list_store_append (store, &iter);
94         gtk_list_store_set (store, &iter, 0, _("y/m/d"), -1);
95         formats[count] = GNCIF_DATE_YMD;
96         count++;
97     }
98 
99     if (fmt & GNCIF_DATE_YDM)
100     {
101         gtk_list_store_append (store, &iter);
102         gtk_list_store_set (store, &iter, 0, _("y/d/m"), -1);
103         formats[count] = GNCIF_DATE_YDM;
104         count++;
105     }
106 
107     g_assert(count > 1);
108 
109     combo = GTK_COMBO_BOX(gtk_combo_box_new_with_model(GTK_TREE_MODEL(store)));
110     g_object_unref(store);
111 
112     /* Create cell renderer. */
113     cell = gtk_cell_renderer_text_new();
114 
115     /* Pack it to the combo box. */
116     gtk_cell_layout_pack_start( GTK_CELL_LAYOUT( combo ), cell, FALSE );
117 
118     /* Connect renderer to data source */
119     gtk_cell_layout_set_attributes( GTK_CELL_LAYOUT( combo ), cell, "text", 0, NULL );
120 
121     g_signal_connect(G_OBJECT(combo), "changed",
122                      G_CALLBACK(option_changed_cb), index_p);
123 
124     gtk_box_pack_start(GTK_BOX(menu_box), GTK_WIDGET(combo), TRUE, TRUE, 0);
125 
126     gtk_widget_show_all(dialog);
127     gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
128     gtk_dialog_run(GTK_DIALOG(dialog));
129     gtk_widget_destroy(dialog);
130 
131     return formats[index];
132 }
133 
134 
135 GncImportFormat
gnc_import_choose_fmt(const char * msg,GncImportFormat fmts,gpointer data)136 gnc_import_choose_fmt(const char* msg, GncImportFormat fmts, gpointer data)
137 {
138     GtkBuilder *builder;
139     GtkWidget *dialog;
140     GtkWidget *widget;
141 
142     g_return_val_if_fail(fmts, FALSE);
143 
144     /* if there is only one format available, just return it */
145     if (!(fmts & (fmts - 1)))
146     {
147         return fmts;
148     }
149     /* Open the Glade Builder file */
150     builder = gtk_builder_new();
151     gnc_builder_add_from_file (builder, "dialog-import.glade", "format_picker_dialog");
152     dialog = GTK_WIDGET(gtk_builder_get_object (builder, "format_picker_dialog"));
153     widget = GTK_WIDGET(gtk_builder_get_object (builder, "msg_label"));
154     gtk_label_set_text(GTK_LABEL(widget), msg);
155 
156     widget = GTK_WIDGET(gtk_builder_get_object (builder, "menu_box"));
157 
158     g_object_unref(G_OBJECT(builder));
159 
160     return add_menu_and_run_dialog(dialog, widget, fmts);
161 }
162