+/* Delay callback dispatching
+ */
+
+enum callback_types
+{
+ CALLBACK_GET_INFO,
+ CALLBACK_GET_FOLDER,
+ CALLBACK_CREATE_FOLDER,
+ CALLBACK_VOLUME_MOUNT
+};
+
+static void queue_callback (GtkFileSystemUnix *system_unix, enum callback_types type, gpointer data);
+
+struct get_info_callback
+{
+ GtkFileSystemGetInfoCallback callback;
+ GtkFileSystemHandle *handle;
+ GtkFileInfo *file_info;
+ GError *error;
+ gpointer data;
+};
+
+static inline void
+dispatch_get_info_callback (struct get_info_callback *info)
+{
+ (* info->callback) (info->handle, info->file_info, info->error, info->data);
+
+ if (info->file_info)
+ gtk_file_info_free (info->file_info);
+
+ if (info->error)
+ g_error_free (info->error);
+
+ g_object_unref (info->handle);
+
+ g_free (info);
+}
+
+static inline void
+queue_get_info_callback (GtkFileSystemGetInfoCallback callback,
+ GtkFileSystemHandle *handle,
+ GtkFileInfo *file_info,
+ GError *error,
+ gpointer data)
+{
+ struct get_info_callback *info;
+
+ info = g_new (struct get_info_callback, 1);
+ info->callback = callback;
+ info->handle = handle;
+ info->file_info = file_info;
+ info->error = error;
+ info->data = data;
+
+ queue_callback (GTK_FILE_SYSTEM_UNIX (handle->file_system), CALLBACK_GET_INFO, info);
+}
+
+
+struct get_folder_callback
+{
+ GtkFileSystemGetFolderCallback callback;
+ GtkFileSystemHandle *handle;
+ GtkFileFolder *folder;
+ GError *error;
+ gpointer data;
+};
+
+static inline void
+dispatch_get_folder_callback (struct get_folder_callback *info)
+{
+ (* info->callback) (info->handle, info->folder, info->error, info->data);
+
+ if (info->error)
+ g_error_free (info->error);
+
+ g_object_unref (info->handle);
+
+ g_free (info);
+}
+
+static inline void
+queue_get_folder_callback (GtkFileSystemGetFolderCallback callback,
+ GtkFileSystemHandle *handle,
+ GtkFileFolder *folder,
+ GError *error,
+ gpointer data)
+{
+ struct get_folder_callback *info;
+
+ info = g_new (struct get_folder_callback, 1);
+ info->callback = callback;
+ info->handle = handle;
+ info->folder = folder;
+ info->error = error;
+ info->data = data;
+
+ queue_callback (GTK_FILE_SYSTEM_UNIX (handle->file_system), CALLBACK_GET_FOLDER, info);
+}
+
+
+struct create_folder_callback
+{
+ GtkFileSystemCreateFolderCallback callback;
+ GtkFileSystemHandle *handle;
+ GtkFilePath *path;
+ GError *error;
+ gpointer data;
+};
+
+static inline void
+dispatch_create_folder_callback (struct create_folder_callback *info)
+{
+ (* info->callback) (info->handle, info->path, info->error, info->data);
+
+ if (info->error)
+ g_error_free (info->error);
+
+ if (info->path)
+ gtk_file_path_free (info->path);
+
+ g_object_unref (info->handle);
+
+ g_free (info);
+}
+
+static inline void
+queue_create_folder_callback (GtkFileSystemCreateFolderCallback callback,
+ GtkFileSystemHandle *handle,
+ const GtkFilePath *path,
+ GError *error,
+ gpointer data)
+{
+ struct create_folder_callback *info;
+
+ info = g_new (struct create_folder_callback, 1);
+ info->callback = callback;
+ info->handle = handle;
+ info->path = gtk_file_path_copy (path);
+ info->error = error;
+ info->data = data;
+
+ queue_callback (GTK_FILE_SYSTEM_UNIX (handle->file_system), CALLBACK_CREATE_FOLDER, info);
+}
+
+
+struct volume_mount_callback
+{
+ GtkFileSystemVolumeMountCallback callback;
+ GtkFileSystemHandle *handle;
+ GtkFileSystemVolume *volume;
+ GError *error;
+ gpointer data;
+};
+
+static inline void
+dispatch_volume_mount_callback (struct volume_mount_callback *info)
+{
+ (* info->callback) (info->handle, info->volume, info->error, info->data);
+
+ if (info->error)
+ g_error_free (info->error);
+
+ g_object_unref (info->handle);
+
+ g_free (info);
+}
+
+static inline void
+queue_volume_mount_callback (GtkFileSystemVolumeMountCallback callback,
+ GtkFileSystemHandle *handle,
+ GtkFileSystemVolume *volume,
+ GError *error,
+ gpointer data)
+{
+ struct volume_mount_callback *info;
+
+ info = g_new (struct volume_mount_callback, 1);
+ info->callback = callback;
+ info->handle = handle;
+ info->volume = volume;
+ info->error = error;
+ info->data = data;
+
+ queue_callback (GTK_FILE_SYSTEM_UNIX (handle->file_system), CALLBACK_VOLUME_MOUNT, info);
+}
+
+
+struct callback_info
+{
+ enum callback_types type;
+
+ union
+ {
+ struct get_info_callback *get_info;
+ struct get_folder_callback *get_folder;
+ struct create_folder_callback *create_folder;
+ struct volume_mount_callback *volume_mount;
+ } info;
+};
+
+
+
+static gboolean
+execute_callbacks (gpointer data)
+{
+ GSList *l;
+ gboolean unref_file_system = TRUE;
+ GtkFileSystemUnix *system_unix = GTK_FILE_SYSTEM_UNIX (data);
+
+ if (!system_unix->execute_callbacks_idle_id)
+ unref_file_system = FALSE;
+ else
+ g_object_ref (system_unix);
+
+ for (l = system_unix->callbacks; l; l = l->next)
+ {
+ struct callback_info *info = l->data;
+
+ switch (info->type)
+ {
+ case CALLBACK_GET_INFO:
+ dispatch_get_info_callback (info->info.get_info);
+ break;
+
+ case CALLBACK_GET_FOLDER:
+ dispatch_get_folder_callback (info->info.get_folder);
+ break;
+
+ case CALLBACK_CREATE_FOLDER:
+ dispatch_create_folder_callback (info->info.create_folder);
+ break;
+
+ case CALLBACK_VOLUME_MOUNT:
+ dispatch_volume_mount_callback (info->info.volume_mount);
+ break;
+ }
+
+ g_free (info);
+ }
+
+ g_slist_free (system_unix->callbacks);
+ system_unix->callbacks = NULL;
+
+ if (unref_file_system)
+ g_object_unref (system_unix);
+
+ system_unix->execute_callbacks_idle_id = 0;
+
+ return FALSE;
+}
+
+static void
+queue_callback (GtkFileSystemUnix *system_unix,
+ enum callback_types type,
+ gpointer data)
+{
+ struct callback_info *info;
+
+ info = g_new (struct callback_info, 1);
+ info->type = type;
+
+ switch (type)
+ {
+ case CALLBACK_GET_INFO:
+ info->info.get_info = data;
+ break;
+
+ case CALLBACK_GET_FOLDER:
+ info->info.get_folder = data;
+ break;
+
+ case CALLBACK_CREATE_FOLDER:
+ info->info.create_folder = data;
+ break;
+
+ case CALLBACK_VOLUME_MOUNT:
+ info->info.volume_mount = data;
+ break;
+ }
+
+ system_unix->callbacks = g_slist_append (system_unix->callbacks, info);
+
+ if (!system_unix->execute_callbacks_idle_id)
+ system_unix->execute_callbacks_idle_id = gdk_threads_add_idle (execute_callbacks, system_unix);
+}
+
+static GtkFileSystemHandle *
+create_handle (GtkFileSystem *file_system)
+{
+ GtkFileSystemUnix *system_unix;
+ GtkFileSystemHandle *handle;
+
+ system_unix = GTK_FILE_SYSTEM_UNIX (file_system);
+
+ handle = g_object_new (GTK_TYPE_FILE_SYSTEM_HANDLE_UNIX, NULL);
+ handle->file_system = file_system;
+
+ g_assert (g_hash_table_lookup (system_unix->handles, handle) == NULL);
+ g_hash_table_insert (system_unix->handles, handle, handle);
+
+ return handle;
+}
+
+
+
+static GtkFileSystemHandle *
+gtk_file_system_unix_get_info (GtkFileSystem *file_system,
+ const GtkFilePath *path,
+ GtkFileInfoType types,
+ GtkFileSystemGetInfoCallback callback,
+ gpointer data)
+{
+ GError *error = NULL;
+ GtkFileSystemUnix *system_unix;
+ GtkFileSystemHandle *handle;
+ const char *filename;
+ GtkFileInfo *info;
+ gchar *basename;
+ struct stat statbuf;
+ const char *mime_type;
+
+ system_unix = GTK_FILE_SYSTEM_UNIX (file_system);
+ handle = create_handle (file_system);
+
+ filename = gtk_file_path_get_string (path);
+ g_return_val_if_fail (filename != NULL, NULL);
+ g_return_val_if_fail (g_path_is_absolute (filename), NULL);
+
+ if (!stat_with_error (filename, &statbuf, &error))
+ {
+ g_object_ref (handle);
+ queue_get_info_callback (callback, handle, NULL, error, data);
+ return handle;
+ }
+
+ if ((types & GTK_FILE_INFO_MIME_TYPE) != 0)
+ mime_type = xdg_mime_get_mime_type_for_file (filename, &statbuf);
+ else
+ mime_type = NULL;
+
+ basename = g_path_get_basename (filename);
+
+ info = create_file_info (NULL, filename, basename, types, &statbuf,
+ mime_type);
+ g_free (basename);
+ g_object_ref (handle);
+ queue_get_info_callback (callback, handle, info, NULL, data);
+
+ return handle;
+}
+
+static gboolean
+load_folder (gpointer data)
+{
+ GtkFileFolderUnix *folder_unix = data;
+ GSList *children;
+
+ if ((folder_unix->types & STAT_NEEDED_MASK) != 0)
+ fill_in_stats (folder_unix);
+
+ if ((folder_unix->types & GTK_FILE_INFO_MIME_TYPE) != 0)
+ fill_in_mime_type (folder_unix);
+
+ if (gtk_file_folder_unix_list_children (GTK_FILE_FOLDER (folder_unix), &children, NULL))
+ {
+ folder_unix->is_finished_loading = TRUE;
+ g_signal_emit_by_name (folder_unix, "files-added", children);
+ gtk_file_paths_free (children);
+ }
+
+ folder_unix->load_folder_id = 0;
+
+ g_signal_emit_by_name (folder_unix, "finished-loading", 0);
+
+ return FALSE;
+}
+
+static GtkFileSystemHandle *
+gtk_file_system_unix_get_folder (GtkFileSystem *file_system,
+ const GtkFilePath *path,
+ GtkFileInfoType types,
+ GtkFileSystemGetFolderCallback callback,
+ gpointer data)