1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2
3 /* nemo-open-with-main.c - Start the "Open with" dialog.
4 * Nemo
5 *
6 * Copyright (C) 2005 Vincent Untz
7 *
8 * Nemo 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.
12 *
13 * Nemo 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.
17 *
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., 51 Franklin Street - Suite 500,
21 * Boston, MA 02110-1335, USA.
22 *
23 * Authors:
24 * Vincent Untz <vincent@vuntz.net>
25 * Cosimo Cecchi <cosimoc@gnome.org>
26 */
27
28 #include <config.h>
29
30 #include <glib/gi18n.h>
31
32 #include <gtk/gtk.h>
33 #include <gdk/gdk.h>
34
35 #include <stdlib.h>
36
37 #include <eel/eel-stock-dialogs.h>
38 #include <eel/eel-string.h>
39
40 #include <libnemo-private/nemo-icon-names.h>
41 #include <libnemo-private/nemo-global-preferences.h>
42 #include <libnemo-private/nemo-mime-application-chooser.h>
43 #include <libnemo-private/nemo-program-choosing.h>
44 #include <libnemo-private/nemo-file-utilities.h>
45
46 static void
main_dialog_destroyed(GtkWidget * widget,gpointer user_data)47 main_dialog_destroyed (GtkWidget *widget,
48 gpointer user_data)
49 {
50 /* this only happens when user clicks "cancel"
51 * on the main dialog or when we are all done.
52 */
53 gtk_main_quit ();
54 }
55
56 static void
app_chooser_dialog_response_cb(GtkDialog * dialog,gint response_id,gpointer user_data)57 app_chooser_dialog_response_cb (GtkDialog *dialog,
58 gint response_id,
59 gpointer user_data)
60 {
61
62 NemoFile *file;
63 GAppInfo *info;
64 GList files;
65
66 if (response_id != GTK_RESPONSE_OK) {
67 gtk_widget_destroy (GTK_WIDGET (dialog));
68 return;
69 }
70
71 NemoMimeApplicationChooser *chooser = NEMO_MIME_APPLICATION_CHOOSER (user_data);
72
73 info = nemo_mime_application_chooser_get_info (chooser);
74 file = nemo_file_get_by_uri (nemo_mime_application_chooser_get_uri (chooser));
75
76 files.next = NULL;
77 files.prev = NULL;
78 files.data = file;
79 nemo_launch_application (info, &files, NULL);
80
81 gtk_widget_destroy (GTK_WIDGET (dialog));
82 g_object_unref (info);
83 }
84
85 int
main(int argc,char * argv[])86 main (int argc, char *argv[])
87 {
88 GtkWidget *dialog;
89 GtkWidget *ok_button;
90
91 GOptionContext *context;
92 GError *error;
93 const GOptionEntry options[] = {
94 { NULL }
95 };
96
97 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
98 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
99 textdomain (GETTEXT_PACKAGE);
100
101 error = NULL;
102 /* Translators: This is the --help description for the open-with app,
103 the initial newlines are between the command line arg and the description */
104 context = g_option_context_new (N_("\n\nShow an open-with dialog given a uri, "
105 "to allow the user to change the default mimetype handler."));
106 g_option_context_set_translation_domain (context, GETTEXT_PACKAGE);
107 g_option_context_add_main_entries (context, options, GETTEXT_PACKAGE);
108 g_option_context_add_group (context, gtk_get_option_group (TRUE));
109
110 if (!g_option_context_parse (context, &argc, &argv, &error)) {
111 g_critical ("Failed to parse arguments: %s", error->message);
112 g_error_free (error);
113 g_option_context_free (context);
114 exit (1);
115 }
116
117 g_option_context_free (context);
118
119 if (argc != 2) {
120 g_critical ("uri required");
121 exit(1);
122 }
123
124 nemo_global_preferences_init ();
125
126 const gchar *uri;
127 gchar *mime_type, *basename;
128
129 uri = argv[1];
130
131 GFile *file = g_file_new_for_uri (uri);
132
133 GFileInfo *info = g_file_query_info (file,
134 G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE "," G_FILE_ATTRIBUTE_STANDARD_SIZE,
135 G_FILE_QUERY_INFO_NONE,
136 NULL, NULL);
137
138 if (info == NULL) {
139 g_critical ("Unable to query mimetype from given uri");
140 g_object_unref (info);
141 g_object_unref (file);
142 exit(1);
143 }
144
145 basename = g_file_get_basename (file);
146
147 mime_type = nemo_get_best_guess_file_mimetype (basename, info, g_file_info_get_size (info));
148
149 g_clear_pointer (&basename, g_free);
150
151 dialog = gtk_dialog_new_with_buttons (_("Open with"),
152 NULL,
153 GTK_DIALOG_DESTROY_WITH_PARENT,
154 GTK_STOCK_CANCEL,
155 GTK_RESPONSE_CANCEL,
156 NULL);
157 ok_button = gtk_dialog_add_button (GTK_DIALOG (dialog),
158 GTK_STOCK_OK,
159 GTK_RESPONSE_OK);
160
161 gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK);
162
163 GtkWidget *chooser = nemo_mime_application_chooser_new (uri, NULL, mime_type, ok_button);
164
165 eel_ref_str_unref (mime_type);
166
167 GtkWidget *content = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
168
169 gtk_box_pack_start (GTK_BOX (content), chooser, TRUE, TRUE, 0);
170
171 gtk_widget_show_all (dialog);
172
173 g_signal_connect_object (dialog, "response",
174 G_CALLBACK (app_chooser_dialog_response_cb),
175 chooser, 0);
176
177 g_signal_connect (dialog, "destroy",
178 G_CALLBACK (main_dialog_destroyed), NULL);
179
180 gtk_widget_show (dialog);
181
182 gtk_main ();
183
184 return 0;
185 }
186