nautilus-3.6.3/src/nautilus-connect-server.c

No issues found

  1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*-
  2  *
  3  * Copyright (C) 2005 Vincent Untz
  4  * Copyright (C) 2012 Red Hat, Inc.
  5  *
  6  * Nautilus is free software; you can redistribute it and/or
  7  * modify it under the terms of the GNU General Public License as
  8  * published by the Free Software Foundation; either version 2 of the
  9  * License, or (at your option) any later version.
 10  *
 11  * Nautilus is distributed in the hope that it will be useful,
 12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 14  * General Public License for more details.
 15  *
 16  * You should have received a copy of the GNU General Public License
 17  * along with this program; if not, write to the Free Software
 18  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 19  *
 20  */
 21 
 22 #include <config.h>
 23 #include <stdlib.h>
 24 #include <glib/gi18n.h>
 25 #include <gtk/gtk.h>
 26 
 27 #include "nautilus-connect-server-dialog.h"
 28 #include <eel/eel-stock-dialogs.h>
 29 
 30 static gboolean just_print_uri = FALSE;
 31 
 32 static void
 33 mount_ready_callback (GObject *source_object,
 34 		      GAsyncResult *res,
 35 		      gpointer user_data)
 36 {
 37 	GError *error;
 38 	GFile *location;
 39 	gboolean show = TRUE;
 40 	GtkWidget *dialog = user_data;
 41 
 42 	location = G_FILE (source_object);
 43 
 44 	error = NULL;
 45 	if (!g_file_mount_enclosing_volume_finish (location, res, &error)) {
 46 		show = FALSE;
 47 		if (error->domain == G_IO_ERROR && error->code == G_IO_ERROR_ALREADY_MOUNTED) {
 48 			show = TRUE;
 49 		} else if (error->domain != G_IO_ERROR ||
 50 			   (error->code != G_IO_ERROR_CANCELLED &&
 51 			    error->code != G_IO_ERROR_FAILED_HANDLED)) {
 52 			/* if it wasn't cancelled show a dialog */
 53 			eel_show_error_dialog (_("Unable to access location"), error->message, GTK_WINDOW (dialog));
 54 		}
 55 		g_clear_error (&error);
 56 	}
 57 
 58 	if (show) {
 59 		char *uri;
 60 		uri = g_file_get_uri (location);
 61 		if (just_print_uri) {
 62 			g_print ("%s\n", uri);
 63 		} else {
 64 			GdkAppLaunchContext *launch_context;
 65 
 66 			launch_context = gdk_display_get_app_launch_context (gtk_widget_get_display (dialog));
 67 			gdk_app_launch_context_set_screen (launch_context,
 68 							   gtk_widget_get_screen (dialog));
 69 			error = NULL;
 70 			g_app_info_launch_default_for_uri (uri,
 71 							   G_APP_LAUNCH_CONTEXT (launch_context),
 72 							   &error);
 73 			if (error != NULL) {
 74 				eel_show_error_dialog (_("Unable to display location"), error->message, GTK_WINDOW (dialog));
 75 				g_clear_error (&error);
 76 			}
 77 			g_object_unref (launch_context);
 78 		}
 79 		g_free (uri);
 80 	}
 81 	gtk_widget_destroy (GTK_WIDGET (dialog));
 82 }
 83 
 84 static void
 85 mount_location (GtkWidget *dialog,
 86 		GFile     *location)
 87 {
 88 	GMountOperation *mount_op;
 89 
 90 	mount_op = gtk_mount_operation_new (GTK_WINDOW (dialog));
 91 	g_mount_operation_set_password_save (mount_op, G_PASSWORD_SAVE_FOR_SESSION);
 92 	g_file_mount_enclosing_volume (location,
 93 				       0,
 94 				       mount_op,
 95 				       NULL,
 96 				       mount_ready_callback,
 97 				       dialog);
 98 	/* unref mount_op here - g_file_mount_enclosing_volume() does ref for itself */
 99 	g_object_unref (mount_op);
100 }
101 
102 static void
103 on_connect_server_destroy (GtkWidget *widget,
104 			   gpointer   user_data)
105 {
106 	/* this only happens when user clicks "cancel"
107 	 * on the main dialog or when we are all done.
108 	 */
109 	gtk_main_quit ();
110 }
111 
112 static void
113 on_connect_server_response (GtkDialog      *dialog,
114 			    int             response,
115 			    gpointer        user_data)
116 {
117 	if (response == GTK_RESPONSE_OK) {
118 		GFile *location;
119 
120 		location = nautilus_connect_server_dialog_get_location (NAUTILUS_CONNECT_SERVER_DIALOG (dialog));
121 		if (location != NULL) {
122 			mount_location (GTK_WIDGET (dialog), location);
123 			g_object_unref (location);
124 		} else {
125 			g_warning ("Unable to get remote server location");
126 			gtk_widget_destroy (GTK_WIDGET (dialog));
127 		}
128 	} else {
129 		gtk_widget_destroy (GTK_WIDGET (dialog));
130 	}
131 }
132 
133 int
134 main (int argc, char *argv[])
135 {
136 	GtkWidget *dialog;
137 	GOptionContext *context;
138 	GError *error;
139 	const GOptionEntry options[] = {
140 		{ "print-uri", 0, 0, G_OPTION_ARG_NONE, &just_print_uri, N_("Print but do not open the URI"), NULL },
141 		{ NULL }
142 	};
143 
144 	bindtextdomain (GETTEXT_PACKAGE, GNOMELOCALEDIR);
145 	bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
146 	textdomain (GETTEXT_PACKAGE);
147 
148 	g_set_prgname ("nautilus-connect-server");
149 
150 	/* Translators: This is the --help description for the connect to server app,
151 	   the initial newlines are between the command line arg and the description */
152 	context = g_option_context_new (N_("\n\nAdd connect to server mount"));
153 	g_option_context_set_translation_domain (context, GETTEXT_PACKAGE);
154 	g_option_context_add_main_entries (context, options, GETTEXT_PACKAGE);
155 	g_option_context_add_group (context, gtk_get_option_group (TRUE));
156 
157 	error = NULL;
158 	if (!g_option_context_parse (context, &argc, &argv, &error)) {
159 		g_critical ("Failed to parse arguments: %s", error->message);
160 		g_error_free (error);
161 		g_option_context_free (context);
162 		exit (1);
163 	}
164 
165 	g_option_context_free (context);
166 
167 	dialog = nautilus_connect_server_dialog_new (NULL);
168 	nautilus_connect_server_dialog_set_show_browse (NAUTILUS_CONNECT_SERVER_DIALOG (dialog), FALSE);
169 
170 	gtk_window_set_default_icon_name ("folder-remote-symbolic");
171 
172 	g_signal_connect (dialog, "response",
173 			  G_CALLBACK (on_connect_server_response),
174 			  NULL);
175 	g_signal_connect (dialog, "destroy",
176 			  G_CALLBACK (on_connect_server_destroy),
177 			  NULL);
178 
179 	gtk_widget_show (dialog);
180 
181 	gtk_main ();
182 
183 	return 0;
184 }