1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* GTK - The GIMP Toolkit
3 * Copyright (C) Christian Kellner <gicmo@gnome.org>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
22 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
23 * file for a list of people on the GTK+ Team. See the ChangeLog
24 * files for a list of changes. These files are distributed with
25 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
32 #include "gtkmountoperationprivate.h"
33 #include "gtkalignment.h"
40 #include "gtkmessagedialog.h"
42 #include "gtkmountoperation.h"
43 #include "gtkprivate.h"
44 #include "gtkradiobutton.h"
47 #include "gtkwindow.h"
48 #include "gtktreeview.h"
49 #include "gtktreeselection.h"
50 #include "gtkcellrenderertext.h"
51 #include "gtkcellrendererpixbuf.h"
52 #include "gtkscrolledwindow.h"
53 #include "gtkicontheme.h"
54 #include "gtkimagemenuitem.h"
60 * @short_description: Functions for working with GIO
62 * The functions and objects described here make working with GTK+ and
63 * GIO more convenient. #GtkMountOperation is needed when mounting volumes
64 * and gtk_show_uri() is a convenient way to launch applications for URIs.
65 * Another object that is worth mentioning in this context is
66 * #GdkAppLaunchContext, which provides visual feedback when lauching
73 * #GtkMountOperation is an implementation of #GMountOperation that
74 * can be used with GIO functions for mounting volumes such as
75 * g_file_mount_enclosing_volume(), g_file_mount_mountable(),
76 * g_volume_mount(), g_mount_unmount() and others.
78 * When necessary, #GtkMountOperation shows dialogs to ask for
79 * passwords, questions or show processes blocking unmount.
82 static void gtk_mount_operation_finalize (GObject *object);
83 static void gtk_mount_operation_set_property (GObject *object,
87 static void gtk_mount_operation_get_property (GObject *object,
92 static void gtk_mount_operation_ask_password (GMountOperation *op,
94 const char *default_user,
95 const char *default_domain,
96 GAskPasswordFlags flags);
98 static void gtk_mount_operation_ask_question (GMountOperation *op,
100 const char *choices[]);
102 static void gtk_mount_operation_show_processes (GMountOperation *op,
105 const char *choices[]);
107 static void gtk_mount_operation_aborted (GMountOperation *op);
109 G_DEFINE_TYPE (GtkMountOperation, gtk_mount_operation, G_TYPE_MOUNT_OPERATION);
119 struct _GtkMountOperationPrivate {
120 GtkWindow *parent_window;
124 /* for the ask-password dialog */
125 GtkWidget *entry_container;
126 GtkWidget *username_entry;
127 GtkWidget *domain_entry;
128 GtkWidget *password_entry;
129 GtkWidget *anonymous_toggle;
131 GAskPasswordFlags ask_flags;
132 GPasswordSave password_save;
135 /* for the show-processes dialog */
136 GtkWidget *process_tree_view;
137 GtkListStore *process_list_store;
141 gtk_mount_operation_class_init (GtkMountOperationClass *klass)
143 GObjectClass *object_class = G_OBJECT_CLASS (klass);
144 GMountOperationClass *mount_op_class = G_MOUNT_OPERATION_CLASS (klass);
146 g_type_class_add_private (klass, sizeof (GtkMountOperationPrivate));
148 object_class->finalize = gtk_mount_operation_finalize;
149 object_class->get_property = gtk_mount_operation_get_property;
150 object_class->set_property = gtk_mount_operation_set_property;
152 mount_op_class->ask_password = gtk_mount_operation_ask_password;
153 mount_op_class->ask_question = gtk_mount_operation_ask_question;
154 mount_op_class->show_processes = gtk_mount_operation_show_processes;
155 mount_op_class->aborted = gtk_mount_operation_aborted;
157 g_object_class_install_property (object_class,
159 g_param_spec_object ("parent",
161 P_("The parent window"),
163 GTK_PARAM_READWRITE));
165 g_object_class_install_property (object_class,
167 g_param_spec_boolean ("is-showing",
169 P_("Are we showing a dialog"),
171 GTK_PARAM_READABLE));
173 g_object_class_install_property (object_class,
175 g_param_spec_object ("screen",
177 P_("The screen where this window will be displayed."),
179 GTK_PARAM_READWRITE));
183 gtk_mount_operation_init (GtkMountOperation *operation)
185 operation->priv = G_TYPE_INSTANCE_GET_PRIVATE (operation,
186 GTK_TYPE_MOUNT_OPERATION,
187 GtkMountOperationPrivate);
191 gtk_mount_operation_finalize (GObject *object)
193 GtkMountOperation *operation = GTK_MOUNT_OPERATION (object);
194 GtkMountOperationPrivate *priv = operation->priv;
196 if (priv->parent_window)
197 g_object_unref (priv->parent_window);
200 g_object_unref (priv->screen);
202 G_OBJECT_CLASS (gtk_mount_operation_parent_class)->finalize (object);
206 gtk_mount_operation_set_property (GObject *object,
211 GtkMountOperation *operation = GTK_MOUNT_OPERATION (object);
216 gtk_mount_operation_set_parent (operation, g_value_get_object (value));
220 gtk_mount_operation_set_screen (operation, g_value_get_object (value));
223 case PROP_IS_SHOWING:
225 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
231 gtk_mount_operation_get_property (GObject *object,
236 GtkMountOperation *operation = GTK_MOUNT_OPERATION (object);
237 GtkMountOperationPrivate *priv = operation->priv;
242 g_value_set_object (value, priv->parent_window);
245 case PROP_IS_SHOWING:
246 g_value_set_boolean (value, priv->dialog != NULL);
250 g_value_set_object (value, priv->screen);
254 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
260 remember_button_toggled (GtkToggleButton *button,
261 GtkMountOperation *operation)
263 GtkMountOperationPrivate *priv = operation->priv;
265 if (gtk_toggle_button_get_active (button))
269 data = g_object_get_data (G_OBJECT (button), "password-save");
270 priv->password_save = GPOINTER_TO_INT (data);
275 pw_dialog_got_response (GtkDialog *dialog,
277 GtkMountOperation *mount_op)
279 GtkMountOperationPrivate *priv = mount_op->priv;
280 GMountOperation *op = G_MOUNT_OPERATION (mount_op);
282 if (response_id == GTK_RESPONSE_OK)
286 if (priv->ask_flags & G_ASK_PASSWORD_ANONYMOUS_SUPPORTED)
287 g_mount_operation_set_anonymous (op, priv->anonymous);
289 if (priv->username_entry)
291 text = gtk_entry_get_text (GTK_ENTRY (priv->username_entry));
292 g_mount_operation_set_username (op, text);
295 if (priv->domain_entry)
297 text = gtk_entry_get_text (GTK_ENTRY (priv->domain_entry));
298 g_mount_operation_set_domain (op, text);
301 if (priv->password_entry)
303 text = gtk_entry_get_text (GTK_ENTRY (priv->password_entry));
304 g_mount_operation_set_password (op, text);
307 if (priv->ask_flags & G_ASK_PASSWORD_SAVING_SUPPORTED)
308 g_mount_operation_set_password_save (op, priv->password_save);
310 g_mount_operation_reply (op, G_MOUNT_OPERATION_HANDLED);
313 g_mount_operation_reply (op, G_MOUNT_OPERATION_ABORTED);
316 g_object_notify (G_OBJECT (op), "is-showing");
317 gtk_widget_destroy (GTK_WIDGET (dialog));
322 entry_has_input (GtkWidget *entry_widget)
326 if (entry_widget == NULL)
329 text = gtk_entry_get_text (GTK_ENTRY (entry_widget));
331 return text != NULL && text[0] != '\0';
335 pw_dialog_input_is_valid (GtkMountOperation *operation)
337 GtkMountOperationPrivate *priv = operation->priv;
338 gboolean is_valid = TRUE;
340 /* We don't require password to be non-empty here
341 * since there are situations where it is not needed,
343 * We may add a way for the backend to specify that it
344 * definitively needs a password.
346 is_valid = entry_has_input (priv->username_entry) &&
347 entry_has_input (priv->domain_entry);
353 pw_dialog_verify_input (GtkEditable *editable,
354 GtkMountOperation *operation)
356 GtkMountOperationPrivate *priv = operation->priv;
359 is_valid = pw_dialog_input_is_valid (operation);
360 gtk_dialog_set_response_sensitive (GTK_DIALOG (priv->dialog),
366 pw_dialog_anonymous_toggled (GtkWidget *widget,
367 GtkMountOperation *operation)
369 GtkMountOperationPrivate *priv = operation->priv;
372 priv->anonymous = widget == priv->anonymous_toggle;
377 is_valid = pw_dialog_input_is_valid (operation);
379 gtk_widget_set_sensitive (priv->entry_container, priv->anonymous == FALSE);
380 gtk_dialog_set_response_sensitive (GTK_DIALOG (priv->dialog),
387 pw_dialog_cycle_focus (GtkWidget *widget,
388 GtkMountOperation *operation)
390 GtkMountOperationPrivate *priv;
391 GtkWidget *next_widget = NULL;
393 priv = operation->priv;
395 if (widget == priv->username_entry)
397 if (priv->domain_entry != NULL)
398 next_widget = priv->domain_entry;
399 else if (priv->password_entry != NULL)
400 next_widget = priv->password_entry;
402 else if (widget == priv->domain_entry && priv->password_entry)
403 next_widget = priv->password_entry;
406 gtk_widget_grab_focus (next_widget);
407 else if (pw_dialog_input_is_valid (operation))
408 gtk_window_activate_default (GTK_WINDOW (priv->dialog));
412 table_add_entry (GtkWidget *table,
414 const char *label_text,
421 label = gtk_label_new_with_mnemonic (label_text);
422 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
424 entry = gtk_entry_new ();
427 gtk_entry_set_text (GTK_ENTRY (entry), value);
429 gtk_table_attach (GTK_TABLE (table), label,
431 GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
432 gtk_table_attach_defaults (GTK_TABLE (table), entry,
434 gtk_label_set_mnemonic_widget (GTK_LABEL (label), entry);
436 g_signal_connect (entry, "changed",
437 G_CALLBACK (pw_dialog_verify_input), user_data);
439 g_signal_connect (entry, "activate",
440 G_CALLBACK (pw_dialog_cycle_focus), user_data);
446 gtk_mount_operation_ask_password (GMountOperation *mount_op,
448 const char *default_user,
449 const char *default_domain,
450 GAskPasswordFlags flags)
452 GtkMountOperation *operation;
453 GtkMountOperationPrivate *priv;
457 GtkWidget *entry_container;
458 GtkWidget *hbox, *main_vbox, *vbox, *icon;
460 GtkWidget *message_label;
461 gboolean can_anonymous;
463 const gchar *secondary;
465 operation = GTK_MOUNT_OPERATION (mount_op);
466 priv = operation->priv;
468 priv->ask_flags = flags;
470 widget = gtk_dialog_new ();
471 dialog = GTK_DIALOG (widget);
472 window = GTK_WINDOW (widget);
474 priv->dialog = dialog;
476 /* Set the dialog up with HIG properties */
477 gtk_dialog_set_has_separator (dialog, FALSE);
478 gtk_container_set_border_width (GTK_CONTAINER (dialog), 5);
479 gtk_box_set_spacing (GTK_BOX (dialog->vbox), 2); /* 2 * 5 + 2 = 12 */
480 gtk_container_set_border_width (GTK_CONTAINER (dialog->action_area), 5);
481 gtk_box_set_spacing (GTK_BOX (dialog->action_area), 6);
483 gtk_window_set_resizable (window, FALSE);
484 gtk_window_set_title (window, "");
485 gtk_window_set_icon_name (window, GTK_STOCK_DIALOG_AUTHENTICATION);
487 gtk_dialog_add_buttons (dialog,
488 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
489 _("Co_nnect"), GTK_RESPONSE_OK,
491 gtk_dialog_set_default_response (dialog, GTK_RESPONSE_OK);
493 gtk_dialog_set_alternative_button_order (dialog,
499 hbox = gtk_hbox_new (FALSE, 12);
500 gtk_container_set_border_width (GTK_CONTAINER (hbox), 5);
501 gtk_box_pack_start (GTK_BOX (dialog->vbox), hbox, TRUE, TRUE, 0);
503 icon = gtk_image_new_from_stock (GTK_STOCK_DIALOG_AUTHENTICATION,
504 GTK_ICON_SIZE_DIALOG);
506 gtk_misc_set_alignment (GTK_MISC (icon), 0.5, 0.0);
507 gtk_box_pack_start (GTK_BOX (hbox), icon, FALSE, FALSE, 0);
509 main_vbox = gtk_vbox_new (FALSE, 18);
510 gtk_box_pack_start (GTK_BOX (hbox), main_vbox, TRUE, TRUE, 0);
512 secondary = strstr (message, "\n");
513 if (secondary != NULL)
518 primary = g_strndup (message, secondary - message + 1);
519 s = g_strdup_printf ("<big><b>%s</b></big>%s", primary, secondary);
521 message_label = gtk_label_new (NULL);
522 gtk_label_set_markup (GTK_LABEL (message_label), s);
523 gtk_misc_set_alignment (GTK_MISC (message_label), 0.0, 0.5);
524 gtk_label_set_line_wrap (GTK_LABEL (message_label), TRUE);
525 gtk_box_pack_start (GTK_BOX (main_vbox), GTK_WIDGET (message_label),
533 message_label = gtk_label_new (message);
534 gtk_misc_set_alignment (GTK_MISC (message_label), 0.0, 0.5);
535 gtk_label_set_line_wrap (GTK_LABEL (message_label), TRUE);
536 gtk_box_pack_start (GTK_BOX (main_vbox), GTK_WIDGET (message_label),
540 vbox = gtk_vbox_new (FALSE, 6);
541 gtk_box_pack_start (GTK_BOX (main_vbox), vbox, FALSE, FALSE, 0);
543 can_anonymous = flags & G_ASK_PASSWORD_ANONYMOUS_SUPPORTED;
545 priv->anonymous_toggle = NULL;
552 anon_box = gtk_vbox_new (FALSE, 6);
553 gtk_box_pack_start (GTK_BOX (vbox), anon_box,
556 choice = gtk_radio_button_new_with_mnemonic (NULL, _("Connect _anonymously"));
557 gtk_box_pack_start (GTK_BOX (anon_box),
560 g_signal_connect (choice, "toggled",
561 G_CALLBACK (pw_dialog_anonymous_toggled), operation);
562 priv->anonymous_toggle = choice;
564 group = gtk_radio_button_get_group (GTK_RADIO_BUTTON (choice));
565 choice = gtk_radio_button_new_with_mnemonic (group, _("Connect as u_ser:"));
566 gtk_box_pack_start (GTK_BOX (anon_box),
569 g_signal_connect (choice, "toggled",
570 G_CALLBACK (pw_dialog_anonymous_toggled), operation);
575 if (flags & G_ASK_PASSWORD_NEED_PASSWORD)
578 if (flags & G_ASK_PASSWORD_NEED_USERNAME)
581 if (flags &G_ASK_PASSWORD_NEED_DOMAIN)
584 /* The table that holds the entries */
585 entry_container = gtk_alignment_new (0.0, 0.0, 1.0, 1.0);
587 gtk_alignment_set_padding (GTK_ALIGNMENT (entry_container),
588 0, 0, can_anonymous ? 12 : 0, 0);
590 gtk_box_pack_start (GTK_BOX (vbox), entry_container,
592 priv->entry_container = entry_container;
594 table = gtk_table_new (rows, 2, FALSE);
595 gtk_table_set_col_spacings (GTK_TABLE (table), 12);
596 gtk_table_set_row_spacings (GTK_TABLE (table), 6);
597 gtk_container_add (GTK_CONTAINER (entry_container), table);
601 priv->username_entry = NULL;
602 if (flags & G_ASK_PASSWORD_NEED_USERNAME)
603 priv->username_entry = table_add_entry (table, rows++, _("_Username:"),
604 default_user, operation);
606 priv->domain_entry = NULL;
607 if (flags & G_ASK_PASSWORD_NEED_DOMAIN)
608 priv->domain_entry = table_add_entry (table, rows++, _("_Domain:"),
609 default_domain, operation);
611 priv->password_entry = NULL;
612 if (flags & G_ASK_PASSWORD_NEED_PASSWORD)
614 priv->password_entry = table_add_entry (table, rows++, _("_Password:"),
616 gtk_entry_set_visibility (GTK_ENTRY (priv->password_entry), FALSE);
619 if (flags & G_ASK_PASSWORD_SAVING_SUPPORTED)
622 GtkWidget *remember_box;
624 GPasswordSave password_save;
626 remember_box = gtk_vbox_new (FALSE, 6);
627 gtk_box_pack_start (GTK_BOX (vbox), remember_box,
630 password_save = g_mount_operation_get_password_save (mount_op);
632 choice = gtk_radio_button_new_with_mnemonic (NULL, _("Forget password _immediately"));
633 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (choice),
634 password_save == G_PASSWORD_SAVE_NEVER);
635 g_object_set_data (G_OBJECT (choice), "password-save",
636 GINT_TO_POINTER (G_PASSWORD_SAVE_NEVER));
637 g_signal_connect (choice, "toggled",
638 G_CALLBACK (remember_button_toggled), operation);
639 gtk_box_pack_start (GTK_BOX (remember_box), choice, FALSE, FALSE, 0);
641 group = gtk_radio_button_get_group (GTK_RADIO_BUTTON (choice));
642 choice = gtk_radio_button_new_with_mnemonic (group, _("Remember password until you _logout"));
643 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (choice),
644 password_save == G_PASSWORD_SAVE_FOR_SESSION);
645 g_object_set_data (G_OBJECT (choice), "password-save",
646 GINT_TO_POINTER (G_PASSWORD_SAVE_FOR_SESSION));
647 g_signal_connect (choice, "toggled",
648 G_CALLBACK (remember_button_toggled), operation);
649 gtk_box_pack_start (GTK_BOX (remember_box), choice, FALSE, FALSE, 0);
651 group = gtk_radio_button_get_group (GTK_RADIO_BUTTON (choice));
652 choice = gtk_radio_button_new_with_mnemonic (group, _("Remember _forever"));
653 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (choice),
654 password_save == G_PASSWORD_SAVE_PERMANENTLY);
655 g_object_set_data (G_OBJECT (choice), "password-save",
656 GINT_TO_POINTER (G_PASSWORD_SAVE_PERMANENTLY));
657 g_signal_connect (choice, "toggled",
658 G_CALLBACK (remember_button_toggled), operation);
659 gtk_box_pack_start (GTK_BOX (remember_box), choice, FALSE, FALSE, 0);
662 g_signal_connect (G_OBJECT (dialog), "response",
663 G_CALLBACK (pw_dialog_got_response), operation);
667 /* The anonymous option will be active by default,
668 * ensure the toggled signal is emitted for it.
670 gtk_toggle_button_toggled (GTK_TOGGLE_BUTTON (priv->anonymous_toggle));
672 else if (! pw_dialog_input_is_valid (operation))
673 gtk_dialog_set_response_sensitive (dialog, GTK_RESPONSE_OK, FALSE);
675 g_object_notify (G_OBJECT (operation), "is-showing");
677 if (priv->parent_window)
679 gtk_window_set_transient_for (window, priv->parent_window);
680 gtk_window_set_modal (window, TRUE);
682 else if (priv->screen)
683 gtk_window_set_screen (GTK_WINDOW (dialog), priv->screen);
685 gtk_widget_show_all (GTK_WIDGET (dialog));
687 g_object_ref (operation);
691 question_dialog_button_clicked (GtkDialog *dialog,
695 GtkMountOperationPrivate *priv;
696 GtkMountOperation *operation;
698 operation = GTK_MOUNT_OPERATION (op);
699 priv = operation->priv;
701 if (button_number >= 0)
703 g_mount_operation_set_choice (op, button_number);
704 g_mount_operation_reply (op, G_MOUNT_OPERATION_HANDLED);
707 g_mount_operation_reply (op, G_MOUNT_OPERATION_ABORTED);
710 g_object_notify (G_OBJECT (operation), "is-showing");
711 gtk_widget_destroy (GTK_WIDGET (dialog));
716 gtk_mount_operation_ask_question (GMountOperation *op,
718 const char *choices[])
720 GtkMountOperationPrivate *priv;
722 const char *secondary = NULL;
726 g_return_if_fail (GTK_IS_MOUNT_OPERATION (op));
727 g_return_if_fail (message != NULL);
728 g_return_if_fail (choices != NULL);
730 priv = GTK_MOUNT_OPERATION (op)->priv;
732 primary = strstr (message, "\n");
735 secondary = primary + 1;
736 primary = g_strndup (message, primary - message);
739 dialog = gtk_message_dialog_new (priv->parent_window, 0,
740 GTK_MESSAGE_QUESTION,
741 GTK_BUTTONS_NONE, "%s",
742 primary != NULL ? primary : message);
746 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
749 /* First count the items in the list then
750 * add the buttons in reverse order */
752 while (choices[len] != NULL)
755 for (count = len - 1; count >= 0; count--)
756 gtk_dialog_add_button (GTK_DIALOG (dialog), choices[count], count);
758 g_signal_connect (G_OBJECT (dialog), "response",
759 G_CALLBACK (question_dialog_button_clicked), op);
761 priv->dialog = GTK_DIALOG (dialog);
762 g_object_notify (G_OBJECT (op), "is-showing");
764 if (priv->parent_window == NULL && priv->screen)
765 gtk_window_set_screen (GTK_WINDOW (dialog), priv->screen);
767 gtk_widget_show (dialog);
772 show_processes_button_clicked (GtkDialog *dialog,
776 GtkMountOperationPrivate *priv;
777 GtkMountOperation *operation;
779 operation = GTK_MOUNT_OPERATION (op);
780 priv = operation->priv;
782 if (button_number >= 0)
784 g_mount_operation_set_choice (op, button_number);
785 g_mount_operation_reply (op, G_MOUNT_OPERATION_HANDLED);
788 g_mount_operation_reply (op, G_MOUNT_OPERATION_ABORTED);
791 g_object_notify (G_OBJECT (operation), "is-showing");
792 gtk_widget_destroy (GTK_WIDGET (dialog));
797 pid_equal (gconstpointer a,
805 return GPOINTER_TO_INT(pb) - GPOINTER_TO_INT(pa);
809 diff_sorted_arrays (GArray *array1,
811 GCompareFunc compare,
812 GArray *added_indices,
813 GArray *removed_indices)
821 elem_size = g_array_get_element_size (array1);
822 g_assert (elem_size == g_array_get_element_size (array2));
824 while (n1 < array1->len && n2 < array2->len)
826 order = (*compare) (((const char*) array1->data) + n1 * elem_size,
827 ((const char*) array2->data) + n2 * elem_size);
830 g_array_append_val (removed_indices, n1);
835 g_array_append_val (added_indices, n2);
845 while (n1 < array1->len)
847 g_array_append_val (removed_indices, n1);
850 while (n2 < array2->len)
852 g_array_append_val (added_indices, n2);
859 add_pid_to_process_list_store (GtkMountOperation *mount_operation,
860 GtkMountOperationLookupContext *lookup_context,
861 GtkListStore *list_store,
873 _gtk_mount_operation_lookup_info (lookup_context,
881 name = g_strdup_printf (_("Unknown Application (pid %d)"), pid);
883 if (command_line == NULL)
884 command_line = g_strdup ("");
889 theme = gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET (mount_operation->priv->dialog)));
890 pixbuf = gtk_icon_theme_load_icon (theme,
891 "application-x-executable",
897 markup = g_strdup_printf ("<b>%s</b>\n"
902 gtk_list_store_append (list_store, &iter);
903 gtk_list_store_set (list_store, &iter,
910 g_object_unref (pixbuf);
913 g_free (command_line);
917 remove_pid_from_process_list_store (GtkMountOperation *mount_operation,
918 GtkListStore *list_store,
924 if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (list_store), &iter))
928 gtk_tree_model_get (GTK_TREE_MODEL (list_store),
933 if (pid_of_item == pid)
935 gtk_list_store_remove (list_store, &iter);
939 while (gtk_tree_model_iter_next (GTK_TREE_MODEL (list_store), &iter));
945 update_process_list_store (GtkMountOperation *mount_operation,
946 GtkListStore *list_store,
950 GtkMountOperationLookupContext *lookup_context;
951 GArray *current_pids;
952 GArray *pid_indices_to_add;
953 GArray *pid_indices_to_remove;
957 /* Just removing all items and adding new ones will screw up the
958 * focus handling in the treeview - so compute the delta, and add/remove
959 * items as appropriate
961 current_pids = g_array_new (FALSE, FALSE, sizeof (GPid));
962 pid_indices_to_add = g_array_new (FALSE, FALSE, sizeof (gint));
963 pid_indices_to_remove = g_array_new (FALSE, FALSE, sizeof (gint));
965 if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (list_store), &iter))
969 gtk_tree_model_get (GTK_TREE_MODEL (list_store),
974 g_array_append_val (current_pids, pid);
976 while (gtk_tree_model_iter_next (GTK_TREE_MODEL (list_store), &iter));
979 g_array_sort (current_pids, pid_equal);
980 g_array_sort (processes, pid_equal);
982 diff_sorted_arrays (current_pids, processes, pid_equal, pid_indices_to_add, pid_indices_to_remove);
984 if (pid_indices_to_add->len > 0)
985 lookup_context = _gtk_mount_operation_lookup_context_get (gtk_widget_get_display (mount_operation->priv->process_tree_view));
986 for (n = 0; n < pid_indices_to_add->len; n++)
988 pid = g_array_index (processes, GPid, n);
989 add_pid_to_process_list_store (mount_operation, lookup_context, list_store, pid);
992 for (n = 0; n < pid_indices_to_remove->len; n++)
994 pid = g_array_index (current_pids, GPid, n);
995 remove_pid_from_process_list_store (mount_operation, list_store, pid);
997 if (pid_indices_to_add->len > 0)
998 _gtk_mount_operation_lookup_context_free (lookup_context);
1000 /* select the first item, if we went from a zero to a non-zero amount of processes */
1001 if (current_pids->len == 0 && pid_indices_to_add->len > 0)
1003 if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (list_store), &iter))
1005 GtkTreeSelection *tree_selection;
1006 tree_selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (mount_operation->priv->process_tree_view));
1007 gtk_tree_selection_select_iter (tree_selection, &iter);
1011 g_array_unref (current_pids);
1012 g_array_unref (pid_indices_to_add);
1013 g_array_unref (pid_indices_to_remove);
1017 on_end_process_activated (GtkMenuItem *item,
1020 GtkMountOperation *op = GTK_MOUNT_OPERATION (user_data);
1021 GtkTreeSelection *selection;
1026 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (op->priv->process_tree_view));
1028 if (!gtk_tree_selection_get_selected (selection,
1033 gtk_tree_model_get (GTK_TREE_MODEL (op->priv->process_list_store),
1038 /* TODO: We might want to either
1040 * - Be smart about things and send SIGKILL rather than SIGTERM if
1041 * this is the second time the user requests killing a process
1043 * - Or, easier (but worse user experience), offer both "End Process"
1044 * and "Terminate Process" options
1046 * But that's not how things work right now....
1049 if (!_gtk_mount_operation_kill_process (pid_to_kill, &error))
1054 /* Use GTK_DIALOG_DESTROY_WITH_PARENT here since the parent dialog can be
1055 * indeed be destroyed via the GMountOperation::abort signal... for example,
1056 * this is triggered if the user yanks the device while we are showing
1059 dialog = gtk_message_dialog_new (GTK_WINDOW (op->priv->dialog),
1060 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
1063 _("Unable to end process"));
1064 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
1068 gtk_widget_show_all (dialog);
1069 response = gtk_dialog_run (GTK_DIALOG (dialog));
1071 /* GTK_RESPONSE_NONE means the dialog were programmatically destroy, e.g. that
1072 * GTK_DIALOG_DESTROY_WITH_PARENT kicked in - so it would trigger a warning to
1073 * destroy the dialog in that case
1075 if (response != GTK_RESPONSE_NONE)
1076 gtk_widget_destroy (dialog);
1078 g_error_free (error);
1086 do_popup_menu_for_process_tree_view (GtkWidget *widget,
1087 GdkEventButton *event,
1088 GtkMountOperation *op)
1094 gboolean popped_up_menu;
1096 popped_up_menu = FALSE;
1098 menu = gtk_menu_new ();
1100 item = gtk_image_menu_item_new_with_mnemonic (_("_End Process"));
1101 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
1102 gtk_image_new_from_stock (GTK_STOCK_CLOSE, GTK_ICON_SIZE_MENU));
1103 g_signal_connect (item, "activate",
1104 G_CALLBACK (on_end_process_activated),
1106 gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
1107 gtk_widget_show_all (menu);
1112 GtkTreeSelection *selection;
1114 if (gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (op->priv->process_tree_view),
1122 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (op->priv->process_tree_view));
1123 gtk_tree_selection_select_path (selection, path);
1124 gtk_tree_path_free (path);
1128 /* don't popup a menu if the user right-clicked in an area with no rows */
1132 button = event->button;
1133 event_time = event->time;
1138 event_time = gtk_get_current_event_time ();
1141 gtk_menu_popup (GTK_MENU (menu),
1149 popped_up_menu = TRUE;
1152 return popped_up_menu;
1156 on_popup_menu_for_process_tree_view (GtkWidget *widget,
1159 GtkMountOperation *op = GTK_MOUNT_OPERATION (user_data);
1160 return do_popup_menu_for_process_tree_view (widget, NULL, op);
1164 on_button_press_event_for_process_tree_view (GtkWidget *widget,
1165 GdkEventButton *event,
1168 GtkMountOperation *op = GTK_MOUNT_OPERATION (user_data);
1173 /* Ignore double-clicks and triple-clicks */
1174 if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
1176 ret = do_popup_menu_for_process_tree_view (widget, event, op);
1183 create_show_processes_dialog (GMountOperation *op,
1184 const char *message,
1185 const char *choices[])
1187 GtkMountOperationPrivate *priv;
1189 const char *secondary = NULL;
1193 GtkWidget *tree_view;
1194 GtkWidget *scrolled_window;
1196 GtkWidget *content_area;
1197 GtkTreeViewColumn *column;
1198 GtkCellRenderer *renderer;
1199 GtkListStore *list_store;
1202 priv = GTK_MOUNT_OPERATION (op)->priv;
1204 primary = strstr (message, "\n");
1207 secondary = primary + 1;
1208 primary = g_strndup (message, primary - message);
1211 dialog = gtk_dialog_new ();
1213 if (priv->parent_window != NULL)
1214 gtk_window_set_transient_for (GTK_WINDOW (dialog), priv->parent_window);
1215 gtk_window_set_title (GTK_WINDOW (dialog), "");
1216 gtk_dialog_set_has_separator (GTK_DIALOG (dialog), FALSE);
1218 content_area = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
1219 vbox = gtk_vbox_new (FALSE, 12);
1220 gtk_container_set_border_width (GTK_CONTAINER (vbox), 12);
1221 gtk_box_pack_start (GTK_BOX (content_area), vbox, TRUE, TRUE, 0);
1223 if (secondary != NULL)
1225 s = g_strdup_printf ("<big><b>%s</b></big>\n\n%s", primary, secondary);
1229 s = g_strdup_printf ("%s", primary);
1232 label = gtk_label_new (NULL);
1233 gtk_label_set_markup (GTK_LABEL (label), s);
1235 gtk_box_pack_start (GTK_BOX (vbox), label, TRUE, TRUE, 0);
1237 /* First count the items in the list then
1238 * add the buttons in reverse order */
1240 while (choices[len] != NULL)
1243 for (count = len - 1; count >= 0; count--)
1244 gtk_dialog_add_button (GTK_DIALOG (dialog), choices[count], count);
1246 g_signal_connect (G_OBJECT (dialog), "response",
1247 G_CALLBACK (show_processes_button_clicked), op);
1249 priv->dialog = GTK_DIALOG (dialog);
1250 g_object_notify (G_OBJECT (op), "is-showing");
1252 if (priv->parent_window == NULL && priv->screen)
1253 gtk_window_set_screen (GTK_WINDOW (dialog), priv->screen);
1255 tree_view = gtk_tree_view_new ();
1256 /* TODO: should use EM's when gtk+ RI patches land */
1257 gtk_widget_set_size_request (tree_view,
1261 column = gtk_tree_view_column_new ();
1262 renderer = gtk_cell_renderer_pixbuf_new ();
1263 gtk_tree_view_column_pack_start (column, renderer, FALSE);
1264 gtk_tree_view_column_set_attributes (column, renderer,
1267 renderer = gtk_cell_renderer_text_new ();
1268 g_object_set (renderer,
1269 "ellipsize", PANGO_ELLIPSIZE_MIDDLE,
1270 "ellipsize-set", TRUE,
1272 gtk_tree_view_column_pack_start (column, renderer, TRUE);
1273 gtk_tree_view_column_set_attributes (column, renderer,
1276 gtk_tree_view_append_column (GTK_TREE_VIEW (tree_view), column);
1277 gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (tree_view), FALSE);
1280 scrolled_window = gtk_scrolled_window_new (NULL, NULL);
1281 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
1283 GTK_POLICY_AUTOMATIC);
1284 gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_window), GTK_SHADOW_IN);
1286 gtk_container_add (GTK_CONTAINER (scrolled_window), tree_view);
1287 gtk_box_pack_start (GTK_BOX (vbox), scrolled_window, TRUE, TRUE, 0);
1289 g_signal_connect (tree_view, "popup-menu",
1290 G_CALLBACK (on_popup_menu_for_process_tree_view),
1292 g_signal_connect (tree_view, "button-press-event",
1293 G_CALLBACK (on_button_press_event_for_process_tree_view),
1296 list_store = gtk_list_store_new (3,
1301 gtk_tree_view_set_model (GTK_TREE_VIEW (tree_view), GTK_TREE_MODEL (list_store));
1303 priv->process_list_store = list_store;
1304 priv->process_tree_view = tree_view;
1305 /* set pointers to NULL when dialog goes away */
1306 g_object_add_weak_pointer (G_OBJECT (list_store), (gpointer *) &priv->process_list_store);
1307 g_object_add_weak_pointer (G_OBJECT (tree_view), (gpointer *) &priv->process_tree_view);
1309 g_object_unref (list_store);
1311 gtk_widget_show_all (dialog);
1316 gtk_mount_operation_show_processes (GMountOperation *op,
1317 const char *message,
1319 const char *choices[])
1321 GtkMountOperationPrivate *priv;
1323 g_return_if_fail (GTK_IS_MOUNT_OPERATION (op));
1324 g_return_if_fail (message != NULL);
1325 g_return_if_fail (processes != NULL);
1326 g_return_if_fail (choices != NULL);
1328 priv = GTK_MOUNT_OPERATION (op)->priv;
1330 if (priv->process_list_store == NULL)
1332 /* need to create the dialog */
1333 create_show_processes_dialog (op, message, choices);
1336 /* otherwise, we're showing the dialog, assume messages+choices hasn't changed */
1338 update_process_list_store (GTK_MOUNT_OPERATION (op),
1339 priv->process_list_store,
1344 gtk_mount_operation_aborted (GMountOperation *op)
1346 GtkMountOperationPrivate *priv;
1348 priv = GTK_MOUNT_OPERATION (op)->priv;
1350 if (priv->dialog != NULL)
1352 gtk_widget_destroy (GTK_WIDGET (priv->dialog));
1353 priv->dialog = NULL;
1354 g_object_notify (G_OBJECT (op), "is-showing");
1355 g_object_unref (op);
1360 * gtk_mount_operation_new:
1361 * @parent: transient parent of the window, or %NULL
1363 * Creates a new #GtkMountOperation
1365 * Returns: a new #GtkMountOperation
1370 gtk_mount_operation_new (GtkWindow *parent)
1372 GMountOperation *mount_operation;
1374 mount_operation = g_object_new (GTK_TYPE_MOUNT_OPERATION,
1375 "parent", parent, NULL);
1377 return mount_operation;
1381 * gtk_mount_operation_is_showing:
1382 * @op: a #GtkMountOperation
1384 * Returns whether the #GtkMountOperation is currently displaying
1387 * Returns: %TRUE if @op is currently displaying a window
1392 gtk_mount_operation_is_showing (GtkMountOperation *op)
1394 g_return_val_if_fail (GTK_IS_MOUNT_OPERATION (op), FALSE);
1396 return op->priv->dialog != NULL;
1400 * gtk_mount_operation_set_parent:
1401 * @op: a #GtkMountOperation
1402 * @parent: transient parent of the window, or %NULL
1404 * Sets the transient parent for windows shown by the
1405 * #GtkMountOperation.
1410 gtk_mount_operation_set_parent (GtkMountOperation *op,
1413 GtkMountOperationPrivate *priv;
1415 g_return_if_fail (GTK_IS_MOUNT_OPERATION (op));
1416 g_return_if_fail (parent == NULL || GTK_IS_WINDOW (parent));
1420 if (priv->parent_window == parent)
1423 if (priv->parent_window)
1425 g_signal_handlers_disconnect_by_func (priv->parent_window,
1426 gtk_widget_destroyed,
1427 &priv->parent_window);
1428 priv->parent_window = NULL;
1433 priv->parent_window = g_object_ref (parent);
1435 g_signal_connect (parent, "destroy",
1436 G_CALLBACK (gtk_widget_destroyed),
1437 &priv->parent_window);
1440 gtk_window_set_transient_for (GTK_WINDOW (priv->dialog), parent);
1443 g_object_notify (G_OBJECT (op), "parent");
1447 * gtk_mount_operation_get_parent:
1448 * @op: a #GtkMountOperation
1450 * Gets the transient parent used by the #GtkMountOperation
1452 * Returns: the transient parent for windows shown by @op
1457 gtk_mount_operation_get_parent (GtkMountOperation *op)
1459 g_return_val_if_fail (GTK_IS_MOUNT_OPERATION (op), NULL);
1461 return op->priv->parent_window;
1465 * gtk_mount_operation_set_screen:
1466 * @op: a #GtkMountOperation
1467 * @screen: a #GdkScreen
1469 * Sets the screen to show windows of the #GtkMountOperation on.
1474 gtk_mount_operation_set_screen (GtkMountOperation *op,
1477 GtkMountOperationPrivate *priv;
1479 g_return_if_fail (GTK_IS_MOUNT_OPERATION (op));
1480 g_return_if_fail (GDK_IS_SCREEN (screen));
1484 if (priv->screen == screen)
1488 g_object_unref (priv->screen);
1490 priv->screen = g_object_ref (screen);
1493 gtk_window_set_screen (GTK_WINDOW (priv->dialog), screen);
1495 g_object_notify (G_OBJECT (op), "screen");
1499 * gtk_mount_operation_get_screen:
1500 * @op: a #GtkMountOperation
1502 * Gets the screen on which windows of the #GtkMountOperation
1505 * Returns: the screen on which windows of @op are shown
1510 gtk_mount_operation_get_screen (GtkMountOperation *op)
1512 GtkMountOperationPrivate *priv;
1514 g_return_val_if_fail (GTK_IS_MOUNT_OPERATION (op), NULL);
1519 return gtk_window_get_screen (GTK_WINDOW (priv->dialog));
1520 else if (priv->parent_window)
1521 return gtk_window_get_screen (GTK_WINDOW (priv->parent_window));
1522 else if (priv->screen)
1523 return priv->screen;
1525 return gdk_screen_get_default ();
1528 #define __GTK_MOUNT_OPERATION_C__
1529 #include "gtkaliasdef.c"