2008-07-04 [colin] 3.5.0cvs6
[claws.git] / src / importpine.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2002-2007 Match Grun and the Claws Mail team
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 3 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, see <http://www.gnu.org/licenses/>.
17  * 
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 <glib/gi18n.h>
32 #include <gdk/gdkkeysyms.h>
33 #include <gtk/gtkwindow.h>
34 #include <gtk/gtksignal.h>
35 #include <gtk/gtklabel.h>
36 #include <gtk/gtkentry.h>
37 #include <gtk/gtktable.h>
38 #include <gtk/gtkbutton.h>
39
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 #include "filesel.h"
49
50 #define IMPORTPINE_GUESS_NAME "Pine Import"
51
52 static struct _ImpPine_Dlg {
53         GtkWidget *window;
54         GtkWidget *file_entry;
55         GtkWidget *name_entry;
56         GtkWidget *ok_btn;
57         GtkWidget *cancel_btn;
58         GtkWidget *statusbar;
59         gint status_cid;
60 } imppine_dlg;
61
62 static struct _AddressFileSelection _imp_pine_file_selector_;
63 static AddressBookFile *_importedBook_;
64 static AddressIndex *_imp_addressIndex_;
65
66 /*
67 * Edit functions.
68 */
69 static void imp_pine_status_show( gchar *msg ) {
70         if( imppine_dlg.statusbar != NULL ) {
71                 gtk_statusbar_pop( GTK_STATUSBAR(imppine_dlg.statusbar), imppine_dlg.status_cid );
72                 if( msg ) {
73                         gtk_statusbar_push( GTK_STATUSBAR(imppine_dlg.statusbar), imppine_dlg.status_cid, msg );
74                 }
75         }
76 }
77
78 static gboolean imp_pine_import_file( gchar *sName, gchar *sFile ) {
79         gboolean retVal = FALSE;
80         gchar *newFile;
81         AddressBookFile *abf = NULL;
82         PineFile *pdf = NULL;
83
84         if( _importedBook_ ) {
85                 addrbook_free_book( _importedBook_ );
86         }
87
88         abf = addrbook_create_book();
89         addrbook_set_path( abf, _imp_addressIndex_->filePath );
90         addrbook_set_name( abf, sName );
91         newFile = addrbook_guess_next_file( abf );
92         addrbook_set_file( abf, newFile );
93         g_free( newFile );
94
95         /* Import data from file */
96         pdf = pine_create();
97         pine_set_file( pdf, sFile );
98         if( pine_import_data( pdf, abf->addressCache ) == MGU_SUCCESS ) {
99                 addrbook_save_data( abf );
100                 _importedBook_ = abf;
101                 retVal = TRUE;
102         }
103         else {
104                 addrbook_free_book( abf );
105         }
106         pine_free(pdf);
107
108         return retVal;
109 }
110
111 static void imp_pine_ok( GtkWidget *widget, gboolean *cancelled ) {
112         gchar *sName;
113         gchar *sFile;
114         gchar *sMsg = NULL;
115         gboolean errFlag = FALSE;
116
117         sFile = gtk_editable_get_chars( GTK_EDITABLE(imppine_dlg.file_entry), 0, -1 );
118         g_strchug( sFile ); g_strchomp( sFile );
119         gtk_entry_set_text( GTK_ENTRY(imppine_dlg.file_entry), sFile );
120
121         sName = gtk_editable_get_chars( GTK_EDITABLE(imppine_dlg.name_entry), 0, -1 );
122         g_strchug( sName ); g_strchomp( sName );
123         gtk_entry_set_text( GTK_ENTRY(imppine_dlg.name_entry), sName );
124
125         if( *sFile == '\0'|| strlen( sFile ) < 1 ) {
126                 sMsg = _( "Please select a file." );
127                 errFlag = TRUE;
128         }
129
130         if( *sName == '\0'|| strlen( sName ) < 1 ) {
131                 if( ! errFlag ) sMsg = _( "Address book name must be supplied." );
132                 errFlag = TRUE;
133         }
134
135         if( errFlag ) {
136                 imp_pine_status_show( sMsg );
137         }
138         else {
139                 /* Import the file */
140                 if( imp_pine_import_file( sName, sFile ) ) {
141                         *cancelled = FALSE;
142                         gtk_main_quit();
143                 }
144                 else {
145                         imp_pine_status_show( _( "Error importing Pine file." ) );
146                 }
147         }
148
149         g_free( sFile );
150         g_free( sName );
151
152 }
153
154 static void imp_pine_cancel( GtkWidget *widget, gboolean *cancelled ) {
155         *cancelled = TRUE;
156         gtk_main_quit();
157 }
158
159 static void imp_pine_file_select_create( AddressFileSelection *afs ) {
160         gchar *file = filesel_select_file_open(_("Select Pine File"), NULL);
161         
162         if (file == NULL)
163                 afs->cancelled = TRUE;
164         else {
165                 afs->cancelled = FALSE;
166                 gtk_entry_set_text( GTK_ENTRY(imppine_dlg.file_entry), file );
167                 g_free(file);
168         }
169 }
170
171 static void imp_pine_file_select( void ) {
172         imp_pine_file_select_create( & _imp_pine_file_selector_ );
173 }
174
175 static gint imp_pine_delete_event( GtkWidget *widget, GdkEventAny *event, gboolean *cancelled ) {
176         *cancelled = TRUE;
177         gtk_main_quit();
178         return TRUE;
179 }
180
181 static gboolean imp_pine_key_pressed( GtkWidget *widget, GdkEventKey *event, gboolean *cancelled ) {
182         if (event && event->keyval == GDK_Escape) {
183                 *cancelled = TRUE;
184                 gtk_main_quit();
185         }
186         return FALSE;
187 }
188
189 static void imp_pine_create( gboolean *cancelled ) {
190         GtkWidget *window;
191         GtkWidget *vbox;
192         GtkWidget *table;
193         GtkWidget *label;
194         GtkWidget *file_entry;
195         GtkWidget *name_entry;
196         GtkWidget *hbbox;
197         GtkWidget *ok_btn;
198         GtkWidget *cancel_btn;
199         GtkWidget *file_btn;
200         GtkWidget *statusbar;
201         GtkWidget *hsbox;
202         gint top;
203
204         window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "importpine");
205         gtk_widget_set_size_request(window, 450, -1);
206         gtk_container_set_border_width( GTK_CONTAINER(window), 0 );
207         gtk_window_set_title( GTK_WINDOW(window), _("Import Pine file into Address Book") );
208         gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
209         gtk_window_set_modal(GTK_WINDOW(window), TRUE); 
210         g_signal_connect(G_OBJECT(window), "delete_event",
211                          G_CALLBACK(imp_pine_delete_event), cancelled);
212         g_signal_connect(G_OBJECT(window), "key_press_event",
213                          G_CALLBACK(imp_pine_key_pressed), cancelled);
214
215         vbox = gtk_vbox_new(FALSE, 8);
216         gtk_container_add(GTK_CONTAINER(window), vbox);
217         gtk_container_set_border_width( GTK_CONTAINER(vbox), 0 );
218
219         table = gtk_table_new(2, 3, FALSE);
220         gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
221         gtk_container_set_border_width( GTK_CONTAINER(table), 8 );
222         gtk_table_set_row_spacings(GTK_TABLE(table), 8);
223         gtk_table_set_col_spacings(GTK_TABLE(table), 8 );
224
225         /* First row */
226         top = 0;
227         label = gtk_label_new(_("Name"));
228         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
229         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
230
231         name_entry = gtk_entry_new();
232         gtk_table_attach(GTK_TABLE(table), name_entry, 1, 2, top, (top + 1), GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
233
234         /* Second row */
235         top = 1;
236         label = gtk_label_new(_("File"));
237         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
238         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
239
240         file_entry = gtk_entry_new();
241         gtk_table_attach(GTK_TABLE(table), file_entry, 1, 2, top, (top + 1), GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
242
243         file_btn = gtkut_get_browse_file_btn(_("_Browse"));
244         gtk_table_attach(GTK_TABLE(table), file_btn, 2, 3, top, (top + 1), GTK_FILL, 0, 3, 0);
245
246         /* Status line */
247         hsbox = gtk_hbox_new(FALSE, 0);
248         gtk_box_pack_end(GTK_BOX(vbox), hsbox, FALSE, FALSE, BORDER_WIDTH);
249         statusbar = gtk_statusbar_new();
250         gtk_box_pack_start(GTK_BOX(hsbox), statusbar, TRUE, TRUE, BORDER_WIDTH);
251
252         /* Button panel */
253         gtkut_stock_button_set_create(&hbbox, &cancel_btn, GTK_STOCK_CANCEL,
254                                       &ok_btn, GTK_STOCK_OK,
255                                       NULL, NULL);
256         gtk_box_pack_end(GTK_BOX(vbox), hbbox, FALSE, FALSE, 0);
257         gtk_container_set_border_width( GTK_CONTAINER(hbbox), 5);
258         gtk_widget_grab_default(ok_btn);
259
260         g_signal_connect(G_OBJECT(ok_btn), "clicked",
261                          G_CALLBACK(imp_pine_ok), cancelled);
262         g_signal_connect(G_OBJECT(cancel_btn), "clicked",
263                          G_CALLBACK(imp_pine_cancel), cancelled);
264         g_signal_connect(G_OBJECT(file_btn), "clicked",
265                          G_CALLBACK(imp_pine_file_select), NULL);
266
267         gtk_widget_show_all(vbox);
268
269         imppine_dlg.window     = window;
270         imppine_dlg.file_entry = file_entry;
271         imppine_dlg.name_entry = name_entry;
272         imppine_dlg.ok_btn     = ok_btn;
273         imppine_dlg.cancel_btn = cancel_btn;
274         imppine_dlg.statusbar  = statusbar;
275         imppine_dlg.status_cid = gtk_statusbar_get_context_id(
276                        GTK_STATUSBAR(statusbar), "Import Pine Dialog" );
277 }
278
279 AddressBookFile *addressbook_imp_pine( AddressIndex *addrIndex ) {
280         static gboolean cancelled;
281         gchar *pineFile;
282
283         _importedBook_ = NULL;
284         _imp_addressIndex_ = addrIndex;
285
286         if( ! imppine_dlg.window )
287                 imp_pine_create(&cancelled);
288         gtk_widget_grab_focus(imppine_dlg.ok_btn);
289         gtk_widget_grab_focus(imppine_dlg.file_entry);
290         gtk_widget_show(imppine_dlg.window);
291         manage_window_set_transient(GTK_WINDOW(imppine_dlg.window));
292
293         imp_pine_status_show( _( "Please select a file to import." ) );
294         pineFile = pine_find_file();
295         gtk_entry_set_text( GTK_ENTRY(imppine_dlg.name_entry), IMPORTPINE_GUESS_NAME );
296         gtk_entry_set_text( GTK_ENTRY(imppine_dlg.file_entry), pineFile );
297         g_free( pineFile );
298         pineFile = NULL;
299
300         gtk_main();
301         gtk_widget_hide(imppine_dlg.window);
302         _imp_addressIndex_ = NULL;
303
304         if (cancelled == TRUE) return NULL;
305         return _importedBook_;
306 }
307
308 /*
309 * End of Source.
310 */
311