2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2001 Match Grun
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.
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.
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.
21 * Import Mutt address book data.
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>
41 #include "addressbook.h"
42 #include "addressitem.h"
44 #include "prefs_common.h"
45 #include "manage_window.h"
49 #define IMPORTMUTT_GUESS_NAME "MUTT Import"
51 static struct _ImpMutt_Dlg
{
53 GtkWidget
*file_entry
;
54 GtkWidget
*name_entry
;
56 GtkWidget
*cancel_btn
;
61 static struct _AddressFileSelection _imp_mutt_file_selector_
;
62 static AddressBookFile
*_importedBook_
;
63 static AddressIndex
*_imp_addressIndex_
;
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
);
72 gtk_statusbar_push( GTK_STATUSBAR(impmutt_dlg
.statusbar
), impmutt_dlg
.status_cid
, msg
);
77 static gboolean
imp_mutt_import_file( gchar
*sName
, gchar
*sFile
) {
78 gboolean retVal
= FALSE
;
80 AddressBookFile
*abf
= NULL
;
83 if( _importedBook_
) {
84 addrbook_free_book( _importedBook_
);
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
);
94 /* Import data from file */
96 mutt_set_file( mdf
, sFile
);
97 if( mutt_import_data( mdf
, abf
->addressCache
) == MGU_SUCCESS
) {
98 addrbook_save_data( abf
);
103 addrbook_free_book( abf
);
109 static void imp_mutt_ok( GtkWidget
*widget
, gboolean
*cancelled
) {
113 gboolean errFlag
= FALSE
;
115 sFile
= gtk_editable_get_chars( GTK_EDITABLE(impmutt_dlg
.file_entry
), 0, -1 );
116 g_strchug( sFile
); g_strchomp( sFile
);
117 gtk_entry_set_text( GTK_ENTRY(impmutt_dlg
.file_entry
), sFile
);
119 sName
= gtk_editable_get_chars( GTK_EDITABLE(impmutt_dlg
.name_entry
), 0, -1 );
120 g_strchug( sName
); g_strchomp( sName
);
121 gtk_entry_set_text( GTK_ENTRY(impmutt_dlg
.name_entry
), sName
);
123 if( *sFile
== '\0'|| strlen( sFile
) < 1 ) {
124 sMsg
= _( "Please select a file." );
128 if( *sName
== '\0'|| strlen( sName
) < 1 ) {
129 if( ! errFlag
) sMsg
= _( "Address book name must be supplied." );
134 imp_mutt_status_show( sMsg
);
137 /* Import the file */
138 if( imp_mutt_import_file( sName
, sFile
) ) {
143 imp_mutt_status_show( _( "Error importing MUTT file." ) );
152 static void imp_mutt_cancel( GtkWidget
*widget
, gboolean
*cancelled
) {
157 static void imp_mutt_file_ok( GtkWidget
*widget
, gpointer data
) {
159 AddressFileSelection
*afs
;
162 afs
= ( AddressFileSelection
* ) data
;
163 fileSel
= afs
->fileSelector
;
164 sFile
= gtk_file_selection_get_filename( GTK_FILE_SELECTION(fileSel
) );
166 afs
->cancelled
= FALSE
;
167 gtk_entry_set_text( GTK_ENTRY(impmutt_dlg
.file_entry
), sFile
);
168 gtk_widget_hide( afs
->fileSelector
);
169 gtk_grab_remove( afs
->fileSelector
);
170 gtk_widget_grab_focus( impmutt_dlg
.file_entry
);
171 imp_mutt_status_show( _( "Please select a file to import." ) );
174 static void imp_mutt_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( impmutt_dlg
.file_entry
);
182 static void imp_mutt_file_select_create( AddressFileSelection
*afs
) {
183 GtkWidget
*fileSelector
;
185 fileSelector
= gtk_file_selection_new( _("Select MUTT 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_mutt_file_ok
), ( gpointer
) afs
);
189 gtk_signal_connect( GTK_OBJECT (GTK_FILE_SELECTION(fileSelector
)->cancel_button
),
190 "clicked", GTK_SIGNAL_FUNC (imp_mutt_file_cancel
), ( gpointer
) afs
);
191 afs
->fileSelector
= fileSelector
;
192 afs
->cancelled
= TRUE
;
195 static void imp_mutt_file_select( void ) {
197 if (! _imp_mutt_file_selector_
.fileSelector
)
198 imp_mutt_file_select_create( & _imp_mutt_file_selector_
);
200 sFile
= gtk_editable_get_chars( GTK_EDITABLE(impmutt_dlg
.file_entry
), 0, -1 );
201 gtk_file_selection_set_filename( GTK_FILE_SELECTION( _imp_mutt_file_selector_
.fileSelector
), sFile
);
203 gtk_widget_show( _imp_mutt_file_selector_
.fileSelector
);
204 gtk_grab_add( _imp_mutt_file_selector_
.fileSelector
);
207 static gint
imp_mutt_delete_event( GtkWidget
*widget
, GdkEventAny
*event
, gboolean
*cancelled
) {
213 static void imp_mutt_key_pressed( GtkWidget
*widget
, GdkEventKey
*event
, gboolean
*cancelled
) {
214 if (event
&& event
->keyval
== GDK_Escape
) {
220 static void imp_mutt_create( gboolean
*cancelled
) {
225 GtkWidget
*file_entry
;
226 GtkWidget
*name_entry
;
230 GtkWidget
*cancel_btn
;
232 GtkWidget
*statusbar
;
236 window
= gtk_window_new(GTK_WINDOW_DIALOG
);
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 MUTT 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_mutt_delete_event
),
245 gtk_signal_connect(GTK_OBJECT(window
), "key_press_event",
246 GTK_SIGNAL_FUNC(imp_mutt_key_pressed
),
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 );
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 );
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);
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);
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);
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);
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);
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
);
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
);
293 hsep
= gtk_hseparator_new();
294 gtk_box_pack_end(GTK_BOX(vbox
), hsep
, FALSE
, FALSE
, 0);
296 gtk_signal_connect(GTK_OBJECT(ok_btn
), "clicked",
297 GTK_SIGNAL_FUNC(imp_mutt_ok
), cancelled
);
298 gtk_signal_connect(GTK_OBJECT(cancel_btn
), "clicked",
299 GTK_SIGNAL_FUNC(imp_mutt_cancel
), cancelled
);
300 gtk_signal_connect(GTK_OBJECT(file_btn
), "clicked",
301 GTK_SIGNAL_FUNC(imp_mutt_file_select
), NULL
);
303 gtk_widget_show_all(vbox
);
305 impmutt_dlg
.window
= window
;
306 impmutt_dlg
.file_entry
= file_entry
;
307 impmutt_dlg
.name_entry
= name_entry
;
308 impmutt_dlg
.ok_btn
= ok_btn
;
309 impmutt_dlg
.cancel_btn
= cancel_btn
;
310 impmutt_dlg
.statusbar
= statusbar
;
311 impmutt_dlg
.status_cid
= gtk_statusbar_get_context_id( GTK_STATUSBAR(statusbar
), "Import Mutt Dialog" );
314 AddressBookFile
*addressbook_imp_mutt( AddressIndex
*addrIndex
) {
315 static gboolean cancelled
;
318 _importedBook_
= NULL
;
319 _imp_addressIndex_
= addrIndex
;
321 if( ! impmutt_dlg
.window
)
322 imp_mutt_create(&cancelled
);
323 gtk_widget_grab_focus(impmutt_dlg
.ok_btn
);
324 gtk_widget_grab_focus(impmutt_dlg
.file_entry
);
325 gtk_widget_show(impmutt_dlg
.window
);
326 manage_window_set_transient(GTK_WINDOW(impmutt_dlg
.window
));
328 imp_mutt_status_show( _( "Please select a file to import." ) );
329 muttFile
= mutt_find_file();
330 gtk_entry_set_text( GTK_ENTRY(impmutt_dlg
.name_entry
), IMPORTMUTT_GUESS_NAME
);
331 gtk_entry_set_text( GTK_ENTRY(impmutt_dlg
.file_entry
), muttFile
);
336 gtk_widget_hide(impmutt_dlg
.window
);
337 _imp_addressIndex_
= NULL
;
339 if (cancelled
== TRUE
) return NULL
;
340 return _importedBook_
;