nautilus-connect-server-dialog-main.c 5.42 KB
Newer Older
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
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */

/* nautilus-connect-server-main.c - Start the "Connect to Server" dialog.
 * Nautilus
 *
 * Copyright (C) 2005 Vincent Untz
 *
 * Nautilus 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.
 *
 * Nautilus 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; see the file COPYING.  If not,
 * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * Authors:
 *   Vincent Untz <vincent@vuntz.net>
 */

#include <config.h>

#include <glib/gi18n.h>

31
#include <gtk/gtk.h>
32
#include <gdk/gdk.h>
33

34
#include <stdlib.h>
35 36 37 38

#include <eel/eel-preferences.h>
#include <eel/eel-stock-dialogs.h>

39 40
#include <libnautilus-private/nautilus-icon-names.h>

41 42 43 44 45 46
#include "nautilus-window.h"
#include "nautilus-connect-server-dialog.h"

static int open_dialogs;

static void
47 48 49 50 51 52 53 54 55 56 57 58
main_dialog_destroyed (GtkWidget *widget,
		       gpointer   user_data)
{
	/* this only happens when user clicks "cancel"
	 * on the main dialog or when we are all done.
	 */
	gtk_main_quit ();
}

static void
error_dialog_destroyed (GtkWidget *widget,
			GtkWidget *main_dialog)
59 60
{
	if (--open_dialogs <= 0)
61
		gtk_widget_destroy (main_dialog);
62 63 64
}

static void
65 66 67
display_error_dialog (GError *error, 
		      const char *uri,
		      GtkWidget *parent)
68 69
{
	GtkDialog *error_dialog;
70 71
	char *error_message;

72
	error_message = g_strdup_printf (_("Cannot display location \"%s\""),
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
					 uri);
	error_dialog = eel_show_error_dialog (error_message,
					      error->message,
					      NULL);

	open_dialogs++;

	g_signal_connect (error_dialog, "destroy",
			  G_CALLBACK (error_dialog_destroyed), parent);

	gtk_window_set_screen (GTK_WINDOW (error_dialog),
			       gtk_widget_get_screen (parent));

	g_free (error_message);
}

static void
show_uri (const char *uri,
	  GtkWidget  *widget)
{
93
	GError    *error;
94
	GdkAppLaunchContext *launch_context;
95

96 97
	launch_context = gdk_app_launch_context_new ();
	gdk_app_launch_context_set_screen (launch_context,
98
					   gtk_widget_get_screen (widget));
99 100

	error = NULL;
101 102 103 104 105
	g_app_info_launch_default_for_uri (uri,
					   G_APP_LAUNCH_CONTEXT (launch_context),
					   &error);

	g_object_unref (launch_context);
106 107

	if (error) {
108
		display_error_dialog (error, uri, widget);
109
		g_error_free (error);
110 111 112
	} else {
		/* everything is OK, destroy the main dialog and quit */
		gtk_widget_destroy (widget);
113 114 115
	}
}

116 117 118 119 120 121
static void
mount_enclosing_ready_cb (GFile *location,
			  GAsyncResult *res,
			  GtkWidget *widget)
{
	char *uri;
122
	gboolean success;
123
	GError *error = NULL;
124

125
	uri = g_file_get_uri (location);
126 127 128 129 130
	success = g_file_mount_enclosing_volume_finish (location,
							res, &error);

	if (success ||
	    g_error_matches (error, G_IO_ERROR, G_IO_ERROR_ALREADY_MOUNTED)) {
131 132
		/* volume is mounted, show it */
		show_uri (uri, widget);
133 134 135 136 137 138
	} else {
		display_error_dialog (error, uri, widget);
	}

	if (error) {
		g_error_free (error);
139
	}
140 141

	g_object_unref (location);
142 143 144
	g_free (uri);
}

145 146
void
nautilus_connect_server_dialog_present_uri (NautilusApplication *application,
147
					    GFile *location,
148 149
					    GtkWidget *widget)
{
150 151
	GMountOperation *op;

152
	op = gtk_mount_operation_new (GTK_WINDOW (widget));
153
	g_mount_operation_set_password_save (op, G_PASSWORD_SAVE_FOR_SESSION);
154 155 156 157 158
	g_file_mount_enclosing_volume (location,
				       0, op,
				       NULL,
				       (GAsyncReadyCallback) mount_enclosing_ready_cb,
				       widget);
159
	g_object_unref (op);
160 161 162 163 164 165
}

int
main (int argc, char *argv[])
{
	GtkWidget *dialog;
Alexander Larsson's avatar
Alexander Larsson committed
166
	GOptionContext *context;
167
	const char **args;
168
	GFile *location;
169
	GError *error;
Alexander Larsson's avatar
Alexander Larsson committed
170 171 172 173
	const GOptionEntry options[] = {
		{ G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_STRING_ARRAY, &args, NULL,  N_("[URI]") },
		{ NULL }
	};
174 175 176 177 178

	bindtextdomain (GETTEXT_PACKAGE, GNOMELOCALEDIR);
	bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
	textdomain (GETTEXT_PACKAGE);

Alexander Larsson's avatar
Alexander Larsson committed
179
	args = NULL;
180 181
	error = NULL;
	/* Translators: This is the --help description for the connect to server app,
182
	   the initial newlines are between the command line arg and the description */
183
	context = g_option_context_new (N_("\n\nAdd connect to server mount"));
Alexander Larsson's avatar
Alexander Larsson committed
184 185
	g_option_context_add_main_entries (context, options, GETTEXT_PACKAGE);

186 187
	g_option_context_set_translation_domain (context, GETTEXT_PACKAGE);
	g_option_context_add_group (context, gtk_get_option_group (TRUE));
188

189 190 191 192 193 194
	if (!g_option_context_parse (context, &argc, &argv, &error)) {
		g_critical ("Failed to parse arguments: %s", error->message);
		g_error_free (error);
		g_option_context_free (context);
		exit (1);
	}
195

196
	g_option_context_free (context);
197

198 199
	eel_preferences_init ("/apps/nautilus");

200
	gtk_window_set_default_icon_name (NAUTILUS_ICON_FOLDER);
Dennis Cranston's avatar
Dennis Cranston committed
201

202 203

	/* command line arguments, null terminated array */
204 205 206 207 208 209 210 211 212 213
	location = NULL;
	if (args) {
		location = g_file_new_for_commandline_arg (*args);
	}

	dialog = nautilus_connect_server_dialog_new (NULL, location);

	if (location) {
		g_object_unref (location);
	}
214

215
	open_dialogs = 0;
216
	g_signal_connect (dialog, "destroy",
217
			  G_CALLBACK (main_dialog_destroyed), NULL);
218 219 220 221 222 223 224

	gtk_widget_show (dialog);

	gtk_main ();
	
	return 0;
}