summaryrefslogtreecommitdiff
path: root/thunar-volman/tvm-prompt.c
blob: f2d9b00f7cd0a242253e5c25480a2ad4131c861b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/* vi:set et ai sw=2 sts=2 ts=2: */
/*-
 * Copyright (c) 2007 Benedikt Meurer <benny@xfce.org>
 * Copyright (c) 2010 Jannis Pohlmann <jannis@xfce.org>
 *
 * This program is free software; you can redistribute it and/or 
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of 
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public 
 * License along with this program; if not, write to the Free 
 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#ifdef HAVE_STDARG_H
#include <stdarg.h>
#endif

#include <gudev/gudev.h>

#include <gtk/gtk.h>

#include <libxfce4ui/libxfce4ui.h>

#include <thunar-volman/tvm-context.h>
#include <thunar-volman/tvm-pango-extensions.h>
#include <thunar-volman/tvm-prompt.h>



static void
tvm_prompt_uevent (GUdevClient *client,
                   const gchar *action,
                   GUdevDevice *device,
                   GtkWidget   *dialog)
{
  GUdevDevice *dialog_device;

  g_return_if_fail (G_UDEV_IS_CLIENT (client));
  g_return_if_fail (action != NULL && *action != '\0');
  g_return_if_fail (G_UDEV_IS_DEVICE (device));
  g_return_if_fail (GTK_IS_DIALOG (dialog));

  /* ignore "move" and "add" actions, "change" might only be correct 
   * for CDs/DVDs though */
  if (g_strcmp0 (action, "remove") != 0 && g_strcmp0 (action, "change") != 0)
    return;

  /* determine the device belonging to the dialog */
  dialog_device = g_object_get_data (G_OBJECT (dialog), "device");

  /* the device should be set, otherwise it's a bug in the application */
  g_assert (dialog_device != NULL);

  /* check if the removed/changed device belongs to the dialog */
  if (g_strcmp0 (g_udev_device_get_sysfs_path (device),
                 g_udev_device_get_sysfs_path (dialog_device)) == 0)
    {
      /* cancel the dialog */
      gtk_dialog_response (GTK_DIALOG (dialog), GTK_RESPONSE_CANCEL);
    }
}



gint
tvm_prompt (TvmContext  *context,
            const gchar *icon,
            const gchar *title,
            const gchar *primary_text,
            const gchar *secondary_text,
            const gchar *first_button_text,
            ...)
{
  GtkWidget *dialog;
  GtkWidget *hbox;
  GtkWidget *image;
  GtkWidget *label;
  GtkWidget *vbox;
  va_list    args;
  gint       response;

  g_return_val_if_fail (context != NULL, GTK_RESPONSE_CANCEL);
  
  /* create a new dialog */
  dialog = gtk_dialog_new ();
  gtk_window_set_resizable (GTK_WINDOW (dialog), FALSE);
  g_object_set_data_full (G_OBJECT (dialog), "device", g_object_ref (context->device), 
                          g_object_unref);
  gtk_container_set_border_width (GTK_CONTAINER (gtk_dialog_get_content_area (GTK_DIALOG (dialog))), 6);
  gtk_container_set_border_width (GTK_CONTAINER (gtk_dialog_get_action_area (GTK_DIALOG (dialog))), 12);

  /* apply the specified title */
  if (title != NULL && *title != '\0')
    gtk_window_set_title (GTK_WINDOW (dialog), title);

  /* create the specified buttons */
  if (first_button_text != NULL)
    {
      va_start (args, first_button_text);
      for (response = va_arg (args, gint); first_button_text != NULL; )
        {
          /* insert the button */
          gtk_dialog_add_button (GTK_DIALOG (dialog), first_button_text, response);
          first_button_text = va_arg (args, const gchar *);
          if (first_button_text == NULL)
            break;
          response = va_arg (args, gint);
        }
      va_end (args);
    }

  /* create the hbox */
  hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
  gtk_container_set_border_width (GTK_CONTAINER (hbox), 12);
  gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area  (GTK_DIALOG (dialog))), hbox, TRUE, TRUE, 0);
  gtk_widget_show (hbox);

  /* apply the specified icon */
  if (G_LIKELY (icon != NULL))
    {
      gtk_window_set_icon_name (GTK_WINDOW (dialog), icon);

      /* setup an image for the icon */
      image = gtk_image_new_from_icon_name (icon, GTK_ICON_SIZE_DIALOG);
      gtk_misc_set_alignment (GTK_MISC (image), 0.0f, 0.0f);
      gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
      gtk_widget_show (image);
    }

  /* setup the vbox */
  vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
  gtk_box_pack_start (GTK_BOX (hbox), vbox, FALSE, FALSE, 0);
  gtk_widget_show (vbox);

  /* setup the primary text */
  label = gtk_label_new (primary_text);
  gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
  gtk_misc_set_alignment (GTK_MISC (label), 0.0f, 0.5f);
  gtk_label_set_attributes (GTK_LABEL (label), tvm_pango_attr_list_big_bold ());
  gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
  gtk_widget_show (label);

  /* setup the secondary text */
  if (G_LIKELY (secondary_text != NULL))
    {
      label = gtk_label_new (secondary_text);
      gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
      gtk_misc_set_alignment (GTK_MISC (label), 0.0f, 0.5f);
      gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
      gtk_widget_show (label);
    }

  g_signal_connect (context->client, "uevent", G_CALLBACK (tvm_prompt_uevent), dialog);

  /* run the dialog */
  response = gtk_dialog_run (GTK_DIALOG (dialog));

  /* clean up */
  gtk_widget_destroy (dialog);

  return response;
}