36
36
/* sort the structures based on the file's display_name */
38
38
vfs_sort_array (const VfsFileInfo **i1, const VfsFileInfo **i2) {
39
return g_utf8_collate ((gchar *)(*i1)->display_name, (gchar *)(*i2)->display_name);
39
return g_utf8_collate ((gchar *)(*i1)->display_name, (gchar *)(*i2)->display_name);
41
41
/******************************************************************************/
43
43
vfs_file_exists (const gchar *file_name) {
44
return g_file_test (file_name, G_FILE_TEST_EXISTS);
44
return g_file_test (file_name, G_FILE_TEST_EXISTS);
46
46
/******************************************************************************/
48
48
vfs_file_is_executable (const gchar *file_name) {
49
if (DEBUG) g_printf ("In %s\n", __FUNCTION__);
51
GFile* file = g_file_new_for_path (file_name);
52
GFileInfo* file_info = g_file_query_info (file,
53
/*G_FILE_ATTRIBUTE_ACCESS_CAN_EXECUTE ,*/
54
"standard::content-type,"
55
"access::can-execute",
60
gboolean ret = g_file_info_get_attribute_boolean (file_info,
61
G_FILE_ATTRIBUTE_ACCESS_CAN_EXECUTE) &&
62
g_content_type_can_be_executable (g_file_info_get_content_type (file_info));
64
g_object_unref (file_info);
65
g_object_unref (file);
49
if (DEBUG) g_printf ("In %s\n", __FUNCTION__);
51
GFile* file = g_file_new_for_path (file_name);
52
GFileInfo* file_info = g_file_query_info (file,
53
/*G_FILE_ATTRIBUTE_ACCESS_CAN_EXECUTE ,*/
54
"standard::content-type,"
55
"access::can-execute",
60
gboolean ret = g_file_info_get_attribute_boolean (file_info,
61
G_FILE_ATTRIBUTE_ACCESS_CAN_EXECUTE) &&
62
g_content_type_can_be_executable (g_file_info_get_content_type (file_info));
64
g_object_unref (file_info);
65
g_object_unref (file);
68
68
/******************************************************************************/
70
70
vfs_file_is_directory (const gchar *file_name) {
71
if (DEBUG) g_printf ("In %s\n", __FUNCTION__);
73
GFile* file = g_file_new_for_path (file_name);
74
GFileInfo* file_info = g_file_query_info (file,
75
G_FILE_ATTRIBUTE_STANDARD_TYPE,
80
gboolean ret = (g_file_info_get_file_type (file_info) == G_FILE_TYPE_DIRECTORY);
81
g_object_unref (file_info);
82
g_object_unref (file);
71
if (DEBUG) g_printf ("In %s\n", __FUNCTION__);
73
GFile* file = g_file_new_for_path (file_name);
74
GFileInfo* file_info = g_file_query_info (file,
75
G_FILE_ATTRIBUTE_STANDARD_TYPE,
80
gboolean ret = (g_file_info_get_file_type (file_info) == G_FILE_TYPE_DIRECTORY);
81
g_object_unref (file_info);
82
g_object_unref (file);
85
85
/******************************************************************************/
87
87
vfs_file_is_desktop (const gchar *file_name) {
88
if (DEBUG) g_printf ("In %s\n", __FUNCTION__);
88
if (DEBUG) g_printf ("In %s\n", __FUNCTION__);
90
GDesktopAppInfo *app_info = g_desktop_app_info_new_from_filename (file_name);
91
gboolean ret = !(app_info == NULL);
92
app_info ? g_object_unref (app_info) : NULL;
90
GDesktopAppInfo *app_info = g_desktop_app_info_new_from_filename (file_name);
91
gboolean ret = !(app_info == NULL);
92
app_info ? g_object_unref (app_info) : NULL;
95
95
/******************************************************************************/
96
96
/* Gets the executable file name of the application associated with the passed
97
97
* file. The caller must free the returned value. */
99
99
vfs_get_default_mime_application (const gchar *file_name) {
100
if (DEBUG) g_printf ("In %s\n", __FUNCTION__);
102
GFile* file = g_file_new_for_path (file_name);
103
GFileInfo* file_info = g_file_query_info (file,
104
G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
109
const gchar* content_type = g_file_info_get_content_type (file_info);
110
GAppInfo *app_info = g_app_info_get_default_for_type (content_type, FALSE);
111
gchar* exec = g_strdup (g_app_info_get_executable (app_info));
113
g_object_unref (file_info);
114
g_object_unref (file);
115
g_object_unref (app_info);
100
if (DEBUG) g_printf ("In %s\n", __FUNCTION__);
102
GFile* file = g_file_new_for_path (file_name);
103
GFileInfo* file_info = g_file_query_info (file,
104
G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
109
const gchar* content_type = g_file_info_get_content_type (file_info);
110
GAppInfo *app_info = g_app_info_get_default_for_type (content_type, FALSE);
111
gchar* exec = g_strdup (g_app_info_get_executable (app_info));
113
g_object_unref (file_info);
114
g_object_unref (file);
115
g_object_unref (app_info);
119
119
/******************************************************************************/
120
120
/* The caller must free the return value. */
122
122
vfs_get_desktop_app_name (const gchar *file_name) {
123
GDesktopAppInfo *info = g_desktop_app_info_new_from_filename (file_name);
124
gchar* ret = g_strdup (g_app_info_get_name (G_APP_INFO (info)));
125
g_object_unref (info);
123
GDesktopAppInfo *info = g_desktop_app_info_new_from_filename (file_name);
124
gchar* ret = g_strdup (g_app_info_get_name (G_APP_INFO (info)));
125
g_object_unref (info);
128
128
/******************************************************************************/
130
vfs_file_trash (gchar *file_name) {
131
if (DEBUG) g_printf ("In %s\n", __FUNCTION__);
133
GError *error = NULL;
134
GFile *file = g_file_new_for_path (file_name);
136
/* Try moving it to the trash */
137
g_file_trash (file, NULL, &error);
139
/* Let the user know if we failed. */
140
utils_gerror_ok (&error, TRUE);
130
vfs_file_trash (const gchar *file_name) {
131
if (DEBUG) g_printf ("In %s\n", __FUNCTION__);
133
GError *error = NULL;
134
GFile *file = g_file_new_for_path (file_name);
136
/* Try moving it to the trash */
137
g_file_trash (file, NULL, &error);
139
/* Let the user know if we failed. */
140
utils_gerror_ok (&error, TRUE);
143
142
/******************************************************************************/
145
144
vfs_get_icon_for_file (const gchar *file_name) {
147
GtkWidget *icon_widget = NULL;
148
GDesktopAppInfo *app_info = g_desktop_app_info_new_from_filename (file_name);
150
if (app_info != NULL) {
151
GIcon *icon = g_app_info_get_icon (G_APP_INFO (app_info));
152
icon_widget = gtk_image_new_from_gicon (icon, GTK_ICON_SIZE_MENU);
153
g_object_unref (app_info);
156
GFile *file = g_file_new_for_path (file_name);
157
GFileInfo *file_info = g_file_query_info (file,
158
G_FILE_ATTRIBUTE_STANDARD_ICON,
163
GIcon *icon = g_file_info_get_icon (file_info);
164
icon_widget = gtk_image_new_from_gicon (icon, GTK_ICON_SIZE_MENU);
165
g_object_unref (file);
166
g_object_unref (file_info);
146
GtkWidget *icon_widget = NULL;
147
GDesktopAppInfo *app_info = g_desktop_app_info_new_from_filename (file_name);
149
if (app_info != NULL) {
150
GIcon *icon = g_app_info_get_icon (G_APP_INFO (app_info));
151
icon_widget = gtk_image_new_from_gicon (icon, GTK_ICON_SIZE_MENU);
152
g_object_unref (app_info);
155
GFile *file = g_file_new_for_path (file_name);
156
GFileInfo *file_info = g_file_query_info (file,
157
G_FILE_ATTRIBUTE_STANDARD_ICON,
162
GIcon *icon = g_file_info_get_icon (file_info);
163
icon_widget = gtk_image_new_from_gicon (icon, GTK_ICON_SIZE_MENU);
164
g_object_unref (file);
165
g_object_unref (file_info);
170
169
/******************************************************************************/
172
171
vfs_get_all_mime_applications (const gchar *file_name) {
173
if (DEBUG) g_printf ("In %s\n", __FUNCTION__);
175
GFile* file = g_file_new_for_path (file_name);
176
GFileInfo* file_info = g_file_query_info (file,
177
G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
182
const gchar* content_type = g_file_info_get_content_type (file_info);
183
GList *app_infos = g_app_info_get_all_for_type (content_type);
185
g_object_unref (file_info);
186
g_object_unref (file);
172
if (DEBUG) g_printf ("In %s\n", __FUNCTION__);
174
GFile* file = g_file_new_for_path (file_name);
175
GFileInfo* file_info = g_file_query_info (file,
176
G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
181
const gchar* content_type = g_file_info_get_content_type (file_info);
182
GList *app_infos = g_app_info_get_all_for_type (content_type);
184
g_object_unref (file_info);
185
g_object_unref (file);
190
189
/******************************************************************************/
191
190
/* Takes a null-terminated array of strings at least 2 items long. The first
216
215
g_object_unref (G_OBJECT(recent));
219
/* Set the current working dir. Do we use ARG_APP or ARG_FILE to set
220
* cwd? If ARG_FILE == NULL, use ARG_APP, otherwise use ARG_FILE */
221
const gchar *cwd_root = args[ARG_FILE] == NULL ? args[ARG_APP] : args[ARG_FILE];
222
gchar *working_dir = vfs_file_is_directory (cwd_root) ? g_strdup (cwd_root) : g_path_get_dirname (cwd_root);
223
g_chdir (working_dir);
224
g_free (working_dir);
226
/* No agrs[ARG_APP] was NULL, open file with default mime handler */
227
if (args[ARG_APP] == NULL) {
228
GFile *file = g_file_new_for_path (args[ARG_FILE]);
229
gchar *uri = g_file_get_uri (file);
230
ret = g_app_info_launch_default_for_uri (uri, NULL, &error);
231
utils_gerror_ok (&error, TRUE);
233
g_object_unref (file);
236
GAppInfo *app_info = NULL;
237
if (vfs_file_is_desktop (args[ARG_APP])) {
238
app_info = G_APP_INFO (g_desktop_app_info_new_from_filename (args[ARG_APP]));
241
command_line = vfs_file_is_executable (args[ARG_APP]) ?
242
utils_escape_str (args[ARG_APP], " ", "\\ ") : g_strdup (args[ARG_APP]);
243
app_info = g_app_info_create_from_commandline (command_line, NULL, 0, &error);
244
utils_gerror_ok (&error, TRUE);
248
ret = g_app_info_launch (app_info, files, NULL, &error);
249
utils_gerror_ok (&error, TRUE);
250
g_object_unref (app_info);
253
gchar *msg = g_strdup_printf (_("Could not open \"%s\".\n"), args[ARG_FILE]);
254
utils_show_dialog (_("Error"), msg, GTK_MESSAGE_ERROR);
258
g_list_foreach (files, (GFunc)g_object_unref, NULL);
260
g_free (command_line);
262
g_chdir (g_get_home_dir());
218
/* Set the current working dir. Do we use ARG_APP or ARG_FILE to set
219
* cwd? If ARG_FILE == NULL, use ARG_APP, otherwise use ARG_FILE */
220
const gchar *cwd_root = args[ARG_FILE] == NULL ? args[ARG_APP] : args[ARG_FILE];
221
gchar *working_dir = vfs_file_is_directory (cwd_root) ? g_strdup (cwd_root) : g_path_get_dirname (cwd_root);
222
g_chdir (working_dir);
223
g_free (working_dir);
225
/* No agrs[ARG_APP] was NULL, open file with default mime handler */
226
if (args[ARG_APP] == NULL) {
227
GFile *file = g_file_new_for_path (args[ARG_FILE]);
228
gchar *uri = g_file_get_uri (file);
229
ret = g_app_info_launch_default_for_uri (uri, NULL, &error);
230
utils_gerror_ok (&error, TRUE);
232
g_object_unref (file);
235
GAppInfo *app_info = NULL;
236
if (vfs_file_is_desktop (args[ARG_APP])) {
237
app_info = G_APP_INFO (g_desktop_app_info_new_from_filename (args[ARG_APP]));
240
command_line = vfs_file_is_executable (args[ARG_APP]) ?
241
utils_escape_str (args[ARG_APP], " ", "\\ ") : g_strdup (args[ARG_APP]);
242
app_info = g_app_info_create_from_commandline (command_line, NULL, 0, &error);
243
utils_gerror_ok (&error, TRUE);
247
ret = g_app_info_launch (app_info, files, NULL, &error);
248
utils_gerror_ok (&error, TRUE);
249
g_object_unref (app_info);
252
gchar *msg = g_strdup_printf (_("Could not open \"%s\".\n"), args[ARG_FILE]);
253
utils_show_dialog (_("Error"), msg, GTK_MESSAGE_ERROR);
257
g_list_foreach (files, (GFunc)g_object_unref, NULL);
259
g_free (command_line);
261
g_chdir (g_get_home_dir());
266
265
/******************************************************************************/
268
267
vfs_file_do_default_action (const gchar *file_name) {
269
gchar **args = g_strv_new (ARGS_SIZE);
270
gchar *_file_name = g_strdup (file_name);
268
gchar **args = g_strv_new (ARGS_SIZE);
269
gchar *_file_name = g_strdup (file_name);
272
if (vfs_file_is_desktop (_file_name) ||
273
vfs_file_is_executable (_file_name)) {
274
args[ARG_APP] = _file_name;
276
/* open it with the default mime app */
278
args[ARG_FILE] = _file_name;
280
gboolean ret = vfs_launch_application ((const gchar*const*)args);
271
if (vfs_file_is_desktop (_file_name) ||
272
vfs_file_is_executable (_file_name)) {
273
args[ARG_APP] = _file_name;
275
/* open it with the default mime app */
277
args[ARG_FILE] = _file_name;
279
gboolean ret = vfs_launch_application ((const gchar*const*)args);
284
283
/******************************************************************************/
285
284
/* Gets the contents of a directory. Puts the files and directories in separate
286
285
* arrays, and sorts them both. The caller must free the return value. */
288
287
vfs_get_dir_listings (GPtrArray *files,
290
gboolean show_hidden,
292
if (DEBUG) g_printf ("In %s\n", __FUNCTION__);
294
GError *error = NULL;
296
GFile *file = g_file_new_for_path (path);
297
/* get ALL the info about the files */
298
GFileEnumerator *enumerator = g_file_enumerate_children (file,
301
"standard::is-hidden,"
303
"standard::display-name,"
304
"access::can-execute",
308
/* did we read the dir correctly? */
310
gchar *error_msg = g_strdup (error->message);
311
utils_gerror_ok (&error, FALSE);
315
GFileInfo *file_info = NULL;
316
while ((file_info = g_file_enumerator_next_file (enumerator, NULL, &error)) != NULL) {
317
/* skip the file if it's hidden and we aren't showing hidden files */
318
if (g_file_info_get_is_hidden (file_info) && !show_hidden) {
319
g_object_unref (file_info);
322
VfsFileInfo *vfs_file_info = g_new0 (VfsFileInfo ,1);
324
vfs_file_info->file_name = g_strdup_printf ("%s/%s", path, g_file_info_get_name (file_info));
326
/* get the file's human readable name, including if it's a desktop file */
329
vfs_file_info->display_name = (vfs_file_is_desktop (vfs_file_info->file_name)) ?
330
vfs_get_desktop_app_name (vfs_file_info->file_name) :
331
g_strdup (g_file_info_get_display_name (file_info));
333
vfs_file_info->is_executable = g_file_info_get_attribute_boolean (file_info,
334
G_FILE_ATTRIBUTE_ACCESS_CAN_EXECUTE) &&
335
(g_file_info_get_file_type (file_info) != G_FILE_TYPE_DIRECTORY);
337
vfs_file_info->icon = vfs_get_icon_for_file (vfs_file_info->file_name);
339
/* add it to the array */
340
if (g_file_info_get_file_type (file_info) == G_FILE_TYPE_DIRECTORY) {
341
g_ptr_array_add (dirs, (gpointer)vfs_file_info);
344
g_ptr_array_add (files, (gpointer)vfs_file_info);
346
g_object_unref (file_info);
348
g_object_unref (enumerator);
349
g_object_unref (file);
351
/* always check for errors */
353
gchar *error_msg = g_strdup (error->message);
354
utils_gerror_ok (&error, FALSE);
358
g_ptr_array_sort (dirs, (GCompareFunc)&vfs_sort_array);
359
g_ptr_array_sort (files, (GCompareFunc)&vfs_sort_array);
289
gboolean show_hidden,
291
if (DEBUG) g_printf ("In %s\n", __FUNCTION__);
293
GError *error = NULL;
295
GFile *file = g_file_new_for_path (path);
296
/* get ALL the info about the files */
297
GFileEnumerator *enumerator = g_file_enumerate_children (file,
300
"standard::is-hidden,"
302
"standard::display-name,"
303
"access::can-execute",
307
/* did we read the dir correctly? */
309
gchar *error_msg = g_strdup (error->message);
310
utils_gerror_ok (&error, FALSE);
314
GFileInfo *file_info = NULL;
315
while ((file_info = g_file_enumerator_next_file (enumerator, NULL, &error)) != NULL) {
316
/* skip the file if it's hidden and we aren't showing hidden files */
317
if (g_file_info_get_is_hidden (file_info) && !show_hidden) {
318
g_object_unref (file_info);
321
VfsFileInfo *vfs_file_info = g_new0 (VfsFileInfo ,1);
323
vfs_file_info->file_name = g_strdup_printf ("%s/%s", path, g_file_info_get_name (file_info));
325
/* get the file's human readable name, including if it's a desktop file */
328
vfs_file_info->display_name = (vfs_file_is_desktop (vfs_file_info->file_name)) ?
329
vfs_get_desktop_app_name (vfs_file_info->file_name) :
330
g_strdup (g_file_info_get_display_name (file_info));
332
vfs_file_info->is_executable = g_file_info_get_attribute_boolean (file_info,
333
G_FILE_ATTRIBUTE_ACCESS_CAN_EXECUTE) &&
334
(g_file_info_get_file_type (file_info) != G_FILE_TYPE_DIRECTORY);
336
vfs_file_info->icon = vfs_get_icon_for_file (vfs_file_info->file_name);
338
/* add it to the array */
339
if (g_file_info_get_file_type (file_info) == G_FILE_TYPE_DIRECTORY) {
340
g_ptr_array_add (dirs, (gpointer)vfs_file_info);
343
g_ptr_array_add (files, (gpointer)vfs_file_info);
345
g_object_unref (file_info);
347
g_object_unref (enumerator);
348
g_object_unref (file);
350
/* always check for errors */
352
gchar *error_msg = g_strdup (error->message);
353
utils_gerror_ok (&error, FALSE);
357
g_ptr_array_sort (dirs, (GCompareFunc)&vfs_sort_array);
358
g_ptr_array_sort (files, (GCompareFunc)&vfs_sort_array);
363
362
/******************************************************************************/