clean up a little bit
[claws.git] / src / importmutt.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2001 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 Mutt 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 "mutt.h"
48
49 #define IMPORTMUTT_GUESS_NAME "MUTT Import"
50
51 static struct _ImpMutt_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 } impmutt_dlg;
60
61 static struct _AddressFileSelection _imp_mutt_file_selector_;
62 static AddressBookFile *_importedBook_;
63 static AddressIndex *_imp_addressIndex_;
64
65 /*
66 * Edit functions.
67 */
68 void imp_mutt_status_show( gchar *msg ) {
69         if( impmutt_dlg.statusbar != NULL ) {
70                 gtk_statusbar_pop( GTK_STATUSBAR(impmutt_dlg.statusbar), impmutt_dlg.status_cid );
71                 if( msg ) {
72                         gtk_statusbar_push( GTK_STATUSBAR(impmutt_dlg.statusbar), impmutt_dlg.status_cid, msg );
73                 }
74         }
75 }
76
77 static gchar *imp_mutt_guess_file( AddressBookFile *abf ) {
78         gchar *newFile = NULL;
79         GList *fileList = NULL;
80         gint fileNum = 1;
81         fileList = addrbook_get_bookfile_list( abf );
82         if( fileList ) {
83                 fileNum = 1 + abf->maxValue;
84         }
85         newFile = addrbook_gen_new_file_name( fileNum );
86         g_list_free( fileList );
87         fileList = NULL;
88         return newFile;
89 }
90
91 static gboolean imp_mutt_import_file( gchar *sName, gchar *sFile ) {
92         gboolean retVal = FALSE;
93         gchar *newFile;
94         AddressBookFile *abf = NULL;
95         MuttFile *mdf = NULL;
96
97         if( _importedBook_ ) {
98                 addrbook_free_book( _importedBook_ );
99         }
100
101         abf = addrbook_create_book();
102         addrbook_set_path( abf, _imp_addressIndex_->filePath );
103         addrbook_set_name( abf, sName );
104         newFile = imp_mutt_guess_file( abf );
105         addrbook_set_file( abf, newFile );
106         g_free( newFile );
107
108         /* Import data from file */
109         mdf = mutt_create();
110         mutt_set_file( mdf, sFile );
111         if( mutt_import_data( mdf, abf->addressCache ) == MGU_SUCCESS ) {
112                 addrbook_save_data( abf );
113                 abf->dirtyFlag = TRUE;
114                 _importedBook_ = abf;
115                 retVal = TRUE;
116         }
117         else {
118                 addrbook_free_book( abf );
119         }
120
121         return retVal;
122 }
123
124 static void imp_mutt_ok( GtkWidget *widget, gboolean *cancelled ) {
125         gchar *sName;
126         gchar *sFile;
127         gchar *sMsg = NULL;
128         gboolean errFlag = FALSE;
129
130         sFile = gtk_editable_get_chars( GTK_EDITABLE(impmutt_dlg.file_entry), 0, -1 );
131         g_strchug( sFile ); g_strchomp( sFile );
132         gtk_entry_set_text( GTK_ENTRY(impmutt_dlg.file_entry), sFile );
133
134         sName = gtk_editable_get_chars( GTK_EDITABLE(impmutt_dlg.name_entry), 0, -1 );
135         g_strchug( sName ); g_strchomp( sName );
136         gtk_entry_set_text( GTK_ENTRY(impmutt_dlg.name_entry), sName );
137
138         if( *sFile == '\0'|| strlen( sFile ) < 1 ) {
139                 sMsg = _( "Please select a file." );
140                 errFlag = TRUE;
141         }
142
143         if( *sName == '\0'|| strlen( sName ) < 1 ) {
144                 if( ! errFlag ) sMsg = _( "Address book name must be supplied." );
145                 errFlag = TRUE;
146         }
147
148         if( errFlag ) {
149                 imp_mutt_status_show( sMsg );
150         }
151         else {
152                 /* Import the file */
153                 if( imp_mutt_import_file( sName, sFile ) ) {
154                         *cancelled = FALSE;
155                         gtk_main_quit();
156                 }
157                 else {
158                         imp_mutt_status_show( _( "Error importing MUTT file." ) );
159                 }
160         }
161
162         g_free( sFile );
163         g_free( sName );
164
165 }
166
167 static void imp_mutt_cancel( GtkWidget *widget, gboolean *cancelled ) {
168         *cancelled = TRUE;
169         gtk_main_quit();
170 }
171
172 static void imp_mutt_file_ok( GtkWidget *widget, gpointer data ) {
173         gchar *sFile;
174         AddressFileSelection *afs;
175         GtkWidget *fileSel;
176
177         afs = ( AddressFileSelection * ) data;
178         fileSel = afs->fileSelector;
179         sFile = gtk_file_selection_get_filename( GTK_FILE_SELECTION(fileSel) );
180
181         afs->cancelled = FALSE;
182         gtk_entry_set_text( GTK_ENTRY(impmutt_dlg.file_entry), sFile );
183         gtk_widget_hide( afs->fileSelector );
184         gtk_grab_remove( afs->fileSelector );
185         gtk_widget_grab_focus( impmutt_dlg.file_entry );
186         imp_mutt_status_show( _( "Please select a file to import." ) );
187 }
188
189 static void imp_mutt_file_cancel( GtkWidget *widget, gpointer data ) {
190         AddressFileSelection *afs = ( AddressFileSelection * ) data;
191         afs->cancelled = TRUE;
192         gtk_widget_hide( afs->fileSelector );
193         gtk_grab_remove( afs->fileSelector );
194         gtk_widget_grab_focus( impmutt_dlg.file_entry );
195 }
196
197 static void imp_mutt_file_select_create( AddressFileSelection *afs ) {
198         GtkWidget *fileSelector;
199
200         fileSelector = gtk_file_selection_new( _("Select MUTT File") );
201         gtk_file_selection_hide_fileop_buttons( GTK_FILE_SELECTION(fileSelector) );
202         gtk_signal_connect( GTK_OBJECT (GTK_FILE_SELECTION(fileSelector)->ok_button),
203                              "clicked", GTK_SIGNAL_FUNC (imp_mutt_file_ok), ( gpointer ) afs );
204         gtk_signal_connect( GTK_OBJECT (GTK_FILE_SELECTION(fileSelector)->cancel_button),
205                              "clicked", GTK_SIGNAL_FUNC (imp_mutt_file_cancel), ( gpointer ) afs );
206         afs->fileSelector = fileSelector;
207         afs->cancelled = TRUE;
208 }
209
210 static void imp_mutt_file_select( void ) {
211         gchar *sFile;
212         if (! _imp_mutt_file_selector_.fileSelector )
213                 imp_mutt_file_select_create( & _imp_mutt_file_selector_ );
214
215         sFile = gtk_editable_get_chars( GTK_EDITABLE(impmutt_dlg.file_entry), 0, -1 );
216         gtk_file_selection_set_filename( GTK_FILE_SELECTION( _imp_mutt_file_selector_.fileSelector ), sFile );
217         g_free( sFile );
218         gtk_widget_show( _imp_mutt_file_selector_.fileSelector );
219         gtk_grab_add( _imp_mutt_file_selector_.fileSelector );
220 }
221
222 static gint imp_mutt_delete_event( GtkWidget *widget, GdkEventAny *event, gboolean *cancelled ) {
223         *cancelled = TRUE;
224         gtk_main_quit();
225         return TRUE;
226 }
227
228 static void imp_mutt_key_pressed( GtkWidget *widget, GdkEventKey *event, gboolean *cancelled ) {
229         if (event && event->keyval == GDK_Escape) {
230                 *cancelled = TRUE;
231                 gtk_main_quit();
232         }
233 }
234
235 static void imp_mutt_create( gboolean *cancelled ) {
236         GtkWidget *window;
237         GtkWidget *vbox;
238         GtkWidget *table;
239         GtkWidget *label;
240         GtkWidget *file_entry;
241         GtkWidget *name_entry;
242         GtkWidget *hbbox;
243         GtkWidget *hsep;
244         GtkWidget *ok_btn;
245         GtkWidget *cancel_btn;
246         GtkWidget *file_btn;
247         GtkWidget *statusbar;
248         GtkWidget *hsbox;
249         gint top;
250
251         window = gtk_window_new(GTK_WINDOW_DIALOG);
252         gtk_widget_set_usize(window, 450, -1);
253         gtk_container_set_border_width( GTK_CONTAINER(window), 0 );
254         gtk_window_set_title( GTK_WINDOW(window), _("Import MUTT file into Address Book") );
255         gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
256         gtk_window_set_modal(GTK_WINDOW(window), TRUE); 
257         gtk_signal_connect(GTK_OBJECT(window), "delete_event",
258                            GTK_SIGNAL_FUNC(imp_mutt_delete_event),
259                            cancelled);
260         gtk_signal_connect(GTK_OBJECT(window), "key_press_event",
261                            GTK_SIGNAL_FUNC(imp_mutt_key_pressed),
262                            cancelled);
263
264         vbox = gtk_vbox_new(FALSE, 8);
265         gtk_container_add(GTK_CONTAINER(window), vbox);
266         gtk_container_set_border_width( GTK_CONTAINER(vbox), 0 );
267
268         table = gtk_table_new(2, 3, FALSE);
269         gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
270         gtk_container_set_border_width( GTK_CONTAINER(table), 8 );
271         gtk_table_set_row_spacings(GTK_TABLE(table), 8);
272         gtk_table_set_col_spacings(GTK_TABLE(table), 8 );
273
274         /* First row */
275         top = 0;
276         label = gtk_label_new(_("Name"));
277         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
278         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
279
280         name_entry = gtk_entry_new();
281         gtk_table_attach(GTK_TABLE(table), name_entry, 1, 2, top, (top + 1), GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
282
283         /* Second row */
284         top = 1;
285         label = gtk_label_new(_("File"));
286         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
287         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
288
289         file_entry = gtk_entry_new();
290         gtk_table_attach(GTK_TABLE(table), file_entry, 1, 2, top, (top + 1), GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
291
292         file_btn = gtk_button_new_with_label( _(" ... "));
293         gtk_table_attach(GTK_TABLE(table), file_btn, 2, 3, top, (top + 1), GTK_FILL, 0, 3, 0);
294
295         /* Status line */
296         hsbox = gtk_hbox_new(FALSE, 0);
297         gtk_box_pack_end(GTK_BOX(vbox), hsbox, FALSE, FALSE, BORDER_WIDTH);
298         statusbar = gtk_statusbar_new();
299         gtk_box_pack_start(GTK_BOX(hsbox), statusbar, TRUE, TRUE, BORDER_WIDTH);
300
301         /* Button panel */
302         gtkut_button_set_create(&hbbox, &ok_btn, _("OK"),
303                                 &cancel_btn, _("Cancel"), NULL, NULL);
304         gtk_box_pack_end(GTK_BOX(vbox), hbbox, FALSE, FALSE, 0);
305         gtk_container_set_border_width( GTK_CONTAINER(hbbox), 0 );
306         gtk_widget_grab_default(ok_btn);
307
308         hsep = gtk_hseparator_new();
309         gtk_box_pack_end(GTK_BOX(vbox), hsep, FALSE, FALSE, 0);
310
311         gtk_signal_connect(GTK_OBJECT(ok_btn), "clicked",
312                            GTK_SIGNAL_FUNC(imp_mutt_ok), cancelled);
313         gtk_signal_connect(GTK_OBJECT(cancel_btn), "clicked",
314                            GTK_SIGNAL_FUNC(imp_mutt_cancel), cancelled);
315         gtk_signal_connect(GTK_OBJECT(file_btn), "clicked",
316                            GTK_SIGNAL_FUNC(imp_mutt_file_select), NULL);
317
318         gtk_widget_show_all(vbox);
319
320         impmutt_dlg.window     = window;
321         impmutt_dlg.file_entry = file_entry;
322         impmutt_dlg.name_entry = name_entry;
323         impmutt_dlg.ok_btn     = ok_btn;
324         impmutt_dlg.cancel_btn = cancel_btn;
325         impmutt_dlg.statusbar  = statusbar;
326         impmutt_dlg.status_cid = gtk_statusbar_get_context_id( GTK_STATUSBAR(statusbar), "Import Mutt Dialog" );
327 }
328
329 AddressBookFile *addressbook_imp_mutt( AddressIndex *addrIndex ) {
330         static gboolean cancelled;
331         gchar *muttFile;
332
333         _importedBook_ = NULL;
334         _imp_addressIndex_ = addrIndex;
335
336         if( ! impmutt_dlg.window )
337                 imp_mutt_create(&cancelled);
338         gtk_widget_grab_focus(impmutt_dlg.ok_btn);
339         gtk_widget_grab_focus(impmutt_dlg.file_entry);
340         gtk_widget_show(impmutt_dlg.window);
341         manage_window_set_transient(GTK_WINDOW(impmutt_dlg.window));
342
343         imp_mutt_status_show( _( "Please select a file to import." ) );
344         muttFile = mutt_find_file();
345         gtk_entry_set_text( GTK_ENTRY(impmutt_dlg.name_entry), IMPORTMUTT_GUESS_NAME );
346         gtk_entry_set_text( GTK_ENTRY(impmutt_dlg.file_entry), muttFile );
347         g_free( muttFile );
348         muttFile = NULL;
349
350         gtk_main();
351         gtk_widget_hide(impmutt_dlg.window);
352         _imp_addressIndex_ = NULL;
353
354         if (cancelled == TRUE) return NULL;
355         return _importedBook_;
356 }
357
358 /*
359 * End of Source.
360 */
361