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