summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOrangerot <purple@orangerot.dev>2023-04-07 01:07:16 +0200
committerOrangerot <purple@orangerot.dev>2023-04-07 01:07:16 +0200
commitaf9d2c446ea5d8ed9d7d13b364f517abcaeb0ae5 (patch)
tree2ccfff1bfbaf53712ddc76df7c29a0f776ed5cc7
parent79d2167a226dafbdfa58b45ce76c3cb0617efb29 (diff)
add/remove icon based on directory changesfile-listener
-rw-r--r--dicons.c98
1 files changed, 57 insertions, 41 deletions
diff --git a/dicons.c b/dicons.c
index 34d67a3..dd5b096 100644
--- a/dicons.c
+++ b/dicons.c
@@ -11,46 +11,78 @@ enum
NUM_COLS
};
-GtkListStore *store;
+GtkIconTheme *theme;
-// Callback function for file created signal
-static void file_changed_cb(GFileMonitor *monitor, GFile *file, GFile *other_file, GFileMonitorEvent evtype, gpointer user_data)
+void remove_row_by_file(GtkListStore *store, GFile *file)
{
- char *fpath = g_file_get_path(file);
- char *opath = NULL;
- if (other_file) {
- opath = g_file_get_path(other_file);
+ GtkTreeIter iter;
+ GFile *file_iter;
+ gboolean valid = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store), &iter);
+
+ while (valid)
+ {
+ gtk_tree_model_get(GTK_TREE_MODEL(store), &iter, COL_FILE, &file_iter, -1);
+
+ if (g_file_equal(file, file_iter))
+ {
+ gtk_list_store_remove(store, &iter);
+ return;
+ }
+ valid = gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &iter);
}
+
+}
+
+void append_row_from_file(GtkListStore *store, GFile *file)
+{
+ GtkTreeIter iter;
+ GFileInfo *file_info;
+ const gchar *display_name;
+ GdkPixbuf *pixbuf;
+
+ file_info = g_file_query_info(file, "standard::*,ownser::user", 0, 0, 0);
+ display_name = g_file_info_get_display_name(file_info);
+ pixbuf = gtk_icon_info_load_icon(
+ gtk_icon_theme_lookup_by_gicon(
+ theme,
+ g_file_info_get_icon(file_info),
+ 48, 0), 0);
+
+ gtk_list_store_append(store, &iter);
+ gtk_list_store_set(store, &iter,
+ COL_PIXBUF, pixbuf,
+ COL_DISPLAY_NAME, display_name,
+ COL_FILE, file,
+ COL_FILE_INFO, file_info,
+ -1
+ );
+}
+// Callback function for file created signal
+static void file_changed_cb(GFileMonitor *monitor, GFile *file, GFile *other_file, GFileMonitorEvent evtype, gpointer user_data)
+{
+ GtkListStore *store = GTK_LIST_STORE(user_data);
switch(evtype) {
case G_FILE_MONITOR_EVENT_DELETED:
- g_print("%s deleted\n", fpath);
+ remove_row_by_file(store, file);
break;
case G_FILE_MONITOR_EVENT_CREATED:
- g_print("%s created\n", fpath);
+ append_row_from_file(store, file);
break;
}
- if (opath) {
- g_free(opath);
- }
- g_free(fpath);
}
static GtkListStore *create_desktop_list(void)
{
GtkTreeIter iter;
+ GtkListStore *store;
GDir *dir;
GFile *file, *dir_file;
GFileMonitor *monitor;
- GFileInfo *file_info;
- const gchar *file_name, *display_name;
- GtkIconTheme *theme;
- GdkPixbuf *pixbuf;
-
- const gchar* desktop_path = g_get_user_special_dir(G_USER_DIRECTORY_DESKTOP);
- printf("n %s\n", desktop_path);
+ const gchar *desktop_path, *file_name;
- theme = gtk_icon_theme_get_default();
+ desktop_path = g_get_user_special_dir(G_USER_DIRECTORY_DESKTOP);
+ printf("desktop-path: %s\n", desktop_path);
store = gtk_list_store_new(NUM_COLS,
GDK_TYPE_PIXBUF,
@@ -59,34 +91,17 @@ static GtkListStore *create_desktop_list(void)
G_TYPE_FILE_INFO);
dir = g_dir_open(desktop_path, 0, 0);
-
dir_file = g_file_new_for_path(desktop_path);
- // monitor = g_file_monitor_directory(dir_file, G_FILE_MONITOR_WATCH_MOVES, NULL, NULL);
monitor = g_file_monitor_directory(dir_file, G_FILE_MONITOR_NONE, NULL, NULL);
while ( (file_name = g_dir_read_name(dir)) ) {
- printf("g %s\n", file_name);
+ printf("contains: %s\n", file_name);
file = g_file_new_for_path(g_build_filename(desktop_path, file_name, NULL));
- display_name = g_filename_to_utf8(file_name, -1, 0, 0, 0);
- file_info = g_file_query_info(file, "standard::*,ownser::user", 0, 0, 0);
- pixbuf = gtk_icon_info_load_icon(
- gtk_icon_theme_lookup_by_gicon(
- theme,
- g_file_info_get_icon(file_info),
- 48, 0), 0);
-
- gtk_list_store_append(store, &iter);
- gtk_list_store_set(store, &iter,
- COL_PIXBUF, pixbuf,
- COL_DISPLAY_NAME, display_name,
- COL_FILE, file,
- COL_FILE_INFO, file_info,
- -1
- );
+ append_row_from_file(store, file);
}
- g_signal_connect(monitor, "changed", G_CALLBACK(file_changed_cb), NULL);
+ g_signal_connect(monitor, "changed", G_CALLBACK(file_changed_cb), store);
return GTK_LIST_STORE(store);
}
@@ -126,6 +141,7 @@ static void activate (GtkApplication* app, gpointer user_data)
gtk_layer_set_margin(GTK_WINDOW(window), GTK_LAYER_SHELL_EDGE_TOP, 20);
+ theme = gtk_icon_theme_get_default();
model = create_desktop_list();
icon_view = gtk_icon_view_new_with_model(GTK_TREE_MODEL(model));