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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
|
/***
Copyright (c) 2000 Eazel, Inc.
Copyright (c) 2011 ammonkey <am.monkeyd@gmail.com>
Copyright (c) 2013-2016 elementary LLC (http://launchpad.net/elementary)
This program is free software: you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License version 3, as published
by the Free Software Foundation.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranties of
MERCHANTABILITY, SATISFACTORY QUALITY, 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. If not, see <http://www.gnu.org/licenses/>.
Authors: Maciej Stachowiak <mjs@eazel.com>
ammonkey <am.monkeyd@gmail.com>
Julián Unrrein <junrrein@gmail.com>
***/
public class Marlin.MimeActions {
public static AppInfo? get_default_application_for_file (GOF.File file) {
AppInfo app = file.get_default_handler ();
if (app == null) {
string uri_scheme = file.location.get_uri_scheme ();
if (uri_scheme != null)
app = AppInfo.get_default_for_uri_scheme (uri_scheme);
}
return app;
}
public static AppInfo? get_default_application_for_files (GLib.List<unowned GOF.File> files) {
assert (files != null);
/* Need to make a new list to avoid corrupting the selection */
GLib.List<unowned GOF.File> sorted_files = null;
files.@foreach ((file) => {
sorted_files.prepend (file);
});
sorted_files.sort (file_compare_by_mime_type);
AppInfo? app = null;
GOF.File? previous_file = null;
foreach (GOF.File file in sorted_files) {
if (previous_file == null) {
app = get_default_application_for_file (file);
previous_file = file;
continue;
}
if (file_compare_by_mime_type (file, previous_file) == 0 &&
file_compare_by_parent_uri (file, previous_file) == 0)
continue;
var one_app = get_default_application_for_file (file);
if (one_app == null || (app != null) && !app.equal (one_app)) {
app = null;
break;
}
if (app == null)
app = one_app;
previous_file = file;
}
return app;
}
public static List<AppInfo> get_applications_for_file (GOF.File file) {
List<AppInfo> result = null;
string? type = file.get_ftype ();
if (type == null)
return result;
result = AppInfo.get_all_for_type (type);
string uri_scheme = file.location.get_uri_scheme ();
if (uri_scheme != null) {
var uri_handler = AppInfo.get_default_for_uri_scheme (uri_scheme);
if (uri_handler != null)
result.prepend (uri_handler);
}
if (!file_has_local_path (file))
filter_non_uri_apps (result);
result.sort (application_compare_by_name);
return result;
}
public static List<AppInfo> get_applications_for_folder (GOF.File file) {
List<AppInfo> result = AppInfo.get_all_for_type (ContentType.get_mime_type ("inode/directory"));
string uri_scheme = file.location.get_uri_scheme ();
if (uri_scheme != null) {
var uri_handler = AppInfo.get_default_for_uri_scheme (uri_scheme);
if (uri_handler != null)
result.prepend (uri_handler);
}
if (!file_has_local_path (file))
result = filter_non_uri_apps (result);
result.sort (application_compare_by_name);
return result;
}
public static List<AppInfo>? get_applications_for_files (GLib.List<unowned GOF.File> files) {
assert (files != null);
/* Need to make a new list to avoid corrupting the selection */
GLib.List<unowned GOF.File> sorted_files = null;
files.@foreach ((file) => {
sorted_files.prepend (file);
});
sorted_files.sort (file_compare_by_mime_type);
List<AppInfo> result = null;
unowned GOF.File previous_file = null;
foreach (unowned GOF.File file in sorted_files) {
if (previous_file == null) {
result = get_applications_for_file (file);
previous_file = file;
continue;
}
if (file_compare_by_mime_type (file, previous_file) == 0 &&
file_compare_by_parent_uri (file, previous_file) == 0)
continue;
List<AppInfo> one_result = get_applications_for_file (file);
one_result.sort (application_compare_by_id);
if (result != null)
result = intersect_application_lists (result, one_result);
else
result = one_result.copy ();
if (result == null)
return null;
previous_file = file;
}
result.sort (application_compare_by_name);
return result;
}
private static bool file_has_local_path (GOF.File file) {
if (file.location.is_native ()) {
return true;
} else {
var path = file.location.get_path ();
return path != null;
}
}
private static int file_compare_by_mime_type (GOF.File a, GOF.File b) {
return strcmp (a.get_ftype (), b.get_ftype ());
}
private static string? gof_get_parent_uri (GOF.File file) {
return file.directory != null ? file.directory.get_uri () : null;
}
private static int file_compare_by_parent_uri (GOF.File a, GOF.File b) {
return strcmp (gof_get_parent_uri (a), gof_get_parent_uri (b));
}
private static int application_compare_by_name (AppInfo a, AppInfo b) {
return a.get_display_name ().collate (b.get_display_name ());
}
private static int application_compare_by_id (AppInfo a, AppInfo b) {
return strcmp (a.get_id (), b.get_id ());
}
private static List<AppInfo> filter_non_uri_apps (List<AppInfo> apps) {
List<AppInfo> uri_apps = null;
foreach (var app in apps) {
if (app.supports_uris ()) {
uri_apps.append (app);
}
}
return uri_apps;
}
private static List<AppInfo> intersect_application_lists (List<AppInfo> a, List<AppInfo> b) {
List<AppInfo> result = null;
/* This is going to look ugly, but doing the same thing using
"foreach" would take m*n operations. */
unowned List<AppInfo> iterator_a = a;
unowned List<AppInfo> iterator_b = b;
while (iterator_a != null && iterator_b != null) {
AppInfo app_a = iterator_a.data;
AppInfo app_b = iterator_b.data;
int cmp = application_compare_by_id (app_a, app_b);
if (cmp > 0) {
iterator_b = iterator_b.next;
} else if (cmp < 0) {
iterator_a = iterator_a.next;
} else {
result.append (app_a);
iterator_a = iterator_a.next;
iterator_b = iterator_b.next;
}
}
return result;
}
public static AppInfo? get_default_application_for_glib_file (GLib.File file) {
return get_default_application_for_file (GOF.File.@get (file));
}
public static void open_glib_file_request (GLib.File file_to_open, Gtk.Widget parent, AppInfo? app = null) {
if (app == null) {
var choice = choose_app_for_glib_file (file_to_open, parent);
if (choice != null) {
launch_glib_file_with_app (file_to_open, parent, choice);
}
} else {
launch_glib_file_with_app (file_to_open, parent, app);
}
}
public static AppInfo? choose_app_for_glib_file (GLib.File file_to_open, Gtk.Widget parent) {
Gtk.Widget? toplevel = parent != null ? parent.get_toplevel () : null;
var chooser = new PF.ChooseAppDialog (toplevel, file_to_open);
return chooser.get_app_info ();
}
private static void launch_glib_file_with_app (GLib.File file_to_open, Gtk.Widget parent, AppInfo app) {
var goffile = GOF.File.get (file_to_open);
goffile.launch (parent.get_screen (), app);
}
}
|