Typo fix.
[geeqie.git] / src / utilops.c
1 /*
2  * Geeqie
3  * (C) 2006 John Ellis
4  * Copyright (C) 2008 The Geeqie Team
5  *
6  * Author: John Ellis
7  *
8  * This software is released under the GNU General Public License (GNU GPL).
9  * Please read the included file COPYING for more information.
10  * This software comes with no warranty of any kind, use at your own risk!
11  */
12
13
14 #include "main.h"
15 #include "utilops.h"
16
17
18 #include "cache.h"
19 #include "cache_maint.h"
20 #include "collect.h"
21 #include "dupe.h"
22 #include "filedata.h"
23 #include "filefilter.h"
24 #include "image.h"
25 #include "img-view.h"
26 #include "layout.h"
27 #include "search.h"
28 #include "thumb_standard.h"
29 #include "trash.h"
30 #include "ui_bookmark.h"
31 #include "ui_fileops.h"
32 #include "ui_misc.h"
33 #include "ui_tabcomp.h"
34 #include "editors.h"
35
36 /*
37  *--------------------------------------------------------------------------
38  * Adds 1 or 2 images (if 2, side by side) to a GenericDialog
39  *--------------------------------------------------------------------------
40  */
41
42 #define DIALOG_DEF_IMAGE_DIM_X 200
43 #define DIALOG_DEF_IMAGE_DIM_Y 150
44
45 static void generic_dialog_add_image(GenericDialog *gd, GtkWidget *box,
46                                      FileData *fd1, const gchar *header1,
47                                      FileData *fd2, const gchar *header2,
48                                      gint show_filename)
49 {
50         ImageWindow *imd;
51         GtkWidget *hbox = NULL;
52         GtkWidget *vbox;
53         GtkWidget *label = NULL;
54
55         if (!box) box = gd->vbox;
56
57         if (fd2)
58                 {
59                 hbox = pref_box_new(box, TRUE, GTK_ORIENTATION_HORIZONTAL, PREF_PAD_SPACE);
60                 }
61
62         /* image 1 */
63
64         vbox = gtk_vbox_new(FALSE, PREF_PAD_GAP);
65         if (hbox)
66                 {
67                 GtkWidget *sep;
68
69                 gtk_box_pack_start(GTK_BOX(hbox), vbox, TRUE, TRUE, 0);
70
71                 sep = gtk_vseparator_new();
72                 gtk_box_pack_start(GTK_BOX(hbox), sep, FALSE, FALSE, 0);
73                 gtk_widget_show(sep);
74                 }
75         else
76                 {
77                 gtk_box_pack_start(GTK_BOX(box), vbox, TRUE, TRUE, PREF_PAD_GAP);
78                 }
79         gtk_widget_show(vbox);
80
81         if (header1)
82                 {
83                 GtkWidget *head;
84
85                 head = pref_label_new(vbox, header1);
86                 pref_label_bold(head, TRUE, FALSE);
87                 gtk_misc_set_alignment(GTK_MISC(head), 0.0, 0.5);
88                 }
89
90         imd = image_new(FALSE);
91         g_object_set(G_OBJECT(imd->pr), "zoom_expand", FALSE, NULL);
92         gtk_widget_set_size_request(imd->widget, DIALOG_DEF_IMAGE_DIM_X, DIALOG_DEF_IMAGE_DIM_Y);
93         gtk_box_pack_start(GTK_BOX(vbox), imd->widget, TRUE, TRUE, 0);
94         image_change_fd(imd, fd1, 0.0);
95         gtk_widget_show(imd->widget);
96
97         if (show_filename)
98                 {
99                 label = pref_label_new(vbox, (fd1 == NULL) ? "" : fd1->name);
100                 }
101
102         /* only the first image is stored (for use in gd_image_set) */
103         g_object_set_data(G_OBJECT(gd->dialog), "img_image", imd);
104         g_object_set_data(G_OBJECT(gd->dialog), "img_label", label);
105
106
107         /* image 2 */
108
109         if (hbox && fd2)
110                 {
111                 vbox = pref_box_new(hbox, TRUE, GTK_ORIENTATION_VERTICAL, PREF_PAD_GAP);
112
113                 if (header2)
114                         {
115                         GtkWidget *head;
116
117                         head = pref_label_new(vbox, header2);
118                         pref_label_bold(head, TRUE, FALSE);
119                         gtk_misc_set_alignment(GTK_MISC(head), 0.0, 0.5);
120                         }
121
122                 imd = image_new(FALSE);
123                 g_object_set(G_OBJECT(imd->pr), "zoom_expand", FALSE, NULL);
124                 gtk_widget_set_size_request(imd->widget, DIALOG_DEF_IMAGE_DIM_X, DIALOG_DEF_IMAGE_DIM_Y);
125                 gtk_box_pack_start(GTK_BOX(vbox), imd->widget, TRUE, TRUE, 0);
126                 image_change_fd(imd, fd2, 0.0);
127                 gtk_widget_show(imd->widget);
128
129                 pref_label_new(vbox, fd2->name);
130                 }
131 }
132
133 static void generic_dialog_image_set(GenericDialog *gd, FileData *fd)
134 {
135         ImageWindow *imd;
136         GtkWidget *label;
137
138         imd = g_object_get_data(G_OBJECT(gd->dialog), "img_image");
139         label = g_object_get_data(G_OBJECT(gd->dialog), "img_label");
140
141         if (!imd) return;
142
143         image_change_fd(imd, fd, 0.0);
144         if (label) gtk_label_set_text(GTK_LABEL(label), fd->name);
145 }
146
147 /*
148  *--------------------------------------------------------------------------
149  * Wrappers to aid in setting additional dialog properties (unde mouse, etc.)
150  *--------------------------------------------------------------------------
151  */
152
153 GenericDialog *file_util_gen_dlg(const gchar *title,
154                                  const gchar *wmclass, const gchar *wmsubclass,
155                                  GtkWidget *parent, gint auto_close,
156                                  void (*cancel_cb)(GenericDialog *, gpointer), gpointer data)
157 {
158         GenericDialog *gd;
159
160         gd = generic_dialog_new(title, wmclass, wmsubclass, parent, auto_close, cancel_cb, data);
161         if (options->place_dialogs_under_mouse)
162                 {
163                 gtk_window_set_position(GTK_WINDOW(gd->dialog), GTK_WIN_POS_MOUSE);
164                 }
165
166         return gd;
167 }
168
169 FileDialog *file_util_file_dlg(const gchar *title,
170                                const gchar *wmclass, const gchar *wmsubclass,
171                                GtkWidget *parent,
172                                void (*cancel_cb)(FileDialog *, gpointer), gpointer data)
173 {
174         FileDialog *fdlg;
175
176         fdlg = file_dialog_new(title, wmclass, wmsubclass, parent, cancel_cb, data);
177         if (options->place_dialogs_under_mouse)
178                 {
179                 gtk_window_set_position(GTK_WINDOW(GENERIC_DIALOG(fdlg)->dialog), GTK_WIN_POS_MOUSE);
180                 }
181
182         return fdlg;
183 }
184
185 /* this warning dialog is copied from SLIK's ui_utildg.c,
186  * because it does not have a mouse center option,
187  * and we must center it before show, implement it here.
188  */
189 static void file_util_warning_dialog_ok_cb(GenericDialog *gd, gpointer data)
190 {
191         /* no op */
192 }
193
194 GenericDialog *file_util_warning_dialog(const gchar *heading, const gchar *message,
195                                         const gchar *icon_stock_id, GtkWidget *parent)
196 {
197         GenericDialog *gd;
198
199         gd = file_util_gen_dlg(heading, GQ_WMCLASS, "warning", parent, TRUE, NULL, NULL);
200         generic_dialog_add_message(gd, icon_stock_id, heading, message);
201         generic_dialog_add_button(gd, GTK_STOCK_OK, NULL, file_util_warning_dialog_ok_cb, TRUE);
202         if (options->place_dialogs_under_mouse)
203                 {
204                 gtk_window_set_position(GTK_WINDOW(gd->dialog), GTK_WIN_POS_MOUSE);
205                 }
206         gtk_widget_show(gd->dialog);
207
208         return gd;
209 }
210
211 static gint filename_base_length(const gchar *name)
212 {
213         gint n;
214
215         if (!name) return 0;
216
217         n = strlen(name);
218
219         if (filter_name_exists(name))
220                 {
221                 const gchar *ext;
222
223                 ext = extension_from_path(name);
224                 if (ext) n -= strlen(ext);
225                 }
226
227         return n;
228 }
229
230
231
232 /*
233  *--------------------------------------------------------------------------
234  * call these when names change, files move, deleted, etc.
235  * so that any open windows are also updated
236  *--------------------------------------------------------------------------
237  */
238
239 /* FIXME this is a temporary solution */
240 void file_data_notify_ci(FileData *fd)
241 {
242         FileDataChangeType type = fd->change->type;
243         switch (type)
244                 {
245                 case FILEDATA_CHANGE_MOVE:
246                         cache_maint_moved(fd);
247                         collection_maint_renamed(fd);
248
249                         layout_maint_moved(fd, NULL);
250                         view_window_maint_moved(fd);
251                         dupe_maint_renamed(fd);
252                         search_maint_renamed(fd);
253                         break;
254                 case FILEDATA_CHANGE_COPY:
255                         cache_maint_copied(fd);
256                         break;
257                 case FILEDATA_CHANGE_RENAME:
258                         cache_maint_moved(fd);
259                         collection_maint_renamed(fd);
260
261                         layout_maint_renamed(fd);
262                         view_window_maint_moved(fd);
263                         dupe_maint_renamed(fd);
264                         search_maint_renamed(fd);
265                 case FILEDATA_CHANGE_DELETE:
266                         layout_maint_removed(fd, NULL);
267                         view_window_maint_removed(fd, NULL);
268                         dupe_maint_removed(fd);
269                         search_maint_removed(fd);
270
271                         collection_maint_removed(fd);
272                         cache_maint_removed(fd);
273                         break;
274                 case FILEDATA_CHANGE_UNSPECIFIED:
275                         /* FIXME */
276                         break;
277                 }
278 }
279
280 void file_data_sc_notify_ci(FileData *fd)
281 {
282         GList *work;
283         if (fd->parent) fd = fd->parent;
284         
285         file_data_notify_ci(fd);
286         
287         work = fd->sidecar_files;
288         while (work)
289                 {
290                 FileData *sfd = work->data;
291                 file_data_notify_ci(sfd);
292                 work = work->next;
293                 }
294 }
295
296
297 typedef enum {
298         UTILITY_TYPE_COPY,
299         UTILITY_TYPE_MOVE,
300         UTILITY_TYPE_RENAME,
301         UTILITY_TYPE_EDITOR,
302         UTILITY_TYPE_FILTER,
303         UTILITY_TYPE_DELETE,
304         UTILITY_TYPE_DELETE_LINK,
305         UTILITY_TYPE_DELETE_FOLDER
306 } UtilityType;
307
308 typedef enum {
309         UTILITY_PHASE_START = 0,
310         UTILITY_PHASE_ENTERING,
311         UTILITY_PHASE_CHECKED,
312         UTILITY_PHASE_DONE,
313         UTILITY_PHASE_CANCEL
314 } UtilityPhase;
315
316 enum {
317         UTILITY_RENAME = 0,
318         UTILITY_RENAME_AUTO,
319         UTILITY_RENAME_FORMATTED
320 };
321
322 typedef struct _UtilityDataMessages UtilityDataMessages;
323 struct _UtilityDataMessages {
324         gchar *title;
325         gchar *question;
326         gchar *desc_flist;
327         gchar *desc_dlist;
328         gchar *desc_source_fd;
329         gchar *fail;
330 };
331
332 typedef struct _UtilityData UtilityData;
333
334 struct _UtilityData {
335         UtilityType type;
336         UtilityPhase phase;
337         
338         FileData *source_fd;
339         GList *dlist;
340         GList *flist;
341
342         GtkWidget *parent;
343         GenericDialog *gd;
344         FileDialog *fdlg;
345         
346         gint update_idle_id;
347         
348         /* alternative dialog parts */
349         GtkWidget *notebook;
350
351         UtilityDataMessages messages;
352
353         /* helper entries for various modes */
354         GtkWidget *rename_entry;
355         GtkWidget *rename_label;
356         GtkWidget *auto_entry_front;
357         GtkWidget *auto_entry_end;
358         GtkWidget *auto_spin_start;
359         GtkWidget *auto_spin_pad;
360         GtkWidget *format_entry;
361         GtkWidget *format_spin;
362
363         GtkWidget *listview;
364
365
366         gchar *dest_path;
367
368         /* data for the operation itself, internal or external */
369         gboolean external; /* TRUE for external command, FALSE for internal */
370         
371         gint external_command;
372         gpointer resume_data;
373 };
374
375 enum {
376         UTILITY_COLUMN_FD = 0,
377         UTILITY_COLUMN_PATH,
378         UTILITY_COLUMN_NAME,
379         UTILITY_COLUMN_SIDECARS,
380         UTILITY_COLUMN_DEST_PATH,
381         UTILITY_COLUMN_DEST_NAME,
382         UTILITY_COLUMN_COUNT
383 };
384
385 #define UTILITY_LIST_MIN_WIDTH  250
386 #define UTILITY_LIST_MIN_HEIGHT 150
387
388 /* thumbnail spec has a max depth of 4 (.thumb??/fail/appname/??.png) */
389 #define UTILITY_DELETE_MAX_DEPTH 5
390
391 static UtilityData *file_util_data_new(UtilityType type)
392 {
393         UtilityData *ud;
394
395         ud = g_new0(UtilityData, 1);
396         ud->type = type;
397         ud->phase = UTILITY_PHASE_START;
398         ud->update_idle_id = -1;
399         ud->external_command = -1;
400         return ud;
401 }
402
403 static void file_util_data_free(UtilityData *ud)
404 {
405         if (!ud) return;
406
407         if (ud->update_idle_id != -1) g_source_remove(ud->update_idle_id);
408
409         file_data_unref(ud->source_fd);
410         filelist_free(ud->dlist);
411         filelist_free(ud->flist);
412
413         if (ud->gd) generic_dialog_close(ud->gd);
414         
415         g_free(ud->dest_path);
416
417         g_free(ud);
418 }
419
420 static GtkTreeViewColumn *file_util_dialog_add_list_column(GtkWidget *view, const gchar *text, gint n)
421 {
422         GtkTreeViewColumn *column;
423         GtkCellRenderer *renderer;
424
425         column = gtk_tree_view_column_new();
426         gtk_tree_view_column_set_title(column, text);
427         gtk_tree_view_column_set_min_width(column, 4);
428         gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_GROW_ONLY);
429         renderer = gtk_cell_renderer_text_new();
430         gtk_tree_view_column_pack_start(column, renderer, TRUE);
431         gtk_tree_view_column_add_attribute(column, renderer, "text", n);
432         gtk_tree_view_append_column(GTK_TREE_VIEW(view), column);
433
434         return column;
435 }
436
437 static void file_util_dialog_list_select(GtkWidget *view, gint n)
438 {
439         GtkTreeModel *store;
440         GtkTreeIter iter;
441         GtkTreeSelection *selection;
442
443         store = gtk_tree_view_get_model(GTK_TREE_VIEW(view));
444         if (gtk_tree_model_iter_nth_child(store, &iter, NULL, n))
445                 {
446                 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(view));
447                 gtk_tree_selection_select_iter(selection, &iter);
448                 }
449 }
450
451 static GtkWidget *file_util_dialog_add_list(GtkWidget *box, GList *list, gint full_paths)
452 {
453         GtkWidget *scrolled;
454         GtkWidget *view;
455         GtkListStore *store;
456
457         scrolled = gtk_scrolled_window_new(NULL, NULL);
458         gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled), GTK_SHADOW_IN);
459         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled),
460                                        GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
461         gtk_box_pack_start(GTK_BOX(box), scrolled, TRUE, TRUE, 0);
462         gtk_widget_show(scrolled);
463
464         store = gtk_list_store_new(UTILITY_COLUMN_COUNT, G_TYPE_POINTER, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);
465         view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
466         g_object_unref(store);
467
468         gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(view), TRUE);
469         gtk_tree_view_set_enable_search(GTK_TREE_VIEW(view), FALSE);
470
471         if (full_paths)
472                 {
473                 file_util_dialog_add_list_column(view, _("Location"), UTILITY_COLUMN_PATH);
474                 }
475         else
476                 {
477                 file_util_dialog_add_list_column(view, _("Name"), UTILITY_COLUMN_NAME);
478                 }
479
480         gtk_widget_set_size_request(view, UTILITY_LIST_MIN_WIDTH, UTILITY_LIST_MIN_HEIGHT);
481         gtk_container_add(GTK_CONTAINER(scrolled), view);
482         gtk_widget_show(view);
483
484         while (list)
485                 {
486                 FileData *fd = list->data;
487                 GtkTreeIter iter;
488                 gchar *sidecars;
489                 
490                 sidecars = file_data_sc_list_to_string(fd);
491
492                 gtk_list_store_append(store, &iter);
493                 gtk_list_store_set(store, &iter, UTILITY_COLUMN_FD, fd,
494                                                  UTILITY_COLUMN_PATH, fd->path,
495                                                  UTILITY_COLUMN_NAME, fd->name,
496                                                  UTILITY_COLUMN_SIDECARS, sidecars,
497                                                  UTILITY_COLUMN_DEST_PATH, fd->change ? fd->change->dest : "error",
498                                                  UTILITY_COLUMN_DEST_NAME, fd->change ? filename_from_path(fd->change->dest) : "error",
499                                                  -1);
500                 g_free(sidecars);
501
502                 list = list->next;
503                 }
504
505         return view;
506 }
507
508 // FIXME
509 static void file_util_delete_dir_ok_cb(GenericDialog *gd, gpointer data);
510
511 void file_util_perform_ci_internal(UtilityData *ud);
512 void file_util_dialog_run(UtilityData *ud);
513 static gint file_util_perform_ci_cb(gpointer resume_data, gint flags, GList *list, gpointer data);
514
515 /* call file_util_perform_ci_internal or start_editor_from_filelist_full */
516
517
518 static void file_util_resume_cb(GenericDialog *gd, gpointer data)
519 {
520         UtilityData *ud = data;
521         if (ud->external)
522                 editor_resume(ud->resume_data);
523         else
524                 file_util_perform_ci_internal(ud);
525 }
526
527 static void file_util_abort_cb(GenericDialog *gd, gpointer data)
528 {
529         UtilityData *ud = data;
530         if (ud->external)
531                 editor_skip(ud->resume_data);
532         else
533                 file_util_perform_ci_cb(NULL, EDITOR_ERROR_SKIPPED, ud->flist, ud);
534
535 }
536
537
538 static gint file_util_perform_ci_cb(gpointer resume_data, gint flags, GList *list, gpointer data)
539 {
540         UtilityData *ud = data;
541         ud->resume_data = resume_data;
542         
543         gint ret = EDITOR_CB_CONTINUE;
544         if ((flags & EDITOR_ERROR_MASK) && !(flags & EDITOR_ERROR_SKIPPED))
545                 {
546                         GString *msg = g_string_new(editor_get_error_str(flags));
547                         GenericDialog *d;
548                         g_string_append(msg, "\n");
549                         g_string_append(msg, ud->messages.fail);
550                         g_string_append(msg, "\n");
551                         while (list)
552                                 {
553                                 FileData *fd = list->data;
554
555                                 g_string_append(msg, fd->path);
556                                 g_string_append(msg, "\n");
557                                 list = list->next;
558                                 }
559                         if (resume_data)
560                                 {
561                                 g_string_append(msg, _("\n Continue multiple file operation?"));
562                                 d = file_util_gen_dlg(ud->messages.fail, GQ_WMCLASS, "dlg_confirm",
563                                                       NULL, TRUE,
564                                                       file_util_abort_cb, ud);
565
566                                 generic_dialog_add_message(d, GTK_STOCK_DIALOG_WARNING, NULL, msg->str);
567
568                                 generic_dialog_add_button(d, GTK_STOCK_GO_FORWARD, _("Co_ntinue"),
569                                                           file_util_resume_cb, TRUE);
570                                 gtk_widget_show(d->dialog);
571                                 ret = EDITOR_CB_SUSPEND;
572                                 }
573                         else
574                                 {
575                                 file_util_warning_dialog(ud->messages.fail, msg->str, GTK_STOCK_DIALOG_ERROR, NULL);
576                                 }
577                         g_string_free(msg, TRUE);
578                 }
579
580
581         while (list)  /* be careful, file_util_perform_ci_internal can pass ud->flist as list */
582                 {
583                 FileData *fd = list->data;
584                 list = list->next; 
585
586                 if (!(flags & EDITOR_ERROR_MASK)) /* files were successfully deleted, call the maint functions */
587                         file_data_sc_notify_ci(fd);
588                 
589                 ud->flist = g_list_remove(ud->flist, fd);
590                 file_data_sc_free_ci(fd);
591                 file_data_unref(fd);
592                 }
593                 
594         if (!resume_data) /* end of the list */
595                 {
596                 ud->phase = UTILITY_PHASE_DONE;
597                 file_util_dialog_run(ud);
598                 }
599         
600         return ret;
601 }
602
603
604 /*
605  * Perform the operation described by FileDataChangeInfo on all files in the list
606  * it is an alternative to start_editor_from_filelist_full, it should use similar interface
607  */ 
608
609
610 void file_util_perform_ci_internal(UtilityData *ud)
611 {
612         
613         while (ud->flist)
614                 {
615                 gint ret;
616                 
617                 /* take a single entry each time, this allows better control over the operation */
618                 GList *single_entry = g_list_append(NULL, ud->flist->data);
619                 gboolean last = !ud->flist->next;
620                 
621                 if (file_data_sc_perform_ci(single_entry->data))
622                         {
623                         ret = file_util_perform_ci_cb(GINT_TO_POINTER(!last), 0 /* OK */, single_entry, ud);
624                         }
625                 else
626                         {
627                         ret = file_util_perform_ci_cb(GINT_TO_POINTER(!last), EDITOR_ERROR_STATUS, single_entry, ud);
628                         }
629                 g_list_free(single_entry);
630                 
631                 if (ret == EDITOR_CB_SUSPEND || last) return;
632                 
633                 if (ret == EDITOR_CB_SKIP)
634                         {
635                         file_util_perform_ci_cb(NULL, EDITOR_ERROR_SKIPPED, ud->flist, ud);
636                         }
637                 
638                 /* FIXME: convert the loop to idle call */
639                 
640                 }
641 }
642
643
644 void file_util_perform_ci(UtilityData *ud)
645 {
646         switch (ud->type)
647                 {
648                 case UTILITY_TYPE_COPY:
649                         ud->external_command = CMD_COPY;
650                         break;
651                 case UTILITY_TYPE_MOVE:
652                         ud->external_command = CMD_MOVE;
653                         break;
654                 case UTILITY_TYPE_RENAME:
655                         ud->external_command = CMD_RENAME;
656                         break;
657                 case UTILITY_TYPE_DELETE:
658                 case UTILITY_TYPE_DELETE_LINK:
659                 case UTILITY_TYPE_DELETE_FOLDER:
660                         ud->external_command = CMD_DELETE;
661                         break;
662                 case UTILITY_TYPE_FILTER:
663                 case UTILITY_TYPE_EDITOR:
664                         g_assert(ud->external_command != -1); /* it should be already set */
665                         break;
666                 }
667
668         if (ud->external_command != -1 && options->editor[ud->external_command].command)
669                 {
670                 gint flags;
671                 ud->external = TRUE;
672                 if ((flags = start_editor_from_filelist_full(ud->external_command, ud->flist, file_util_perform_ci_cb, ud)))
673                         {
674                         gchar *text = g_strdup_printf(_("%s\nUnable to start external command.\n"), editor_get_error_str(flags));
675                         file_util_warning_dialog(ud->messages.fail, text, GTK_STOCK_DIALOG_ERROR, NULL);
676                         g_free(text);
677                         }
678                 }
679         else
680                 {
681                 ud->external = FALSE;
682                 file_util_perform_ci_internal(ud);
683                 }
684 }
685
686
687
688
689 static void  file_util_cancel_cb(GenericDialog *gd, gpointer data)
690 {
691         UtilityData *ud = data;
692         
693         generic_dialog_close(gd);
694
695         ud->gd = NULL;
696         
697         ud->phase = UTILITY_PHASE_CANCEL;
698         file_util_dialog_run(ud);
699 }
700
701 static void  file_util_ok_cb(GenericDialog *gd, gpointer data)
702 {
703         UtilityData *ud = data;
704         
705         generic_dialog_close(gd);
706         
707         ud->gd = NULL;
708
709         file_util_dialog_run(ud);
710 }
711
712 static void  file_util_fdlg_cancel_cb(FileDialog *fdlg, gpointer data)
713 {
714         UtilityData *ud = data;
715         
716         file_dialog_close(fdlg);
717
718         ud->fdlg = NULL;
719         
720         ud->phase = UTILITY_PHASE_CANCEL;
721         file_util_dialog_run(ud);
722 }
723
724 static void  file_util_fdlg_ok_cb(FileDialog *fdlg, gpointer data)
725 {
726         UtilityData *ud = data;
727         
728         file_dialog_close(fdlg);
729         
730         ud->fdlg = NULL;
731
732         file_util_dialog_run(ud);
733 }
734
735
736 static void file_util_dest_folder_entry_cb(GtkWidget *entry, gpointer data)
737 {
738         UtilityData *ud = data;
739         
740         g_free(ud->dest_path);
741         ud->dest_path = g_strdup(gtk_entry_get_text(GTK_ENTRY(entry)));
742         
743         switch (ud->type)
744                 {
745                 case UTILITY_TYPE_COPY:
746                         file_data_sc_update_ci_copy_list(ud->flist, ud->dest_path);
747                         break;
748                 case UTILITY_TYPE_MOVE:
749                         file_data_sc_update_ci_move_list(ud->flist, ud->dest_path);
750                         break;
751                 case UTILITY_TYPE_FILTER:
752                 case UTILITY_TYPE_EDITOR:
753                         file_data_sc_update_ci_unspecified_list(ud->flist, ud->dest_path);
754                         break;
755                 case UTILITY_TYPE_DELETE:
756                 case UTILITY_TYPE_DELETE_LINK:
757                 case UTILITY_TYPE_DELETE_FOLDER:
758                 case UTILITY_TYPE_RENAME:
759                         g_warning("unhandled operation");
760                 }
761 }
762
763
764 /* format: * = filename without extension, ## = number position, extension is kept */
765 static gchar *file_util_rename_multiple_auto_format_name(const gchar *format, const gchar *name, gint n)
766 {
767         gchar *new_name;
768         gchar *parsed;
769         const gchar *ext;
770         gchar *middle;
771         gchar *tmp;
772         gchar *pad_start;
773         gchar *pad_end;
774         gint padding;
775
776         if (!format || !name) return NULL;
777
778         tmp = g_strdup(format);
779         pad_start = strchr(tmp, '#');
780         if (pad_start)
781                 {
782                 pad_end = pad_start;
783                 padding = 0;
784                 while (*pad_end == '#')
785                         {
786                         pad_end++;
787                         padding++;
788                         }
789                 *pad_start = '\0';
790
791                 parsed = g_strdup_printf("%s%0*d%s", tmp, padding, n, pad_end);
792                 g_free(tmp);
793                 }
794         else
795                 {
796                 parsed = tmp;
797                 }
798
799         ext = extension_from_path(name);
800
801         middle = strchr(parsed, '*');
802         if (middle)
803                 {
804                 gchar *base;
805
806                 *middle = '\0';
807                 middle++;
808
809                 base = remove_extension_from_path(name);
810                 new_name = g_strconcat(parsed, base, middle, ext, NULL);
811                 g_free(base);
812                 }
813         else
814                 {
815                 new_name = g_strconcat(parsed, ext, NULL);
816                 }
817
818         g_free(parsed);
819
820         return new_name;
821 }
822
823
824 static void file_util_rename_preview_update(UtilityData *ud)
825 {
826         GtkTreeModel *store;
827         GtkTreeSelection *selection;
828         GtkTreeIter iter;
829         const gchar *front;
830         const gchar *end;
831         const gchar *format;
832         gint valid;
833         gint start_n;
834         gint padding;
835         gint n;
836         gint mode;
837
838         mode = gtk_notebook_get_current_page(GTK_NOTEBOOK(ud->notebook));
839
840         if (mode == UTILITY_RENAME)
841                 {
842                 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(ud->listview));
843                 if (gtk_tree_selection_get_selected(selection, &store, &iter))
844                         {
845                         FileData *fd;
846                         const gchar *dest = gtk_entry_get_text(GTK_ENTRY(ud->rename_entry));
847                         
848                         gtk_tree_model_get(store, &iter, UTILITY_COLUMN_FD, &fd, -1);
849                         file_data_sc_update_ci_rename(fd, dest);
850                         gtk_list_store_set(GTK_LIST_STORE(store), &iter, 
851                                            UTILITY_COLUMN_DEST_PATH, fd->change->dest,
852                                            UTILITY_COLUMN_DEST_NAME, filename_from_path(fd->change->dest),
853                                            -1);
854                         }
855                 return;
856                 }
857
858
859         front = gtk_entry_get_text(GTK_ENTRY(ud->auto_entry_front));
860         end = gtk_entry_get_text(GTK_ENTRY(ud->auto_entry_end));
861         padding = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(ud->auto_spin_pad));
862
863         format = gtk_entry_get_text(GTK_ENTRY(ud->format_entry));
864
865         if (mode == UTILITY_RENAME_FORMATTED)
866                 {
867                 start_n = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(ud->format_spin));
868                 }
869         else
870                 {
871                 start_n = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(ud->auto_spin_start));
872                 }
873
874         store = gtk_tree_view_get_model(GTK_TREE_VIEW(ud->listview));
875         n = start_n;
876         valid = gtk_tree_model_get_iter_first(store, &iter);
877         while (valid)
878                 {
879                 gchar *dest;
880                 FileData *fd;
881                 gtk_tree_model_get(store, &iter, UTILITY_COLUMN_FD, &fd, -1);
882
883                 if (mode == UTILITY_RENAME_FORMATTED)
884                         {
885                         dest = file_util_rename_multiple_auto_format_name(format, fd->name, n);
886                         }
887                 else
888                         {
889                         dest = g_strdup_printf("%s%0*d%s", front, padding, n, end);
890                         }
891
892                 file_data_sc_update_ci_rename(fd, dest);
893                 gtk_list_store_set(GTK_LIST_STORE(store), &iter, 
894                                    UTILITY_COLUMN_DEST_PATH, fd->change->dest,
895                                    UTILITY_COLUMN_DEST_NAME, filename_from_path(fd->change->dest),
896                                    -1);
897                 g_free(dest);
898
899                 n++;
900                 valid = gtk_tree_model_iter_next(store, &iter);
901                 }
902
903 }
904
905 static void file_util_rename_preview_entry_cb(GtkWidget *entry, gpointer data)
906 {
907         UtilityData *ud = data;
908         file_util_rename_preview_update(ud);
909 }
910
911 static void file_util_rename_preview_adj_cb(GtkWidget *spin, gpointer data)
912 {
913         UtilityData *ud = data;
914         file_util_rename_preview_update(ud);
915 }
916
917 static gboolean file_util_rename_idle_cb(gpointer data)
918 {
919         UtilityData *ud = data;
920
921         file_util_rename_preview_update(ud);
922
923         ud->update_idle_id = -1;
924         return FALSE;
925 }
926
927 static void file_util_rename_preview_order_cb(GtkTreeModel *treemodel, GtkTreePath *tpath,
928                                                        GtkTreeIter *iter, gpointer data)
929 {
930         UtilityData *ud = data;
931
932         if (ud->update_idle_id == -1)
933                 {
934                 ud->update_idle_id = g_idle_add(file_util_rename_idle_cb, ud);
935                 }
936 }
937
938
939 static gboolean file_util_preview_cb(GtkTreeSelection *selection, GtkTreeModel *store,
940                                                 GtkTreePath *tpath, gboolean path_currently_selected,
941                                                 gpointer data)
942 {
943         UtilityData *ud = data;
944         GtkTreeIter iter;
945         FileData *fd = NULL;
946
947         if (path_currently_selected ||
948             !gtk_tree_model_get_iter(store, &iter, tpath)) return TRUE;
949
950         gtk_tree_model_get(store, &iter, UTILITY_COLUMN_FD, &fd, -1);
951         generic_dialog_image_set(ud->gd, fd);
952         
953         if (ud->type == UTILITY_TYPE_RENAME)
954                 {
955                 const gchar *name = filename_from_path(fd->change->dest);
956                 gtk_widget_grab_focus(ud->rename_entry);
957                 gtk_label_set_text(GTK_LABEL(ud->rename_label), fd->name);
958                 g_signal_handlers_block_by_func(ud->rename_entry, G_CALLBACK(file_util_rename_preview_entry_cb), ud);
959                 gtk_entry_set_text(GTK_ENTRY(ud->rename_entry), name);
960                 gtk_editable_select_region(GTK_EDITABLE(ud->rename_entry), 0, filename_base_length(name));
961                 g_signal_handlers_unblock_by_func(ud->rename_entry, G_CALLBACK(file_util_rename_preview_entry_cb), ud);
962                 }
963
964         return TRUE;
965 }
966
967
968
969 static void box_append_safe_delete_status(GenericDialog *gd)
970 {
971         GtkWidget *label;
972         gchar *buf;
973
974         buf = file_util_safe_delete_status();
975         label = pref_label_new(gd->vbox, buf);
976         g_free(buf);
977
978         gtk_misc_set_alignment(GTK_MISC(label), 1.0, 0.5);
979         gtk_widget_set_sensitive(label, FALSE);
980 }
981
982
983 static void file_util_dialog_init_simple_list(UtilityData *ud)
984 {
985         GtkWidget *box;
986         GtkTreeSelection *selection;
987         ud->gd = file_util_gen_dlg(ud->messages.title, GQ_WMCLASS, "dlg_confirm",
988                                            ud->parent, FALSE,  file_util_cancel_cb, ud);
989         generic_dialog_add_button(ud->gd, GTK_STOCK_DELETE, NULL, file_util_ok_cb, TRUE);
990
991         box = generic_dialog_add_message(ud->gd, GTK_STOCK_DIALOG_QUESTION,
992                                                  ud->messages.question,
993                                                  ud->messages.desc_flist);
994
995         box = pref_group_new(box, TRUE, ud->messages.desc_flist, GTK_ORIENTATION_HORIZONTAL);
996
997         ud->listview = file_util_dialog_add_list(box, ud->flist, FALSE);
998         file_util_dialog_add_list_column(ud->listview, _("Sidecars"), UTILITY_COLUMN_SIDECARS);
999
1000         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(ud->listview));
1001         gtk_tree_selection_set_mode(selection, GTK_SELECTION_SINGLE);
1002         gtk_tree_selection_set_select_function(selection, file_util_preview_cb, ud, NULL);
1003
1004         generic_dialog_add_image(ud->gd, box, NULL, NULL, NULL, NULL, FALSE);
1005
1006         box_append_safe_delete_status(ud->gd);
1007
1008         gtk_widget_show(ud->gd->dialog);
1009
1010         file_util_dialog_list_select(ud->listview, 0);
1011 }
1012
1013 static void file_util_dialog_init_dest_folder(UtilityData *ud)
1014 {
1015         FileDialog *fdlg;
1016         GtkWidget *label;
1017         const gchar *stock_id;
1018
1019         if (ud->type == UTILITY_TYPE_COPY) 
1020                 {
1021                 stock_id = GTK_STOCK_COPY;
1022                 }
1023         else
1024                 {
1025                 stock_id = GTK_STOCK_OK;
1026                 }
1027
1028         fdlg = file_util_file_dlg(ud->messages.title, GQ_WMCLASS, "dlg_dest_folder", ud->parent,
1029                                 file_util_fdlg_cancel_cb, ud);
1030         
1031         ud->fdlg = fdlg;
1032         
1033         generic_dialog_add_message(GENERIC_DIALOG(fdlg), NULL, ud->messages.question, NULL);
1034
1035         label = pref_label_new(GENERIC_DIALOG(fdlg)->vbox, _("Choose the destination folder."));
1036         gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
1037         pref_spacer(GENERIC_DIALOG(fdlg)->vbox, 0);
1038
1039         file_dialog_add_button(fdlg, stock_id, ud->messages.title, file_util_fdlg_ok_cb, TRUE);
1040
1041         file_dialog_add_path_widgets(fdlg, NULL, ud->dest_path, "move_copy", NULL, NULL);
1042
1043         g_signal_connect(G_OBJECT(fdlg->entry), "changed",
1044                          G_CALLBACK(file_util_dest_folder_entry_cb), ud);
1045
1046         gtk_widget_show(GENERIC_DIALOG(fdlg)->dialog);
1047 }
1048
1049
1050 static GtkWidget *furm_simple_vlabel(GtkWidget *box, const gchar *text, gint expand)
1051 {
1052         GtkWidget *vbox;
1053         GtkWidget *label;
1054
1055         vbox = gtk_vbox_new(FALSE, 0);
1056         gtk_box_pack_start(GTK_BOX(box), vbox, expand, expand, 0);
1057         gtk_widget_show(vbox);
1058
1059         label = gtk_label_new(text);
1060         gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
1061         gtk_widget_show(label);
1062
1063         return vbox;
1064 }
1065
1066
1067 static void file_util_dialog_init_source_dest(UtilityData *ud)
1068 {
1069         GtkTreeModel *store;
1070         GtkTreeSelection *selection;
1071         GtkWidget *box;
1072         GtkWidget *hbox;
1073         GtkWidget *box2;
1074         GtkWidget *table;
1075         GtkWidget *combo;
1076         GtkWidget *page;
1077
1078         ud->gd = file_util_gen_dlg(ud->messages.title, GQ_WMCLASS, "dlg_confirm",
1079                                            ud->parent, FALSE,  file_util_cancel_cb, ud);
1080
1081
1082         box = generic_dialog_add_message(ud->gd, NULL, ud->messages.question, NULL);
1083         generic_dialog_add_button(ud->gd, GTK_STOCK_OK, ud->messages.title, file_util_ok_cb, TRUE);
1084
1085         box = pref_group_new(box, TRUE, ud->messages.desc_flist, GTK_ORIENTATION_HORIZONTAL);
1086
1087         ud->listview = file_util_dialog_add_list(box, ud->flist, FALSE);
1088         file_util_dialog_add_list_column(ud->listview, _("Sidecars"), UTILITY_COLUMN_SIDECARS);
1089
1090         file_util_dialog_add_list_column(ud->listview, _("New name"), UTILITY_COLUMN_DEST_NAME);
1091
1092         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(ud->listview));
1093         gtk_tree_selection_set_mode(GTK_TREE_SELECTION(selection), GTK_SELECTION_SINGLE);
1094         gtk_tree_selection_set_select_function(selection, file_util_preview_cb, ud, NULL);
1095
1096
1097 //      column = file_util_rename_multiple_add_column(rd, _("Preview"), RENAME_COLUMN_PREVIEW);
1098 //      gtk_tree_view_column_set_visible(column, FALSE);
1099
1100         gtk_tree_view_set_reorderable(GTK_TREE_VIEW(ud->listview), TRUE);
1101         
1102         store = gtk_tree_view_get_model(GTK_TREE_VIEW(ud->listview));
1103         g_signal_connect(G_OBJECT(store), "row_changed",
1104                          G_CALLBACK(file_util_rename_preview_order_cb), ud);
1105         gtk_widget_set_size_request(ud->listview, 300, 150);
1106
1107         generic_dialog_add_image(ud->gd, box, NULL, NULL, NULL, NULL, FALSE);
1108
1109 //      gtk_container_add(GTK_CONTAINER(scrolled), view);
1110         gtk_widget_show(ud->gd->dialog);
1111
1112
1113         ud->notebook = gtk_notebook_new();
1114         
1115         gtk_box_pack_start(GTK_BOX(ud->gd->vbox), ud->notebook, FALSE, FALSE, 0);
1116         gtk_widget_show(ud->notebook);
1117         
1118
1119         page = gtk_vbox_new(FALSE, PREF_PAD_GAP);
1120         gtk_notebook_append_page(GTK_NOTEBOOK(ud->notebook), page, gtk_label_new(_("Manual rename")));
1121         gtk_widget_show(page);
1122         
1123         table = pref_table_new(page, 2, 2, FALSE, FALSE);
1124
1125         pref_table_label(table, 0, 0, _("Original name:"), 1.0);
1126         ud->rename_label = pref_table_label(table, 1, 0, "", 0.0);
1127
1128         pref_table_label(table, 0, 1, _("New name:"), 1.0);
1129
1130         ud->rename_entry = gtk_entry_new();
1131         gtk_table_attach(GTK_TABLE(table), ud->rename_entry, 1, 2, 1, 2, GTK_EXPAND | GTK_FILL, 0, 0, 0);
1132         generic_dialog_attach_default(GENERIC_DIALOG(ud->gd), ud->rename_entry);
1133         gtk_widget_grab_focus(ud->rename_entry);
1134
1135         g_signal_connect(G_OBJECT(ud->rename_entry), "changed",
1136                          G_CALLBACK(file_util_rename_preview_entry_cb), ud);
1137
1138         gtk_widget_show(ud->rename_entry);
1139
1140         page = gtk_vbox_new(FALSE, PREF_PAD_GAP);
1141         gtk_notebook_append_page(GTK_NOTEBOOK(ud->notebook), page, gtk_label_new(_("Auto rename")));
1142         gtk_widget_show(page);
1143
1144
1145         hbox = pref_box_new(page, FALSE, GTK_ORIENTATION_HORIZONTAL, PREF_PAD_GAP);
1146
1147         box2 = furm_simple_vlabel(hbox, _("Begin text"), TRUE);
1148
1149         combo = history_combo_new(&ud->auto_entry_front, "", "numerical_rename_prefix", -1);
1150         g_signal_connect(G_OBJECT(ud->auto_entry_front), "changed",
1151                          G_CALLBACK(file_util_rename_preview_entry_cb), ud);
1152         gtk_box_pack_start(GTK_BOX(box2), combo, TRUE, TRUE, 0);
1153         gtk_widget_show(combo);
1154
1155         box2 = furm_simple_vlabel(hbox, _("Start #"), FALSE);
1156
1157         ud->auto_spin_start = pref_spin_new(box2, NULL, NULL,
1158                                             0.0, 1000000.0, 1.0, 0, 1.0,
1159                                             G_CALLBACK(file_util_rename_preview_adj_cb), ud);
1160
1161         box2 = furm_simple_vlabel(hbox, _("End text"), TRUE);
1162
1163         combo = history_combo_new(&ud->auto_entry_end, "", "numerical_rename_suffix", -1);
1164         g_signal_connect(G_OBJECT(ud->auto_entry_end), "changed",
1165                          G_CALLBACK(file_util_rename_preview_entry_cb), ud);
1166         gtk_box_pack_start(GTK_BOX(box2), combo, TRUE, TRUE, 0);
1167         gtk_widget_show(combo);
1168
1169         ud->auto_spin_pad = pref_spin_new(page, _("Padding:"), NULL,
1170                                           1.0, 8.0, 1.0, 0, 1.0,
1171                                           G_CALLBACK(file_util_rename_preview_adj_cb), ud);
1172
1173         page = gtk_vbox_new(FALSE, PREF_PAD_GAP);
1174         gtk_notebook_append_page(GTK_NOTEBOOK(ud->notebook), page, gtk_label_new(_("Formatted rename")));
1175         gtk_widget_show(page);
1176
1177         hbox = pref_box_new(page, FALSE, GTK_ORIENTATION_HORIZONTAL, PREF_PAD_GAP);
1178
1179         box2 = furm_simple_vlabel(hbox, _("Format (* = original name, ## = numbers)"), TRUE);
1180
1181         combo = history_combo_new(&ud->format_entry, "", "auto_rename_format", -1);
1182         g_signal_connect(G_OBJECT(ud->format_entry), "changed",
1183                          G_CALLBACK(file_util_rename_preview_entry_cb), ud);
1184         gtk_box_pack_start(GTK_BOX(box2), combo, TRUE, TRUE, 0);
1185         gtk_widget_show(combo);
1186
1187         box2 = furm_simple_vlabel(hbox, _("Start #"), FALSE);
1188
1189         ud->format_spin = pref_spin_new(box2, NULL, NULL,
1190                                         0.0, 1000000.0, 1.0, 0, 1.0,
1191                                         G_CALLBACK(file_util_rename_preview_adj_cb), ud);
1192
1193 //      gtk_combo_box_set_active(GTK_COMBO_BOX(ud->combo_type), 0); /* callback will take care of the rest */
1194
1195         file_util_dialog_list_select(ud->listview, 0);
1196 }
1197
1198
1199 void file_util_dialog_run(UtilityData *ud)
1200 {
1201         switch (ud->phase)
1202                 {
1203                 case UTILITY_PHASE_START:
1204                         /* create the dialogs */
1205                         switch (ud->type)
1206                                 {
1207                                 case UTILITY_TYPE_DELETE:
1208                                 case UTILITY_TYPE_DELETE_LINK:
1209                                 case UTILITY_TYPE_DELETE_FOLDER:
1210                                 case UTILITY_TYPE_EDITOR:
1211                                         file_util_dialog_init_simple_list(ud);
1212                                         break;
1213                                 case UTILITY_TYPE_RENAME:
1214                                         file_util_dialog_init_source_dest(ud);
1215                                         break;
1216                                 case UTILITY_TYPE_COPY:
1217                                 case UTILITY_TYPE_MOVE:
1218                                 case UTILITY_TYPE_FILTER:
1219                                         file_util_dialog_init_dest_folder(ud);
1220                                         break;
1221                                 }
1222                         ud->phase = UTILITY_PHASE_ENTERING;
1223                         break;
1224                 case UTILITY_PHASE_ENTERING:
1225                         /* FIXME use file_data_sc_check_ci_dest to detect problems and eventually go back to PHASE_START 
1226                         or to PHASE_CANCEL */
1227
1228                 
1229                         ud->phase = UTILITY_PHASE_CHECKED;
1230                 case UTILITY_PHASE_CHECKED:
1231                         file_util_perform_ci(ud);
1232                         break;
1233                         
1234                 case UTILITY_PHASE_CANCEL:
1235                 case UTILITY_PHASE_DONE:
1236                         file_data_sc_free_ci_list(ud->flist);
1237                         file_data_sc_free_ci_list(ud->dlist);
1238                         if (ud->source_fd) file_data_sc_free_ci(ud->source_fd);
1239                         file_util_data_free(ud);
1240                         break;
1241                 }
1242 }
1243
1244
1245
1246
1247 static gint file_util_unlink(FileData *fd)
1248 {
1249 }
1250
1251
1252 static void file_util_delete_full(FileData *source_fd, GList *source_list, GtkWidget *parent, UtilityPhase phase)
1253 {
1254         UtilityData *ud;
1255         GList *flist = filelist_copy(source_list);
1256         
1257         if (source_fd)
1258                 flist = g_list_append(flist, file_data_ref(source_fd));
1259
1260         if (!file_data_sc_add_ci_delete_list(flist))
1261                 {
1262                 file_util_warning_dialog(_("File deletion failed"), _("Another operation in progress.\n"), GTK_STOCK_DIALOG_ERROR, NULL);
1263                 filelist_free(flist);
1264                 return;
1265                 }
1266
1267         ud = file_util_data_new(UTILITY_TYPE_DELETE);
1268         
1269         ud->phase = phase;
1270
1271         ud->source_fd = NULL;
1272         ud->flist = flist;
1273         ud->dlist = NULL;
1274         ud->parent = parent;
1275         
1276         ud->messages.title = _("Delete");
1277         ud->messages.question = _("Delete files?");
1278         ud->messages.desc_flist = _("This will delete the following files");
1279         ud->messages.desc_dlist = "";
1280         ud->messages.desc_source_fd = "";
1281         ud->messages.fail = _("File deletion failed");
1282
1283         file_util_dialog_run(ud);
1284 }
1285
1286 static void file_util_move_full(FileData *source_fd, GList *source_list, const gchar *dest_path, GtkWidget *parent, UtilityPhase phase)
1287 {
1288         UtilityData *ud;
1289         GList *flist = filelist_copy(source_list);
1290         
1291         if (source_fd)
1292                 flist = g_list_append(flist, file_data_ref(source_fd));
1293
1294         if (!file_data_sc_add_ci_move_list(flist, dest_path))
1295                 {
1296                 file_util_warning_dialog(_("Move failed"), _("Another operation in progress.\n"), GTK_STOCK_DIALOG_ERROR, NULL);
1297                 filelist_free(flist);
1298                 return;
1299                 }
1300
1301         ud = file_util_data_new(UTILITY_TYPE_MOVE);
1302
1303         ud->phase = phase;
1304
1305         ud->source_fd = NULL;
1306         ud->flist = flist;
1307         ud->dlist = NULL;
1308         ud->parent = parent;
1309
1310         ud->dest_path = g_strdup(dest_path ? dest_path : "");
1311         
1312         ud->messages.title = _("Move");
1313         ud->messages.question = _("Move files?");
1314         ud->messages.desc_flist = _("This will move the following files");
1315         ud->messages.desc_dlist = "";
1316         ud->messages.desc_source_fd = "";
1317         ud->messages.fail = _("Move failed");
1318
1319         file_util_dialog_run(ud);
1320 }
1321 static void file_util_copy_full(FileData *source_fd, GList *source_list, const gchar *dest_path, GtkWidget *parent, UtilityPhase phase)
1322 {
1323         UtilityData *ud;
1324         GList *flist = filelist_copy(source_list);
1325         
1326         if (source_fd)
1327                 flist = g_list_append(flist, file_data_ref(source_fd));
1328
1329         if (!file_data_sc_add_ci_copy_list(flist, dest_path))
1330                 {
1331                 file_util_warning_dialog(_("Copy failed"), _("Another operation in progress.\n"), GTK_STOCK_DIALOG_ERROR, NULL);
1332                 filelist_free(flist);
1333                 return;
1334                 }
1335
1336         ud = file_util_data_new(UTILITY_TYPE_COPY);
1337
1338         ud->phase = phase;
1339
1340         ud->source_fd = NULL;
1341         ud->flist = flist;
1342         ud->dlist = NULL;
1343         ud->parent = parent;
1344
1345         ud->dest_path = g_strdup(dest_path ? dest_path : "");
1346         
1347         ud->messages.title = _("Copy");
1348         ud->messages.question = _("Copy files?");
1349         ud->messages.desc_flist = _("This will copy the following files");
1350         ud->messages.desc_dlist = "";
1351         ud->messages.desc_source_fd = "";
1352         ud->messages.fail = _("Copy failed");
1353
1354         file_util_dialog_run(ud);
1355 }
1356
1357 static void file_util_rename_full(FileData *source_fd, GList *source_list, const gchar *dest_path, GtkWidget *parent, UtilityPhase phase)
1358 {
1359         UtilityData *ud;
1360         GList *flist = filelist_copy(source_list);
1361         
1362         if (source_fd)
1363                 flist = g_list_append(flist, file_data_ref(source_fd));
1364
1365         if (!file_data_sc_add_ci_rename_list(flist, dest_path))
1366                 {
1367                 file_util_warning_dialog(_("Rename failed"), _("Another operation in progress.\n"), GTK_STOCK_DIALOG_ERROR, NULL);
1368                 filelist_free(flist);
1369                 return;
1370                 }
1371
1372         ud = file_util_data_new(UTILITY_TYPE_RENAME);
1373
1374         ud->phase = phase;
1375
1376         ud->source_fd = NULL;
1377         ud->flist = flist;
1378         ud->dlist = NULL;
1379         ud->parent = parent;
1380         
1381         ud->messages.title = _("Rename");
1382         ud->messages.question = _("Rename files?");
1383         ud->messages.desc_flist = _("This will rename the following files");
1384         ud->messages.desc_dlist = "";
1385         ud->messages.desc_source_fd = "";
1386         ud->messages.fail = _("Rename failed");
1387
1388         file_util_dialog_run(ud);
1389 }
1390
1391 static void file_util_start_editor_full(gint n, FileData *source_fd, GList *source_list, const gchar *dest_path, GtkWidget *parent, UtilityPhase phase)
1392 {
1393         UtilityData *ud;
1394         GList *flist = filelist_copy(source_list);
1395         
1396         if (source_fd)
1397                 flist = g_list_append(flist, file_data_ref(source_fd));
1398
1399         if (!file_data_sc_add_ci_unspecified_list(flist, dest_path))
1400                 {
1401                 file_util_warning_dialog(_("Can't run external editor"), _("Another operation in progress.\n"), GTK_STOCK_DIALOG_ERROR, NULL);
1402                 filelist_free(flist);
1403                 return;
1404                 }
1405
1406         if (editor_is_filter(n))
1407                 ud = file_util_data_new(UTILITY_TYPE_FILTER);
1408         else
1409                 ud = file_util_data_new(UTILITY_TYPE_EDITOR);
1410                 
1411                 
1412         /* ask for destination if we don't have it */
1413         if (ud->type == UTILITY_TYPE_FILTER && dest_path == NULL) phase = UTILITY_PHASE_START;
1414         
1415         ud->phase = phase;
1416         
1417         ud->external_command = n;
1418
1419         ud->source_fd = NULL;
1420         ud->flist = flist;
1421         ud->dlist = NULL;
1422         ud->parent = parent;
1423
1424         ud->dest_path = g_strdup(dest_path ? dest_path : "");
1425         
1426         ud->messages.title = _("Editor");
1427         ud->messages.question = _("Run editor?");
1428         ud->messages.desc_flist = _("This will copy the following files");
1429         ud->messages.desc_dlist = "";
1430         ud->messages.desc_source_fd = "";
1431         ud->messages.fail = _("External command failed");
1432
1433         file_util_dialog_run(ud);
1434 }
1435
1436
1437 /* FIXME: */
1438 void file_util_create_dir(const gchar *path, GtkWidget *parent)
1439 {
1440 }
1441 gint file_util_rename_dir(FileData *source_fd, const gchar *new_path, GtkWidget *parent)
1442 {
1443 }
1444
1445 /* full-featured entru points
1446 */
1447
1448 void file_util_delete(FileData *source_fd, GList *source_list, GtkWidget *parent)
1449 {
1450         file_util_delete_full(source_fd, source_list, parent, UTILITY_PHASE_START);
1451 }
1452
1453 void file_util_copy(FileData *source_fd, GList *source_list, const gchar *dest_path, GtkWidget *parent)
1454 {
1455         file_util_copy_full(source_fd, source_list, dest_path, parent, UTILITY_PHASE_START);
1456 }
1457 void file_util_move(FileData *source_fd, GList *source_list, const gchar *dest_path, GtkWidget *parent)
1458 {
1459         file_util_move_full(source_fd, source_list, dest_path, parent, UTILITY_PHASE_START);
1460 }
1461 void file_util_rename(FileData *source_fd, GList *source_list, GtkWidget *parent)
1462 {
1463         file_util_rename_full(source_fd, source_list, NULL, parent, UTILITY_PHASE_START);
1464 }
1465
1466 /* these avoid the location entry dialog unless there is an error, list must be files only and
1467  * dest_path must be a valid directory path
1468 */
1469 void file_util_move_simple(GList *list, const gchar *dest_path, GtkWidget *parent)
1470 {
1471         file_util_move_full(NULL, list, dest_path, parent, UTILITY_PHASE_ENTERING);
1472 }
1473 void file_util_copy_simple(GList *list, const gchar *dest_path, GtkWidget *parent)
1474 {
1475         file_util_copy_full(NULL, list, dest_path, parent, UTILITY_PHASE_ENTERING);
1476 }
1477 void file_util_rename_simple(FileData *fd, const gchar *dest_path, GtkWidget *parent)
1478 {
1479         file_util_rename_full(fd, NULL, dest_path, parent, UTILITY_PHASE_ENTERING);
1480 }
1481
1482
1483 void file_util_start_editor_from_file(gint n, FileData *fd, GtkWidget *parent)
1484 {
1485         file_util_start_editor_full(n, fd, NULL, NULL, parent, UTILITY_PHASE_ENTERING);
1486 }
1487
1488 void file_util_start_editor_from_filelist(gint n, GList *list, GtkWidget *parent)
1489 {
1490         file_util_start_editor_full(n, NULL, list, NULL, parent, UTILITY_PHASE_ENTERING);
1491 }
1492
1493 void file_util_start_filter_from_file(gint n, FileData *fd, const gchar *dest_path, GtkWidget *parent)
1494 {
1495         file_util_start_editor_full(n, fd, NULL, dest_path, parent, UTILITY_PHASE_ENTERING);
1496 }
1497
1498 void file_util_start_filter_from_filelist(gint n, GList *list, const gchar *dest_path, GtkWidget *parent)
1499 {
1500         file_util_start_editor_full(n, NULL, list, dest_path, parent, UTILITY_PHASE_ENTERING);
1501 }
1502
1503
1504 /*
1505  *--------------------------------------------------------------------------
1506  * Delete directory routines
1507  *--------------------------------------------------------------------------
1508  */
1509
1510 // FIXME
1511
1512
1513
1514 FileData *file_util_delete_dir_empty_path(FileData *fd, gint real_content, gint level)
1515 {
1516         GList *dlist = NULL;
1517         GList *flist = NULL;
1518         GList *work;
1519         FileData *fail = NULL;
1520
1521         DEBUG_1("deltree into: %s", fd->path);
1522
1523         level++;
1524         if (level > UTILITY_DELETE_MAX_DEPTH)
1525                 {
1526                 log_printf("folder recursion depth past %d, giving up\n", UTILITY_DELETE_MAX_DEPTH);
1527                 return file_data_ref(fd);
1528                 }
1529
1530         if (!filelist_read_lstat(fd->path, &flist, &dlist)) file_data_ref(fd);
1531
1532         work = dlist;
1533         while (work && !fail)
1534                 {
1535                 FileData *lfd;
1536
1537                 lfd = work->data;
1538                 work = work->next;
1539
1540                 fail = file_util_delete_dir_empty_path(lfd, real_content, level);
1541                 }
1542
1543         work = flist;
1544         while (work && !fail)
1545                 {
1546                 FileData *lfd;
1547
1548                 lfd = work->data;
1549                 work = work->next;
1550
1551                 DEBUG_1("deltree child: %s", lfd->path);
1552
1553                 if (real_content && !islink(lfd->path))
1554                         {
1555                         if (!file_util_unlink(lfd)) fail = file_data_ref(lfd);
1556                         }
1557                 else
1558                         {
1559                         if (!unlink_file(lfd->path)) fail = file_data_ref(lfd);
1560                         }
1561                 }
1562
1563         filelist_free(dlist);
1564         filelist_free(flist);
1565
1566         if (!fail && !rmdir_utf8(fd->path))
1567                 {
1568                 fail = file_data_ref(fd);
1569                 }
1570
1571         DEBUG_1("deltree done: %s", fd->path);
1572
1573         return fail;
1574 }
1575
1576 static void file_util_delete_dir_ok_cb(GenericDialog *gd, gpointer data)
1577 {
1578         UtilityData *ud = data;
1579
1580         ud->gd = NULL;
1581
1582         if (ud->type == UTILITY_TYPE_DELETE_LINK)
1583                 {
1584                 if (!unlink_file(ud->source_fd->path))
1585                         {
1586                         gchar *text;
1587
1588                         text = g_strdup_printf("Unable to remove symbolic link:\n %s", ud->source_fd->path);
1589                         file_util_warning_dialog(_("Delete failed"), text,
1590                                                  GTK_STOCK_DIALOG_ERROR, NULL);
1591                         g_free(text);
1592                         }
1593                 }
1594         else
1595                 {
1596                 FileData *fail = NULL;
1597                 GList *work;
1598
1599                 work = ud->dlist;
1600                 while (work && !fail)
1601                         {
1602                         FileData *fd;
1603
1604                         fd = work->data;
1605                         work = work->next;
1606
1607                         fail = file_util_delete_dir_empty_path(fd, FALSE, 0);
1608                         }
1609
1610                 work = ud->flist;
1611                 while (work && !fail)
1612                         {
1613                         FileData *fd;
1614
1615                         fd = work->data;
1616                         work = work->next;
1617
1618                         DEBUG_1("deltree unlink: %s", fd->path);
1619
1620                         if (islink(fd->path))
1621                                 {
1622                                 if (!unlink_file(fd->path)) fail = file_data_ref(fd);
1623                                 }
1624                         else
1625                                 {
1626                                 if (!file_util_unlink(fd)) fail = file_data_ref(fd);
1627                                 }
1628                         }
1629
1630                 if (!fail)
1631                         {
1632                         if (!rmdir_utf8(ud->source_fd->path)) fail = file_data_ref(ud->source_fd);
1633                         }
1634
1635                 if (fail)
1636                         {
1637                         gchar *text;
1638
1639                         text = g_strdup_printf(_("Unable to delete folder:\n\n%s"), ud->source_fd->path);
1640                         gd = file_util_warning_dialog(_("Delete failed"), text, GTK_STOCK_DIALOG_ERROR, NULL);
1641                         g_free(text);
1642
1643                         if (fail != ud->source_fd)
1644                                 {
1645                                 pref_spacer(gd->vbox, PREF_PAD_GROUP);
1646                                 text = g_strdup_printf(_("Removal of folder contents failed at this file:\n\n%s"),
1647                                                         fail->path);
1648                                 pref_label_new(gd->vbox, text);
1649                                 g_free(text);
1650                                 }
1651
1652                         file_data_unref(fail);
1653                         }
1654                 }
1655
1656         file_util_data_free(ud);
1657 }
1658
1659 static GList *file_util_delete_dir_remaining_folders(GList *dlist)
1660 {
1661         GList *rlist = NULL;
1662
1663         while (dlist)
1664                 {
1665                 FileData *fd;
1666
1667                 fd = dlist->data;
1668                 dlist = dlist->next;
1669
1670                 if (!fd->name ||
1671                     (strcmp(fd->name, THUMB_FOLDER_GLOBAL) != 0 &&
1672                      strcmp(fd->name, THUMB_FOLDER_LOCAL) != 0 &&
1673                      strcmp(fd->name, GQ_CACHE_LOCAL_METADATA) != 0) )
1674                         {
1675                         rlist = g_list_prepend(rlist, fd);
1676                         }
1677                 }
1678
1679         return g_list_reverse(rlist);
1680 }
1681
1682 void file_util_delete_dir(FileData *fd, GtkWidget *parent)
1683 {
1684         GList *dlist = NULL;
1685         GList *flist = NULL;
1686         GList *rlist;
1687
1688         if (!isdir(fd->path)) return;
1689
1690         if (islink(fd->path))
1691                 {
1692                 UtilityData *ud;
1693                 gchar *text;
1694
1695                 ud = g_new0(UtilityData, 1);
1696                 ud->type = UTILITY_TYPE_DELETE_LINK;
1697                 ud->source_fd = file_data_ref(fd);
1698                 ud->dlist = NULL;
1699                 ud->flist = NULL;
1700
1701                 ud->gd = file_util_gen_dlg(_("Delete folder"), GQ_WMCLASS, "dlg_confirm",
1702                                            parent, TRUE,
1703                                             file_util_cancel_cb, ud);
1704
1705                 text = g_strdup_printf(_("This will delete the symbolic link:\n\n%s\n\n"
1706                                          "The folder this link points to will not be deleted."),
1707                                         fd->path);
1708                 generic_dialog_add_message(ud->gd, GTK_STOCK_DIALOG_QUESTION,
1709                                            _("Delete symbolic link to folder?"),
1710                                            text);
1711                 g_free(text);
1712
1713                 generic_dialog_add_button(ud->gd, GTK_STOCK_DELETE, NULL, file_util_delete_dir_ok_cb, TRUE);
1714
1715                 gtk_widget_show(ud->gd->dialog);
1716
1717                 return;
1718                 }
1719
1720         if (!access_file(fd->path, W_OK | X_OK))
1721                 {
1722                 gchar *text;
1723
1724                 text = g_strdup_printf(_("Unable to remove folder %s\n"
1725                                          "Permissions do not allow writing to the folder."), fd->path);
1726                 file_util_warning_dialog(_("Delete failed"), text, GTK_STOCK_DIALOG_ERROR, parent);
1727                 g_free(text);
1728
1729                 return;
1730                 }
1731
1732         if (!filelist_read_lstat(fd->path, &flist, &dlist))
1733                 {
1734                 gchar *text;
1735
1736                 text = g_strdup_printf(_("Unable to list contents of folder %s"), fd->path);
1737                 file_util_warning_dialog(_("Delete failed"), text, GTK_STOCK_DIALOG_ERROR, parent);
1738                 g_free(text);
1739
1740                 return;
1741                 }
1742
1743         rlist = file_util_delete_dir_remaining_folders(dlist);
1744         if (rlist)
1745                 {
1746                 GenericDialog *gd;
1747                 GtkWidget *box;
1748                 gchar *text;
1749
1750                 gd = file_util_gen_dlg(_("Folder contains subfolders"), GQ_WMCLASS, "dlg_warning",
1751                                         parent, TRUE, NULL, NULL);
1752                 generic_dialog_add_button(gd, GTK_STOCK_CLOSE, NULL, NULL, TRUE);
1753
1754                 text = g_strdup_printf(_("Unable to delete the folder:\n\n%s\n\n"
1755                                          "This folder contains subfolders which must be moved before it can be deleted."),
1756                                         fd->path);
1757                 box = generic_dialog_add_message(gd, GTK_STOCK_DIALOG_WARNING,
1758                                                  _("Folder contains subfolders"),
1759                                                  text);
1760                 g_free(text);
1761
1762                 box = pref_group_new(box, TRUE, _("Subfolders:"), GTK_ORIENTATION_VERTICAL);
1763
1764                 rlist = filelist_sort_path(rlist);
1765                 file_util_dialog_add_list(box, rlist, FALSE);
1766
1767                 gtk_widget_show(gd->dialog);
1768                 }
1769         else
1770                 {
1771                 UtilityData *ud;
1772                 GtkWidget *box;
1773                 GtkWidget *view;
1774                 GtkTreeSelection *selection;
1775                 gchar *text;
1776
1777                 ud = g_new0(UtilityData, 1);
1778                 ud->type = UTILITY_TYPE_DELETE_FOLDER;
1779                 ud->source_fd = file_data_ref(fd);
1780                 ud->dlist = dlist;
1781                 dlist = NULL;
1782                 ud->flist = filelist_sort_path(flist);
1783                 flist = NULL;
1784
1785                 ud->gd = file_util_gen_dlg(_("Delete folder"), GQ_WMCLASS, "dlg_confirm",
1786                                            parent, TRUE,  file_util_cancel_cb, ud);
1787                 generic_dialog_add_button(ud->gd, GTK_STOCK_DELETE, NULL, file_util_delete_dir_ok_cb, TRUE);
1788
1789                 text = g_strdup_printf(_("This will delete the folder:\n\n%s\n\n"
1790                                          "The contents of this folder will also be deleted."),
1791                                         fd->path);
1792                 box = generic_dialog_add_message(ud->gd, GTK_STOCK_DIALOG_QUESTION,
1793                                                  _("Delete folder?"),
1794                                                  text);
1795                 g_free(text);
1796
1797                 box = pref_group_new(box, TRUE, _("Contents:"), GTK_ORIENTATION_HORIZONTAL);
1798
1799                 view = file_util_dialog_add_list(box, ud->flist, FALSE);
1800                 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(view));
1801                 gtk_tree_selection_set_mode(selection, GTK_SELECTION_SINGLE);
1802                 gtk_tree_selection_set_select_function(selection, file_util_preview_cb, ud, NULL);
1803
1804                 generic_dialog_add_image(ud->gd, box, NULL, NULL, NULL, NULL, FALSE);
1805
1806                 box_append_safe_delete_status(ud->gd);
1807
1808                 gtk_widget_show(ud->gd->dialog);
1809                 }
1810
1811         g_list_free(rlist);
1812         filelist_free(dlist);
1813         filelist_free(flist);
1814 }
1815
1816
1817 void file_util_copy_path_to_clipboard(FileData *fd)
1818 {
1819         GtkClipboard *clipboard;
1820
1821         if (!fd || !*fd->path) return;
1822
1823         clipboard = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
1824         gtk_clipboard_set_text(clipboard, g_shell_quote(fd->path), -1);
1825 }
1826
1827 void file_util_copy_path_list_to_clipboard(GList *list)
1828 {
1829         GtkClipboard *clipboard;
1830         GList *work;
1831         GString *new;
1832
1833         clipboard = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
1834         
1835         new = g_string_new("");
1836         work = list;
1837         while (work) {
1838                 FileData *fd = work->data;
1839                 work = work->next;
1840
1841                 if (!fd || !*fd->path) continue;
1842         
1843                 g_string_append(new, g_shell_quote(fd->path));
1844                 if (work) g_string_append_c(new, ' ');
1845                 }
1846         
1847         gtk_clipboard_set_text(clipboard, new->str, new->len);
1848         g_string_free(new, TRUE);
1849 }