inital gtk2 patch
[claws.git] / src / importpine.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2002 Match Grun
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 /*
21  * Import Pine address book data.
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #  include "config.h"
26 #endif
27
28 #include "defs.h"
29
30 #include <glib.h>
31 #include <gdk/gdkkeysyms.h>
32 #include <gtk/gtkwindow.h>
33 #include <gtk/gtksignal.h>
34 #include <gtk/gtklabel.h>
35 #include <gtk/gtkentry.h>
36 #include <gtk/gtktable.h>
37 #include <gtk/gtkbutton.h>
38
39 #include "intl.h"
40 #include "addrbook.h"
41 #include "addressbook.h"
42 #include "addressitem.h"
43 #include "gtkutils.h"
44 #include "prefs_common.h"
45 #include "manage_window.h"
46 #include "mgutils.h"
47 #include "pine.h"
48
49 #define IMPORTPINE_GUESS_NAME "Pine Import"
50
51 static struct _ImpPine_Dlg {
52         GtkWidget *window;
53         GtkWidget *file_entry;
54         GtkWidget *name_entry;
55         GtkWidget *ok_btn;
56         GtkWidget *cancel_btn;
57         GtkWidget *statusbar;
58         gint status_cid;
59 } imppine_dlg;
60
61 static struct _AddressFileSelection _imp_pine_file_selector_;
62 static AddressBookFile *_importedBook_;
63 static AddressIndex *_imp_addressIndex_;
64
65 /*
66 * Edit functions.
67 */
68 void imp_pine_status_show( gchar *msg ) {
69         if( imppine_dlg.statusbar != NULL ) {
70                 gtk_statusbar_pop( GTK_STATUSBAR(imppine_dlg.statusbar), imppine_dlg.status_cid );
71                 if( msg ) {
72                         gtk_statusbar_push( GTK_STATUSBAR(imppine_dlg.statusbar), imppine_dlg.status_cid, msg );
73                 }
74         }
75 }
76
77 static gboolean imp_pine_import_file( gchar *sName, gchar *sFile ) {
78         gboolean retVal = FALSE;
79         gchar *newFile;
80         AddressBookFile *abf = NULL;
81         PineFile *pdf = NULL;
82
83         if( _importedBook_ ) {
84                 addrbook_free_book( _importedBook_ );
85         }
86
87         abf = addrbook_create_book();
88         addrbook_set_path( abf, _imp_addressIndex_->filePath );
89         addrbook_set_name( abf, sName );
90         newFile = addrbook_guess_next_file( abf );
91         addrbook_set_file( abf, newFile );
92         g_free( newFile );
93
94         /* Import data from file */
95         pdf = pine_create();
96         pine_set_file( pdf, sFile );
97         if( pine_import_data( pdf, abf->addressCache ) == MGU_SUCCESS ) {
98                 addrbook_save_data( abf );
99                 _importedBook_ = abf;
100                 retVal = TRUE;
101         }
102         else {
103                 addrbook_free_book( abf );
104         }
105
106         return retVal;
107 }
108
109 static void imp_pine_ok( GtkWidget *widget, gboolean *cancelled ) {
110         gchar *sName;
111         gchar *sFile;
112         gchar *sMsg = NULL;
113         gboolean errFlag = FALSE;
114
115         sFile = gtk_editable_get_chars( GTK_EDITABLE(imppine_dlg.file_entry), 0, -1 );
116         g_strchug( sFile ); g_strchomp( sFile );
117         gtk_entry_set_text( GTK_ENTRY(imppine_dlg.file_entry), sFile );
118
119         sName = gtk_editable_get_chars( GTK_EDITABLE(imppine_dlg.name_entry), 0, -1 );
120         g_strchug( sName ); g_strchomp( sName );
121         gtk_entry_set_text( GTK_ENTRY(imppine_dlg.name_entry), sName );
122
123         if( *sFile == '\0'|| strlen( sFile ) < 1 ) {
124                 sMsg = _( "Please select a file." );
125                 errFlag = TRUE;
126         }
127
128         if( *sName == '\0'|| strlen( sName ) < 1 ) {
129                 if( ! errFlag ) sMsg = _( "Address book name must be supplied." );
130                 errFlag = TRUE;
131         }
132
133         if( errFlag ) {
134                 imp_pine_status_show( sMsg );
135         }
136         else {
137                 /* Import the file */
138                 if( imp_pine_import_file( sName, sFile ) ) {
139                         *cancelled = FALSE;
140                         gtk_main_quit();
141                 }
142                 else {
143                         imp_pine_status_show( _( "Error importing Pine file." ) );
144                 }
145         }
146
147         g_free( sFile );
148         g_free( sName );
149
150 }
151
152 static void imp_pine_cancel( GtkWidget *widget, gboolean *cancelled ) {
153         *cancelled = TRUE;
154         gtk_main_quit();
155 }
156
157 static void imp_pine_file_ok( GtkWidget *widget, gpointer data ) {
158         const gchar *sFile;
159         AddressFileSelection *afs;
160         GtkWidget *fileSel;
161
162         afs = ( AddressFileSelection * ) data;
163         fileSel = afs->fileSelector;
164         sFile = gtk_file_selection_get_filename( GTK_FILE_SELECTION(fileSel) );
165
166         afs->cancelled = FALSE;
167         gtk_entry_set_text( GTK_ENTRY(imppine_dlg.file_entry), sFile );
168         gtk_widget_hide( afs->fileSelector );
169         gtk_grab_remove( afs->fileSelector );
170         gtk_widget_grab_focus( imppine_dlg.file_entry );
171         imp_pine_status_show( _( "Please select a file to import." ) );
172 }
173
174 static void imp_pine_file_cancel( GtkWidget *widget, gpointer data ) {
175         AddressFileSelection *afs = ( AddressFileSelection * ) data;
176         afs->cancelled = TRUE;
177         gtk_widget_hide( afs->fileSelector );
178         gtk_grab_remove( afs->fileSelector );
179         gtk_widget_grab_focus( imppine_dlg.file_entry );
180 }
181
182 static void imp_pine_file_select_create( AddressFileSelection *afs ) {
183         GtkWidget *fileSelector;
184
185         fileSelector = gtk_file_selection_new( _("Select Pine File") );
186         gtk_file_selection_hide_fileop_buttons( GTK_FILE_SELECTION(fileSelector) );
187         gtk_signal_connect( GTK_OBJECT (GTK_FILE_SELECTION(fileSelector)->ok_button),
188                              "clicked", GTK_SIGNAL_FUNC (imp_pine_file_ok), ( gpointer ) afs );
189         gtk_signal_connect( GTK_OBJECT (GTK_FILE_SELECTION(fileSelector)->cancel_button),
190                              "clicked", GTK_SIGNAL_FUNC (imp_pine_file_cancel), ( gpointer ) afs );
191         afs->fileSelector = fileSelector;
192         afs->cancelled = TRUE;
193 }
194
195 static void imp_pine_file_select( void ) {
196         gchar *sFile;
197         if (! _imp_pine_file_selector_.fileSelector )
198                 imp_pine_file_select_create( & _imp_pine_file_selector_ );
199
200         sFile = gtk_editable_get_chars( GTK_EDITABLE(imppine_dlg.file_entry), 0, -1 );
201         gtk_file_selection_set_filename( GTK_FILE_SELECTION( _imp_pine_file_selector_.fileSelector ), sFile );
202         g_free( sFile );
203         gtk_widget_show( _imp_pine_file_selector_.fileSelector );
204         gtk_grab_add( _imp_pine_file_selector_.fileSelector );
205 }
206
207 static gint imp_pine_delete_event( GtkWidget *widget, GdkEventAny *event, gboolean *cancelled ) {
208         *cancelled = TRUE;
209         gtk_main_quit();
210         return TRUE;
211 }
212
213 static void imp_pine_key_pressed( GtkWidget *widget, GdkEventKey *event, gboolean *cancelled ) {
214         if (event && event->keyval == GDK_Escape) {
215                 *cancelled = TRUE;
216                 gtk_main_quit();
217         }
218 }
219
220 static void imp_pine_create( gboolean *cancelled ) {
221         GtkWidget *window;
222         GtkWidget *vbox;
223         GtkWidget *table;
224         GtkWidget *label;
225         GtkWidget *file_entry;
226         GtkWidget *name_entry;
227         GtkWidget *hbbox;
228         GtkWidget *hsep;
229         GtkWidget *ok_btn;
230         GtkWidget *cancel_btn;
231         GtkWidget *file_btn;
232         GtkWidget *statusbar;
233         GtkWidget *hsbox;
234         gint top;
235
236         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
237         gtk_widget_set_usize(window, 450, -1);
238         gtk_container_set_border_width( GTK_CONTAINER(window), 0 );
239         gtk_window_set_title( GTK_WINDOW(window), _("Import Pine file into Address Book") );
240         gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
241         gtk_window_set_modal(GTK_WINDOW(window), TRUE); 
242         gtk_signal_connect(GTK_OBJECT(window), "delete_event",
243                            GTK_SIGNAL_FUNC(imp_pine_delete_event),
244                            cancelled);
245         gtk_signal_connect(GTK_OBJECT(window), "key_press_event",
246                            GTK_SIGNAL_FUNC(imp_pine_key_pressed),
247                            cancelled);
248
249         vbox = gtk_vbox_new(FALSE, 8);
250         gtk_container_add(GTK_CONTAINER(window), vbox);
251         gtk_container_set_border_width( GTK_CONTAINER(vbox), 0 );
252
253         table = gtk_table_new(2, 3, FALSE);
254         gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
255         gtk_container_set_border_width( GTK_CONTAINER(table), 8 );
256         gtk_table_set_row_spacings(GTK_TABLE(table), 8);
257         gtk_table_set_col_spacings(GTK_TABLE(table), 8 );
258
259         /* First row */
260         top = 0;
261         label = gtk_label_new(_("Name"));
262         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
263         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
264
265         name_entry = gtk_entry_new();
266         gtk_table_attach(GTK_TABLE(table), name_entry, 1, 2, top, (top + 1), GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
267
268         /* Second row */
269         top = 1;
270         label = gtk_label_new(_("File"));
271         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
272         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
273
274         file_entry = gtk_entry_new();
275         gtk_table_attach(GTK_TABLE(table), file_entry, 1, 2, top, (top + 1), GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
276
277         file_btn = gtk_button_new_with_label( _(" ... "));
278         gtk_table_attach(GTK_TABLE(table), file_btn, 2, 3, top, (top + 1), GTK_FILL, 0, 3, 0);
279
280         /* Status line */
281         hsbox = gtk_hbox_new(FALSE, 0);
282         gtk_box_pack_end(GTK_BOX(vbox), hsbox, FALSE, FALSE, BORDER_WIDTH);
283         statusbar = gtk_statusbar_new();
284         gtk_box_pack_start(GTK_BOX(hsbox), statusbar, TRUE, TRUE, BORDER_WIDTH);
285
286         /* Button panel */
287         gtkut_button_set_create(&hbbox, &ok_btn, _("OK"),
288                                 &cancel_btn, _("Cancel"), NULL, NULL);
289         gtk_box_pack_end(GTK_BOX(vbox), hbbox, FALSE, FALSE, 0);
290         gtk_container_set_border_width( GTK_CONTAINER(hbbox), 0 );
291         gtk_widget_grab_default(ok_btn);
292
293         hsep = gtk_hseparator_new();
294         gtk_box_pack_end(GTK_BOX(vbox), hsep, FALSE, FALSE, 0);
295
296         gtk_signal_connect(GTK_OBJECT(ok_btn), "clicked",
297                            GTK_SIGNAL_FUNC(imp_pine_ok), cancelled);
298         gtk_signal_connect(GTK_OBJECT(cancel_btn), "clicked",
299                            GTK_SIGNAL_FUNC(imp_pine_cancel), cancelled);
300         gtk_signal_connect(GTK_OBJECT(file_btn), "clicked",
301                            GTK_SIGNAL_FUNC(imp_pine_file_select), NULL);
302
303         gtk_widget_show_all(vbox);
304
305         imppine_dlg.window     = window;
306         imppine_dlg.file_entry = file_entry;
307         imppine_dlg.name_entry = name_entry;
308         imppine_dlg.ok_btn     = ok_btn;
309         imppine_dlg.cancel_btn = cancel_btn;
310         imppine_dlg.statusbar  = statusbar;
311         imppine_dlg.status_cid = gtk_statusbar_get_context_id(
312                        GTK_STATUSBAR(statusbar), "Import Pine Dialog" );
313 }
314
315 AddressBookFile *addressbook_imp_pine( AddressIndex *addrIndex ) {
316         static gboolean cancelled;
317         gchar *pineFile;
318
319         _importedBook_ = NULL;
320         _imp_addressIndex_ = addrIndex;
321
322         if( ! imppine_dlg.window )
323                 imp_pine_create(&cancelled);
324         gtk_widget_grab_focus(imppine_dlg.ok_btn);
325         gtk_widget_grab_focus(imppine_dlg.file_entry);
326         gtk_widget_show(imppine_dlg.window);
327         manage_window_set_transient(GTK_WINDOW(imppine_dlg.window));
328
329         imp_pine_status_show( _( "Please select a file to import." ) );
330         pineFile = pine_find_file();
331         gtk_entry_set_text( GTK_ENTRY(imppine_dlg.name_entry), IMPORTPINE_GUESS_NAME );
332         gtk_entry_set_text( GTK_ENTRY(imppine_dlg.file_entry), pineFile );
333         g_free( pineFile );
334         pineFile = NULL;
335
336         gtk_main();
337         gtk_widget_hide(imppine_dlg.window);
338         _imp_addressIndex_ = NULL;
339
340         if (cancelled == TRUE) return NULL;
341         return _importedBook_;
342 }
343
344 /*
345 * End of Source.
346 */
347