~ubuntu-branches/ubuntu/trusty/thunar-volman/trusty-proposed

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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/* $Id: tvm-command-entry.c 2340 2007-01-11 23:11:32Z benny $ */
/*-
 * Copyright (c) 2005-2007 Benedikt Meurer <benny@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., 59 Temple
 * Place, Suite 330, Boston, MA  02111-1307  USA
 */

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

#ifdef HAVE_MEMORY_H
#include <memory.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif

#include <thunar-volman/tvm-command-entry.h>



/* Property identifiers */
enum
{
  PROP_0,
  PROP_COMMAND,
};



static void tvm_command_entry_class_init    (TvmCommandEntryClass *klass);
static void tvm_command_entry_init          (TvmCommandEntry      *command_entry);
static void tvm_command_entry_finalize      (GObject              *object);
static void tvm_command_entry_get_property  (GObject              *object,
                                             guint                 prop_id,
                                             GValue               *value,
                                             GParamSpec           *pspec);
static void tvm_command_entry_set_property  (GObject              *object,
                                             guint                 prop_id,
                                             const GValue         *value,
                                             GParamSpec           *pspec);
static void tvm_command_entry_clicked       (GtkWidget            *button,
                                             TvmCommandEntry      *command_entry);



static GObjectClass *tvm_command_entry_parent_class;



GType
tvm_command_entry_get_type (void)
{
  static GType type = G_TYPE_INVALID;

  if (G_UNLIKELY (type == G_TYPE_INVALID))
    {
      static const GTypeInfo info =
      {
        sizeof (TvmCommandEntryClass),
        NULL,
        NULL,
        (GClassInitFunc) tvm_command_entry_class_init,
        NULL,
        NULL,
        sizeof (TvmCommandEntry),
        0,
        (GInstanceInitFunc) tvm_command_entry_init,
        NULL,
      };

      type = g_type_register_static (GTK_TYPE_HBOX, I_("TvmCommandEntry"), &info, 0);
    }

  return type;
}



static void
tvm_command_entry_class_init (TvmCommandEntryClass *klass)
{
  GObjectClass *gobject_class;

  /* determine the parent type class */
  tvm_command_entry_parent_class = g_type_class_peek_parent (klass);

  gobject_class = G_OBJECT_CLASS (klass);
  gobject_class->finalize = tvm_command_entry_finalize;
  gobject_class->get_property = tvm_command_entry_get_property;
  gobject_class->set_property = tvm_command_entry_set_property;

  /**
   * TvmCommandEntry:command:
   *
   * The command currently entered into this command entry widget.
   **/
  g_object_class_install_property (gobject_class,
                                   PROP_COMMAND,
                                   g_param_spec_string ("command",
                                                        "command",
                                                        "command",
                                                        NULL,
                                                        EXO_PARAM_READWRITE));
}



static void
tvm_command_entry_init (TvmCommandEntry *command_entry)
{
  GtkWidget *button;
  GtkWidget *align;
  GtkWidget *image;

  gtk_box_set_spacing (GTK_BOX (command_entry), 2);

  align = g_object_new (GTK_TYPE_ALIGNMENT, "width-request", 10, NULL);
  gtk_box_pack_start (GTK_BOX (command_entry), align, FALSE, FALSE, 0);
  gtk_widget_show (align);

  command_entry->label = g_object_new (GTK_TYPE_LABEL, "use-underline", TRUE, "xalign", 0.0f, NULL);
  gtk_box_pack_start (GTK_BOX (command_entry), command_entry->label, FALSE, FALSE, 10);
  gtk_widget_show (command_entry->label);

  command_entry->entry = gtk_entry_new ();
  gtk_label_set_mnemonic_widget (GTK_LABEL (command_entry->label), command_entry->entry);
  exo_mutual_binding_new (G_OBJECT (command_entry->entry), "text", G_OBJECT (command_entry), "command");
  gtk_box_pack_start (GTK_BOX (command_entry), command_entry->entry, TRUE, TRUE, 0);
  gtk_widget_show (command_entry->entry);

  button = gtk_button_new ();
  g_signal_connect (G_OBJECT (button), "clicked", G_CALLBACK (tvm_command_entry_clicked), command_entry);
  gtk_box_pack_start (GTK_BOX (command_entry), button, FALSE, FALSE, 0);
  gtk_widget_show (button);

  image = gtk_image_new_from_stock (GTK_STOCK_OPEN, GTK_ICON_SIZE_BUTTON);
  gtk_container_add (GTK_CONTAINER (button), image);
  gtk_widget_show (image);
}



