r2525: Use stock icons in pinboard and panel menus.
[rox-filer.git] / ROX-Filer / src / action.c
blob3d758e1dcad77f5bf8bf6963124d38da7f6bbe82
1 /*
2 * $Id$
4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 2003, the ROX-Filer team.
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
10 * any later version.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19 * Place, Suite 330, Boston, MA 02111-1307 USA
22 /* action.c - code for handling the filer action windows.
23 * These routines generally fork() and talk to us via pipes.
26 #include "config.h"
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <ctype.h>
31 #include <sys/param.h>
32 #include <errno.h>
33 #include <signal.h>
34 #include <sys/time.h>
35 #include <utime.h>
36 #include <stdarg.h>
38 #include "global.h"
40 #include "action.h"
41 #include "abox.h"
42 #include "string.h"
43 #include "support.h"
44 #include "gui_support.h"
45 #include "filer.h"
46 #include "display.h"
47 #include "main.h"
48 #include "options.h"
49 #include "modechange.h"
50 #include "find.h"
51 #include "dir.h"
52 #include "icon.h"
53 #include "mount.h"
55 /* Parent->Child messages are one character each:
57 * Y/N Yes/No button clicked
58 * F Force deletion of non-writeable items
59 * Q Quiet toggled
60 * E Entry text changed
61 * W neWer toggled
64 typedef struct _GUIside GUIside;
65 typedef void ActionChild(gpointer data);
66 typedef void ForDirCB(const char *path, const char *dest_path);
68 struct _GUIside
70 ABox *abox; /* The action window widget */
72 int from_child; /* File descriptor */
73 FILE *to_child;
74 int input_tag; /* gdk_input_add() */
75 pid_t child; /* Process ID */
76 int errors; /* Number of errors so far */
77 gboolean show_info; /* For Disk Usage */
79 guchar **default_string; /* Changed when the entry changes */
80 void (*entry_string_func)(GtkWidget *widget,
81 const guchar *string);
83 int abort_attempts;
86 /* These don't need to be in a structure because we fork() before
87 * using them again.
89 static gboolean mount_open_dir = FALSE;
90 static int from_parent = 0;
91 static FILE *to_parent = NULL;
92 static gboolean quiet = FALSE;
93 static GString *message = NULL;
94 static const char *action_dest = NULL;
95 static const char *action_leaf = NULL;
96 static void (*action_do_func)(const char *source, const char *dest);
97 static double size_tally; /* For Disk Usage */
98 static unsigned long dir_counter; /* For Disk Usage */
99 static unsigned long file_counter; /* For Disk Usage */
101 static struct mode_change *mode_change = NULL; /* For Permissions */
102 static FindCondition *find_condition = NULL; /* For Find */
104 /* Only used by child */
105 static gboolean o_force = FALSE;
106 static gboolean o_brief = FALSE;
107 static gboolean o_recurse = FALSE;
108 static gboolean o_newer = FALSE;
110 static Option o_action_copy, o_action_move, o_action_link;
111 static Option o_action_delete, o_action_mount;
112 static Option o_action_force, o_action_brief, o_action_recurse;
113 static Option o_action_newer;
115 /* Whenever the text in these boxes is changed we store a copy of the new
116 * string to be used as the default next time.
118 static guchar *last_chmod_string = NULL;
119 static guchar *last_find_string = NULL;
121 /* Set to one of the above before forking. This may change over a call to
122 * reply(). It is reset to NULL once the text is parsed.
124 static guchar *new_entry_string = NULL;
126 /* Static prototypes */
127 static void send_done(void);
128 static void send_check_path(const gchar *path);
129 static void send_mount_path(const gchar *path);
130 static gboolean printf_send(const char *msg, ...);
131 static gboolean send(void);
132 static gboolean send_error(void);
133 static gboolean send_dir(const char *dir);
134 static gboolean read_exact(int source, char *buffer, ssize_t len);
135 static void do_mount(const guchar *path, gboolean mount);
136 static gboolean printf_reply(int fd, gboolean ignore_quiet,
137 const char *msg, ...);
138 static gboolean remove_pinned_ok(GList *paths);
140 /* SUPPORT */
143 /* This is called whenever the user edits the entry box (if any) - send the
144 * new string.
146 static void entry_changed(GtkEditable *entry, GUIside *gui_side)
148 guchar *text;
150 g_return_if_fail(gui_side->default_string != NULL);
152 text = gtk_editable_get_chars(entry, 0, -1);
154 if (gui_side->entry_string_func)
155 gui_side->entry_string_func(GTK_WIDGET(entry), text);
157 g_free(*(gui_side->default_string));
158 *(gui_side->default_string) = text; /* Gets text's ref */
160 if (!gui_side->to_child)
161 return;
163 fputc('E', gui_side->to_child);
164 fputs(text, gui_side->to_child);
165 fputc('\n', gui_side->to_child);
166 fflush(gui_side->to_child);
169 void show_condition_help(gpointer data)
171 GtkWidget *help;
172 GtkWidget *text;
174 help = gtk_dialog_new_with_buttons(
175 _("Find expression reference"),
176 NULL, 0,
177 GTK_STOCK_CLOSE, GTK_RESPONSE_CANCEL,
178 NULL);
179 gtk_dialog_set_default_response(GTK_DIALOG(help), GTK_RESPONSE_CANCEL);
181 text = gtk_label_new(NULL);
182 gtk_misc_set_padding(GTK_MISC(text), 2, 2);
183 gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(help)->vbox), text);
184 gtk_label_set_selectable(GTK_LABEL(text), TRUE);
185 gtk_label_set_markup(GTK_LABEL(text), _(
186 "<u>Quick Start</u>\n"
187 "Just put the name of the file you're looking for in single quotes:\n"
188 "<b>'index.html'</b> (to find a file called 'index.html')\n"
189 "\n"
190 "<u>Examples</u>\n"
191 "<b>'*.htm', '*.html'</b> (finds HTML files)\n"
192 "<b>IsDir 'lib'</b> (finds directories called 'lib')\n"
193 "<b>IsReg 'core'</b> (finds a regular file called 'core')\n"
194 "<b>! (IsDir, IsReg)</b> (is neither a directory nor a regular file)\n"
195 "<b>mtime after 1 day ago and size > 1Mb</b> (big, and recently modified)\n"
196 "<b>'CVS' prune, isreg</b> (a regular file not in CVS)\n"
197 "<b>IsReg system(grep -q fred \"%\")</b> (contains the word 'fred')\n"
198 "\n"
199 "<u>Simple Tests</u>\n"
200 "<b>IsReg, IsLink, IsDir, IsChar, IsBlock, IsDev, IsPipe, IsSocket, IsDoor</b> "
201 "(types)\n"
202 "<b>IsSUID, IsSGID, IsSticky, IsReadable, IsWriteable, IsExecutable</b> "
203 "(permissions)\n"
204 "<b>IsEmpty, IsMine</b>\n"
205 "A pattern in single quotes is a shell-style wildcard pattern to match. If it\n"
206 "contains a slash then the match is against the full path; otherwise it is\n"
207 "against the leafname only.\n"
208 "\n"
209 "<u>Comparisons</u>\n"
210 "<b>&lt;, &lt;=, =, !=, &gt;, &gt;=, After, Before</b> (compare two values)\n"
211 "<b>5 bytes, 1Kb, 2Mb, 3Gb</b> (file sizes)\n"
212 "<b>2 secs|mins|hours|days|weeks|years ago|hence</b> (times)\n"
213 "<b>atime, ctime, mtime, now, size, inode, nlinks, uid, gid, blocks</b> "
214 "(values)\n"
215 "\n"
216 "<u>Specials</u>\n"
217 "<b>system(command)</b> (true if 'command' returns with a zero exit status;\n"
218 "a % in 'command' is replaced with the path of the current file)\n"
219 "<b>prune</b> (false, and prevents searching the contents of a directory)."));
221 g_signal_connect(help, "response",
222 G_CALLBACK(gtk_widget_destroy), NULL);
224 gtk_widget_show_all(help);
227 static void show_chmod_help(gpointer data)
229 GtkWidget *help;
230 GtkWidget *text;
232 help = gtk_dialog_new_with_buttons(
233 _("Find expression reference"),
234 NULL, 0,
235 GTK_STOCK_CLOSE, GTK_RESPONSE_CANCEL,
236 NULL);
237 gtk_dialog_set_default_response(GTK_DIALOG(help), GTK_RESPONSE_CANCEL);
239 text = gtk_label_new(NULL);
240 gtk_misc_set_padding(GTK_MISC(text), 2, 2);
241 gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(help)->vbox), text);
242 gtk_label_set_selectable(GTK_LABEL(text), TRUE);
243 gtk_label_set_markup(GTK_LABEL(text), _(
244 "Normally, you can just select a command from the menu (click \n"
245 "on the arrow beside the command box). Sometimes, you need more...\n"
246 "\n"
247 "The format of a command is: <b>CHANGE, CHANGE, ...</b>\n"
248 "Each <b>CHANGE</b> is: <b>WHO HOW PERMISSIONS</b>\n"
249 "<b>WHO</b> is some combination of <b>u</b>, <b>g</b> and <b>o</b> which "
250 "determines whether to\n"
251 "change the permissions for the User (owner), Group or Others.\n"
252 "<b>HOW</b> is <b>+</b>, <b>-</b> or <b>=</b> to add, remove or set "
253 "exactly the permissions.\n"
254 "<b>PERMISSIONS</b> is some combination of the letters <b>rwxXstugo</b>\n"
255 "\n"
256 "Bracketed text and spaces are ignored.\n"
257 "\n"
258 "<u>Examples</u>\n"
259 "<b>u+rw</b>: the file owner gains read and write permission\n"
260 "<b>g=u</b>: the group permissions are set to be the same as the user's\n"
261 "<b>o=u-w</b>: others get the same permissions as the owner, but without "
262 "write permission\n"
263 "<b>a+x</b>: <b>a</b>ll get execute/access permission - same as <b>ugo+x</b>\n"
264 "<b>a+X</b>: directories become accessable by everyone; files which were\n"
265 "executable by anyone become executable by everyone\n"
266 "<b>u+rw, go+r</b>: two commands at once!\n"
267 "<b>u+s</b>: set the SetUID bit - often has no effect on script files\n"
268 "<b>755</b>: set the permissions directly\n"
269 "\n"
270 "See the chmod(1) man page for full details."));
272 g_signal_connect(help, "response",
273 G_CALLBACK(gtk_widget_destroy), NULL);
275 gtk_widget_show_all(help);
278 static void process_message(GUIside *gui_side, const gchar *buffer)
280 ABox *abox = gui_side->abox;
282 if (*buffer == '?')
283 abox_ask(abox, buffer + 1);
284 else if (*buffer == 's')
285 dir_check_this(buffer + 1); /* Update this item */
286 else if (*buffer == '=')
287 abox_add_filename(abox, buffer + 1);
288 else if (*buffer == '#')
289 abox_clear_results(abox);
290 else if (*buffer == 'X')
292 filer_close_recursive(buffer + 1);
293 /* Let child know it's safe to continue... */
294 fputc('X', gui_side->to_child);
295 fflush(gui_side->to_child);
297 else if (*buffer == 'm' || *buffer == 'M')
299 /* Mount / major changes to this path */
300 if (*buffer == 'M')
302 mount_update(TRUE);
303 mount_user_mount(buffer + 1);
305 filer_check_mounted(buffer + 1);
307 else if (*buffer == '/')
308 abox_set_current_object(abox, buffer + 1);
309 else if (*buffer == 'o')
310 filer_opendir(buffer + 1, NULL, NULL);
311 else if (*buffer == '!')
313 gui_side->errors++;
314 abox_log(abox, buffer + 1, "error");
316 else
317 abox_log(abox, buffer + 1, NULL);
320 /* Called when the child sends us a message */
321 static void message_from_child(gpointer data,
322 gint source,
323 GdkInputCondition condition)
325 char buf[5];
326 GUIside *gui_side = (GUIside *) data;
327 ABox *abox = gui_side->abox;
328 GtkTextBuffer *text_buffer;
330 text_buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(abox->log));
332 if (read_exact(source, buf, 4))
334 ssize_t message_len;
335 char *buffer;
337 buf[4] = '\0';
338 message_len = strtol(buf, NULL, 16);
339 buffer = g_malloc(message_len + 1);
340 if (message_len > 0 && read_exact(source, buffer, message_len))
342 buffer[message_len] = '\0';
343 process_message(gui_side, buffer);
344 g_free(buffer);
345 return;
347 g_printerr("Child died in the middle of a message.\n");
350 if (gui_side->abort_attempts)
351 abox_log(abox, _("\nProcess terminated.\n"), "error");
353 /* The child is dead */
354 gui_side->child = 0;
356 fclose(gui_side->to_child);
357 gui_side->to_child = NULL;
358 close(gui_side->from_child);
359 g_source_remove(gui_side->input_tag);
360 abox_cancel_ask(gui_side->abox);
362 if (gui_side->errors)
364 guchar *report;
366 if (gui_side->errors == 1)
367 report = g_strdup(_("There was one error.\n"));
368 else
369 report = g_strdup_printf(_("There were %d errors.\n"),
370 gui_side->errors);
372 gtk_text_buffer_insert_at_cursor(text_buffer, report, -1);
374 g_free(report);
376 else if (gui_side->show_info == FALSE)
377 gtk_widget_destroy(GTK_WIDGET(gui_side->abox));
380 /* Scans src_dir, calling cb(item, dest_path) for each item */
381 static void for_dir_contents(ForDirCB *cb,
382 const char *src_dir,
383 const char *dest_path)
385 DIR *d;
386 struct dirent *ent;
387 GList *list = NULL, *next;
389 d = mc_opendir(src_dir);
390 if (!d)
392 /* Message displayed is "ERROR reading 'path': message" */
393 printf_send("!%s '%s': %s\n", _("ERROR reading"),
394 src_dir, g_strerror(errno));
395 return;
398 send_dir(src_dir);
400 while ((ent = mc_readdir(d)))
402 if (ent->d_name[0] == '.' && (ent->d_name[1] == '\0'
403 || (ent->d_name[1] == '.' && ent->d_name[2] == '\0')))
404 continue;
405 list = g_list_prepend(list, g_strdup(make_path(src_dir,
406 ent->d_name)));
408 mc_closedir(d);
410 for (next = list; next; next = next->next)
412 cb((char *) next->data, dest_path);
414 g_free(next->data);
416 g_list_free(list);
419 /* Read this many bytes into the buffer. TRUE on success. */
420 static gboolean read_exact(int source, char *buffer, ssize_t len)
422 while (len > 0)
424 ssize_t got;
425 got = read(source, buffer, len);
426 if (got < 1)
427 return FALSE;
428 len -= got;
429 buffer += got;
431 return TRUE;
434 static void send_done(void)
436 printf_send(_("'\nDone\n"));
439 /* Notify the filer that this item has been updated */
440 static void send_check_path(const gchar *path)
442 printf_send("s%s", path);
445 /* Notify the filer that this whole subtree has changed (eg, been unmounted) */
446 static void send_mount_path(const gchar *path)
448 printf_send("m%s", path);
451 /* Send a message to the filer process. The first character indicates the
452 * type of the message.
454 static gboolean printf_send(const char *msg, ...)
456 va_list args;
457 gchar *tmp;
459 va_start(args, msg);
460 tmp = g_strdup_vprintf(msg, args);
461 va_end(args);
463 g_string_assign(message, tmp);
464 g_free(tmp);
466 return send();
469 /* Send 'message' to our parent process. TRUE on success. */
470 static gboolean send(void)
472 char len_buffer[5];
473 ssize_t len;
475 g_return_val_if_fail(message->len < 0xffff, FALSE);
477 sprintf(len_buffer, "%04x", message->len);
478 fwrite(len_buffer, 1, 4, to_parent);
479 len = fwrite(message->str, 1, message->len, to_parent);
480 fflush(to_parent);
481 return len == (ssize_t) message->len;
484 /* Set the directory indicator at the top of the window */
485 static gboolean send_dir(const char *dir)
487 return printf_send("/%s", dir);
490 static gboolean send_error(void)
492 return printf_send("!%s: %s\n", _("ERROR"), g_strerror(errno));
495 static void response(GtkDialog *dialog, gint response, GUIside *gui_side)
497 gchar code;
499 if (!gui_side->to_child)
500 return;
502 if (response == GTK_RESPONSE_YES)
503 code = 'Y';
504 else if (response == GTK_RESPONSE_NO)
505 code = 'N';
506 else
507 return;
509 fputc(code, gui_side->to_child);
510 fflush(gui_side->to_child);
513 static void flag_toggled(ABox *abox, gint flag, GUIside *gui_side)
515 if (!gui_side->to_child)
516 return;
518 fputc(flag, gui_side->to_child);
519 fflush(gui_side->to_child);
522 static void read_new_entry_text(void)
524 int len;
525 char c;
526 GString *new;
528 new = g_string_new(NULL);
530 for (;;)
532 len = read(from_parent, &c, 1);
533 if (len != 1)
535 fprintf(stderr, "read() error: %s\n",
536 g_strerror(errno));
537 _exit(1); /* Parent died? */
540 if (c == '\n')
541 break;
542 g_string_append_c(new, c);
545 g_free(new_entry_string);
546 new_entry_string = new->str;
547 g_string_free(new, FALSE);
550 static void process_flag(char flag)
552 switch (flag)
554 case 'Q':
555 quiet = !quiet;
556 break;
557 case 'F':
558 o_force = !o_force;
559 break;
560 case 'R':
561 o_recurse = !o_recurse;
562 break;
563 case 'B':
564 o_brief = !o_brief;
565 break;
566 case 'W':
567 o_newer = !o_newer;
568 break;
569 case 'E':
570 read_new_entry_text();
571 break;
572 default:
573 printf_send("!ERROR: Bad message '%c'\n", flag);
574 break;
578 /* If the parent has sent any flag toggles, read them */
579 static void check_flags(void)
581 fd_set set;
582 int got;
583 char retval;
584 struct timeval tv;
586 FD_ZERO(&set);
588 while (1)
590 FD_SET(from_parent, &set);
591 tv.tv_sec = 0;
592 tv.tv_usec = 0;
593 got = select(from_parent + 1, &set, NULL, NULL, &tv);
595 if (got == -1)
596 g_error("select() failed: %s\n", g_strerror(errno));
597 else if (!got)
598 return;
600 got = read(from_parent, &retval, 1);
601 if (got != 1)
602 g_error("read() error: %s\n", g_strerror(errno));
604 process_flag(retval);
608 /* Read until the user sends a reply. If ignore_quiet is TRUE then
609 * the user MUST click Yes or No, else treat quiet on as Yes.
610 * If the user needs prompting then does send().
612 static gboolean printf_reply(int fd, gboolean ignore_quiet,
613 const char *msg, ...)
615 ssize_t len;
616 char retval;
617 va_list args;
618 gchar *tmp;
620 if (quiet && !ignore_quiet)
621 return TRUE;
623 va_start(args, msg);
624 tmp = g_strdup_vprintf(msg, args);
625 va_end(args);
627 g_string_assign(message, tmp);
628 g_free(tmp);
630 send();
632 while (1)
634 len = read(fd, &retval, 1);
635 if (len != 1)
637 fprintf(stderr, "read() error: %s\n",
638 g_strerror(errno));
639 _exit(1); /* Parent died? */
642 switch (retval)
644 case 'Y':
645 printf_send("' %s\n", _("Yes"));
646 return TRUE;
647 case 'N':
648 printf_send("' %s\n", _("No"));
649 return FALSE;
650 default:
651 process_flag(retval);
652 break;
657 static void abort_operation(GtkWidget *widget, gpointer data)
659 GUIside *gui_side = (GUIside *) data;
661 if (gui_side->child)
663 if (gui_side->abort_attempts == 0)
665 abox_log(ABOX(widget),
666 _("\nAsking child process to terminate...\n"),
667 "error");
668 kill(-gui_side->child, SIGTERM);
670 else
672 abox_log(ABOX(widget),
673 _("\nTrying to KILL run-away process...\n"),
674 "error");
675 kill(-gui_side->child, SIGKILL);
676 kill(-gui_side->child, SIGCONT);
678 gui_side->abort_attempts++;
680 else
681 gtk_widget_destroy(widget);
684 static void destroy_action_window(GtkWidget *widget, gpointer data)
686 GUIside *gui_side = (GUIside *) data;
688 if (gui_side->child)
690 kill(-gui_side->child, SIGTERM);
691 fclose(gui_side->to_child);
692 close(gui_side->from_child);
693 g_source_remove(gui_side->input_tag);
696 g_free(gui_side);
698 one_less_window();
701 /* Create two pipes, fork() a child and return a pointer to a GUIside struct
702 * (NULL on failure). The child calls func().
704 static GUIside *start_action(GtkWidget *abox, ActionChild *func, gpointer data,
705 int force, int brief, int recurse, int newer)
707 gboolean autoq;
708 int filedes[4]; /* 0 and 2 are for reading */
709 GUIside *gui_side;
710 pid_t child;
711 struct sigaction act;
713 if (pipe(filedes))
715 report_error("pipe: %s", g_strerror(errno));
716 gtk_widget_destroy(abox);
717 return NULL;
720 if (pipe(filedes + 2))
722 close(filedes[0]);
723 close(filedes[1]);
724 report_error("pipe: %s", g_strerror(errno));
725 gtk_widget_destroy(abox);
726 return NULL;
729 autoq = gtk_toggle_button_get_active(
730 GTK_TOGGLE_BUTTON(ABOX(abox)->quiet));
732 o_force = force;
733 o_brief = brief;
734 o_recurse = recurse;
735 o_newer = newer;
737 child = fork();
738 switch (child)
740 case -1:
741 report_error("fork: %s", g_strerror(errno));
742 gtk_widget_destroy(abox);
743 return NULL;
744 case 0:
745 /* We are the child */
747 /* Create a new process group */
748 setpgid(0, 0);
750 quiet = autoq;
752 dir_drop_all_dnotifies();
754 /* Reset the SIGCHLD handler */
755 act.sa_handler = SIG_DFL;
756 sigemptyset(&act.sa_mask);
757 act.sa_flags = 0;
758 sigaction(SIGCHLD, &act, NULL);
760 message = g_string_new(NULL);
761 close(filedes[0]);
762 close(filedes[3]);
763 to_parent = fdopen(filedes[1], "wb");
764 from_parent = filedes[2];
765 func(data);
766 send_dir("");
767 _exit(0);
770 /* We are the parent */
771 close(filedes[1]);
772 close(filedes[2]);
773 gui_side = g_new(GUIside, 1);
774 gui_side->from_child = filedes[0];
775 gui_side->to_child = fdopen(filedes[3], "wb");
776 gui_side->child = child;
777 gui_side->errors = 0;
778 gui_side->show_info = FALSE;
779 gui_side->default_string = NULL;
780 gui_side->entry_string_func = NULL;
781 gui_side->abort_attempts = 0;
783 gui_side->abox = ABOX(abox);
784 g_signal_connect(abox, "destroy",
785 G_CALLBACK(destroy_action_window), gui_side);
787 g_signal_connect(abox, "response", G_CALLBACK(response), gui_side);
788 g_signal_connect(abox, "flag_toggled",
789 G_CALLBACK(flag_toggled), gui_side);
790 g_signal_connect(abox, "abort_operation",
791 G_CALLBACK(abort_operation), gui_side);
793 gui_side->input_tag = gtk_input_add_full(gui_side->from_child,
794 GDK_INPUT_READ,
795 message_from_child,
796 NULL, gui_side, NULL);
798 return gui_side;
801 /* ACTIONS ON ONE ITEM */
803 /* These may call themselves recursively, or ask questions, etc */
805 /* Updates the global size_tally, file_counter and dir_counter */
806 static void do_usage(const char *src_path, const char *unused)
808 struct stat info;
810 check_flags();
812 if (mc_lstat(src_path, &info))
814 printf_send("'%s:\n", src_path);
815 send_error();
817 else if (S_ISREG(info.st_mode) || S_ISLNK(info.st_mode))
819 file_counter++;
820 size_tally += info.st_size;
822 else if (S_ISDIR(info.st_mode))
824 dir_counter++;
825 if (printf_reply(from_parent, FALSE,
826 _("?Count contents of %s?"), src_path))
828 char *safe_path;
829 safe_path = g_strdup(src_path);
830 for_dir_contents(do_usage, safe_path, safe_path);
831 g_free(safe_path);
834 else
835 file_counter++;
838 /* dest_path is the dir containing src_path */
839 static void do_delete(const char *src_path, const char *unused)
841 struct stat info;
842 gboolean write_prot;
843 char *safe_path;
845 check_flags();
847 if (mc_lstat(src_path, &info))
849 send_error();
850 return;
853 write_prot = S_ISLNK(info.st_mode) ? FALSE
854 : access(src_path, W_OK) != 0;
855 if (write_prot || !quiet)
857 if (!printf_reply(from_parent, write_prot && !o_force,
858 _("?Delete %s'%s'?"),
859 write_prot ? _("WRITE-PROTECTED ") : "",
860 src_path))
861 return;
863 else if (!o_brief)
864 printf_send(_("'Deleting '%s'\n"), src_path);
866 safe_path = g_strdup(src_path);
868 if (S_ISDIR(info.st_mode))
870 for_dir_contents(do_delete, safe_path, safe_path);
871 if (rmdir(safe_path))
873 g_free(safe_path);
874 send_error();
875 return;
877 printf_send(_("'Directory '%s' deleted\n"), safe_path);
878 send_mount_path(safe_path);
880 else if (unlink(src_path))
881 send_error();
882 else
884 send_check_path(safe_path);
885 if (strcmp(g_basename(safe_path), ".DirIcon") == 0)
887 gchar *dir;
888 dir = g_path_get_dirname(safe_path);
889 send_check_path(dir);
890 g_free(dir);
894 g_free(safe_path);
897 /* path is the item to check. If is is a directory then we may recurse
898 * (unless prune is used).
900 static void do_find(const char *path, const char *unused)
902 FindInfo info;
904 check_flags();
906 if (!quiet)
908 if (!printf_reply(from_parent, FALSE, _("?Check '%s'?"), path))
909 return;
912 for (;;)
914 if (new_entry_string)
916 find_condition_free(find_condition);
917 find_condition = find_compile(new_entry_string);
918 null_g_free(&new_entry_string);
921 if (find_condition)
922 break;
924 printf_send(_("!Invalid find condition - "
925 "change it and try again\n"));
926 if (!printf_reply(from_parent, TRUE,
927 _("?Check '%s'?"), path))
928 return;
931 if (mc_lstat(path, &info.stats))
933 send_error();
934 printf_send(_("'(while checking '%s')\n"), path);
935 return;
938 info.fullpath = path;
939 time(&info.now); /* XXX: Not for each check! */
941 info.leaf = g_basename(path);
942 info.prune = FALSE;
943 if (find_test_condition(find_condition, &info))
944 printf_send("=%s", path);
946 if (S_ISDIR(info.stats.st_mode) && !info.prune)
948 char *safe_path;
949 safe_path = g_strdup(path);
950 for_dir_contents(do_find, safe_path, safe_path);
951 g_free(safe_path);
955 /* Like mode_compile(), but ignores spaces and bracketed bits */
956 static struct mode_change *nice_mode_compile(const char *mode_string,
957 unsigned int masked_ops)
959 GString *new;
960 int brackets = 0;
961 struct mode_change *retval = NULL;
963 new = g_string_new(NULL);
965 for (; *mode_string; mode_string++)
967 if (*mode_string == '(')
968 brackets++;
969 if (*mode_string == ')')
971 brackets--;
972 if (brackets < 0)
973 break;
974 continue;
977 if (brackets == 0 && *mode_string != ' ')
978 g_string_append_c(new, *mode_string);
981 if (brackets == 0)
982 retval = mode_compile(new->str, masked_ops);
983 g_string_free(new, TRUE);
984 return retval;
987 static void do_chmod(const char *path, const char *unused)
989 struct stat info;
990 mode_t new_mode;
992 check_flags();
994 if (mc_lstat(path, &info))
996 send_error();
997 return;
999 if (S_ISLNK(info.st_mode))
1000 return;
1002 if (!quiet)
1004 if (!printf_reply(from_parent, FALSE,
1005 _("?Change permissions of '%s'?"), path))
1006 return;
1008 else if (!o_brief)
1009 printf_send(_("'Changing permissions of '%s'\n"), path);
1011 for (;;)
1013 if (new_entry_string)
1015 if (mode_change)
1016 mode_free(mode_change);
1017 mode_change = nice_mode_compile(new_entry_string,
1018 MODE_MASK_ALL);
1019 null_g_free(&new_entry_string);
1022 if (mode_change)
1023 break;
1025 printf_send(
1026 _("!Invalid mode command - change it and try again\n"));
1027 if (!printf_reply(from_parent, TRUE,
1028 _("?Change permissions of '%s'?"), path))
1029 return;
1032 if (mc_lstat(path, &info))
1034 send_error();
1035 return;
1037 if (S_ISLNK(info.st_mode))
1038 return;
1040 new_mode = mode_adjust(info.st_mode, mode_change);
1041 if (chmod(path, new_mode))
1043 send_error();
1044 return;
1047 send_check_path(path);
1049 if (S_ISDIR(info.st_mode))
1051 send_mount_path(path);
1053 if (o_recurse)
1055 guchar *safe_path;
1056 safe_path = g_strdup(path);
1057 for_dir_contents(do_chmod, safe_path, safe_path);
1058 g_free(safe_path);
1063 /* We want to copy 'object' into directory 'dir'. If 'action_leaf'
1064 * is set then that is the new leafname, otherwise the leafname stays
1065 * the same.
1067 static const char *make_dest_path(const char *object, const char *dir)
1069 const char *leaf;
1071 if (action_leaf)
1072 leaf = action_leaf;
1073 else
1075 leaf = strrchr(object, '/');
1076 if (!leaf)
1077 leaf = object; /* Error? */
1078 else
1079 leaf++;
1082 return make_path(dir, leaf);
1085 /* If action_leaf is not NULL it specifies the new leaf name */
1086 static void do_copy2(const char *path, const char *dest)
1088 const char *dest_path;
1089 struct stat info;
1090 struct stat dest_info;
1092 check_flags();
1094 dest_path = make_dest_path(path, dest);
1096 if (mc_lstat(path, &info))
1098 send_error();
1099 return;
1102 if (mc_lstat(dest_path, &dest_info) == 0)
1104 int err;
1105 gboolean merge;
1107 merge = S_ISDIR(info.st_mode) && S_ISDIR(dest_info.st_mode);
1109 if (!merge && o_newer && info.st_mtime > dest_info.st_mtime)
1111 /* Newer; keep going */
1113 else
1115 if (!printf_reply(from_parent, TRUE,
1116 _("?'%s' already exists - %s?"),
1117 dest_path,
1118 merge ? _("merge contents")
1119 : _("overwrite")))
1120 return;
1123 if (!merge)
1125 if (S_ISDIR(dest_info.st_mode))
1126 err = rmdir(dest_path);
1127 else
1128 err = unlink(dest_path);
1130 if (err)
1132 send_error();
1133 if (errno != ENOENT)
1134 return;
1135 printf_send(_("'Trying copy anyway...\n"));
1139 else if (!quiet)
1141 if (!printf_reply(from_parent, FALSE,
1142 _("?Copy %s as %s?"), path, dest_path))
1143 return;
1145 else
1146 printf_send(_("'Copying %s as %s\n"), path, dest_path);
1148 if (S_ISDIR(info.st_mode))
1150 mode_t mode = info.st_mode;
1151 char *safe_path, *safe_dest;
1152 struct stat dest_info;
1153 gboolean exists;
1155 safe_path = g_strdup(path);
1156 safe_dest = g_strdup(dest_path);
1158 exists = !mc_lstat(dest_path, &dest_info);
1160 if (exists && !S_ISDIR(dest_info.st_mode))
1161 printf_send(_("!ERROR: Destination already exists, "
1162 "but is not a directory\n"));
1163 else if (exists == FALSE && mkdir(dest_path, 0700 | mode))
1164 send_error();
1165 else
1167 if (!exists)
1168 /* (just been created then) */
1169 send_check_path(dest_path);
1171 action_leaf = NULL;
1172 for_dir_contents(do_copy2, safe_path, safe_dest);
1173 /* Note: dest_path now invalid... */
1175 if (!exists)
1177 struct utimbuf utb;
1179 /* We may have created the directory with
1180 * more permissions than the source so that
1181 * we could write to it... change it back now.
1183 if (chmod(safe_dest, mode))
1185 /* Some filesystems don't support
1186 * SetGID and SetUID bits. Ignore
1187 * these errors.
1189 if (errno != EPERM)
1190 send_error();
1193 /* Also, try to preserve the timestamps */
1194 utb.actime = info.st_atime;
1195 utb.modtime = info.st_mtime;
1197 utime(safe_dest, &utb);
1201 g_free(safe_path);
1202 g_free(safe_dest);
1204 else if (S_ISLNK(info.st_mode))
1206 char *target;
1208 /* Not all versions of cp(1) can make symlinks,
1209 * so we special-case it.
1212 target = readlink_dup(path);
1213 if (target)
1215 if (symlink(target, dest_path))
1216 send_error();
1217 else
1218 send_check_path(dest_path);
1220 g_free(target);
1222 else
1223 send_error();
1225 else
1227 guchar *error;
1229 error = copy_file(path, dest_path);
1231 if (error)
1233 printf_send(_("!%s\nFailed to copy '%s'\n"),
1234 error, path);
1235 g_free(error);
1237 else
1238 send_check_path(dest_path);
1242 /* If action_leaf is not NULL it specifies the new leaf name */
1243 static void do_move2(const char *path, const char *dest)
1245 const char *dest_path;
1246 const char *argv[] = {"mv", "-f", NULL, NULL, NULL};
1247 struct stat info2;
1248 gboolean is_dir;
1249 char *err;
1251 check_flags();
1253 dest_path = make_dest_path(path, dest);
1255 is_dir = mc_lstat(path, &info2) == 0 && S_ISDIR(info2.st_mode);
1257 if (access(dest_path, F_OK) == 0)
1259 struct stat info;
1260 int err;
1262 if (mc_lstat(dest_path, &info))
1264 send_error();
1265 return;
1268 if (!is_dir && o_newer && info2.st_mtime > info.st_mtime)
1270 /* Newer; keep going */
1272 else if (!printf_reply(from_parent, TRUE,
1273 _("?'%s' already exists - overwrite?"),
1274 dest_path))
1275 return;
1277 if (S_ISDIR(info.st_mode))
1278 err = rmdir(dest_path);
1279 else
1280 err = unlink(dest_path);
1282 if (err)
1284 send_error();
1285 if (errno != ENOENT)
1286 return;
1287 printf_send(_("'Trying move anyway...\n"));
1290 else if (!quiet)
1292 if (!printf_reply(from_parent, FALSE,
1293 _("?Move %s as %s?"), path, dest_path))
1294 return;
1296 else
1297 printf_send(_("'Moving %s as %s\n"), path, dest_path);
1299 argv[2] = path;
1300 argv[3] = dest_path;
1302 err = fork_exec_wait(argv);
1303 if (err)
1305 printf_send(_("!%s\nFailed to move %s as %s\n"),
1306 err, path, dest_path);
1307 g_free(err);
1309 else
1311 send_check_path(dest_path);
1313 if (is_dir)
1314 send_mount_path(path);
1315 else
1316 send_check_path(path);
1320 /* Copy path to dest.
1321 * Check that path not copied into itself.
1323 static void do_copy(const char *path, const char *dest)
1325 if (is_sub_dir(make_dest_path(path, dest), path))
1326 printf_send(_("!ERROR: Can't copy object into itself\n"));
1327 else
1329 do_copy2(path, dest);
1330 send_check_path(dest);
1334 /* Move path to dest.
1335 * Check that path not moved into itself.
1337 static void do_move(const char *path, const char *dest)
1339 if (is_sub_dir(make_dest_path(path, dest), path))
1340 printf_send(
1341 _("!ERROR: Can't move/rename object into itself\n"));
1342 else
1344 do_move2(path, dest);
1345 send_check_path(dest);
1349 static void do_link(const char *path, const char *dest)
1351 const char *dest_path;
1353 check_flags();
1355 dest_path = make_dest_path(path, dest);
1357 if (quiet)
1358 printf_send(_("'Linking %s as %s\n"), path, dest_path);
1359 else if (!printf_reply(from_parent, FALSE,
1360 _("?Link %s as %s?"), path, dest_path))
1361 return;
1363 if (symlink(path, dest_path))
1364 send_error();
1365 else
1366 send_check_path(dest_path);
1369 /* Mount/umount this item (depending on 'mount') */
1370 static void do_mount(const guchar *path, gboolean mount)
1372 const char *argv[3] = {NULL, NULL, NULL};
1373 char *err;
1375 check_flags();
1377 argv[0] = mount ? "mount" : "umount";
1378 argv[1] = path;
1380 if (quiet)
1381 printf_send(mount ? _("'Mounting %s\n")
1382 : _("'Unmounting %s\n"),
1383 path);
1384 else if (!printf_reply(from_parent, FALSE,
1385 mount ? _("?Mount %s?")
1386 : _("?Unmount %s?"),
1387 path))
1388 return;
1390 if (!mount)
1392 char c = '?';
1393 /* Need to close all sub-directories now, or we
1394 * can't unmount if dnotify is used.
1396 printf_send("X%s", path);
1397 /* Wait until it's safe... */
1398 read(from_parent, &c, 1);
1399 g_return_if_fail(c == 'X');
1402 err = fork_exec_wait(argv);
1403 if (err)
1405 printf_send(mount ?
1406 _("!%s\nMount failed\n") :
1407 _("!%s\nUnmount failed\n"), err);
1408 g_free(err);
1410 else
1412 printf_send("M%s", path);
1413 if (mount && mount_open_dir)
1414 printf_send("o%s", path);
1418 /* CHILD MAIN LOOPS */
1420 /* After forking, the child calls one of these functions */
1422 /* We use a double for total size in order to count beyond 4Gb */
1423 static void usage_cb(gpointer data)
1425 GList *paths = (GList *) data;
1426 double total_size = 0;
1428 dir_counter = file_counter = 0;
1430 for (; paths; paths = paths->next)
1432 guchar *path = (guchar *) paths->data;
1434 send_dir(path);
1436 size_tally = 0;
1438 do_usage(path, NULL);
1440 printf_send("'%s: %s\n",
1441 g_basename(path),
1442 format_double_size(size_tally));
1443 total_size += size_tally;
1446 g_string_printf(message, _("'\nTotal: %s ("),
1447 format_double_size(total_size));
1449 if (file_counter)
1450 g_string_append_printf(message,
1451 "%ld %s%s", file_counter,
1452 file_counter == 1 ? _("file") : _("files"),
1453 dir_counter ? ", " : ")\n");
1455 if (file_counter == 0 && dir_counter == 0)
1456 g_string_append(message, _("no directories)\n"));
1457 else if (dir_counter)
1458 g_string_append_printf(message,
1459 "%ld %s)\n", dir_counter,
1460 dir_counter == 1 ? _("directory")
1461 : _("directories"));
1463 send();
1466 #ifdef DO_MOUNT_POINTS
1467 static void mount_cb(gpointer data)
1469 GList *paths = (GList *) data;
1470 gboolean mount_points = FALSE;
1472 for (; paths; paths = paths->next)
1474 guchar *path = (guchar *) paths->data;
1475 guchar *target;
1477 target = readlink_dup(path);
1478 if (!target)
1479 target = path;
1481 if (mount_is_mounted(target, NULL, NULL))
1483 mount_points = TRUE;
1484 do_mount(target, FALSE); /* Unmount */
1486 else if (g_hash_table_lookup(fstab_mounts, target))
1488 mount_points = TRUE;
1489 do_mount(target, TRUE); /* Mount */
1492 if (target != path)
1493 g_free(target);
1496 if (mount_points)
1497 send_done();
1498 else
1499 printf_send(_("!No mount points selected!\n"));
1501 #endif
1503 /* (use g_dirname() instead?) */
1504 static guchar *dirname(guchar *path)
1506 guchar *slash;
1508 slash = strrchr(path, '/');
1509 g_return_val_if_fail(slash != NULL, g_strdup(path));
1511 if (slash != path)
1512 return g_strndup(path, slash - path);
1513 return g_strdup("/");
1516 static void delete_cb(gpointer data)
1518 GList *paths = (GList *) data;
1520 for (; paths; paths = paths->next)
1522 guchar *path = (guchar *) paths->data;
1523 guchar *dir;
1525 dir = dirname(path);
1526 send_dir(dir);
1528 do_delete(path, dir);
1530 g_free(dir);
1533 send_done();
1536 static void find_cb(gpointer data)
1538 GList *all_paths = (GList *) data;
1539 GList *paths;
1541 while (1)
1543 for (paths = all_paths; paths; paths = paths->next)
1545 guchar *path = (guchar *) paths->data;
1547 send_dir(path);
1549 do_find(path, NULL);
1552 if (!printf_reply(from_parent, TRUE,
1553 _("?Another search?")))
1554 break;
1555 printf_send("#");
1558 send_done();
1561 static void chmod_cb(gpointer data)
1563 GList *paths = (GList *) data;
1565 for (; paths; paths = paths->next)
1567 guchar *path = (guchar *) paths->data;
1568 struct stat info;
1570 send_dir(path);
1572 if (mc_stat(path, &info) != 0)
1573 send_error();
1574 else if (S_ISLNK(info.st_mode))
1575 printf_send(_("!'%s' is a symbolic link\n"),
1576 g_basename(path));
1577 else
1578 do_chmod(path, NULL);
1581 send_done();
1584 static void list_cb(gpointer data)
1586 GList *paths = (GList *) data;
1588 for (; paths; paths = paths->next)
1590 send_dir((char *) paths->data);
1592 action_do_func((char *) paths->data, action_dest);
1595 send_done();
1598 /* EXTERNAL INTERFACE */
1600 void action_find(GList *paths)
1602 GUIside *gui_side;
1603 GtkWidget *abox;
1605 if (!paths)
1607 report_error(_("You need to select some items "
1608 "to search through"));
1609 return;
1612 if (!last_find_string)
1613 last_find_string = g_strdup("'core'");
1615 new_entry_string = last_find_string;
1617 abox = abox_new(_("Find"), FALSE);
1618 gui_side = start_action(abox, find_cb, paths,
1619 o_action_force.int_value,
1620 o_action_brief.int_value,
1621 o_action_recurse.int_value,
1622 o_action_newer.int_value);
1623 if (!gui_side)
1624 return;
1626 abox_add_results(ABOX(abox));
1628 gui_side->default_string = &last_find_string;
1629 abox_add_entry(ABOX(abox), last_find_string,
1630 new_help_button(show_condition_help, NULL));
1631 g_signal_connect(ABOX(abox)->entry, "changed",
1632 G_CALLBACK(entry_changed), gui_side);
1633 set_find_string_colour(ABOX(abox)->entry, last_find_string);
1635 gui_side->show_info = TRUE;
1636 gui_side->entry_string_func = set_find_string_colour;
1638 number_of_windows++;
1639 gtk_widget_show_all(abox);
1642 /* Count disk space used by selected items */
1643 void action_usage(GList *paths)
1645 GUIside *gui_side;
1646 GtkWidget *abox;
1648 if (!paths)
1650 report_error(_("You need to select some items to count"));
1651 return;
1654 abox = abox_new(_("Disk Usage"), TRUE);
1656 gui_side = start_action(abox, usage_cb, paths,
1657 o_action_force.int_value,
1658 o_action_brief.int_value,
1659 o_action_recurse.int_value,
1660 o_action_newer.int_value);
1661 if (!gui_side)
1662 return;
1664 gui_side->show_info = TRUE;
1666 number_of_windows++;
1668 gtk_widget_show(abox);
1671 /* Mount/unmount listed items (paths).
1672 * Free the list after this function returns.
1673 * If open_dir is TRUE and the dir is successfully mounted, open it.
1674 * quiet can be -1 for default.
1676 void action_mount(GList *paths, gboolean open_dir, int quiet)
1678 #ifdef DO_MOUNT_POINTS
1679 GUIside *gui_side;
1680 GtkWidget *abox;
1682 if (quiet == -1)
1683 quiet = o_action_mount.int_value;
1685 mount_open_dir = open_dir;
1687 abox = abox_new(_("Mount / Unmount"), quiet);
1688 gui_side = start_action(abox, mount_cb, paths,
1689 o_action_force.int_value,
1690 o_action_brief.int_value,
1691 o_action_recurse.int_value,
1692 o_action_newer.int_value);
1693 if (!gui_side)
1694 return;
1696 number_of_windows++;
1697 gtk_widget_show(abox);
1698 #else
1699 report_error(
1700 _("ROX-Filer does not yet support mount points on your "
1701 "system. Sorry."));
1702 #endif /* DO_MOUNT_POINTS */
1705 /* Delete these paths */
1706 void action_delete(GList *paths)
1708 GUIside *gui_side;
1709 GtkWidget *abox;
1711 if (!remove_pinned_ok(paths))
1712 return;
1714 abox = abox_new(_("Delete"), o_action_delete.int_value);
1715 gui_side = start_action(abox, delete_cb, paths,
1716 o_action_force.int_value,
1717 o_action_brief.int_value,
1718 o_action_recurse.int_value,
1719 o_action_newer.int_value);
1720 if (!gui_side)
1721 return;
1723 abox_add_flag(ABOX(abox),
1724 _("Force"), _("Don't confirm deletion of non-writeable items"),
1725 'F', o_action_force.int_value);
1726 abox_add_flag(ABOX(abox),
1727 _("Brief"), _("Only log directories being deleted"),
1728 'B', o_action_brief.int_value);
1730 number_of_windows++;
1731 gtk_widget_show(abox);
1734 /* Change the permissions of the selected items */
1735 void action_chmod(GList *paths, gboolean force_recurse, const char *action)
1737 GtkWidget *abox;
1738 GUIside *gui_side;
1739 static GList *presets = NULL;
1740 gboolean recurse = force_recurse || o_action_recurse.int_value;
1742 if (!paths)
1744 report_error(_("You need to select the items "
1745 "whose permissions you want to change"));
1746 return;
1749 if (!presets)
1751 presets = g_list_append(presets, (gchar *)
1752 _("a+x (Make executable/searchable)"));
1753 presets = g_list_append(presets, (gchar *)
1754 _("a-x (Make non-executable/non-searchable)"));
1755 presets = g_list_append(presets, (gchar *)
1756 _("u+rw (Give owner read+write)"));
1757 presets = g_list_append(presets, (gchar *)
1758 _("go-rwx (Private - owner access only)"));
1759 presets = g_list_append(presets, (gchar *)
1760 _("go=u-w (Public access, not write)"));
1763 if (!last_chmod_string)
1764 last_chmod_string = g_strdup((guchar *) presets->data);
1766 if (action)
1767 new_entry_string = g_strdup(action);
1768 else
1769 new_entry_string = g_strdup(last_chmod_string);
1771 abox = abox_new(_("Permissions"), FALSE);
1772 gui_side = start_action(abox, chmod_cb, paths,
1773 o_action_force.int_value,
1774 o_action_brief.int_value,
1775 recurse,
1776 o_action_newer.int_value);
1778 if (!gui_side)
1779 goto out;
1781 abox_add_flag(ABOX(abox),
1782 _("Brief"), _("Don't list processed files"),
1783 'B', o_action_brief.int_value);
1784 abox_add_flag(ABOX(abox),
1785 _("Recurse"), _("Also change contents of subdirectories"),
1786 'R', recurse);
1788 gui_side->default_string = &last_chmod_string;
1789 abox_add_combo(ABOX(abox), presets, new_entry_string,
1790 new_help_button(show_chmod_help, NULL));
1792 g_signal_connect(ABOX(abox)->entry, "changed",
1793 G_CALLBACK(entry_changed), gui_side);
1794 #if 0
1795 g_signal_connect_swapped(gui_side->entry, "activate",
1796 G_CALLBACK(gtk_button_clicked),
1797 gui_side->yes);
1798 #endif
1800 number_of_windows++;
1801 gtk_widget_show(abox);
1803 out:
1804 null_g_free(&new_entry_string);
1807 /* If leaf is NULL then the copy has the same name as the original.
1808 * quiet can be -1 for default.
1810 void action_copy(GList *paths, const char *dest, const char *leaf, int quiet)
1812 GUIside *gui_side;
1813 GtkWidget *abox;
1815 if (quiet == -1)
1816 quiet = o_action_copy.int_value;
1818 action_dest = dest;
1819 action_leaf = leaf;
1820 action_do_func = do_copy;
1822 abox = abox_new(_("Copy"), quiet);
1823 gui_side = start_action(abox, list_cb, paths,
1824 o_action_force.int_value,
1825 o_action_brief.int_value,
1826 o_action_recurse.int_value,
1827 o_action_newer.int_value);
1828 if (!gui_side)
1829 return;
1831 abox_add_flag(ABOX(abox),
1832 _("Newer"),
1833 _("Only over-write if source is newer than destination."),
1834 'W', o_action_newer.int_value);
1836 number_of_windows++;
1837 gtk_widget_show(abox);
1840 /* If leaf is NULL then the file is not renamed.
1841 * quiet can be -1 for default.
1843 void action_move(GList *paths, const char *dest, const char *leaf, int quiet)
1845 GUIside *gui_side;
1846 GtkWidget *abox;
1848 if (quiet == -1)
1849 quiet = o_action_move.int_value;
1851 action_dest = dest;
1852 action_leaf = leaf;
1853 action_do_func = do_move;
1855 abox = abox_new(_("Move"), quiet);
1856 gui_side = start_action(abox, list_cb, paths,
1857 o_action_force.int_value,
1858 o_action_brief.int_value,
1859 o_action_recurse.int_value,
1860 o_action_newer.int_value);
1861 if (!gui_side)
1862 return;
1864 abox_add_flag(ABOX(abox),
1865 _("Newer"),
1866 _("Only over-write if source is newer than destination."),
1867 'W', o_action_newer.int_value);
1868 number_of_windows++;
1869 gtk_widget_show(abox);
1872 /* If leaf is NULL then the link will have the same name */
1873 /* XXX: No quiet option here? */
1874 void action_link(GList *paths, const char *dest, const char *leaf)
1876 GtkWidget *abox;
1877 GUIside *gui_side;
1879 action_dest = dest;
1880 action_leaf = leaf;
1881 action_do_func = do_link;
1883 abox = abox_new(_("Link"), o_action_link.int_value);
1884 gui_side = start_action(abox, list_cb, paths,
1885 o_action_force.int_value,
1886 o_action_brief.int_value,
1887 o_action_recurse.int_value,
1888 o_action_newer.int_value);
1889 if (!gui_side)
1890 return;
1892 number_of_windows++;
1893 gtk_widget_show(abox);
1896 void action_init(void)
1898 option_add_int(&o_action_copy, "action_copy", 1);
1899 option_add_int(&o_action_move, "action_move", 1);
1900 option_add_int(&o_action_link, "action_link", 1);
1901 option_add_int(&o_action_delete, "action_delete", 0);
1902 option_add_int(&o_action_mount, "action_mount", 1);
1903 option_add_int(&o_action_force, "action_force", FALSE);
1904 option_add_int(&o_action_brief, "action_brief", FALSE);
1905 option_add_int(&o_action_recurse, "action_recurse", FALSE);
1906 option_add_int(&o_action_newer, "action_newer", FALSE);
1909 #define MAX_ASK 4
1911 /* Check to see if any of the selected items (or their children) are
1912 * on the pinboard or panel. If so, ask for confirmation.
1914 * TRUE if it's OK to lose them.
1916 static gboolean remove_pinned_ok(GList *paths)
1918 GList *ask = NULL, *next;
1919 GString *message;
1920 int i, ask_n = 0;
1921 gboolean retval;
1923 for (; paths; paths = paths->next)
1925 guchar *path = (guchar *) paths->data;
1927 if (icons_require(path))
1929 if (++ask_n > MAX_ASK)
1930 break;
1931 ask = g_list_append(ask, path);
1935 if (!ask)
1936 return TRUE;
1938 if (ask_n > MAX_ASK)
1940 message = g_string_new(_("Deleting items such as "));
1941 ask_n--;
1943 else if (ask_n == 1)
1944 message = g_string_new(_("Deleting the item "));
1945 else
1946 message = g_string_new(_("Deleting the items "));
1948 i = 0;
1949 for (next = ask; next; next = next->next)
1951 guchar *path = (guchar *) next->data;
1952 guchar *leaf;
1954 leaf = strrchr(path, '/');
1955 if (leaf)
1956 leaf++;
1957 else
1958 leaf = path;
1960 g_string_append_c(message, '`');
1961 g_string_append(message, leaf);
1962 g_string_append_c(message, '\'');
1963 i++;
1964 if (i == ask_n - 1 && i > 0)
1965 g_string_append(message, _(" and "));
1966 else if (i < ask_n)
1967 g_string_append(message, ", ");
1970 g_list_free(ask);
1972 if (ask_n == 1)
1973 message = g_string_append(message,
1974 _(" will affect some items on the pinboard "
1975 "or panel - really delete it?"));
1976 else
1978 if (ask_n > MAX_ASK)
1979 message = g_string_append_c(message, ',');
1980 message = g_string_append(message,
1981 _(" will affect some items on the pinboard "
1982 "or panel - really delete them?"));
1985 retval = confirm(message->str, GTK_STOCK_DELETE, NULL);
1987 g_string_free(message, TRUE);
1989 return retval;
1992 void set_find_string_colour(GtkWidget *widget, const guchar *string)
1994 FindCondition *cond;
1996 cond = find_compile(string);
1997 entry_set_error(widget, !cond);
1999 find_condition_free(cond);