listen for changes in directory

This commit is contained in:
Orangerot 2023-04-06 21:42:42 +02:00
parent 42bacc4e82
commit 79d2167a22

View file

@ -11,12 +11,37 @@ enum
NUM_COLS NUM_COLS
}; };
GtkListStore *store;
// Callback function for file created signal
static void file_changed_cb(GFileMonitor *monitor, GFile *file, GFile *other_file, GFileMonitorEvent evtype, gpointer user_data)
{
char *fpath = g_file_get_path(file);
char *opath = NULL;
if (other_file) {
opath = g_file_get_path(other_file);
}
switch(evtype) {
case G_FILE_MONITOR_EVENT_DELETED:
g_print("%s deleted\n", fpath);
break;
case G_FILE_MONITOR_EVENT_CREATED:
g_print("%s created\n", fpath);
break;
}
if (opath) {
g_free(opath);
}
g_free(fpath);
}
static GtkListStore *create_desktop_list(void) static GtkListStore *create_desktop_list(void)
{ {
GtkListStore *store;
GtkTreeIter iter; GtkTreeIter iter;
GDir *dir; GDir *dir;
GFile *file; GFile *file, *dir_file;
GFileMonitor *monitor;
GFileInfo *file_info; GFileInfo *file_info;
const gchar *file_name, *display_name; const gchar *file_name, *display_name;
GtkIconTheme *theme; GtkIconTheme *theme;
@ -35,6 +60,10 @@ static GtkListStore *create_desktop_list(void)
dir = g_dir_open(desktop_path, 0, 0); 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)) ) { while ( (file_name = g_dir_read_name(dir)) ) {
printf("g %s\n", file_name); printf("g %s\n", file_name);
@ -57,6 +86,8 @@ static GtkListStore *create_desktop_list(void)
); );
} }
g_signal_connect(monitor, "changed", G_CALLBACK(file_changed_cb), NULL);
return GTK_LIST_STORE(store); return GTK_LIST_STORE(store);
} }