RSSyl: Allow use of .netrc by libcurl. Bug/enhancement #3309, by Vincent Pelletier
[claws.git] / src / plugins / archive / archiver_prefs.c
1 /*
2  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2008 Michael Rasmussen 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 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #include "claws-features.h"
23 #endif
24
25 #include <glib.h>
26 #include <glib/gi18n.h>
27
28 #include "defs.h"
29
30 #include <glib.h>
31 #include <gtk/gtk.h>
32
33 #include "gtkutils.h"
34 #include "prefs.h"
35 #include "prefs_gtk.h"
36 #include "prefswindow.h"
37 #include "alertpanel.h"
38 #include "utils.h"
39 #include "filesel.h"
40
41 #include "archiver_prefs.h"
42
43 #define PREFS_BLOCK_NAME "Archiver"
44
45 ArchiverPrefs archiver_prefs;
46
47 struct ArchiverPrefsPage {
48         PrefsPage page;
49         GtkWidget *save_folder;
50         gint compression;
51         GtkWidget *zip_radiobtn;
52         GtkWidget *bzip_radiobtn;
53 #if NEW_ARCHIVE_API
54         GtkWidget *compress_radiobtn;
55 #endif
56         GtkWidget *none_radiobtn;
57         GtkWidget *tar_radiobtn;
58         GtkWidget *shar_radiobtn;
59         GtkWidget *cpio_radiobtn;
60         GtkWidget *pax_radiobtn;
61         GtkWidget *recursive_chkbtn;
62         GtkWidget *md5sum_chkbtn;
63         GtkWidget *rename_chkbtn;
64         GtkWidget *unlink_chkbtn;
65 };
66
67 struct ArchiverPrefsPage archiver_prefs_page;
68
69 static void create_archiver_prefs_page                  (PrefsPage *page,
70                                                          GtkWindow *window,
71                                                          gpointer   data);
72 static void destroy_archiver_prefs_page                 (PrefsPage *page);
73 static void save_archiver_prefs                         (PrefsPage *page);
74
75 static PrefParam param[] = {
76         {"save_folder", NULL, &archiver_prefs.save_folder, P_STRING, NULL, NULL, NULL},
77         {"compression", "0", &archiver_prefs.compression, P_ENUM, NULL, NULL, NULL},
78         {"format", "0", &archiver_prefs.format, P_ENUM, NULL, NULL, NULL},
79         {"recursive", "TRUE", &archiver_prefs.recursive, P_BOOL, NULL, NULL, NULL},
80         {"md5sum",  "FALSE", &archiver_prefs.md5sum, P_BOOL, NULL, NULL, NULL},
81         {"rename", "FALSE", &archiver_prefs.rename, P_BOOL, NULL, NULL, NULL},
82         {"unlink", "FALSE", &archiver_prefs.unlink, P_BOOL, NULL, NULL, NULL},
83
84         {NULL, NULL, NULL, P_OTHER, NULL, NULL, NULL}
85 };
86
87 void archiver_prefs_init(void)
88 {
89         static gchar *path[3];
90         gchar *rcpath;
91
92         path[0] = _("Plugins");
93         path[1] = _("Mail Archiver");
94         path[2] = NULL;
95
96         prefs_set_default(param);
97         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
98         prefs_read_config(param, PREFS_BLOCK_NAME, rcpath, NULL);
99         g_free(rcpath);
100         
101         archiver_prefs_page.page.path = path;
102         archiver_prefs_page.page.create_widget = create_archiver_prefs_page;
103         archiver_prefs_page.page.destroy_widget = destroy_archiver_prefs_page;
104         archiver_prefs_page.page.save_page = save_archiver_prefs;
105         archiver_prefs_page.page.weight = 30.0;
106         
107         prefs_gtk_register_page((PrefsPage *) &archiver_prefs_page);
108 }
109
110 void archiver_prefs_done(void)
111 {
112         prefs_gtk_unregister_page((PrefsPage *) &archiver_prefs_page);
113 }
114
115 static void foldersel_cb(GtkWidget *widget, gpointer data)
116 {
117         struct ArchiverPrefsPage *page = (struct ArchiverPrefsPage *) data;
118         gchar *startdir = NULL;
119         gchar *dirname;
120         gchar *tmp;
121         
122         if (archiver_prefs.save_folder && *archiver_prefs.save_folder)
123                 startdir = g_strconcat(archiver_prefs.save_folder,
124                                        G_DIR_SEPARATOR_S, NULL);
125         else
126                 startdir = g_strdup(get_home_dir());
127
128         dirname = filesel_select_file_save_folder(_("Select destination folder"), startdir);
129         if (!dirname) {
130                 g_free(startdir);
131                 return;
132         }
133         if (!is_dir_exist(dirname)) {
134                 alertpanel_error(_("'%s' is not a directory."),dirname);
135                 g_free(dirname);
136                 g_free(startdir);
137                 return;
138         }
139         if (dirname[strlen(dirname)-1] == G_DIR_SEPARATOR)
140                 dirname[strlen(dirname)-1] = '\0';
141         g_free(startdir);
142
143         tmp =  g_filename_to_utf8(dirname,-1, NULL, NULL, NULL);
144         gtk_entry_set_text(GTK_ENTRY(page->save_folder), tmp);
145
146         g_free(dirname);
147         g_free(tmp);
148 }
149
150 static void create_archiver_prefs_page(PrefsPage * _page,
151                                        GtkWindow *window,
152                                        gpointer data)
153 {
154         struct ArchiverPrefsPage *page = (struct ArchiverPrefsPage *) _page;
155         GtkWidget *vbox1, *vbox2;
156         GtkWidget *hbox1;
157         GtkWidget *save_folder_label;
158         GtkWidget *save_folder;
159         GtkWidget *save_folder_select;
160         GtkWidget *frame;
161         GSList    *compression_group = NULL;
162         GtkWidget *zip_radiobtn;
163         GtkWidget *bzip_radiobtn;
164 #if NEW_ARCHIVE_API
165         GtkWidget *compress_radiobtn;
166 #endif
167         GtkWidget *none_radiobtn;
168         GSList    *format_group = NULL;
169         GtkWidget *tar_radiobtn;
170         GtkWidget *shar_radiobtn;
171         GtkWidget *cpio_radiobtn;
172         GtkWidget *pax_radiobtn;
173         GtkWidget *recursive_chkbtn;
174         GtkWidget *md5sum_chkbtn;
175         GtkWidget *rename_chkbtn;
176         GtkWidget *unlink_chkbtn;
177
178         vbox1 = gtk_vbox_new (FALSE, VSPACING);
179         gtk_widget_show (vbox1);
180         gtk_container_set_border_width (GTK_CONTAINER (vbox1), VBOX_BORDER);
181
182         vbox2 = gtk_vbox_new (FALSE, 4);
183         gtk_widget_show (vbox2);
184         gtk_box_pack_start (GTK_BOX (vbox1), vbox2, FALSE, FALSE, 0);
185
186         hbox1 = gtk_hbox_new (FALSE, 8);
187         gtk_widget_show (hbox1);
188         gtk_box_pack_start (GTK_BOX (vbox2), hbox1, FALSE, FALSE, 0);
189
190         save_folder_label = gtk_label_new(_("Default save folder"));
191         gtk_widget_show (save_folder_label);
192         gtk_box_pack_start (GTK_BOX (hbox1), save_folder_label, FALSE, FALSE, 0);
193
194         save_folder = gtk_entry_new ();
195         gtk_widget_show (save_folder);
196         gtk_box_pack_start (GTK_BOX (hbox1), save_folder, TRUE, TRUE, 0);
197
198         save_folder_select = gtkut_get_browse_directory_btn(_("_Select"));
199         gtk_widget_show (save_folder_select);
200         gtk_box_pack_start (GTK_BOX (hbox1), save_folder_select, FALSE, FALSE, 0);
201         CLAWS_SET_TIP(save_folder_select,
202                              _("Click this button to select the default location for saving archives"));
203
204         g_signal_connect(G_OBJECT(save_folder_select), "clicked", 
205                          G_CALLBACK(foldersel_cb), page);
206
207         if (archiver_prefs.save_folder != NULL)
208                 gtk_entry_set_text(GTK_ENTRY(save_folder),
209                                    archiver_prefs.save_folder);
210
211         PACK_FRAME (vbox1, frame, _("Default compression"));
212
213         hbox1 = gtk_hbox_new(FALSE, 4);
214         gtk_widget_show(hbox1);
215         gtk_container_set_border_width(GTK_CONTAINER(hbox1), 4);
216         gtk_container_add(GTK_CONTAINER(frame), hbox1);
217
218         zip_radiobtn = gtk_radio_button_new_with_label(compression_group, "ZIP");
219         compression_group = gtk_radio_button_get_group(GTK_RADIO_BUTTON(zip_radiobtn));
220         gtk_widget_show(zip_radiobtn);
221         gtk_box_pack_start(GTK_BOX (hbox1), zip_radiobtn, FALSE, FALSE, 0);
222         CLAWS_SET_TIP(zip_radiobtn,
223                         _("Choose this option to use ZIP compression by default"));
224
225         bzip_radiobtn = gtk_radio_button_new_with_label(compression_group, "BZIP2");
226         compression_group = gtk_radio_button_get_group(GTK_RADIO_BUTTON(bzip_radiobtn));
227         gtk_widget_show(bzip_radiobtn);
228         gtk_box_pack_start(GTK_BOX (hbox1), bzip_radiobtn, FALSE, FALSE, 0);
229         CLAWS_SET_TIP(bzip_radiobtn,
230                         _("Choose this option to use BZIP2 compression by default"));
231
232 #if NEW_ARCHIVE_API
233         compress_radiobtn = gtk_radio_button_new_with_label(compression_group, "COMPRESS");
234         compression_group = gtk_radio_button_get_group(GTK_RADIO_BUTTON(compress_radiobtn));
235         gtk_widget_show(compress_radiobtn);
236         gtk_box_pack_start(GTK_BOX (hbox1), compress_radiobtn, FALSE, FALSE, 0);
237         CLAWS_SET_TIP(compress_radiobtn,
238                         _("Choose this option to use COMPRESS compression by default"));
239 #endif
240
241         none_radiobtn = gtk_radio_button_new_with_label(compression_group, _("None"));
242         compression_group = gtk_radio_button_get_group(GTK_RADIO_BUTTON(none_radiobtn));
243         gtk_widget_show(none_radiobtn);
244         gtk_box_pack_start(GTK_BOX (hbox1), none_radiobtn, FALSE, FALSE, 0);
245         CLAWS_SET_TIP(none_radiobtn,
246                         _("Choose this option to disable compression by default"));
247
248         switch (archiver_prefs.compression) {
249         case COMPRESSION_ZIP:
250                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(zip_radiobtn), TRUE);
251                 break;
252         case COMPRESSION_BZIP:
253                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(bzip_radiobtn), TRUE);
254                 break;
255 #if NEW_ARCHIVE_API
256         case COMPRESSION_COMPRESS:       
257                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(compress_radiobtn), TRUE);
258                 break;
259 #endif
260         case COMPRESSION_NONE:
261                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(none_radiobtn), TRUE);
262                 break;
263         }
264
265         PACK_FRAME (vbox1, frame, _("Default format"));
266
267         hbox1 = gtk_hbox_new(FALSE, 4);
268         gtk_widget_show(hbox1);
269         gtk_container_set_border_width(GTK_CONTAINER(hbox1), 4);
270         gtk_container_add(GTK_CONTAINER(frame), hbox1);
271
272         tar_radiobtn = gtk_radio_button_new_with_label(format_group, "TAR");
273         format_group = gtk_radio_button_get_group(GTK_RADIO_BUTTON(tar_radiobtn));
274         gtk_widget_show(tar_radiobtn);
275         gtk_box_pack_start(GTK_BOX (hbox1), tar_radiobtn, FALSE, FALSE, 0);
276         CLAWS_SET_TIP(tar_radiobtn,
277                         _("Choose this option to use the TAR format by default"));
278
279         shar_radiobtn = gtk_radio_button_new_with_label(format_group, "SHAR");
280         format_group = gtk_radio_button_get_group(GTK_RADIO_BUTTON(shar_radiobtn));
281         gtk_widget_show(shar_radiobtn);
282         gtk_box_pack_start(GTK_BOX (hbox1), shar_radiobtn, FALSE, FALSE, 0);
283         CLAWS_SET_TIP(shar_radiobtn,
284                         _("Choose this option to use the SHAR format by default"));
285
286         cpio_radiobtn = gtk_radio_button_new_with_label(format_group, "CPIO");
287         format_group = gtk_radio_button_get_group(GTK_RADIO_BUTTON(cpio_radiobtn));
288         gtk_widget_show(cpio_radiobtn);
289         gtk_box_pack_start(GTK_BOX (hbox1), cpio_radiobtn, FALSE, FALSE, 0);
290         CLAWS_SET_TIP(cpio_radiobtn,
291                         _("Choose this option to use the CPIO format by default"));
292
293         pax_radiobtn = gtk_radio_button_new_with_label(format_group, "PAX");
294         format_group = gtk_radio_button_get_group(GTK_RADIO_BUTTON(pax_radiobtn));
295         gtk_widget_show(pax_radiobtn);
296         gtk_box_pack_start(GTK_BOX (hbox1), pax_radiobtn, FALSE, FALSE, 0);
297         CLAWS_SET_TIP(pax_radiobtn,
298                         _("Choose this option to use the PAX format by default"));
299
300         switch (archiver_prefs.format) {
301         case FORMAT_TAR:
302                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(tar_radiobtn), TRUE);
303                 break;
304         case FORMAT_SHAR:
305                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(shar_radiobtn), TRUE);
306                 break;
307         case FORMAT_CPIO:
308                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(cpio_radiobtn), TRUE);
309                 break;
310         case FORMAT_PAX:
311                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(pax_radiobtn), TRUE);
312                 break;
313         }
314
315         PACK_FRAME (vbox1, frame, _("Default miscellaneous options"));
316
317         hbox1 = gtk_hbox_new(FALSE, 4);
318         gtk_widget_show(hbox1);
319         gtk_container_set_border_width(GTK_CONTAINER(hbox1), 4);
320         gtk_container_add(GTK_CONTAINER(frame), hbox1);
321
322         PACK_CHECK_BUTTON(hbox1, recursive_chkbtn, _("Recursive"));
323         CLAWS_SET_TIP(recursive_chkbtn,
324                 _("Choose this option to include subfolders in the archives by default"));
325         PACK_CHECK_BUTTON(hbox1, md5sum_chkbtn, _("MD5sum"));
326         CLAWS_SET_TIP(md5sum_chkbtn,
327                 _("Choose this option to add MD5 checksums for each file in the archives by default.\n"
328                   "Be aware though, that this dramatically increases the time it\n"
329                   "will take to create the archives"));
330
331         PACK_CHECK_BUTTON(hbox1, rename_chkbtn, _("Rename"));
332         CLAWS_SET_TIP(rename_chkbtn,
333                 _("Choose this option to use descriptive names for each file in the archive.\n"
334                   "The naming scheme: date_from@to@subject.\n"
335                   "Names will be truncated to max 96 characters"));
336
337         PACK_CHECK_BUTTON(hbox1, unlink_chkbtn, _("Delete"));
338         CLAWS_SET_TIP(unlink_chkbtn,
339                 _("Choose this option to delete mails after archiving"));
340
341         if (archiver_prefs.recursive)
342                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(recursive_chkbtn), TRUE);
343         if (archiver_prefs.md5sum)
344                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(md5sum_chkbtn), TRUE);
345         if (archiver_prefs.rename)
346                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(rename_chkbtn), TRUE);
347         if (archiver_prefs.unlink)
348                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(unlink_chkbtn), TRUE);
349
350
351         page->save_folder = save_folder;
352         page->zip_radiobtn = zip_radiobtn;
353         page->bzip_radiobtn = bzip_radiobtn;
354 #if NEW_ARCHIVE_API
355         page->compress_radiobtn = compress_radiobtn;
356 #endif
357         page->none_radiobtn = none_radiobtn;
358         page->tar_radiobtn = tar_radiobtn;
359         page->shar_radiobtn = shar_radiobtn;
360         page->cpio_radiobtn = cpio_radiobtn;
361         page->pax_radiobtn = pax_radiobtn;
362         page->recursive_chkbtn = recursive_chkbtn;
363         page->md5sum_chkbtn = md5sum_chkbtn;
364         page->rename_chkbtn = rename_chkbtn;
365         page->unlink_chkbtn = unlink_chkbtn;
366
367         page->page.widget = vbox1;
368 }
369
370 static void destroy_archiver_prefs_page(PrefsPage *page)
371 {
372         /* Do nothing! */
373 }
374
375 static void save_archiver_prefs(PrefsPage * _page)
376 {
377         struct ArchiverPrefsPage *page = (struct ArchiverPrefsPage *) _page;
378         PrefFile *pref_file;
379         gchar *rc_file_path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
380                                           COMMON_RC, NULL);
381
382         archiver_prefs.save_folder = gtk_editable_get_chars(GTK_EDITABLE(page->save_folder), 0, -1);
383         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->zip_radiobtn)))
384                 archiver_prefs.compression = COMPRESSION_ZIP;
385         else if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->bzip_radiobtn)))
386                 archiver_prefs.compression = COMPRESSION_BZIP;
387 #if NEW_ARCHIVE_API
388         else if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->compress_radiobtn)))
389                 archiver_prefs.compression = COMPRESSION_COMPRESS;
390 #endif
391         else if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->none_radiobtn)))
392                 archiver_prefs.compression = COMPRESSION_NONE;
393
394         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->tar_radiobtn)))
395                 archiver_prefs.format = FORMAT_TAR;
396         else if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->shar_radiobtn)))
397                 archiver_prefs.format = FORMAT_SHAR;
398         else if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->cpio_radiobtn)))
399                 archiver_prefs.format = FORMAT_CPIO;
400         else if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->pax_radiobtn)))
401                 archiver_prefs.format = FORMAT_PAX;
402
403         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->recursive_chkbtn)))
404                 archiver_prefs.recursive = TRUE;
405         else
406                 archiver_prefs.recursive = FALSE;
407         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->md5sum_chkbtn)))
408                 archiver_prefs.md5sum = TRUE;
409         else
410                 archiver_prefs.md5sum = FALSE;
411         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->rename_chkbtn)))
412                 archiver_prefs.rename = TRUE;
413         else
414                 archiver_prefs.rename = FALSE;
415         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->unlink_chkbtn)))
416                 archiver_prefs.unlink = TRUE;
417         else
418                 archiver_prefs.unlink = FALSE;
419
420
421         pref_file = prefs_write_open(rc_file_path);
422         g_free(rc_file_path);
423         
424         if (!(pref_file) ||
425             (prefs_set_block_label(pref_file, PREFS_BLOCK_NAME) < 0))
426           return;
427         
428         if (prefs_write_param(param, pref_file->fp) < 0) {
429           g_warning("failed to write Archiver Plugin configuration\n");
430           prefs_file_close_revert(pref_file);
431           return;
432         }
433         if (fprintf(pref_file->fp, "\n") < 0) {
434                 FILE_OP_ERROR(rc_file_path, "fprintf");
435                 prefs_file_close_revert(pref_file);
436         } else
437                 prefs_file_close(pref_file);
438
439 }