summaryrefslogtreecommitdiff
path: root/thunar-volman/main.c
blob: 511bca8714dbe298a3e0ce02dfecf664134d7ffe (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
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
/* vi:set et ai sw=2 sts=2 ts=2: */
/*-
 * 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_STDLIB_H
#include <stdlib.h>
#endif

#include <glib.h>
#include <glib/gstdio.h>

#include <gtk/gtk.h>

#include <gudev/gudev.h>

#ifdef HAVE_LIBNOTIFY
#include <thunar-volman/tvm-notify.h>
#endif

#include <libxfce4util/libxfce4util.h>

#include <xfconf/xfconf.h>

#include <thunar-volman/tvm-context.h>
#include <thunar-volman/tvm-device.h>



/* variables for command line options */
static gchar   *opt_sysfs_path = NULL;
static gboolean opt_configure = FALSE;
static gboolean opt_version = FALSE;



/* command line options */
static GOptionEntry option_entries[] =
{
  { "device-added", 'a', 0, G_OPTION_ARG_STRING, &opt_sysfs_path, N_ ("The syfs path of the newly added device"), NULL, },
  { "configure", 'c', 0, G_OPTION_ARG_NONE, &opt_configure, N_ ("Configure management of removable drives and media"), NULL, },
  { "version", 'V', 0, G_OPTION_ARG_NONE, &opt_version, N_ ("Print version information and exit"), NULL, },
  { NULL, },
};



/* udev subsystems supported by thunar-volman */
static const gchar *supported_udev_subsystems[] =
{
  "block",
  "input",
  NULL,
};



int
main (int    argc,
      char **argv)
{
  XfconfChannel *channel;
  GUdevClient   *client;
  GUdevDevice   *device;
  TvmContext    *context = NULL;
  GMainLoop     *loop = NULL;
  GError        *error = NULL;
  gint           exit_code = EXIT_SUCCESS;

  /* setup translation domain */
  xfce_textdomain (GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR, "UTF-8");

  /* setup application name */
  g_set_application_name (_("Thunar Volume Manager"));

#ifdef G_ENABLE_DEBUG
  /* Do NOT remove this line for now. If something doesn't work, fix your code instead */
  g_log_set_always_fatal (G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING);
#endif

  /* initialize the threading system */
  if (!g_thread_supported ())
    g_thread_init (NULL);

  /* initialize GTK+ */
  if (!gtk_init_with_args (&argc, &argv, NULL, option_entries, GETTEXT_PACKAGE, &error))
    {
      g_fprintf (stderr, "%s: %s.\n", g_get_prgname (), error->message);
      g_error_free (error);
      return EXIT_FAILURE;
    }

  /* check if we should print version information */
  if (opt_version)
    {
      g_print ("%s %s (Xfce %s)\n\n", PACKAGE_NAME, PACKAGE_VERSION, 
               xfce_version_string ());
      g_print ("%s\n", "Copyright (c) 2004-2007 Benedikt Meurer <benny@xfce.org>");
      g_print ("%s\n", "Copyright (c)      2010 Jannis Pohlmann <jannis@xfce.org>");
      g_print ("\n%s\n\n", _("All rights reserved."));
      g_print (_("Please report bugs to <%s>."), PACKAGE_BUGREPORT);
      g_print ("\n");
      return EXIT_SUCCESS;
    }

  /* initialize xfconf */
  if (!xfconf_init (&error))
    {
      g_fprintf (stderr, "%s: %s.\n", g_get_prgname (), error->message);
      g_error_free (error);
      return EXIT_FAILURE;
    }

  /* check if we should print the version information */
  if (opt_configure)
    {
      /* the --configure/-c option of thunar-volman exists for backwards-compatibility
       * reasons only. what we really do here is spawning thunar-volman-settings */
      if (!g_spawn_command_line_sync ("thunar-volman-settings", NULL, NULL, &exit_code, 
                                      &error))
        {
          g_fprintf (stderr, "%s: %s.\n", g_get_prgname (), error->message);
          g_error_free (error);
          exit_code = WEXITSTATUS (exit_code);
        }
    }
  else if (opt_sysfs_path != NULL)
    {
      /* create an udev client */
      client = g_udev_client_new (supported_udev_subsystems);

      /* determine the device belonging to the sysfs path */
      device = g_udev_client_query_by_sysfs_path (client, opt_sysfs_path);

      if (device != NULL)
        {
          /* get a reference on the thunar-volman settings channel */
          channel = xfconf_channel_get ("thunar-volman");

          /* create a new main loop */
          loop = g_main_loop_new (NULL, FALSE);

          /* allocate a new TvmContext */
          context = tvm_context_new (client, device, channel, loop, &error);

          /* handle the new device in an idle handler */
          g_idle_add ((GSourceFunc) tvm_context_run, context);

          /* release channel and device */
          g_object_unref (device);
        }
      else
        {
          g_set_error (&error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
                       _("There is no device with the sysfs path \"%s\""), 
                       opt_sysfs_path);
        }

      /* release the udev client */
      g_object_unref (client);
    }
  else
    {
      /* TRANSLATORS: thunar-volman wasn't invoked with either --device-added or
       * --configure */
      g_set_error (&error, G_FILE_ERROR, G_FILE_ERROR_FAILED, 
                   _("Must specify the sysfs path of new devices with --device-added"));
    }

  /* run the main loop */
  if (loop != NULL)
    {
      g_main_loop_run (loop);
      g_main_loop_unref (loop);
    }

  if (error != NULL)
    {
      g_fprintf (stderr, "%s: %s.\n", g_get_prgname (), error->message);
      g_error_free (error);
      exit_code = EXIT_FAILURE;
    }

#ifdef HAVE_LIBNOTIFY
  tvm_notify_uninit ();
#endif

  /* release the device context */
  if (context != NULL)
    tvm_context_free (context);

  /* free xfconf resources */
  xfconf_shutdown ();

  return exit_code;
}