Remove inclusion of sys/socket.h from nntp-thread.c
[claws.git] / src / importmutt.c
blob796d0b28c96e60c8d5d4994bd4027708ca6fc138
1 /*
2 * Claws Mail -- a GTK based, lightweight, and fast e-mail client
3 * Copyright (C) 2001-2022 the Claws Mail team and 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 3 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, see <http://www.gnu.org/licenses/>.
20 * Import Mutt address book data.
23 #ifdef HAVE_CONFIG_H
24 # include "config.h"
25 #include "claws-features.h"
26 #endif
28 #include "defs.h"
30 #include <glib.h>
31 #include <glib/gi18n.h>
32 #include <gdk/gdkkeysyms.h>
33 #include <gtk/gtk.h>
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 "mutt.h"
43 #include "filesel.h"
45 #define IMPORTMUTT_GUESS_NAME "MUTT Import"
47 static struct _ImpMutt_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 } impmutt_dlg;
57 static struct _AddressFileSelection _imp_mutt_file_selector_;
58 static AddressBookFile *_importedBook_;
59 static AddressIndex *_imp_addressIndex_;
62 * Edit functions.
64 static void imp_mutt_status_show( gchar *msg ) {
65 if( impmutt_dlg.statusbar != NULL ) {
66 gtk_statusbar_pop( GTK_STATUSBAR(impmutt_dlg.statusbar), impmutt_dlg.status_cid );
67 if( msg ) {
68 gtk_statusbar_push( GTK_STATUSBAR(impmutt_dlg.statusbar), impmutt_dlg.status_cid, msg );
73 static gboolean imp_mutt_import_file( gchar *sName, gchar *sFile ) {
74 gboolean retVal = FALSE;
75 gchar *newFile;
76 AddressBookFile *abf = NULL;
77 MuttFile *mdf = NULL;
79 if( _importedBook_ ) {
80 addrbook_free_book( _importedBook_ );
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 );
90 /* Import data from file */
91 mdf = mutt_create();
92 mutt_set_file( mdf, sFile );
93 if( mutt_import_data( mdf, abf->addressCache ) == MGU_SUCCESS ) {
94 addrbook_save_data( abf );
95 _importedBook_ = abf;
96 retVal = TRUE;
98 else {
99 addrbook_free_book( abf );
102 mutt_free(mdf);
104 return retVal;
107 static void imp_mutt_ok( GtkWidget *widget, gboolean *cancelled ) {
108 gchar *sName;
109 gchar *sFile;
110 gchar *sMsg = NULL;
111 gboolean errFlag = FALSE;
113 sFile = gtk_editable_get_chars( GTK_EDITABLE(impmutt_dlg.file_entry), 0, -1 );
114 g_strstrip( sFile );
115 gtk_entry_set_text( GTK_ENTRY(impmutt_dlg.file_entry), sFile );
117 sName = gtk_editable_get_chars( GTK_EDITABLE(impmutt_dlg.name_entry), 0, -1 );
118 g_strstrip( sName );
119 gtk_entry_set_text( GTK_ENTRY(impmutt_dlg.name_entry), sName );
121 if( *sFile == '\0'|| strlen( sFile ) < 1 ) {
122 sMsg = _( "Please select a file." );
123 errFlag = TRUE;
126 if( *sName == '\0'|| strlen( sName ) < 1 ) {
127 if( ! errFlag ) sMsg = _( "Address book name must be supplied." );
128 errFlag = TRUE;
131 if( errFlag ) {
132 imp_mutt_status_show( sMsg );
134 else {
135 /* Import the file */
136 if( imp_mutt_import_file( sName, sFile ) ) {
137 *cancelled = FALSE;
138 gtk_main_quit();
140 else {
141 imp_mutt_status_show( _( "Error importing MUTT file." ) );
145 g_free( sFile );
146 g_free( sName );
150 static void imp_mutt_cancel( GtkWidget *widget, gboolean *cancelled ) {
151 *cancelled = TRUE;
152 gtk_main_quit();
155 static void imp_mutt_file_select_create( AddressFileSelection *afs ) {
156 gchar *file = filesel_select_file_open(_("Select MUTT File"), NULL);
158 if (file == NULL)
159 afs->cancelled = TRUE;
160 else {
161 afs->cancelled = FALSE;
162 gtk_entry_set_text( GTK_ENTRY(impmutt_dlg.file_entry), file );
163 g_free(file);
167 static void imp_mutt_file_select( void ) {
168 imp_mutt_file_select_create( & _imp_mutt_file_selector_ );
171 static gint imp_mutt_delete_event( GtkWidget *widget, GdkEventAny *event, gboolean *cancelled ) {
172 *cancelled = TRUE;
173 gtk_main_quit();
174 return TRUE;
177 static gboolean imp_mutt_key_pressed( GtkWidget *widget, GdkEventKey *event, gboolean *cancelled ) {
178 if (event && event->keyval == GDK_KEY_Escape) {
179 *cancelled = TRUE;
180 gtk_main_quit();
182 return FALSE;
185 static void imp_mutt_create( gboolean *cancelled ) {
186 GtkWidget *window;
187 GtkWidget *vbox;
188 GtkWidget *table;
189 GtkWidget *label;
190 GtkWidget *file_entry;
191 GtkWidget *name_entry;
192 GtkWidget *hbbox;
193 GtkWidget *ok_btn;
194 GtkWidget *cancel_btn;
195 GtkWidget *file_btn;
196 GtkWidget *statusbar;
197 GtkWidget *hsbox;
199 window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "importmutt");
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 MUTT 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_mutt_delete_event), cancelled);
207 g_signal_connect(G_OBJECT(window), "key_press_event",
208 G_CALLBACK(imp_mutt_key_pressed), cancelled);
210 vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 8);
211 gtk_container_add(GTK_CONTAINER(window), vbox);
212 gtk_container_set_border_width( GTK_CONTAINER(vbox), 0 );
214 table = gtk_grid_new();
215 gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
216 gtk_container_set_border_width( GTK_CONTAINER(table), 8 );
217 gtk_grid_set_row_spacing(GTK_GRID(table), 8);
218 gtk_grid_set_column_spacing(GTK_GRID(table), 8);
220 /* First row */
221 label = gtk_label_new(_("Name"));
222 gtk_label_set_xalign(GTK_LABEL(label), 0.0);
223 gtk_grid_attach(GTK_GRID(table), label, 0, 0, 1, 1);
225 name_entry = gtk_entry_new();
226 gtk_grid_attach(GTK_GRID(table), name_entry, 1, 0, 1, 1);
227 gtk_widget_set_hexpand(name_entry, TRUE);
228 gtk_widget_set_halign(name_entry, GTK_ALIGN_FILL);
230 /* Second row */
231 label = gtk_label_new(_("File"));
232 gtk_label_set_xalign(GTK_LABEL(label), 0.0);
233 gtk_grid_attach(GTK_GRID(table), label, 0, 1, 1, 1);
235 file_entry = gtk_entry_new();
236 gtk_grid_attach(GTK_GRID(table), file_entry, 1, 1, 1, 1);
237 gtk_widget_set_hexpand(file_entry, TRUE);
238 gtk_widget_set_halign(file_entry, GTK_ALIGN_FILL);
240 file_btn = gtkut_get_browse_file_btn(_("_Browse"));
241 gtk_grid_attach(GTK_GRID(table), file_btn, 2, 1, 1, 1);
243 /* Status line */
244 hsbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
245 gtk_box_pack_end(GTK_BOX(vbox), hsbox, FALSE, FALSE, BORDER_WIDTH);
246 statusbar = gtk_statusbar_new();
247 gtk_box_pack_start(GTK_BOX(hsbox), statusbar, TRUE, TRUE, BORDER_WIDTH);
249 /* Button panel */
250 gtkut_stock_button_set_create(&hbbox, &cancel_btn, NULL, _("_Cancel"),
251 &ok_btn, NULL, _("_OK"),
252 NULL, NULL, NULL);
253 gtk_box_pack_end(GTK_BOX(vbox), hbbox, FALSE, FALSE, 0);
254 gtk_container_set_border_width( GTK_CONTAINER(hbbox), 5);
255 gtk_widget_grab_default(ok_btn);
257 g_signal_connect(G_OBJECT(ok_btn), "clicked",
258 G_CALLBACK(imp_mutt_ok), cancelled);
259 g_signal_connect(G_OBJECT(cancel_btn), "clicked",
260 G_CALLBACK(imp_mutt_cancel), cancelled);
261 g_signal_connect(G_OBJECT(file_btn), "clicked",
262 G_CALLBACK(imp_mutt_file_select), NULL);
264 gtk_widget_show_all(vbox);
266 impmutt_dlg.window = window;
267 impmutt_dlg.file_entry = file_entry;
268 impmutt_dlg.name_entry = name_entry;
269 impmutt_dlg.ok_btn = ok_btn;
270 impmutt_dlg.cancel_btn = cancel_btn;
271 impmutt_dlg.statusbar = statusbar;
272 impmutt_dlg.status_cid = gtk_statusbar_get_context_id( GTK_STATUSBAR(statusbar), "Import Mutt Dialog" );
275 AddressBookFile *addressbook_imp_mutt( AddressIndex *addrIndex ) {
276 static gboolean cancelled;
277 gchar *muttFile;
279 _importedBook_ = NULL;
280 _imp_addressIndex_ = addrIndex;
282 if( ! impmutt_dlg.window )
283 imp_mutt_create(&cancelled);
284 gtk_widget_grab_focus(impmutt_dlg.ok_btn);
285 gtk_widget_grab_focus(impmutt_dlg.file_entry);
286 gtk_widget_show(impmutt_dlg.window);
287 manage_window_set_transient(GTK_WINDOW(impmutt_dlg.window));
288 gtk_window_set_modal(GTK_WINDOW(impmutt_dlg.window), TRUE);
290 imp_mutt_status_show( _( "Please select a file to import." ) );
291 muttFile = mutt_find_file();
292 gtk_entry_set_text( GTK_ENTRY(impmutt_dlg.name_entry), IMPORTMUTT_GUESS_NAME );
293 gtk_entry_set_text( GTK_ENTRY(impmutt_dlg.file_entry), muttFile );
294 g_free( muttFile );
295 muttFile = NULL;
297 gtk_main();
298 gtk_widget_hide(impmutt_dlg.window);
299 gtk_window_set_modal(GTK_WINDOW(impmutt_dlg.window), FALSE);
300 _imp_addressIndex_ = NULL;
302 if (cancelled == TRUE) return NULL;
303 return _importedBook_;
307 * End of Source.