Search code examples
casynchronousgtkgio

C Glib GIO - How to list files asynchronously


I am creating a simple file viewer using GTK, and I want to load new directory asynchronously, to prevent hanging the whole program while loading.

In GIO API there is g_file_enumerator_next_files_async function, that allows to asynchronously load files in blocks. But how can I tell, when directory listing is finished? Here code sample of what I'm came up with:

static void add_file_callback(GObject *direnum,
                GAsyncResult *result,
                gpointer user_data){
    GError *error = NULL;
    GList *file_list = g_file_enumerator_next_files_finish(
                    G_FILE_ENUMERATOR(direnum),
                    result, &error);    
    if( file_list == NULL ){
        g_critical("Unable to add files to list, error: %s", error->message);

    }
    GList *next;
    GFileInfo *info;
    GtkTreeIter iter;
    DirList *list = (DirList*)user_data;
    while(file_list){
        ...add file to list
    }
}


int read_dir(const gchar *path, DirList *list){
    g_assert(list != NULL && list->store != NULL);
    GtkTreeIter iter;
    gtk_list_store_clear(list->store);

    list->path = path;

    GFile *dir = g_file_new_for_path(path);
    GFileEnumerator *dirent = g_file_enumerate_children(dir,
                                        "*",
                                        G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
                                        NULL,
                                        NULL);
    while( TRUE ){ /* <============================== When to end? */
        g_file_enumerator_next_files_async(dirent,
                        BLOCK_SIZE,
                        G_PRIORITY_DEFAULT,
                        NULL,
                        add_file_callback,
                        list);
    }
    g_file_enumerator_close(dirent, NULL, NULL);
    g_object_unref(dirent);
    g_object_unref(dir);

    return 0;
}

Solution

  • To do the listing you should call g_file_enumerator_next_files_async recursively, instead calling them in cycle, here' the example:

    static void add_file_callback(GObject *direnum,
                    GAsyncResult *result,
                    gpointer user_data){
        GError *error = NULL;
        GList *file_list = g_file_enumerator_next_files_finish(
                        G_FILE_ENUMERATOR(direnum),
                        result, &error);
        if( error ){
            g_critical("Unable to add files to list, error: %s", error->message);
            g_object_unref(direnum);
            g_error_free(error);
            return;
        }else if( file_list == NULL ){
            /* Done listing */
            g_object_unref(direnum);
            return;
        }else{
    
            GList *node = file_list;
            GFileInfo *info;
            GtkTreeIter iter;
            while(node){
                info = node->data;
                node = node->next;
                ...add to store
                g_object_unref(info);
            }
            g_file_enumerator_next_files_async(G_FILE_ENUMERATOR(direnum),
                            BLOCK_SIZE,
                            G_PRIORITY_LOW,
                            NULL,
                            add_file_callback,
                            list);
        }
        g_list_free(file_list);
    }