[polari/wip/fmuellner/tracker: 14/15] lib: Add helper API to scan log directories



commit 41c6e90f93077d35adc467eac8e1a1a52eb70298
Author: Florian Müllner <fmuellner gnome org>
Date:   Wed Jul 26 19:49:10 2017 +0200

    lib: Add helper API to scan log directories

 src/lib/polari-tpl-importer.c | 145 ++++++++++++++++++++++++++++++++++++++++++
 src/lib/polari-tpl-importer.h |   9 +++
 2 files changed, 154 insertions(+)
---
diff --git a/src/lib/polari-tpl-importer.c b/src/lib/polari-tpl-importer.c
index d3bc2d0..422ba14 100644
--- a/src/lib/polari-tpl-importer.c
+++ b/src/lib/polari-tpl-importer.c
@@ -351,6 +351,151 @@ polari_tpl_importer_import_finish (PolariTplImporter  *self,
   return g_task_propagate_pointer (G_TASK (result), error);
 }
 
+static void
+free_file_list (GList *files)
+{
+  g_list_free_full (files, g_object_unref);
+}
+
+static GList *
+collect_files_recursively (GFile         *dir,
+                           GCancellable  *cancellable,
+                           GError       **error)
+{
+  GFileEnumerator *direnum;
+  GFileInfo *info;
+  GFile *child;
+  GList *files = NULL;
+
+  direnum = g_file_enumerate_children (dir,
+                                       G_FILE_ATTRIBUTE_STANDARD_TYPE,
+                                       G_FILE_QUERY_INFO_NONE,
+                                       cancellable,
+                                       error);
+
+  if (error && *error)
+    return NULL;
+
+  while (g_file_enumerator_iterate (direnum, &info, &child, cancellable, error))
+    {
+      if (!info)
+        break;
+
+      if (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY)
+        files = g_list_concat (files, collect_files_recursively (child, cancellable, error));
+      else
+        files = g_list_prepend (files, g_object_ref (child));
+
+      if (error && *error)
+        break;
+    }
+  g_object_unref (direnum);
+
+  if (error && *error)
+    {
+      free_file_list (files);
+      return NULL;
+    }
+
+  return files;
+}
+
+static GList *
+collect_log_files (GFile         *dir,
+                   GCancellable  *cancellable,
+                   GError       **error)
+{
+  GFileEnumerator *direnum;
+  GFileInfo *info;
+  GFile *child;
+  GList *files = NULL;
+
+  direnum = g_file_enumerate_children (dir,
+                                       G_FILE_ATTRIBUTE_STANDARD_NAME,
+                                       G_FILE_QUERY_INFO_NONE,
+                                       cancellable,
+                                       error);
+
+  if (error && *error)
+    return NULL;
+
+  while (g_file_enumerator_iterate (direnum, &info, &child, cancellable, error))
+    {
+      if (!info)
+        break;
+
+      if (!g_str_has_prefix (g_file_info_get_name (info), "idle_irc_"))
+        continue;
+
+      files = g_list_concat (files, collect_files_recursively (child, cancellable, error));
+
+      if (error && *error)
+        break;
+    }
+  g_object_unref (direnum);
+
+  if (error && *error)
+    {
+      free_file_list (files);
+      return NULL;
+    }
+
+  return files;
+}
+
+static void
+collect_files_thread_func (GTask        *task,
+                           gpointer      source_object,
+                           gpointer      task_data,
+                           GCancellable *cancellable)
+{
+  GFile *log_root;
+  char *path;
+  GList *files;
+  GError *error = NULL;
+
+  path = g_build_filename (g_get_user_data_dir (), "TpLogger", "logs", NULL);
+  log_root = g_file_new_for_path (path);
+  g_free (path);
+
+  files = collect_log_files (log_root, cancellable, &error);
+
+  if (error)
+    g_task_return_error (task, error);
+  else
+    g_task_return_pointer (task, files, (GDestroyNotify)free_file_list);
+}
+
+void
+polari_tpl_importer_collect_files_async  (PolariTplImporter   *self,
+                                          GCancellable        *cancellable,
+                                          GAsyncReadyCallback  callback,
+                                          gpointer             user_data)
+{
+  GTask *task;
+
+  g_return_if_fail (POLARI_IS_TPL_IMPORTER (self));
+
+  task = g_task_new (self, cancellable, callback, user_data);
+  g_task_set_source_tag (task, polari_tpl_importer_collect_files_async);
+
+  g_task_run_in_thread (task, collect_files_thread_func);
+}
+
+/**
+ * polari_tpl_importer_collect_files_finish:
+ *
+ * Returns: (transfer full) (element-type GFile):
+ */
+GList *
+polari_tpl_importer_collect_files_finish (PolariTplImporter  *self,
+                                          GAsyncResult       *result,
+                                          GError            **error)
+{
+  g_return_val_if_fail (g_task_is_valid (result, self), NULL);
+  return g_task_propagate_pointer (G_TASK (result), error);
+}
+
 static void
 polari_tpl_importer_finalize (GObject *object)
 {
diff --git a/src/lib/polari-tpl-importer.h b/src/lib/polari-tpl-importer.h
index 3e9e982..14a888e 100644
--- a/src/lib/polari-tpl-importer.h
+++ b/src/lib/polari-tpl-importer.h
@@ -42,6 +42,15 @@ GList *polari_tpl_importer_import_finish (PolariTplImporter  *self,
                                           gboolean           *is_room,
                                           GError            **error);
 
+void   polari_tpl_importer_collect_files_async  (PolariTplImporter   *self,
+                                                 GCancellable        *cancellable,
+                                                 GAsyncReadyCallback  callback,
+                                                 gpointer             user_data);
+
+GList *polari_tpl_importer_collect_files_finish (PolariTplImporter  *self,
+                                                 GAsyncResult       *result,
+                                                 GError            **error);
+
 #define POLARI_TYPE_TPL_MESSAGE (polari_tpl_message_get_type())
 
 G_END_DECLS


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]