Defuzzied one entry
[midnight-commander.git] / gnome / gnome-file-property-dialog.c
blob20cd6bded423dffa62c2b065dea4c7e4a14a0085
1 /* gnome-file-property-dialog.c
2 * Copyright (C) 1999 Free Software Foundation
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
19 #include <config.h>
20 #include "dir.h"
21 #include "util.h"
22 #include <gnome.h>
23 #include <time.h>
24 #include <string.h>
25 #include "gnome-file-property-dialog.h"
26 #include "gdesktop.h"
27 #include <pwd.h>
28 #include <grp.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include "fileopctx.h"
32 #include "file.h"
33 #include "../vfs/vfs.h"
34 #include "gicon.h"
35 #include "dialog.h"
36 #include "gmain.h"
38 static void gnome_file_property_dialog_init (GnomeFilePropertyDialog *file_property_dialog);
39 static void gnome_file_property_dialog_class_init (GnomeFilePropertyDialogClass *klass);
40 static void gnome_file_property_dialog_finalize (GtkObject *object);
42 static GnomeDialogClass *parent_class = NULL;
45 GtkType
46 gnome_file_property_dialog_get_type (void)
48 static GtkType file_property_dialog_type = 0;
50 if (!file_property_dialog_type)
52 static const GtkTypeInfo file_property_dialog_info =
54 "GnomeFilePropertyDialog",
55 sizeof (GnomeFilePropertyDialog),
56 sizeof (GnomeFilePropertyDialogClass),
57 (GtkClassInitFunc) gnome_file_property_dialog_class_init,
58 (GtkObjectInitFunc) gnome_file_property_dialog_init,
59 /* reserved_1 */ NULL,
60 /* reserved_2 */ NULL,
63 file_property_dialog_type = gtk_type_unique (gnome_dialog_get_type (), &file_property_dialog_info);
66 return file_property_dialog_type;
69 static void
70 gnome_file_property_dialog_class_init (GnomeFilePropertyDialogClass *klass)
72 GtkObjectClass *object_class;
74 object_class = (GtkObjectClass*) klass;
76 parent_class = gtk_type_class (gnome_dialog_get_type ());
77 object_class->finalize = gnome_file_property_dialog_finalize;
82 static void
83 gnome_file_property_dialog_init (GnomeFilePropertyDialog *file_property_dialog)
85 gnome_dialog_close_hides (GNOME_DIALOG (file_property_dialog), TRUE);
87 file_property_dialog->file_name = NULL;
88 file_property_dialog->file_entry = NULL;
89 file_property_dialog->group_name = NULL;
90 file_property_dialog->modifyable = TRUE;
91 file_property_dialog->user_name = NULL;
92 file_property_dialog->prop1_label = NULL;
93 file_property_dialog->prop2_label = NULL;
94 file_property_dialog->prop1_entry = NULL;
95 file_property_dialog->prop2_entry = NULL;
96 file_property_dialog->prop1_cbox = NULL;
97 file_property_dialog->prop2_cbox = NULL;
98 file_property_dialog->fm_open = NULL;
99 file_property_dialog->fm_view = NULL;
100 file_property_dialog->edit = NULL;
101 file_property_dialog->drop_target = NULL;
102 file_property_dialog->im = NULL;
105 static void
106 gnome_file_property_dialog_finalize (GtkObject *object)
108 GnomeFilePropertyDialog *gfpd;
110 g_return_if_fail (object != NULL);
111 g_return_if_fail (GNOME_IS_FILE_PROPERTY_DIALOG (object));
113 gfpd = GNOME_FILE_PROPERTY_DIALOG (object);
115 if (gfpd->file_name)
116 g_free (gfpd->file_name);
117 if (gfpd->fm_open)
118 g_free (gfpd->fm_open);
119 if (gfpd->fm_view)
120 g_free (gfpd->fm_view);
121 if (gfpd->edit)
122 g_free (gfpd->edit);
123 if (gfpd->drop_target)
124 g_free (gfpd->drop_target);
125 if (gfpd->icon_filename)
126 g_free (gfpd->icon_filename);
127 if (gfpd->desktop_url)
128 g_free (gfpd->desktop_url);
129 if (gfpd->caption)
130 g_free (gfpd->caption);
132 if (gfpd->mode_name)
133 g_free (gfpd->mode_name);
135 (* GTK_OBJECT_CLASS (parent_class)->finalize) (object);
138 /* Create the pane */
140 static GtkWidget *
141 create_general_properties (GnomeFilePropertyDialog *fp_dlg)
143 GtkWidget *vbox;
144 GtkWidget *hbox;
145 GtkWidget *label;
146 GtkWidget *align;
147 gchar *direc;
148 gchar *gen_string;
149 gchar buf[MC_MAXPATHLEN];
150 gchar buf2[MC_MAXPATHLEN];
151 file_entry *fe;
152 GtkWidget *icon;
153 struct tm *time;
154 GtkWidget *table;
155 int n;
157 vbox = gtk_vbox_new (FALSE, GNOME_PAD_SMALL);
158 gtk_container_set_border_width (GTK_CONTAINER (vbox), GNOME_PAD);
160 /* first, we set the icon */
161 direc = g_strdup (fp_dlg->file_name);
162 strrchr (direc, '/')[0] = '\0';
163 fe = file_entry_from_file (fp_dlg->file_name);
164 fp_dlg->im = gicon_get_icon_for_file (direc, fe, FALSE);
165 file_entry_free (fe);
166 g_free (direc);
167 icon = gnome_pixmap_new_from_imlib (fp_dlg->im);
168 gtk_box_pack_start (GTK_BOX (vbox), icon, FALSE, FALSE, 0);
170 /* we set the file part */
171 gen_string = g_strconcat (_("Full Name: "), fp_dlg->file_name, NULL);
172 label = gtk_label_new (gen_string);
173 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
174 g_free (gen_string);
175 gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
176 /* if it's a symlink */
177 align = gtk_alignment_new (0.0, 0.5, 1.0, 1.0);
178 hbox = gtk_hbox_new (FALSE, 2);
179 label = gtk_label_new (_("File Name"));
180 gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
181 fp_dlg->file_entry = gtk_entry_new ();
182 gen_string = strrchr (fp_dlg->file_name, '/');
183 if (gen_string)
184 gtk_entry_set_text (GTK_ENTRY (fp_dlg->file_entry), gen_string + 1);
185 else
186 /* I don't think this should happen anymore, but it can't hurt in
187 * case this ever gets used outside gmc */
188 gtk_entry_set_text (GTK_ENTRY (fp_dlg->file_entry), fp_dlg->file_name);
189 /* We want to prevent editing of the file if
190 * the thing is a BLK, CHAR, or we don't own it */
191 /* FIXME: We can actually edit it if we're in the same grp of the file. */
192 if (fp_dlg->modifyable == FALSE)
193 gtk_widget_set_sensitive (fp_dlg->file_entry, FALSE);
194 gtk_box_pack_start (GTK_BOX (hbox), fp_dlg->file_entry, FALSE, FALSE, 0);
195 gtk_container_add (GTK_CONTAINER (align), hbox);
196 gtk_box_pack_start (GTK_BOX (vbox), align, FALSE, FALSE, 0);
197 gtk_box_pack_start (GTK_BOX (vbox), gtk_hseparator_new (), FALSE, FALSE, 8);
199 /* File statistics */
200 /* File type first */
201 if (S_ISREG (fp_dlg->st.st_mode)) {
202 gen_string = g_strconcat (_("File Type: "), use_magic ?
203 gnome_mime_type_or_default_of_file (fp_dlg->file_name, "text/plain") :
204 gnome_mime_type (fp_dlg->file_name),
205 NULL);
206 label = gtk_label_new (gen_string);
207 g_free (gen_string);
208 } else if (S_ISLNK (fp_dlg->st.st_mode)) {
209 label = gtk_label_new (_("File Type: Symbolic Link"));
210 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
211 gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
212 n = mc_readlink (fp_dlg->file_name, buf, MC_MAXPATHLEN);
213 if (n < 0)
214 label = gtk_label_new (_("Target Name: INVALID LINK"));
215 else {
216 buf [n] = 0;
217 gen_string = g_strconcat (_("Target Name: "), buf, NULL);
218 label = gtk_label_new (gen_string);
219 g_free (gen_string);
221 }else if (S_ISDIR (fp_dlg->st.st_mode))
222 label = gtk_label_new (_("File Type: Directory"));
223 else if (S_ISCHR (fp_dlg->st.st_mode))
224 label = gtk_label_new (_("File Type: Character Device"));
225 else if (S_ISBLK (fp_dlg->st.st_mode))
226 label = gtk_label_new (_("File Type: Block Device"));
227 else if (S_ISSOCK (fp_dlg->st.st_mode))
228 label = gtk_label_new (_("File Type: Socket"));
229 else if (S_ISFIFO (fp_dlg->st.st_mode))
230 label = gtk_label_new (_("File Type: FIFO"));
231 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
232 gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
234 /* Now file size */
235 if (S_ISDIR (fp_dlg->st.st_mode)
236 || S_ISREG (fp_dlg->st.st_mode)
237 || S_ISLNK (fp_dlg->st.st_mode)) {
238 if ((gint)fp_dlg->st.st_size < 1024) {
239 g_snprintf (buf, MC_MAXPATHLEN, "%d", (gint) fp_dlg->st.st_size);
240 gen_string = g_strconcat (_("File Size: "), buf, _(" bytes"), NULL);
241 } else if ((gint)fp_dlg->st.st_size < 1024 * 1024) {
242 g_snprintf (buf, MC_MAXPATHLEN, "%.1f", (gfloat) fp_dlg->st.st_size / 1024.0);
243 g_snprintf (buf2, MC_MAXPATHLEN, "%d", (gint) fp_dlg->st.st_size);
244 gen_string = g_strconcat (_("File Size: "), buf, _(" KBytes ("),
245 buf2, _(" bytes)"), NULL);
246 } else {
247 g_snprintf (buf, MC_MAXPATHLEN, "%.1f",
248 (gfloat) fp_dlg->st.st_size / (1024.0 * 1024.0));
249 g_snprintf (buf2, MC_MAXPATHLEN, "%d", (gint) fp_dlg->st.st_size);
250 gen_string = g_strconcat (_("File Size: "), buf, _(" MBytes ("),
251 buf2, _(" bytes)"), NULL);
253 label = gtk_label_new (gen_string);
254 g_free (gen_string);
255 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
256 gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
257 } else {
258 label = gtk_label_new (_("File Size: N/A"));
259 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
260 gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
263 gtk_box_pack_start (GTK_BOX (vbox), gtk_hseparator_new (), FALSE, FALSE, 8);
265 /* Time Fields */
266 table = gtk_table_new (3, 2, FALSE);
267 gtk_table_set_row_spacings (GTK_TABLE (table), 2);
268 gtk_box_pack_start (GTK_BOX (vbox), table, FALSE, FALSE, 0);
269 label = gtk_label_new (_("File Created on: "));
270 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
271 gtk_table_attach_defaults (GTK_TABLE (table), label, 0, 1, 0, 1);
272 time = localtime (&(fp_dlg->st.st_ctime));
273 strftime (buf, MC_MAXPATHLEN, _("%a, %b %d %Y, %I:%M:%S %p"), time);
274 label = gtk_label_new (buf);
275 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
276 gtk_table_attach_defaults (GTK_TABLE (table), label, 1, 2, 0, 1);
278 label = gtk_label_new (_("Last Modified on: "));
279 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
280 gtk_table_attach_defaults (GTK_TABLE (table), label, 0, 1, 1, 2);
281 time = localtime (&(fp_dlg->st.st_mtime));
282 strftime (buf, MC_MAXPATHLEN, _("%a, %b %d %Y, %I:%M:%S %p"), time);
283 label = gtk_label_new (buf);
284 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
285 gtk_table_attach_defaults (GTK_TABLE (table), label, 1, 2, 1, 2);
287 label = gtk_label_new (_("Last Accessed on: "));
288 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
289 gtk_table_attach_defaults (GTK_TABLE (table), label, 0, 1, 2, 3);
290 time = localtime (&(fp_dlg->st.st_atime));
291 strftime (buf, MC_MAXPATHLEN, _("%a, %b %d %Y, %I:%M:%S %p"), time);
292 label = gtk_label_new (buf);
293 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
294 gtk_table_attach_defaults (GTK_TABLE (table), label, 1, 2, 2, 3);
295 return vbox;
298 static GtkWidget *
299 create_url_properties (GnomeFilePropertyDialog *fp_dlg)
301 GtkWidget *table, *label;
303 table = gtk_table_new (0, 0, 0);
305 label = gtk_label_new (_("URL:"));
306 gtk_table_attach (GTK_TABLE (table), label,
307 1, 2, 1, 2, 0, 0, GNOME_PAD, GNOME_PAD);
308 fp_dlg->desktop_entry = gtk_entry_new ();
309 gtk_table_attach (GTK_TABLE (table), fp_dlg->desktop_entry,
310 2, 3, 1, 2, GTK_FILL | GTK_EXPAND, 0, GNOME_PAD, GNOME_PAD);
312 label = gtk_label_new (_("Caption:"));
313 gtk_table_attach (GTK_TABLE (table), label,
314 1, 2, 2, 3, 0, 0, GNOME_PAD, GNOME_PAD);
315 fp_dlg->caption_entry = gtk_entry_new ();
316 gtk_table_attach (GTK_TABLE (table), fp_dlg->caption_entry,
317 2, 3, 2, 3, GTK_FILL | GTK_EXPAND, 0, GNOME_PAD, GNOME_PAD);
319 gtk_widget_show_all (table);
320 return table;
323 /* Settings Pane */
324 static void
325 metadata_toggled (GtkWidget *cbox, GnomeFilePropertyDialog *fp_dlg)
327 if (fp_dlg->changing)
328 return;
329 if (cbox == fp_dlg->open_cbox) {
330 if (GTK_TOGGLE_BUTTON (cbox)->active) {
331 gtk_widget_set_sensitive (fp_dlg->open_entry, FALSE);
332 if (fp_dlg->mime_fm_open)
333 gtk_entry_set_text (GTK_ENTRY (fp_dlg->open_entry), fp_dlg->mime_fm_open);
334 } else {
335 gtk_widget_set_sensitive (fp_dlg->open_entry, TRUE);
336 if (fp_dlg->fm_open) {
337 gtk_entry_set_text (GTK_ENTRY (fp_dlg->open_entry), fp_dlg->fm_open);
340 } else if (cbox == fp_dlg->prop1_cbox) {
341 if (GTK_TOGGLE_BUTTON (cbox)->active) {
342 gtk_widget_set_sensitive (fp_dlg->prop1_entry, FALSE);
343 if (fp_dlg->executable && fp_dlg->mime_drop_target) {
344 gtk_entry_set_text (GTK_ENTRY (fp_dlg->prop1_entry), fp_dlg->mime_drop_target);
345 } else if (!fp_dlg->executable && fp_dlg->mime_fm_view) {
346 gtk_entry_set_text (GTK_ENTRY (fp_dlg->prop1_entry), fp_dlg->mime_fm_view);
347 } else {
348 gtk_entry_set_text (GTK_ENTRY (fp_dlg->prop1_entry), "");
350 } else {
351 gtk_widget_set_sensitive (fp_dlg->prop1_entry, TRUE);
352 if (fp_dlg->executable && fp_dlg->drop_target) {
353 gtk_entry_set_text (GTK_ENTRY (fp_dlg->prop1_entry), fp_dlg->drop_target);
354 } else if (!fp_dlg->executable && fp_dlg->fm_view) {
355 gtk_entry_set_text (GTK_ENTRY (fp_dlg->prop1_entry), fp_dlg->fm_view);
358 } else {
359 if (GTK_TOGGLE_BUTTON (cbox)->active) {
360 gtk_widget_set_sensitive (fp_dlg->prop2_entry, FALSE);
361 if (fp_dlg->mime_edit) {
362 gtk_entry_set_text (GTK_ENTRY (fp_dlg->prop2_entry), fp_dlg->mime_edit);
363 } else {
364 gtk_entry_set_text (GTK_ENTRY (fp_dlg->prop2_entry), "");
366 } else {
367 gtk_widget_set_sensitive (fp_dlg->prop2_entry, TRUE);
368 if (fp_dlg->edit) {
369 gtk_entry_set_text (GTK_ENTRY (fp_dlg->prop2_entry), fp_dlg->edit);
375 static void
376 switch_metadata_box (GnomeFilePropertyDialog *fp_dlg)
378 if (NULL == fp_dlg->prop1_label)
379 return;
380 fp_dlg->changing = TRUE;
382 if (fp_dlg->desktop_url){
383 gtk_entry_set_text (GTK_ENTRY (fp_dlg->desktop_entry), fp_dlg->desktop_url);
385 gtk_entry_set_text (GTK_ENTRY (fp_dlg->caption_entry), fp_dlg->caption);
388 if (fp_dlg->executable) {
389 gtk_label_set_text (GTK_LABEL (fp_dlg->prop1_label), _("Drop Action"));
390 gtk_label_set_text (GTK_LABEL (GTK_BIN (fp_dlg->prop1_cbox)->child), _("Use default Drop Action options"));
391 if (fp_dlg->drop_target) {
392 gtk_toggle_button_set_active ( GTK_TOGGLE_BUTTON (fp_dlg->prop1_cbox), FALSE);
393 gtk_entry_set_text (GTK_ENTRY (fp_dlg->prop1_entry), fp_dlg->drop_target);
394 gtk_widget_set_sensitive (fp_dlg->prop1_entry, TRUE);
395 } else if (fp_dlg->mime_drop_target) {
396 gtk_entry_set_text (GTK_ENTRY (fp_dlg->prop1_entry), fp_dlg->mime_drop_target);
397 gtk_toggle_button_set_active ( GTK_TOGGLE_BUTTON (fp_dlg->prop1_cbox), TRUE);
398 gtk_widget_set_sensitive (fp_dlg->prop1_entry, FALSE);
399 } else {
400 gtk_entry_set_text (GTK_ENTRY (fp_dlg->prop1_entry), "");
401 gtk_toggle_button_set_active ( GTK_TOGGLE_BUTTON (fp_dlg->prop1_cbox), TRUE);
402 gtk_widget_set_sensitive (fp_dlg->prop1_entry, FALSE);
404 } else {
405 gtk_label_set_text (GTK_LABEL (fp_dlg->prop1_label), _("View"));
406 gtk_label_set_text (GTK_LABEL (GTK_BIN (fp_dlg->prop1_cbox)->child), _("Use default View options"));
407 if (fp_dlg->fm_view) {
408 gtk_entry_set_text (GTK_ENTRY (fp_dlg->prop1_entry), fp_dlg->fm_view);
409 gtk_toggle_button_set_active ( GTK_TOGGLE_BUTTON (fp_dlg->prop1_cbox), FALSE);
410 gtk_widget_set_sensitive (fp_dlg->prop1_entry, TRUE);
411 } else if (fp_dlg->mime_fm_view) {
412 gtk_entry_set_text (GTK_ENTRY (fp_dlg->prop1_entry), fp_dlg->mime_fm_view);
413 gtk_toggle_button_set_active ( GTK_TOGGLE_BUTTON (fp_dlg->prop1_cbox), TRUE);
414 gtk_widget_set_sensitive (fp_dlg->prop1_entry, FALSE);
415 } else {
416 gtk_entry_set_text (GTK_ENTRY (fp_dlg->prop1_entry), "");
417 gtk_toggle_button_set_active ( GTK_TOGGLE_BUTTON (fp_dlg->prop1_cbox), TRUE);
418 gtk_widget_set_sensitive (fp_dlg->prop1_entry, FALSE);
421 if (fp_dlg->executable) {
422 gtk_widget_hide (fp_dlg->prop2_label);
423 gtk_widget_hide (fp_dlg->prop2_entry);
424 gtk_widget_hide (fp_dlg->prop2_cbox);
425 gtk_widget_hide (fp_dlg->prop2_hline);
426 } else {
427 gtk_widget_show (fp_dlg->prop2_label);
428 gtk_widget_show (fp_dlg->prop2_entry);
429 gtk_widget_show (fp_dlg->prop2_cbox);
430 gtk_widget_show (fp_dlg->prop2_hline);
432 fp_dlg->changing = FALSE;
435 static GtkWidget *
436 generate_icon_sel (GnomeFilePropertyDialog *fp_dlg)
438 GtkWidget *retval;
439 gchar *icon;
441 retval = gnome_icon_entry_new ("gmc_file_icon", _("Select an Icon"));
442 icon = g_strdup (gicon_get_filename_for_icon (fp_dlg->im));
444 if (icon == NULL)
445 return retval;
446 gnome_icon_entry_set_icon (GNOME_ICON_ENTRY (retval), icon);
447 fp_dlg->icon_filename = icon;
448 return retval;
451 static GtkWidget *
452 generate_actions_box (GnomeFilePropertyDialog *fp_dlg)
454 GtkWidget *vbox;
455 GtkWidget *table;
457 /* Here's the Logic: */
458 /* All tops of files (other then folders) should let us edit "open" */
459 /* If we are a file, and an executable, we want to edit our "drop-action" */
460 /* Metadata, as it is meaningful to us. */
461 /* If we are non-executable, we want to edit our "edit" and "view" fields. */
462 /* Sym links want to have the same options as above, but use their Target's */
463 /* Executable/set bit in order to determine which one. */
464 /* Note, symlinks can set their own metadata, independent from their */
465 /* targets. */
467 table = gtk_table_new (8, 2, FALSE);
468 gtk_container_set_border_width (GTK_CONTAINER (table), GNOME_PAD_SMALL);
470 /* we do open first */
471 fp_dlg->open_label = gtk_label_new (_("Open"));
472 gtk_misc_set_alignment (GTK_MISC (fp_dlg->open_label), 0.0, 0.5);
473 gtk_misc_set_padding (GTK_MISC (fp_dlg->open_label), 2, 0);
474 gtk_table_attach_defaults (GTK_TABLE (table),
475 fp_dlg->open_label,
476 0, 1, 0, 1);
477 fp_dlg->open_entry = gtk_entry_new ();
478 gtk_table_attach_defaults (GTK_TABLE (table),
479 fp_dlg->open_entry,
480 1, 2, 0, 1);
481 fp_dlg->open_cbox = gtk_check_button_new_with_label (_("Use default Open action"));
482 gtk_signal_connect (GTK_OBJECT (fp_dlg->open_cbox), "toggled", metadata_toggled, fp_dlg);
483 gtk_table_attach_defaults (GTK_TABLE (table), fp_dlg->open_cbox, 0, 2, 1, 2);
486 vbox = gtk_vbox_new (FALSE, 0);
487 gtk_box_pack_start (GTK_BOX (vbox), gtk_hseparator_new (), FALSE, FALSE, GNOME_PAD_SMALL);
488 gtk_table_attach_defaults (GTK_TABLE (table), vbox, 0, 2, 2, 3);
490 if (fp_dlg->executable)
491 fp_dlg->prop1_label = gtk_label_new (_("Drop Action"));
492 else
493 fp_dlg->prop1_label = gtk_label_new (_("View"));
494 gtk_misc_set_alignment (GTK_MISC (fp_dlg->prop1_label), 0.0, 0.5);
495 gtk_misc_set_padding (GTK_MISC (fp_dlg->prop1_label), 2, 0);
496 gtk_table_attach_defaults (GTK_TABLE (table),
497 fp_dlg->prop1_label,
498 0, 1, 3, 4);
499 fp_dlg->prop1_entry = gtk_entry_new ();
500 gtk_table_attach_defaults (GTK_TABLE (table),
501 fp_dlg->prop1_entry,
502 1, 2, 3, 4);
503 if (fp_dlg->executable)
504 fp_dlg->prop1_cbox = gtk_check_button_new_with_label (_("Use default Drop action"));
505 else
506 fp_dlg->prop1_cbox = gtk_check_button_new_with_label (_("Use default View action"));
507 gtk_signal_connect (GTK_OBJECT (fp_dlg->prop1_cbox), "toggled", metadata_toggled, fp_dlg);
508 gtk_table_attach_defaults (GTK_TABLE (table), fp_dlg->prop1_cbox, 0, 2, 4, 5);
510 vbox = gtk_vbox_new (FALSE, 0);
511 fp_dlg->prop2_hline = gtk_hseparator_new ();
512 gtk_box_pack_start (GTK_BOX (vbox), fp_dlg->prop2_hline, FALSE, FALSE, GNOME_PAD_SMALL);
513 gtk_table_attach_defaults (GTK_TABLE (table), vbox, 0, 2, 5, 6);
515 fp_dlg->prop2_label = gtk_label_new (_("Edit"));
516 gtk_misc_set_alignment (GTK_MISC (fp_dlg->prop2_label), 0.0, 0.5);
517 gtk_misc_set_padding (GTK_MISC (fp_dlg->prop2_label), 2, 0);
518 gtk_table_attach_defaults (GTK_TABLE (table),
519 fp_dlg->prop2_label,
520 0, 1, 6, 7);
521 fp_dlg->prop2_entry = gtk_entry_new ();
522 gtk_table_attach_defaults (GTK_TABLE (table),
523 fp_dlg->prop2_entry,
524 1, 2, 6, 7);
525 fp_dlg->prop2_cbox = gtk_check_button_new_with_label (_("Use default Edit action"));
526 gtk_signal_connect (GTK_OBJECT (fp_dlg->prop2_cbox), "toggled", metadata_toggled, fp_dlg);
527 gtk_table_attach_defaults (GTK_TABLE (table), fp_dlg->prop2_cbox, 0, 2, 7, 8);
529 /* we set the open field */
530 fp_dlg->changing = TRUE;
531 if (fp_dlg->fm_open) {
532 gtk_entry_set_text (GTK_ENTRY (fp_dlg->open_entry), fp_dlg->fm_open);
533 gtk_toggle_button_set_active ( GTK_TOGGLE_BUTTON (fp_dlg->open_cbox), FALSE);
534 gtk_widget_set_sensitive (fp_dlg->open_entry, TRUE);
535 } else if (fp_dlg->mime_fm_open) {
536 gtk_entry_set_text (GTK_ENTRY (fp_dlg->open_entry), fp_dlg->mime_fm_open);
537 gtk_toggle_button_set_active ( GTK_TOGGLE_BUTTON (fp_dlg->open_cbox), TRUE);
538 gtk_widget_set_sensitive (fp_dlg->open_entry, FALSE);
539 } else {
540 gtk_entry_set_text (GTK_ENTRY (fp_dlg->open_entry), "");
541 gtk_toggle_button_set_active ( GTK_TOGGLE_BUTTON (fp_dlg->open_cbox), TRUE);
542 gtk_widget_set_sensitive (fp_dlg->open_entry, FALSE);
544 if (fp_dlg->edit) {
545 gtk_toggle_button_set_active ( GTK_TOGGLE_BUTTON (fp_dlg->prop2_cbox), FALSE);
546 gtk_entry_set_text (GTK_ENTRY (fp_dlg->prop2_entry), fp_dlg->edit);
547 gtk_widget_set_sensitive (fp_dlg->prop2_entry, TRUE);
548 } else if (fp_dlg->mime_edit) {
549 gtk_entry_set_text (GTK_ENTRY (fp_dlg->prop2_entry), fp_dlg->mime_edit);
550 gtk_toggle_button_set_active ( GTK_TOGGLE_BUTTON (fp_dlg->prop2_cbox), TRUE);
551 gtk_widget_set_sensitive (fp_dlg->prop2_entry, FALSE);
552 } else {
553 gtk_entry_set_text (GTK_ENTRY (fp_dlg->prop2_entry), "");
554 gtk_toggle_button_set_active ( GTK_TOGGLE_BUTTON (fp_dlg->prop2_cbox), TRUE);
555 gtk_widget_set_sensitive (fp_dlg->prop2_entry, FALSE);
557 fp_dlg->changing = FALSE;
559 return table;
562 static GtkWidget *
563 create_settings_pane (GnomeFilePropertyDialog *fp_dlg)
565 GtkWidget *vbox = NULL;
566 GtkWidget *hbox;
567 GtkWidget *vbox2;
568 GtkWidget *frame;
569 GtkWidget *align;
570 GtkWidget *table;
571 struct stat linkstat;
572 int finish;
574 vbox = gtk_vbox_new (FALSE, GNOME_PAD_SMALL);
575 gtk_container_set_border_width (GTK_CONTAINER (vbox), GNOME_PAD);
577 if (fp_dlg->can_set_icon) {
579 frame = gtk_frame_new (_("Icon"));
580 vbox2 = gtk_vbox_new (FALSE, 0);
581 hbox = gtk_hbox_new (FALSE, 0);
582 align = gtk_alignment_new (0.5, 0.5, 1.0, 1.0);
583 gtk_box_pack_start (GTK_BOX (vbox), frame, TRUE, TRUE, 0);
584 fp_dlg->button = generate_icon_sel (fp_dlg);
585 gtk_container_add (GTK_CONTAINER (frame), vbox2);
586 gtk_container_set_border_width (GTK_CONTAINER (vbox2), GNOME_PAD_SMALL);
587 gtk_box_pack_start (GTK_BOX (vbox2), hbox, TRUE, FALSE, 0);
588 gtk_box_pack_start (GTK_BOX (hbox), align, TRUE, FALSE, 0);
589 gtk_container_add (GTK_CONTAINER (align), fp_dlg->button);
591 /* Are we a directory or device? If so, we do nothing else. */
592 if (S_ISLNK (fp_dlg->st.st_mode))
593 mc_stat (fp_dlg->file_name, &linkstat);
595 finish = 0;
596 if (!S_ISREG (fp_dlg->st.st_mode)){
597 if (S_ISLNK (fp_dlg->st.st_mode)){
598 if (!S_ISREG (linkstat.st_mode))
599 finish = 1;
600 } else
601 finish = 1;
604 if (finish){
605 if (!fp_dlg->can_set_icon) {
606 gtk_widget_unref (vbox);
607 vbox = NULL;
609 return vbox;
612 /* We must be a file or a link to a file. */
613 frame = gtk_frame_new (_("File Actions"));
614 gtk_box_pack_start (GTK_BOX (vbox), frame, TRUE, TRUE, 0);
615 table = generate_actions_box (fp_dlg);
616 gtk_container_add (GTK_CONTAINER (frame), table);
618 frame = gtk_frame_new (_("Open action"));
619 fp_dlg->needs_terminal_check = gtk_check_button_new_with_label (_("Needs terminal to run"));
620 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (fp_dlg->needs_terminal_check),
621 fp_dlg->needs_terminal);
622 gtk_container_add (GTK_CONTAINER (frame), fp_dlg->needs_terminal_check);
624 gtk_box_pack_start (GTK_BOX (vbox), frame, TRUE, TRUE, 0);
625 return vbox;
628 /* Permissions Pane */
630 static GtkWidget *
631 align_label_new (char *text, double xalign, double yalign)
633 GtkWidget *label;
635 label = gtk_label_new (text);
636 gtk_misc_set_alignment (GTK_MISC (label), xalign, yalign);
637 gtk_widget_show (label);
639 return label;
642 static mode_t
643 perm_get_umode (GnomeFilePropertyDialog *fp_dlg)
645 mode_t umode;
647 #define SETBIT(widget, flag) umode |= GTK_TOGGLE_BUTTON (widget)->active ? flag : 0
649 umode = 0;
651 SETBIT (fp_dlg->suid, S_ISUID);
652 SETBIT (fp_dlg->sgid, S_ISGID);
653 SETBIT (fp_dlg->svtx, S_ISVTX);
655 SETBIT (fp_dlg->rusr, S_IRUSR);
656 SETBIT (fp_dlg->wusr, S_IWUSR);
657 SETBIT (fp_dlg->xusr, S_IXUSR);
659 SETBIT (fp_dlg->rgrp, S_IRGRP);
660 SETBIT (fp_dlg->wgrp, S_IWGRP);
661 SETBIT (fp_dlg->xgrp, S_IXGRP);
663 SETBIT (fp_dlg->roth, S_IROTH);
664 SETBIT (fp_dlg->woth, S_IWOTH);
665 SETBIT (fp_dlg->xoth, S_IXOTH);
667 return umode;
669 #undef SETBIT
672 static void
673 perm_set_mode_label (GtkWidget *widget, gpointer data)
675 GnomeFilePropertyDialog *fp_dlg;
676 mode_t umode;
677 char s_mode[5];
679 fp_dlg = GNOME_FILE_PROPERTY_DIALOG (data);
680 umode = perm_get_umode (fp_dlg);
681 if (!fp_dlg->executable && (umode & (S_IXUSR | S_IXGRP | S_IXOTH))) {
682 fp_dlg->executable = TRUE;
683 switch_metadata_box (fp_dlg);
684 } else if (fp_dlg->executable && !(umode & (S_IXUSR | S_IXGRP | S_IXOTH))) {
685 fp_dlg->executable = FALSE;
686 switch_metadata_box (fp_dlg);
689 s_mode[0] = '0' + ((umode & (S_ISUID | S_ISGID | S_ISVTX)) >> 9);
690 s_mode[1] = '0' + ((umode & (S_IRUSR | S_IWUSR | S_IXUSR)) >> 6);
691 s_mode[2] = '0' + ((umode & (S_IRGRP | S_IWGRP | S_IXGRP)) >> 3);
692 s_mode[3] = '0' + ((umode & (S_IROTH | S_IWOTH | S_IXOTH)) >> 0);
693 s_mode[4] = 0;
694 gtk_label_set_text (GTK_LABEL (fp_dlg->mode_label), s_mode);
697 static GtkWidget *
698 perm_check_new (char *text, int state, GnomeFilePropertyDialog *fp_dlg)
700 GtkWidget *check;
702 if (text)
703 check = gtk_check_button_new_with_label (text);
704 else
705 check = gtk_check_button_new ();
707 gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (check), FALSE);
708 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (check), state ? TRUE : FALSE);
710 gtk_signal_connect (GTK_OBJECT (check), "toggled",
711 (GtkSignalFunc) perm_set_mode_label,
712 fp_dlg);
714 gtk_widget_show (check);
715 return check;
718 #define ATTACH(table, widget, left, right, top, bottom) \
719 gtk_table_attach (GTK_TABLE (table), widget, \
720 left, right, top, bottom, \
721 GTK_FILL | GTK_SHRINK, \
722 GTK_FILL | GTK_SHRINK, \
723 0, 0);
725 #define PERMSET(name, r, w, x, rmask, wmask, xmask, y) do { \
726 r = perm_check_new (NULL, fp_dlg->st.st_mode & rmask, fp_dlg); \
727 w = perm_check_new (NULL, fp_dlg->st.st_mode & wmask, fp_dlg); \
728 x = perm_check_new (NULL, fp_dlg->st.st_mode & xmask, fp_dlg); \
730 ATTACH (table, align_label_new (name, 0.0, 0.5), 0, 1, y, y + 1); \
731 ATTACH (table, r, 1, 2, y, y + 1); \
732 ATTACH (table, w, 2, 3, y, y + 1); \
733 ATTACH (table, x, 3, 4, y, y + 1); \
734 } while (0);
736 static GtkWidget *
737 perm_mode_new (GnomeFilePropertyDialog *fp_dlg)
739 GtkWidget *frame;
740 GtkWidget *vbox;
741 GtkWidget *hbox;
742 GtkWidget *table;
743 gchar *mode;
745 frame = gtk_frame_new (_("File Permissions"));
747 vbox = gtk_vbox_new (FALSE, GNOME_PAD_SMALL);
748 gtk_container_set_border_width (GTK_CONTAINER (vbox), 6);
749 gtk_container_add (GTK_CONTAINER (frame), vbox);
750 gtk_widget_show (vbox);
752 hbox = gtk_hbox_new (FALSE, 0);
753 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
754 gtk_widget_show (hbox);
756 gtk_box_pack_start (GTK_BOX (hbox), align_label_new (_("Current mode: "), 0.0, 0.5),
757 FALSE, FALSE, 0);
759 fp_dlg->mode_label = align_label_new ("0000", 0.0, 0.5);
760 gtk_box_pack_start (GTK_BOX (hbox), fp_dlg->mode_label, FALSE, FALSE, 0);
762 table = gtk_table_new (4, 5, FALSE);
763 if (!fp_dlg->modifyable || S_ISLNK (fp_dlg->st.st_mode))
764 gtk_widget_set_sensitive (table, FALSE);
765 gtk_table_set_col_spacings (GTK_TABLE (table), 4);
766 gtk_table_set_row_spacings (GTK_TABLE (table), 6);
767 gtk_box_pack_start (GTK_BOX (vbox), table, FALSE, FALSE, 0);
768 gtk_widget_show (table);
770 /* Headings */
772 ATTACH (table, align_label_new (_("Read"), 0.0, 0.5), 1, 2, 0, 1);
773 ATTACH (table, align_label_new (_("Write"), 0.0, 0.5), 2, 3, 0, 1);
774 ATTACH (table, align_label_new (_("Exec"), 0.0, 0.5), 3, 4, 0, 1);
775 ATTACH (table, align_label_new (_("Special"), 0.0, 0.5), 4, 5, 0, 1);
777 /* Permissions */
779 PERMSET (_("User"), fp_dlg->rusr, fp_dlg->wusr, fp_dlg->xusr, S_IRUSR, S_IWUSR, S_IXUSR, 1);
780 PERMSET (_("Group"), fp_dlg->rgrp, fp_dlg->wgrp, fp_dlg->xgrp, S_IRGRP, S_IWGRP, S_IXGRP, 2);
781 PERMSET (_("Other"), fp_dlg->roth, fp_dlg->woth, fp_dlg->xoth, S_IROTH, S_IWOTH, S_IXOTH, 3);
783 /* Special */
785 fp_dlg->suid = perm_check_new (_("Set UID"), fp_dlg->st.st_mode & S_ISUID, fp_dlg);
786 fp_dlg->sgid = perm_check_new (_("Set GID"), fp_dlg->st.st_mode & S_ISGID, fp_dlg);
787 fp_dlg->svtx = perm_check_new (_("Sticky"), fp_dlg->st.st_mode & S_ISVTX, fp_dlg);
789 ATTACH (table, fp_dlg->suid, 4, 5, 1, 2);
790 ATTACH (table, fp_dlg->sgid, 4, 5, 2, 3);
791 ATTACH (table, fp_dlg->svtx, 4, 5, 3, 4);
793 perm_set_mode_label (NULL, fp_dlg);
794 gtk_label_get (GTK_LABEL (fp_dlg->mode_label), &mode);
795 fp_dlg->mode_name = g_strdup (mode);
796 return frame;
799 #undef ATTACH
800 #undef PERMSET
802 static GtkWidget *
803 perm_owner_new (GnomeFilePropertyDialog *fp_dlg)
805 GtkWidget *gentry;
806 struct passwd *passwd;
808 gentry = gtk_entry_new ();
810 if (fp_dlg->euid != 0)
811 gtk_widget_set_sensitive (gentry, FALSE);
812 passwd = getpwuid (fp_dlg->st.st_uid);
813 if (passwd) {
814 fp_dlg->user_name = passwd->pw_name;
815 gtk_entry_set_text (GTK_ENTRY (gentry), passwd->pw_name);
816 } else {
817 char buf[100];
819 g_snprintf (buf, sizeof (buf), "%d", (int) fp_dlg->st.st_uid);
820 gtk_entry_set_text (GTK_ENTRY (gentry), buf);
823 return gentry;
826 static GtkWidget *
827 perm_group_new (GnomeFilePropertyDialog *fp_dlg)
829 GtkWidget *gentry;
830 struct group *grp;
831 gboolean grp_flag = FALSE;
832 gchar *grpname = NULL;
833 GList *templist;
834 GList *list = NULL;
836 /* Are we root? Do we own the file? */
837 /* In this case we can change it. */
838 /* A little bit of this was swiped from kfm. */
840 if ((fp_dlg->euid == 0) || (fp_dlg->st.st_uid == fp_dlg->euid)) {
841 gentry = gtk_combo_new ();
842 grp = getgrgid (fp_dlg->st.st_gid);
843 if (grp){
844 if (grp->gr_name)
845 fp_dlg->group_name = g_strdup (grp->gr_name);
846 else {
847 fp_dlg->group_name = g_strdup_printf ("%d", (int) grp->gr_gid);
849 } else {
850 fp_dlg->group_name = g_strdup_printf ("%d", (int) fp_dlg->st.st_gid);
853 /* we change this, b/c we are able to set the egid, if we aren't in the group already */
854 grp = getgrgid (getegid());
855 if (grp) {
856 if (grp->gr_name)
857 grpname = g_strdup (grp->gr_name);
858 else
859 grpname = g_strdup_printf ("%d", (int) grp->gr_gid);
860 } else
861 grpname = g_strdup_printf ("%d", (int) fp_dlg->st.st_gid);
863 if (fp_dlg->euid != 0)
864 gtk_editable_set_editable (GTK_EDITABLE (GTK_COMBO (gentry)->entry), FALSE);
865 for (setgrent (); (grp = getgrent ()) != NULL;) {
866 if (!grp_flag && grpname && !strcmp (grp->gr_name, grpname)) {
867 list = g_list_insert_sorted (list, g_strdup (grp->gr_name), (GCompareFunc)strcmp);
868 grp_flag = TRUE;
869 continue;
871 if (fp_dlg->euid == 0) {
872 list = g_list_insert_sorted (list, g_strdup (grp->gr_name), (GCompareFunc)strcmp);
873 } else {
874 gchar **members = grp->gr_mem;
875 gchar *member;
876 while ((member = *members) != 0L) {
877 if (!strcmp (member, fp_dlg->user_name)) {
878 list = g_list_insert_sorted (list, g_strdup (grp->gr_name), (GCompareFunc)strcmp);
879 break;
881 ++members;
885 endgrent ();
887 /* we also might want to add the egid to the list. */
888 if (!grp_flag)
889 list = g_list_insert_sorted (list, g_strdup (grpname), (GCompareFunc)strcmp);
891 /* Now we have a list. We should make our option menu. */
892 gtk_combo_set_popdown_strings (GTK_COMBO (gentry), list);
893 for (templist = list; templist; templist = templist->next) {
894 g_free (templist->data);
896 gtk_entry_set_text (GTK_ENTRY (GTK_COMBO (gentry)->entry), fp_dlg->group_name);
897 g_list_free (list);
898 } else {
899 /* we're neither so we just put an entry down */
900 gentry = gtk_entry_new ();
901 gtk_widget_set_sensitive (gentry, FALSE);
903 grp = getgrgid (fp_dlg->st.st_gid);
904 if (grp)
905 gtk_entry_set_text (GTK_ENTRY (gentry), grp->gr_name);
906 else {
907 char buf[100];
909 g_snprintf (buf, sizeof (buf), _("<Unknown> (%d)"), fp_dlg->st.st_gid);
910 gtk_entry_set_text (GTK_ENTRY (gentry), buf);
913 g_free (grpname);
914 return gentry;
917 static GtkWidget *
918 perm_ownership_new (GnomeFilePropertyDialog *fp_dlg)
920 GtkWidget *frame;
921 GtkWidget *table;
923 frame = gtk_frame_new (_("File ownership"));
925 table = gtk_table_new (2, 2, FALSE);
926 gtk_container_set_border_width (GTK_CONTAINER (table), 6);
927 gtk_table_set_col_spacings (GTK_TABLE (table), 6);
928 gtk_table_set_row_spacings (GTK_TABLE (table), 4);
929 gtk_container_add (GTK_CONTAINER (frame), table);
930 gtk_widget_show (table);
932 /* Owner */
934 gtk_table_attach (GTK_TABLE (table), align_label_new (_("Owner"), 0.0, 0.5),
935 0, 1, 0, 1,
936 GTK_FILL | GTK_SHRINK, GTK_FILL | GTK_SHRINK,
937 0, 0);
939 fp_dlg->owner_entry = perm_owner_new (fp_dlg);
940 gtk_table_attach (GTK_TABLE (table), fp_dlg->owner_entry,
941 1, 2, 0, 1,
942 GTK_EXPAND | GTK_FILL | GTK_SHRINK,
943 GTK_FILL | GTK_SHRINK,
944 0, 0);
945 gtk_widget_show (fp_dlg->owner_entry);
947 /* Group */
949 gtk_table_attach (GTK_TABLE (table), align_label_new (_("Group"), 0.0, 0.5),
950 0, 1, 1, 2,
951 GTK_FILL | GTK_SHRINK, GTK_FILL | GTK_SHRINK,
952 0, 0);
954 fp_dlg->group_entry = perm_group_new (fp_dlg);
955 gtk_table_attach (GTK_TABLE (table), fp_dlg->group_entry,
956 1, 2, 1, 2,
957 GTK_EXPAND | GTK_FILL | GTK_SHRINK,
958 GTK_FILL | GTK_SHRINK,
959 0, 0);
960 gtk_widget_show (fp_dlg->group_entry);
961 return frame;
964 static GtkWidget *
965 create_perm_properties (GnomeFilePropertyDialog *fp_dlg)
967 GtkWidget *vbox;
969 vbox = gtk_vbox_new (FALSE, GNOME_PAD_SMALL);
970 gtk_container_set_border_width (GTK_CONTAINER (vbox), GNOME_PAD);
971 gtk_box_pack_start (GTK_BOX (vbox), perm_mode_new (fp_dlg), FALSE, FALSE, 0);
972 gtk_box_pack_start (GTK_BOX (vbox), perm_ownership_new (fp_dlg), TRUE, TRUE, 0);
973 return vbox;
976 /* finally the new dialog */
977 static void
978 init_metadata (GnomeFilePropertyDialog *fp_dlg)
980 gint size;
981 char *mime_type, *str;
982 gchar link_name[MC_MAXPATHLEN];
983 gint n;
984 gchar *file_name, *desktop_url;
986 if (gnome_metadata_get (fp_dlg->file_name, "fm-open", &size, &fp_dlg->fm_open) != 0)
987 gnome_metadata_get (fp_dlg->file_name, "open", &size, &fp_dlg->fm_open);
988 if (gnome_metadata_get (fp_dlg->file_name, "fm-view", &size, &fp_dlg->fm_view) != 0)
989 gnome_metadata_get (fp_dlg->file_name, "view", &size, &fp_dlg->fm_view);
990 gnome_metadata_get (fp_dlg->file_name, "edit", &size, &fp_dlg->edit);
991 gnome_metadata_get (fp_dlg->file_name, "drop-action", &size, &fp_dlg->drop_target);
994 * Fetch the needs-terminal setting
996 if (gnome_metadata_get (fp_dlg->file_name, "flags", &size, &str) == 0){
997 fp_dlg->needs_terminal = strstr (str, "needsterminal") != 0;
998 g_free (str);
999 } else
1000 fp_dlg->needs_terminal = 0;
1002 /* Mime stuff */
1003 file_name = fp_dlg->file_name;
1004 if (S_ISLNK (fp_dlg->st.st_mode)) {
1005 n = mc_readlink (fp_dlg->file_name, link_name, MC_MAXPATHLEN);
1006 if (n > 0){
1007 link_name [n] = 0;
1008 file_name = link_name;
1012 if (gnome_metadata_get (fp_dlg->file_name, "desktop-url", &size, &desktop_url) == 0)
1013 fp_dlg->desktop_url = desktop_url;
1014 else
1015 fp_dlg->desktop_url = NULL;
1017 if (gnome_metadata_get (fp_dlg->file_name, "icon-caption", &size, &fp_dlg->caption))
1018 fp_dlg->caption = g_strdup (desktop_url);
1021 * Mime type.
1023 if (use_magic)
1025 mime_type = (char *) gnome_mime_type_or_default_of_file (file_name, NULL);
1027 else
1028 mime_type = (char *) gnome_mime_type_or_default (file_name, NULL);
1030 if (!mime_type)
1031 return;
1032 fp_dlg->mime_fm_open = gnome_mime_get_value (mime_type, "fm-open");
1033 if (!fp_dlg->mime_fm_open)
1034 fp_dlg->mime_fm_open = gnome_mime_get_value (mime_type, "open");
1035 fp_dlg->mime_fm_view = gnome_mime_get_value (mime_type, "fm-view");
1036 if (!fp_dlg->mime_fm_view)
1037 fp_dlg->mime_fm_view = gnome_mime_get_value (mime_type, "view");
1038 fp_dlg->mime_edit = gnome_mime_get_value (mime_type, "edit");
1039 fp_dlg->mime_drop_target = gnome_mime_get_value (mime_type, "drop-action");
1042 GtkWidget *
1043 gnome_file_property_dialog_new (gchar *file_name, gboolean can_set_icon)
1045 GnomeFilePropertyDialog *fp_dlg;
1046 GtkWidget *notebook;
1047 GtkWidget *new_page;
1048 gchar *title_string;
1049 mode_t mode;
1051 g_return_val_if_fail (file_name != NULL, NULL);
1052 fp_dlg = gtk_type_new (gnome_file_property_dialog_get_type ());
1054 /* We set non-gui specific things first */
1055 if (mc_lstat (file_name, &fp_dlg->st) == -1) {
1056 /* Bad things man, bad things */
1057 gtk_object_unref (GTK_OBJECT (fp_dlg));
1058 return NULL;
1061 mode = fp_dlg->st.st_mode;
1062 if (S_ISLNK (mode)){
1063 struct stat s;
1065 if (mc_stat (file_name, &s) != -1)
1066 mode = s.st_mode;
1069 if (fp_dlg->st.st_mode & (S_IXUSR | S_IXGRP |S_IXOTH)) {
1070 fp_dlg->executable = TRUE;
1071 } else {
1072 fp_dlg->executable = FALSE;
1075 fp_dlg->file_name = g_strdup (file_name);
1076 fp_dlg->euid = geteuid ();
1077 fp_dlg->can_set_icon = can_set_icon;
1078 if (S_ISCHR (fp_dlg->st.st_mode) || S_ISBLK (fp_dlg->st.st_mode)
1079 || ((fp_dlg->euid != fp_dlg->st.st_uid) && (fp_dlg->euid != 0)))
1080 fp_dlg->modifyable = FALSE;
1081 init_metadata (fp_dlg);
1083 /* and now, we set up the gui. */
1084 notebook = gtk_notebook_new ();
1086 if (fp_dlg->desktop_url)
1087 gtk_notebook_append_page (GTK_NOTEBOOK (notebook),
1088 create_url_properties (fp_dlg),
1089 gtk_label_new (_("URL")));
1090 gtk_notebook_append_page (GTK_NOTEBOOK (notebook),
1091 create_general_properties (fp_dlg),
1092 gtk_label_new (_("Statistics")));
1094 new_page = create_settings_pane (fp_dlg);
1095 if (new_page)
1096 gtk_notebook_append_page (GTK_NOTEBOOK (notebook),
1097 new_page,
1098 gtk_label_new (_("Options")));
1099 gtk_notebook_append_page (GTK_NOTEBOOK (notebook),
1100 create_perm_properties (fp_dlg),
1101 gtk_label_new (_("Permissions")));
1102 gtk_box_pack_start (GTK_BOX (GNOME_DIALOG (fp_dlg)->vbox),
1103 notebook, TRUE, TRUE, 0);
1104 title_string = g_strconcat (strrchr (file_name, '/') + 1, _(" Properties"), NULL);
1105 gtk_window_set_title (GTK_WINDOW (fp_dlg), title_string);
1106 g_free (title_string);
1108 gnome_dialog_append_button ( GNOME_DIALOG(fp_dlg),
1109 GNOME_STOCK_BUTTON_OK);
1110 gnome_dialog_append_button ( GNOME_DIALOG(fp_dlg),
1111 GNOME_STOCK_BUTTON_CANCEL);
1112 gtk_widget_show_all (GNOME_DIALOG (fp_dlg)->vbox);
1114 /* It's okay to do this now... */
1115 /* and set the rest of the fields */
1116 switch_metadata_box (fp_dlg);
1118 return GTK_WIDGET (fp_dlg);
1121 static gint
1122 apply_mode_change (GnomeFilePropertyDialog *fpd)
1124 gchar *new_mode;
1125 gtk_label_get (GTK_LABEL (fpd->mode_label), &new_mode);
1126 if (strcmp (new_mode, fpd->mode_name)) {
1127 mc_chmod (fpd->file_name, (mode_t) strtol(new_mode, (char **)NULL, 8));
1128 return 1;
1130 return 0;
1133 static gint
1134 apply_uid_group_change (GnomeFilePropertyDialog *fpd)
1136 uid_t uid;
1137 gid_t gid;
1138 struct passwd *p;
1139 struct group *g;
1140 gchar *new_user_name = NULL;
1141 gchar *new_group_name = NULL;
1143 uid = fpd->st.st_uid;
1144 gid = fpd->st.st_gid;
1146 /* we only check if our euid == 0 */
1147 if (fpd->euid == 0) {
1148 new_user_name = gtk_entry_get_text (GTK_ENTRY (fpd->owner_entry));
1150 if (fpd->user_name && new_user_name && strcmp (fpd->user_name, new_user_name)) {
1151 /* now we need to get the new uid */
1152 p = getpwnam (new_user_name);
1153 if (!p) {
1154 uid = atoi (new_user_name);
1155 if (uid == 0) {
1156 message (1, MSG_ERROR, _("You entered an invalid username"));
1157 uid = fpd->st.st_uid;
1159 } else
1160 uid = p->pw_uid;
1161 } else {
1162 if (new_user_name){
1163 p = getpwnam (new_user_name);
1164 if (!p) {
1165 uid = atoi (new_user_name);
1166 if (uid == 0) {
1167 message (1, MSG_ERROR, _("You entered an invalid username"));
1168 uid = fpd->st.st_uid;
1170 } else
1171 uid = p->pw_uid;
1176 /* now we check the group */
1177 /* We are only a combo if we are sensitive, and we only want to check if we are
1178 * sensitive. */
1179 if (GTK_WIDGET_IS_SENSITIVE (fpd->group_entry))
1180 new_group_name = gtk_entry_get_text (GTK_ENTRY (GTK_COMBO (fpd->group_entry)->entry));
1182 if (fpd->group_name && new_group_name && strcmp (fpd->group_name, new_group_name)) {
1183 g = getgrnam (new_group_name);
1184 if (!g) {
1185 gid = atoi (new_group_name);
1186 if (gid == 0) {
1187 message (1, MSG_ERROR, _("You entered an invalid group name"));
1188 gid = fpd->st.st_gid;
1190 } else
1191 gid = g->gr_gid;
1193 if ((uid != fpd->st.st_uid) || (gid != fpd->st.st_gid)) {
1194 mc_chown (fpd->file_name, uid, gid);
1195 return 1;
1197 return 0;
1200 static gint
1201 apply_name_change (GnomeFilePropertyDialog *fpd)
1203 char *new_name;
1204 FileOpContext *ctx;
1205 long count = 0;
1206 double bytes = 0;
1208 new_name = gtk_entry_get_text (GTK_ENTRY (fpd->file_entry));
1209 if (!*new_name) {
1210 message (1, MSG_ERROR, _("You must rename your file to something"));
1211 return 0;
1213 /* has it changed? */
1214 if (strcmp (strrchr(fpd->file_name, '/') + 1, new_name)) {
1215 if (strchr (new_name, '/')) {
1216 message (1, MSG_ERROR, _("You cannot rename a file to something containing a '/' character"));
1217 return 0;
1218 } else {
1219 char *p;
1220 int s;
1221 char *base_name;
1222 char *full_target;
1224 /* create the files. */
1225 base_name = g_strdup (fpd->file_name);
1227 p = strrchr (base_name, '/');
1229 if (p)
1230 *p = '\0';
1232 full_target = concat_dir_and_file (base_name, new_name);
1233 g_free (base_name);
1235 ctx = file_op_context_new ();
1236 file_op_context_create_ui (ctx, OP_MOVE, FALSE);
1237 s = move_file_file (ctx, fpd->file_name, full_target, &count, &bytes);
1238 file_op_context_destroy (ctx);
1240 if (s == FILE_CONT){
1241 g_free (fpd->file_name);
1242 fpd->file_name = full_target;
1243 } else
1244 g_free (full_target);
1247 return 1;
1250 static gint
1251 apply_metadata_change (GnomeFilePropertyDialog *fpd)
1253 gchar *text;
1255 /* If we don't have an open_cbox, that means we have no metadata
1256 * to set.
1258 if (fpd->open_cbox != NULL) {
1259 if (!GTK_TOGGLE_BUTTON (fpd->open_cbox)->active) {
1260 text = gtk_entry_get_text (GTK_ENTRY (fpd->open_entry));
1261 if (text && text[0])
1262 gnome_metadata_set (fpd->file_name,
1263 "fm-open",
1264 strlen (text) + 1,
1265 text);
1266 else
1267 gnome_metadata_remove (fpd->file_name,
1268 "fm-open");
1269 } else {
1270 if (fpd->fm_open)
1271 gnome_metadata_remove (fpd->file_name,
1272 "fm-open");
1274 if (fpd->executable) {
1275 if (!GTK_TOGGLE_BUTTON (fpd->prop1_cbox)->active) {
1276 text = gtk_entry_get_text (GTK_ENTRY (fpd->prop1_entry));
1277 if (text && text[0])
1278 gnome_metadata_set (fpd->file_name,
1279 "drop-action",
1280 strlen (text) + 1,
1281 text);
1282 else
1283 gnome_metadata_remove (fpd->file_name,
1284 "drop-action");
1285 } else {
1286 if (fpd->drop_target)
1287 gnome_metadata_remove (fpd->file_name,
1288 "drop-action");
1290 } else {
1291 if (!GTK_TOGGLE_BUTTON (fpd->prop1_cbox)->active) {
1292 text = gtk_entry_get_text (GTK_ENTRY (fpd->prop1_entry));
1293 if (text && text[0])
1294 gnome_metadata_set (fpd->file_name,
1295 "fm-view",
1296 strlen (text) + 1,
1297 text);
1298 else
1299 gnome_metadata_remove (fpd->file_name,
1300 "fm-view");
1301 } else {
1302 if (fpd->fm_view)
1303 gnome_metadata_remove (fpd->file_name,
1304 "fm-view");
1306 if (!GTK_TOGGLE_BUTTON (fpd->prop2_cbox)->active) {
1307 text = gtk_entry_get_text (GTK_ENTRY (fpd->prop2_entry));
1308 if (text && text[0])
1309 gnome_metadata_set (fpd->file_name,
1310 "edit",
1311 strlen (text) + 1,
1312 text);
1313 else
1314 gnome_metadata_remove (fpd->file_name,
1315 "edit");
1316 } else {
1317 if (fpd->edit)
1318 gnome_metadata_remove (fpd->file_name,
1319 "edit");
1324 if (fpd->desktop_url){
1325 char *new_desktop_url = gtk_entry_get_text (GTK_ENTRY (fpd->desktop_entry));
1326 char *new_caption = gtk_entry_get_text (GTK_ENTRY (fpd->caption_entry));
1328 gnome_metadata_set (fpd->file_name, "desktop-url",
1329 strlen (new_desktop_url)+1, new_desktop_url);
1330 gnome_metadata_set (fpd->file_name, "icon-caption",
1331 strlen (new_caption)+1, new_caption);
1335 * "Needs terminal" check button
1337 * This piece of code is more complex than it should, as the "flags"
1338 * metadata value is actually a list of comma separated flags. So
1339 * we need to edit this list when removing the "needsterminal" flag.
1341 if (fpd->needs_terminal_check)
1343 int toggled = 0 != GTK_TOGGLE_BUTTON (fpd->needs_terminal_check)->active;
1344 int size;
1345 char *buf;
1347 if (gnome_metadata_get (fpd->file_name, "flags", &size, &buf) == 0){
1348 char *p;
1350 p = strstr (buf, "needsterminal");
1351 if (toggled){
1352 if (!p){
1353 p = g_strconcat (buf, ",needsterminal", NULL);
1354 gnome_metadata_set (fpd->file_name, "flags", strlen (p)+1, p);
1355 g_free (p);
1357 } else {
1358 if (p){
1359 char *p1, *p2;
1361 if (p != buf){
1362 p1 = g_malloc (p - buf + 1);
1363 strncpy (p1, buf, p - buf);
1364 p1 [p - buf ] = 0;
1365 } else
1366 p1 = g_strdup ("");
1368 p += strlen ("needsterminal");
1370 p2 = g_strconcat (p1, p, NULL);
1371 if (strcmp (p2, ",") == 0)
1372 gnome_metadata_remove (fpd->file_name, "flags");
1373 else
1374 gnome_metadata_set (fpd->file_name, "flags",
1375 strlen (p2)+1, p2);
1376 g_free (p2);
1377 g_free (p1);
1381 } else {
1382 if (toggled){
1383 gnome_metadata_set (fpd->file_name, "flags",
1384 sizeof ("needsterminal"), "needsterminal");
1389 if (!fpd->can_set_icon)
1390 return 1;
1391 /* And finally, we set the metadata on the icon filename */
1392 text = gnome_icon_entry_get_filename (GNOME_ICON_ENTRY (fpd->button));
1393 /*gtk_entry_get_text (GTK_ENTRY (gnome_icon_entry_gtk_entry (GNOME_ICON_ENTRY (fpd->button))));*/
1395 if (text) {
1396 if (fpd->icon_filename == NULL || strcmp (fpd->icon_filename, text) != 0)
1397 gnome_metadata_set (fpd->file_name, "icon-filename", strlen (text) + 1, text);
1398 g_free (text);
1400 /* I suppose we should only do this if we know there's been a change -- I'll try to figure it
1401 * out later if it turns out to be important. */
1402 return 1;
1405 /* This function will apply what changes it can do. It is meant to be used in conjunction
1406 * with a gnome_dialog_run_and_hide. Please note that doing a gnome_dialog_run
1407 * will (obviously) cause greivious bogoness. */
1409 gint
1410 gnome_file_property_dialog_make_changes (GnomeFilePropertyDialog *file_property_dialog)
1412 gint retval = 0;
1414 g_return_val_if_fail (file_property_dialog != NULL, 1);
1415 g_return_val_if_fail (GNOME_IS_FILE_PROPERTY_DIALOG (file_property_dialog), 1);
1417 retval += apply_mode_change (file_property_dialog);
1418 retval += apply_uid_group_change (file_property_dialog);
1419 retval += apply_name_change (file_property_dialog);
1420 retval += apply_metadata_change (file_property_dialog);
1422 return retval;