comparison src/editors.c @ 1736:8e64965c1d92

load desktop files in idle time - scanning all desktop files takes a lot of time because of hdd seek - this change moves the scanning to idle time - the editors appears in the menus some time after startup https://sourceforge.net/tracker/index.php?func=detail&aid=2852522&group_id=222125&atid=1054680
author nadvornik
date Sun, 06 Sep 2009 14:01:03 +0000
parents 58a5d1e01e33
children eb64f542341b
comparison
equal deleted inserted replaced
1735:6cae2af8fdd1 1736:8e64965c1d92
15 #include "editors.h" 15 #include "editors.h"
16 16
17 #include "filedata.h" 17 #include "filedata.h"
18 #include "filefilter.h" 18 #include "filefilter.h"
19 #include "misc.h" 19 #include "misc.h"
20 #include "pixbuf_util.h"
20 #include "ui_fileops.h" 21 #include "ui_fileops.h"
21 #include "ui_spinner.h" 22 #include "ui_spinner.h"
22 #include "ui_utildlg.h" 23 #include "ui_utildlg.h"
23 #include "utilops.h" 24 #include "utilops.h"
24 25
67 *----------------------------------------------------------------------------- 68 *-----------------------------------------------------------------------------
68 */ 69 */
69 70
70 GHashTable *editors = NULL; 71 GHashTable *editors = NULL;
71 GtkListStore *desktop_file_list; 72 GtkListStore *desktop_file_list;
72 73 gboolean editors_finished = FALSE;
73 74
74 #ifdef G_KEY_FILE_DESKTOP_GROUP 75 #ifdef G_KEY_FILE_DESKTOP_GROUP
75 #define DESKTOP_GROUP G_KEY_FILE_DESKTOP_GROUP 76 #define DESKTOP_GROUP G_KEY_FILE_DESKTOP_GROUP
76 #else 77 #else
77 #define DESKTOP_GROUP "Desktop Entry" 78 #define DESKTOP_GROUP "Desktop Entry"
145 list = g_list_concat(list, filter_to_list(conv_table[j][1])); 146 list = g_list_concat(list, filter_to_list(conv_table[j][1]));
146 147
147 return list; 148 return list;
148 } 149 }
149 150
150 static gboolean editor_read_desktop_file(const gchar *path) 151 gboolean editor_read_desktop_file(const gchar *path)
151 { 152 {
152 GKeyFile *key_file; 153 GKeyFile *key_file;
153 EditorDescription *editor; 154 EditorDescription *editor;
154 gchar *extensions; 155 gchar *extensions;
155 gchar *type; 156 gchar *type;
281 282
282 // drop extension 283 // drop extension
283 *ext = '\0'; 284 *ext = '\0';
284 } 285 }
285 } 286 }
287 if (editor->icon && !register_theme_icon_as_stock(editor->key, editor->icon))
288 {
289 g_free(editor->icon);
290 editor->icon = NULL;
291 }
286 292
287 editor->exec = g_key_file_get_string(key_file, DESKTOP_GROUP, "Exec", NULL); 293 editor->exec = g_key_file_get_string(key_file, DESKTOP_GROUP, "Exec", NULL);
288 294
289 editor->menu_path = g_key_file_get_string(key_file, DESKTOP_GROUP, "X-Geeqie-Menu-Path", NULL); 295 editor->menu_path = g_key_file_get_string(key_file, DESKTOP_GROUP, "X-Geeqie-Menu-Path", NULL);
290 if (!editor->menu_path) editor->menu_path = g_strdup("EditMenu/ExternalMenu"); 296 if (!editor->menu_path) editor->menu_path = g_strdup("EditMenu/ExternalMenu");
336 { 342 {
337 EditorDescription *editor = value; 343 EditorDescription *editor = value;
338 return editor->hidden || editor->ignored; 344 return editor->hidden || editor->ignored;
339 } 345 }
340 346
341 static void editor_read_desktop_dir(const gchar *path) 347 void editor_table_finish(void)
348 {
349 g_hash_table_foreach_remove(editors, editor_remove_desktop_file_cb, NULL);
350 editors_finished = TRUE;
351 }
352
353 void editor_table_clear(void)
354 {
355 if (desktop_file_list)
356 {
357 gtk_list_store_clear(desktop_file_list);
358 }
359 else
360 {
361 desktop_file_list = gtk_list_store_new(DESKTOP_FILE_COLUMN_COUNT, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_BOOLEAN, G_TYPE_BOOLEAN, G_TYPE_STRING);
362 }
363 if (editors)
364 {
365 g_hash_table_destroy(editors);
366 }
367 editors = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, (GDestroyNotify)editor_description_free);
368 editors_finished = FALSE;
369 }
370
371 static GList *editor_add_desktop_dir(GList *list, const gchar *path)
342 { 372 {
343 DIR *dp; 373 DIR *dp;
344 struct dirent *dir; 374 struct dirent *dir;
345 gchar *pathl; 375 gchar *pathl;
346 376
348 dp = opendir(pathl); 378 dp = opendir(pathl);
349 g_free(pathl); 379 g_free(pathl);
350 if (!dp) 380 if (!dp)
351 { 381 {
352 /* dir not found */ 382 /* dir not found */
353 return; 383 return list;
354 } 384 }
355 while ((dir = readdir(dp)) != NULL) 385 while ((dir = readdir(dp)) != NULL)
356 { 386 {
357 gchar *namel = dir->d_name; 387 gchar *namel = dir->d_name;
358 388
359 if (g_str_has_suffix(namel, ".desktop")) 389 if (g_str_has_suffix(namel, ".desktop"))
360 { 390 {
361 gchar *name = path_to_utf8(namel); 391 gchar *name = path_to_utf8(namel);
362 gchar *dpath = g_build_filename(path, name, NULL); 392 gchar *dpath = g_build_filename(path, name, NULL);
363 editor_read_desktop_file(dpath); 393 list = g_list_prepend(list, dpath);
364 g_free(dpath);
365 g_free(name); 394 g_free(name);
366 } 395 }
367 } 396 }
368 closedir(dp); 397 closedir(dp);
369 } 398 return list;
370 399 }
371 void editor_load_descriptions(void) 400
401 GList *editor_get_desktop_files(void)
372 { 402 {
373 gchar *path; 403 gchar *path;
374 gchar *xdg_data_dirs; 404 gchar *xdg_data_dirs;
375 gchar *all_dirs; 405 gchar *all_dirs;
376 gchar **split_dirs; 406 gchar **split_dirs;
377 gint i; 407 gint i;
378 408 GList *list = NULL;
379 if (desktop_file_list) 409
380 {
381 gtk_list_store_clear(desktop_file_list);
382 }
383 else
384 {
385 desktop_file_list = gtk_list_store_new(DESKTOP_FILE_COLUMN_COUNT, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_BOOLEAN, G_TYPE_BOOLEAN, G_TYPE_STRING);
386 }
387 if (editors)
388 {
389 g_hash_table_destroy(editors);
390 }
391 editors = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, (GDestroyNotify)editor_description_free);
392
393 xdg_data_dirs = getenv("XDG_DATA_DIRS"); 410 xdg_data_dirs = getenv("XDG_DATA_DIRS");
394 if (xdg_data_dirs && xdg_data_dirs[0]) 411 if (xdg_data_dirs && xdg_data_dirs[0])
395 xdg_data_dirs = path_to_utf8(xdg_data_dirs); 412 xdg_data_dirs = path_to_utf8(xdg_data_dirs);
396 else 413 else
397 xdg_data_dirs = g_strdup("/usr/share"); 414 xdg_data_dirs = g_strdup("/usr/share");
402 419
403 split_dirs = g_strsplit(all_dirs, ":", 0); 420 split_dirs = g_strsplit(all_dirs, ":", 0);
404 421
405 g_free(all_dirs); 422 g_free(all_dirs);
406 423
407 for (i = 0; split_dirs[i]; i++) 424 for (i = 0; split_dirs[i]; i++);
425 for (--i; i >= 0; i--)
408 { 426 {
409 path = g_build_filename(split_dirs[i], "applications", NULL); 427 path = g_build_filename(split_dirs[i], "applications", NULL);
410 editor_read_desktop_dir(path); 428 list = editor_add_desktop_dir(list, path);
411 g_free(path); 429 g_free(path);
412 } 430 }
413 431
414 g_strfreev(split_dirs); 432 g_strfreev(split_dirs);
415 433 return g_list_reverse(list);
416 g_hash_table_foreach_remove(editors, editor_remove_desktop_file_cb, NULL);
417 } 434 }
418 435
419 static void editor_list_add_cb(gpointer key, gpointer value, gpointer data) 436 static void editor_list_add_cb(gpointer key, gpointer value, gpointer data)
420 { 437 {
421 GList **listp = data; 438 GList **listp = data;
444 } 461 }
445 462
446 GList *editor_list_get(void) 463 GList *editor_list_get(void)
447 { 464 {
448 GList *editors_list = NULL; 465 GList *editors_list = NULL;
466
467 if (!editors_finished) return NULL;
468
449 g_hash_table_foreach(editors, editor_list_add_cb, &editors_list); 469 g_hash_table_foreach(editors, editor_list_add_cb, &editors_list);
450 editors_list = g_list_sort(editors_list, editor_sort); 470 editors_list = g_list_sort(editors_list, editor_sort);
451 471
452 return editors_list; 472 return editors_list;
453 } 473 }