static void
tvm_command_entry_finalize (GObject *object)
{
  TvmCommandEntry *command_entry = TVM_COMMAND_ENTRY (object);

  /* cleanup properties */
  g_free (command_entry->command);

  (*G_OBJECT_CLASS (tvm_command_entry_parent_class)->finalize) (object);
}



static void
tvm_command_entry_get_property (GObject    *object,
                                guint       prop_id,
                                GValue     *value,
                                GParamSpec *pspec)
{
  TvmCommandEntry *command_entry = TVM_COMMAND_ENTRY (object);

  switch (prop_id)
    {
    case PROP_COMMAND:
      g_value_set_string (value, tvm_command_entry_get_command (command_entry));
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}



static void
tvm_command_entry_set_property (GObject      *object,
                                guint         prop_id,
                                const GValue *value,
                                GParamSpec   *pspec)
{
  TvmCommandEntry *command_entry = TVM_COMMAND_ENTRY (object);

  switch (prop_id)
    {
    case PROP_COMMAND:
      tvm_command_entry_set_command (command_entry, g_value_get_string (value));
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}




static void
tvm_command_entry_clicked (GtkWidget       *button,
                           TvmCommandEntry *command_entry)
{
  GtkFileFilter *filter;
  GtkWidget     *toplevel;
  GtkWidget     *chooser;
  gchar         *filename;
  gchar         *s;

  g_return_if_fail (GTK_IS_BUTTON (button));
  g_return_if_fail (TVM_IS_COMMAND_ENTRY (command_entry));

  /* determine the toplevel widget */
  toplevel = gtk_widget_get_toplevel (button);
  if (toplevel == NULL || !GTK_WIDGET_TOPLEVEL (toplevel))
    return;

  chooser = gtk_file_chooser_dialog_new (_("Select an Application"),
                                         GTK_WINDOW (toplevel),
                                         GTK_FILE_CHOOSER_ACTION_OPEN,
                                         GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
                                         GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
                                         NULL);
  gtk_file_chooser_set_local_only (GTK_FILE_CHOOSER (chooser), TRUE);

  /* add file chooser filters */
  filter = gtk_file_filter_new ();
  gtk_file_filter_set_name (filter, _("All Files"));
  gtk_file_filter_add_pattern (filter, "*");
  gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);

  filter = gtk_file_filter_new ();
  gtk_file_filter_set_name (filter, _("Executable Files"));
  gtk_file_filter_add_mime_type (filter, "application/x-csh");
  gtk_file_filter_add_mime_type (filter, "application/x-executable");
  gtk_file_filter_add_mime_type (filter, "application/x-perl");
  gtk_file_filter_add_mime_type (filter, "application/x-python");
  gtk_file_filter_add_mime_type (filter, "application/x-ruby");
  gtk_file_filter_add_mime_type (filter, "application/x-shellscript");
  gtk_file_filter_add_pattern (filter, "*.pl");
  gtk_file_filter_add_pattern (filter, "*.py");
  gtk_file_filter_add_pattern (filter, "*.rb");
  gtk_file_filter_add_pattern (filter, "*.sh");
  gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);
  gtk_file_chooser_set_filter (GTK_FILE_CHOOSER (chooser), filter);

  filter = gtk_file_filter_new ();
  gtk_file_filter_set_name (filter, _("Perl Scripts"));
  gtk_file_filter_add_mime_type (filter, "application/x-perl");
  gtk_file_filter_add_pattern (filter, "*.pl");
  gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);

  filter = gtk_file_filter_new ();
  gtk_file_filter_set_name (filter, _("Python Scripts"));
  gtk_file_filter_add_mime_type (filter, "application/x-python");
  gtk_file_filter_add_pattern (filter, "*.py");
  gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);

  filter = gtk_file_filter_new ();
  gtk_file_filter_set_name (filter, _("Ruby Scripts"));
  gtk_file_filter_add_mime_type (filter, "application/x-ruby");
  gtk_file_filter_add_pattern (filter, "*.rb");
  gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);

  filter = gtk_file_filter_new ();
  gtk_file_filter_set_name (filter, _("Shell Scripts"));
  gtk_file_filter_add_mime_type (filter, "application/x-csh");
  gtk_file_filter_add_mime_type (filter, "application/x-shellscript");
  gtk_file_filter_add_pattern (filter, "*.sh");
  gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (chooser), filter);

  /* use the bindir as default folder */
  gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (chooser), BINDIR);

  /* setup the currently selected file */
  g_object_get (G_OBJECT (command_entry), "command", &filename, NULL);
  if (G_LIKELY (filename != NULL))
    {
      /* use only the first argument */
      s = strchr (filename, ' ');
      if (G_UNLIKELY (s != NULL))
        *s = '\0';

      /* check if we have a file name */
      if (G_LIKELY (*filename != '\0'))
        {
          /* check if the filename is not an absolute path */
          if (G_LIKELY (!g_path_is_absolute (filename)))
            {
              /* try to lookup the filename in $PATH */
              s = g_find_program_in_path (filename);
              if (G_LIKELY (s != NULL))
                {
                  /* use the absolute path instead */
                  g_free (filename);
                  filename = s;
                }
            }

          /* check if we have an absolute path now */
          if (G_LIKELY (g_path_is_absolute (filename)))
            gtk_file_chooser_set_filename (GTK_FILE_CHOOSER (chooser), filename);
        }

      /* release the filename */
      g_free (filename);
    }

  /* run the chooser dialog */
  if (gtk_dialog_run (GTK_DIALOG (chooser)) == GTK_RESPONSE_ACCEPT)
    {
      filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (chooser));
      tvm_command_entry_set_command (command_entry, filename);
      g_free (filename);
    }

  gtk_widget_destroy (chooser);
}



