[Telepathy-commits] [telepathy-mission-control/master] Implement directory scanning function.

Alberto Mardegan alberto.mardegan at nokia.com
Mon Nov 17 00:05:17 PST 2008


Add a mcd-misc.c file to hold utility functions that don't pertain to any
specific object.
Implement a function to scan XDG data subdirectories and invoke a callback for
any file found.
---
 src/Makefile.am |    4 ++-
 src/mcd-misc.c  |   90 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/mcd-misc.h  |   41 +++++++++++++++++++++++++
 3 files changed, 134 insertions(+), 1 deletions(-)
 create mode 100644 src/mcd-misc.c
 create mode 100644 src/mcd-misc.h

diff --git a/src/Makefile.am b/src/Makefile.am
index 4ac504c..07cb9ab 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -73,7 +73,8 @@ mission_control_include_HEADERS = \
 	$(mission_control_include) \
 	mcd-account-config.h \
 	mcd-account-priv.h \
-	mcd-enum-types.h
+	mcd-enum-types.h \
+	mcd-misc.h
 
 BUILT_SOURCES = \
 	_gen/all.xml \
@@ -116,6 +117,7 @@ libmissioncontrol_server_la_SOURCES = \
 	mcd-debug.c \
 	mcd-enum-types.c \
 	mcd-signals-marshal.c \
+	mcd-misc.c \
 	mcd-mission.c \
 	mcd-operation.c \
 	mcd-controller.c \
diff --git a/src/mcd-misc.c b/src/mcd-misc.c
new file mode 100644
index 0000000..65af958
--- /dev/null
+++ b/src/mcd-misc.c
@@ -0,0 +1,90 @@
+/* vi: set et sw=4 ts=4 cino=t0,(0: */
+/* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * This file is part of mission-control
+ *
+ * Copyright (C) 2008 Nokia Corporation. 
+ *
+ * Contact: Alberto Mardegan  <alberto.mardegan at nokia.com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * version 2.1 as published by the Free Software Foundation.
+ *
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA
+ *
+ */
+
+#include "mcd-misc.h"
+
+/*
+ * Miscellaneus functions
+ */
+
+static gboolean
+scan_data_subdir (const gchar *dirname, McdXdgDataSubdirFunc callback,
+                  gpointer user_data)
+{
+    const gchar *filename;
+    GError *error = NULL;
+    gboolean proceed = TRUE;
+    GDir *dir;
+
+    if (!g_file_test (dirname, G_FILE_TEST_IS_DIR)) return TRUE;
+
+    if ((dir = g_dir_open (dirname, 0, &error)) == NULL)
+    {
+        g_warning ("Error opening directory %s: %s", dirname,
+                   error->message);
+    }
+
+    while ((filename = g_dir_read_name (dir)) != NULL)
+    {
+        gchar *absolute_filepath;
+
+        absolute_filepath = g_build_filename(dirname, filename, NULL);
+        proceed = callback (absolute_filepath, filename, user_data);
+        g_free(absolute_filepath);
+        if (!proceed) break;
+    }
+    g_dir_close(dir);
+    return proceed;
+}
+
+/* utility function to scan XDG_DATA_DIRS subdirectories */
+void
+_mcd_xdg_data_subdir_foreach (const gchar *subdir,
+                              McdXdgDataSubdirFunc callback,
+                              gpointer user_data)
+{
+    const gchar *dirname;
+    const gchar * const *dirs;
+    gboolean proceed = TRUE;
+    gchar *dir;
+
+    dirs = g_get_system_data_dirs();
+    for (dirname = *dirs; dirname != NULL; dirs++, dirname = *dirs)
+    {
+        dir = g_build_filename (dirname, subdir, NULL);
+        proceed = scan_data_subdir (dir, callback, user_data);
+        g_free (dir);
+        if (!proceed) break;
+    }
+
+    if (proceed)
+    {
+        dir = g_build_filename (g_get_user_data_dir(), subdir, NULL);
+        scan_data_subdir (dir, callback, user_data);
+        g_free (dir);
+    }
+}
+
+
diff --git a/src/mcd-misc.h b/src/mcd-misc.h
new file mode 100644
index 0000000..cfbda68
--- /dev/null
+++ b/src/mcd-misc.h
@@ -0,0 +1,41 @@
+/* vi: set et sw=4 ts=4 cino=t0,(0: */
+/* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * This file is part of mission-control
+ *
+ * Copyright (C) 2007 Nokia Corporation. 
+ *
+ * Contact: Alberto Mardegan  <alberto.mardegan at nokia.com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * version 2.1 as published by the Free Software Foundation.
+ *
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA
+ *
+ */
+
+#ifndef MCD_MISC_H
+#define MCD_MISC_H
+
+#include <glib.h>
+
+G_BEGIN_DECLS
+
+typedef gboolean (*McdXdgDataSubdirFunc) (const gchar *path,
+                                          const gchar *filename,
+                                          gpointer user_data);
+void _mcd_xdg_data_subdir_foreach (const gchar *subdir,
+                                   McdXdgDataSubdirFunc callback,
+                                   gpointer user_data);
+
+G_END_DECLS
+#endif /* MCD_MISC_H */
-- 
1.5.6.5




More information about the Telepathy-commits mailing list