nautilus-connect-server-dialog-main.c 5.28 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 31 32 33 34 35
/* -*- 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>

#include <gtk/gtkmain.h>
#include <gtk/gtkwidget.h>

#include <libgnome/gnome-program.h>
#include <libgnomeui/gnome-ui-init.h>
36
#include <libgnomeui/gnome-authentication-manager.h>
37

38
#include <eel/eel-app-launch-context.h>
39 40
#include <eel/eel-preferences.h>
#include <eel/eel-stock-dialogs.h>
41
#include <eel/eel-mount-operation.h>
42

Luca Ferretti's avatar
Luca Ferretti committed
43 44
#include <libnautilus-private/nautilus-icon-names.h>

45 46 47 48 49 50
#include "nautilus-window.h"
#include "nautilus-connect-server-dialog.h"

static int open_dialogs;

static void
51 52 53 54 55 56 57 58 59 60 61 62
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)
63 64
{
	if (--open_dialogs <= 0)
65
		gtk_widget_destroy (main_dialog);
66 67 68
}

static void
69 70 71
display_error_dialog (GError *error, 
		      const char *uri,
		      GtkWidget *parent)
72 73
{
	GtkDialog *error_dialog;
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
	char *error_message;

	error_message = g_strdup_printf (_("Can't display location \"%s\""),
					 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)
{
97
	GError    *error;
98 99 100
	EelAppLaunchContext *launch_context;

	launch_context = eel_app_launch_context_new ();
101 102
	eel_app_launch_context_set_screen (launch_context,
					   gtk_widget_get_screen (widget));
103 104

	error = NULL;
105 106 107 108 109
	g_app_info_launch_default_for_uri (uri,
					   G_APP_LAUNCH_CONTEXT (launch_context),
					   &error);

	g_object_unref (launch_context);
110 111

	if (error) {
112
		display_error_dialog (error, uri, widget);
113
		g_error_free (error);
114 115 116
	} else {
		/* everything is OK, destroy the main dialog and quit */
		gtk_widget_destroy (widget);
117 118 119
	}
}

120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
static void
mount_enclosing_ready_cb (GFile *location,
			  GAsyncResult *res,
			  GtkWidget *widget)
{
	char *uri;
	GError *error = NULL;
	
	g_file_mount_enclosing_volume_finish (location,
					      res, &error);
	uri = g_file_get_uri (location);
	if (error) {
		display_error_dialog (error, uri, widget);
	} else {
		/* volume is mounted, show it */
		show_uri (uri, widget);
		g_object_unref (location);
	}
	g_free (uri);
}

141 142
void
nautilus_connect_server_dialog_present_uri (NautilusApplication *application,
143
					    GFile *location,
144 145
					    GtkWidget *widget)
{
146 147 148 149 150 151 152 153
	GMountOperation *op;

	op = eel_mount_operation_new (GTK_WINDOW (widget));
	g_file_mount_enclosing_volume (location,
				       0, op,
				       NULL,
				       (GAsyncReadyCallback) mount_enclosing_ready_cb,
				       widget);
154 155 156 157 158
}

int
main (int argc, char *argv[])
{
159
	GnomeProgram *program;
160
	GtkWidget *dialog;
Alexander Larsson's avatar
Alexander Larsson committed
161
	GOptionContext *context;
162
	const char **args;
163
	GFile *location;
Alexander Larsson's avatar
Alexander Larsson committed
164 165 166 167
	const GOptionEntry options[] = {
		{ G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_STRING_ARRAY, &args, NULL,  N_("[URI]") },
		{ NULL }
	};
168 169 170 171 172

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

Alexander Larsson's avatar
Alexander Larsson committed
173
	args = NULL;
174 175
	/* Translators: This is the --help description gor the connect to server app,
	   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"));
Alexander Larsson's avatar
Alexander Larsson committed
177 178
	g_option_context_add_main_entries (context, options, GETTEXT_PACKAGE);

179 180
	g_option_context_set_translation_domain(context, GETTEXT_PACKAGE);

181 182
	program = gnome_program_init ("nautilus-connect-server", VERSION,
				      LIBGNOMEUI_MODULE, argc, argv,
Alexander Larsson's avatar
Alexander Larsson committed
183
				      GNOME_PARAM_GOPTION_CONTEXT, context,
184
				      NULL);
185

186 187
	gnome_authentication_manager_init ();

188 189
	eel_preferences_init ("/apps/nautilus");

Luca Ferretti's avatar
Luca Ferretti committed
190
	gtk_window_set_default_icon_name (NAUTILUS_ICON_FOLDER);
Dennis Cranston's avatar
Dennis Cranston committed
191

192 193

	/* command line arguments, null terminated array */
194 195 196 197 198 199 200 201 202 203
	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);
	}
204

205
	open_dialogs = 0;
206
	g_signal_connect (dialog, "destroy",
207
			  G_CALLBACK (main_dialog_destroyed), NULL);
208 209 210 211 212 213 214

	gtk_widget_show (dialog);

	gtk_main ();
	
	return 0;
}