/**
 * tvm_command_entry_new:
 *
 * Allocates a new #TvmCommandEntry instance.
 *
 * Return value: the newly allocated #TvmCommandEntry.
 **/
GtkWidget*
tvm_command_entry_new (void)
{
  return g_object_new (TVM_TYPE_COMMAND_ENTRY, NULL);
}



/**
 * tvm_command_entry_new:
 * @label : the label for the command entry.
 *
 * Allocates a new #TvmCommandEntry instance with the @label.
 *
 * Return value: the newly allocated #TvmCommandEntry.
 **/
GtkWidget*
tvm_command_entry_new_with_label (const gchar *label)
{
  TvmCommandEntry *entry;

  g_return_val_if_fail (label == NULL || g_utf8_validate (label, -1, NULL), NULL);

  entry = g_object_new (TVM_TYPE_COMMAND_ENTRY, NULL);
  if (G_LIKELY (label != NULL))
    g_object_set (G_OBJECT (entry->label), "label", label, "use-underline", TRUE, NULL);
  return GTK_WIDGET (entry);
}



/**
 * tvm_command_entry_get_command:
 * @command_entry : a #TvmCommandEntry.
 *
 * Returns the command of the @command_entry.
 *
 * Return value: the command in the @command_entry.
 **/
const gchar*
tvm_command_entry_get_command (TvmCommandEntry *command_entry)
{
  g_return_val_if_fail (TVM_IS_COMMAND_ENTRY (command_entry), NULL);
  return command_entry->command;
}



/**
 * tvm_command_entry_set_command:
 * @command_entry : a #TvmCommandEntry.
 * @command       : the new command.
 *
 * Sets the command in @command_entry to @command.
 **/
void
tvm_command_entry_set_command (TvmCommandEntry *command_entry,
                               const gchar     *command)
{
  g_return_if_fail (TVM_IS_COMMAND_ENTRY (command_entry));
  g_return_if_fail (g_utf8_validate (command, -1, NULL));

  /* update to the new command */
  g_free (command_entry->command);
  command_entry->command = g_strdup (command);
  g_object_notify (G_OBJECT (command_entry), "command");
}