2008-04-30 Cosimo Cecchi <cosimoc@gnome.org>
[nautilus.git] / src / nautilus-connect-server-dialog-main.c
blob44f196d5557d03414e836a3818b5dae8c3e7beb1
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
3 /* nautilus-connect-server-main.c - Start the "Connect to Server" dialog.
4 * Nautilus
6 * Copyright (C) 2005 Vincent Untz
8 * Nautilus is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of the
11 * License, or (at your option) any later version.
13 * Nautilus is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public
19 * License along with this program; see the file COPYING. If not,
20 * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
23 * Authors:
24 * Vincent Untz <vincent@vuntz.net>
27 #include <config.h>
29 #include <glib/gi18n.h>
31 #include <gtk/gtkmain.h>
32 #include <gtk/gtkwidget.h>
34 #include <libgnome/gnome-program.h>
35 #include <libgnomeui/gnome-ui-init.h>
36 #include <libgnomeui/gnome-authentication-manager.h>
38 #include <eel/eel-app-launch-context.h>
39 #include <eel/eel-preferences.h>
40 #include <eel/eel-stock-dialogs.h>
41 #include <eel/eel-mount-operation.h>
43 #include <libnautilus-private/nautilus-icon-names.h>
45 #include "nautilus-window.h"
46 #include "nautilus-connect-server-dialog.h"
48 static int open_dialogs;
50 static void
51 main_dialog_destroyed (GtkWidget *widget,
52 gpointer user_data)
54 /* this only happens when user clicks "cancel"
55 * on the main dialog or when we are all done.
57 gtk_main_quit ();
60 static void
61 error_dialog_destroyed (GtkWidget *widget,
62 GtkWidget *main_dialog)
64 if (--open_dialogs <= 0)
65 gtk_widget_destroy (main_dialog);
68 static void
69 display_error_dialog (GError *error,
70 const char *uri,
71 GtkWidget *parent)
73 GtkDialog *error_dialog;
74 char *error_message;
76 error_message = g_strdup_printf (_("Cannot display location \"%s\""),
77 uri);
78 error_dialog = eel_show_error_dialog (error_message,
79 error->message,
80 NULL);
82 open_dialogs++;
84 g_signal_connect (error_dialog, "destroy",
85 G_CALLBACK (error_dialog_destroyed), parent);
87 gtk_window_set_screen (GTK_WINDOW (error_dialog),
88 gtk_widget_get_screen (parent));
90 g_free (error_message);
93 static void
94 show_uri (const char *uri,
95 GtkWidget *widget)
97 GError *error;
98 EelAppLaunchContext *launch_context;
100 launch_context = eel_app_launch_context_new ();
101 eel_app_launch_context_set_screen (launch_context,
102 gtk_widget_get_screen (widget));
104 error = NULL;
105 g_app_info_launch_default_for_uri (uri,
106 G_APP_LAUNCH_CONTEXT (launch_context),
107 &error);
109 g_object_unref (launch_context);
111 if (error) {
112 display_error_dialog (error, uri, widget);
113 g_error_free (error);
114 } else {
115 /* everything is OK, destroy the main dialog and quit */
116 gtk_widget_destroy (widget);
120 static void
121 mount_enclosing_ready_cb (GFile *location,
122 GAsyncResult *res,
123 GtkWidget *widget)
125 char *uri;
126 GError *error = NULL;
128 g_file_mount_enclosing_volume_finish (location,
129 res, &error);
130 uri = g_file_get_uri (location);
131 if (error) {
132 display_error_dialog (error, uri, widget);
133 } else {
134 /* volume is mounted, show it */
135 show_uri (uri, widget);
136 g_object_unref (location);
138 g_free (uri);
141 void
142 nautilus_connect_server_dialog_present_uri (NautilusApplication *application,
143 GFile *location,
144 GtkWidget *widget)
146 GMountOperation *op;
148 op = eel_mount_operation_new (GTK_WINDOW (widget));
149 g_file_mount_enclosing_volume (location,
150 0, op,
151 NULL,
152 (GAsyncReadyCallback) mount_enclosing_ready_cb,
153 widget);
157 main (int argc, char *argv[])
159 GnomeProgram *program;
160 GtkWidget *dialog;
161 GOptionContext *context;
162 const char **args;
163 GFile *location;
164 const GOptionEntry options[] = {
165 { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_STRING_ARRAY, &args, NULL, N_("[URI]") },
166 { NULL }
169 bindtextdomain (GETTEXT_PACKAGE, GNOMELOCALEDIR);
170 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
171 textdomain (GETTEXT_PACKAGE);
173 args = NULL;
174 /* Translators: This is the --help description gor the connect to server app,
175 the initial newlines are between the command line arg and the description */
176 context = g_option_context_new (N_("\n\nAdd connect to server mount"));
177 g_option_context_add_main_entries (context, options, GETTEXT_PACKAGE);
179 g_option_context_set_translation_domain(context, GETTEXT_PACKAGE);
181 program = gnome_program_init ("nautilus-connect-server", VERSION,
182 LIBGNOMEUI_MODULE, argc, argv,
183 GNOME_PARAM_GOPTION_CONTEXT, context,
184 NULL);
186 gnome_authentication_manager_init ();
188 eel_preferences_init ("/apps/nautilus");
190 gtk_window_set_default_icon_name (NAUTILUS_ICON_FOLDER);
193 /* command line arguments, null terminated array */
194 location = NULL;
195 if (args) {
196 location = g_file_new_for_commandline_arg (*args);
199 dialog = nautilus_connect_server_dialog_new (NULL, location);
201 if (location) {
202 g_object_unref (location);
205 open_dialogs = 0;
206 g_signal_connect (dialog, "destroy",
207 G_CALLBACK (main_dialog_destroyed), NULL);
209 gtk_widget_show (dialog);
211 gtk_main ();
213 return 0;