specify the encoding of filesystem paths, and include a comparator function
authorDana Jansens <danakj@orodu.net>
Thu, 27 Jan 2011 22:28:12 +0000 (17:28 -0500)
committerDana Jansens <danakj@orodu.net>
Sun, 16 Oct 2011 22:54:04 +0000 (18:54 -0400)
the comparator compares two ObtLink** objects by their names.  if the names are
equal it uses the path of the source as a tie-breaker.

obt/link.c
obt/link.h

index ab215b50406e854cf0930f0a8818ac9cc7d72f46..9eb912c9adc9e1195a395b3f83bf7f43e1af73f9 100644 (file)
 struct _ObtLink {
     guint ref;
 
-    gchar *path; /*!< The path to the file where the link came from */
+    gchar *path; /*!< The path to the file where the link came from.
+                   Encoded in UTF-8. */
 
     ObtLinkType type;
     gchar *name; /*!< Specific name for the object (eg Firefox) */
+    gchar *collate_key; /*!< Internal key for sorting by name. */
     gboolean display; /*<! When false, do not display this link in menus or
                            launchers, etc */
     gboolean deleted; /*<! When true, the Link could exist but is deleted
@@ -48,8 +50,10 @@ struct _ObtLink {
 
     union _ObtLinkData {
         struct _ObtLinkApp {
-            gchar *exec; /*!< Executable to run for the app */
-            gchar *wdir; /*!< Working dir to run the app in */
+            gchar *exec; /*!< Executable to run for the app.
+                           Encoded in UTF-8. */
+            gchar *wdir; /*!< Working dir to run the app in.
+                           Encoded in UTF-8. */
             gboolean term; /*!< Run the app in a terminal or not */
             ObtLinkAppOpen open;
 
@@ -100,6 +104,11 @@ ObtLink* obt_link_from_ddfile(const gchar *path, ObtPaths *p,
     g_assert(v);
     link->type = v->value.enumerable;
 
+    v = g_hash_table_lookup(keys, "Name");
+    g_assert(v);
+    link->name = v->value.string, v->value.string = NULL; /* steal it */
+    link->collate_key = g_utf8_collate_key(link->name, -1);
+
     if ((v = g_hash_table_lookup(keys, "Hidden")))
         link->deleted = v->value.boolean;
 
@@ -221,6 +230,7 @@ void obt_link_unref(ObtLink *dd)
 {
     if (--dd->ref < 1) {
         g_free(dd->name);
+        g_free(dd->collate_key);
         g_free(dd->generic);
         g_free(dd->comment);
         g_free(dd->icon);
@@ -264,6 +274,21 @@ gchar* obt_link_id_from_ddfile(const gchar *filename)
     return obt_ddparse_file_to_id(filename);
 }
 
+const gchar* obt_link_name(ObtLink *e)
+{
+    return e->name;
+}
+
+const gchar* obt_link_generic_name(ObtLink *e)
+{
+    return e->generic;
+}
+
+const gchar* obt_link_comment(ObtLink *e)
+{
+    return e->comment;
+}
+
 gboolean obt_link_display(ObtLink *e, const guint environments)
 {
     return
@@ -276,3 +301,12 @@ gboolean obt_link_display(ObtLink *e, const guint environments)
            the restrictions */
         (!e->env_restricted || !(e->env_restricted & environments));
 }
+
+int obt_link_cmp_by_name(const void *a, const void *b)
+{
+    const ObtLink *const la = *(ObtLink**)a, *const lb = *(ObtLink**)b;
+    int r = strcmp(la->collate_key, lb->collate_key);
+    if (r) return r;
+    /* fallback to differentiating on the path */
+    return strcmp(la->path, lb->path);
+}
index 6577d8a3877426154cb3189e8a62559e7fa4d610..25f19e27fabcacecce69e59be359accf18f7f822 100644 (file)
@@ -63,7 +63,7 @@ typedef enum {
 typedef struct _ObtLink     ObtLink;
 
 /*! Parse a .desktop (dd) file.
-  @param path The full path to the .desktop file.
+  @param path The full path to the .desktop file, encoded in UTF-8.
   @param o An ObtPaths structure, which contains the executable paths.
 */
 ObtLink* obt_link_from_ddfile(const gchar *path,
@@ -131,6 +131,14 @@ ObtLinkAppOpen obt_link_app_open(ObtLink *e);
 ObtLinkAppStartup obt_link_app_startup_notify(ObtLink *e);
 const gchar* obt_link_app_startup_wmclass(ObtLink *e);
 
+/*! Compares two ObtLink objects.
+  @param a An ObtLink** (pointer to a pointer)
+  @param b An ObtLink** (pointer to a pointer)
+  @return Returns 0 if their names are the same, a number < 0 if a has a name
+    which comes first, and a number > 0 if b has a name which comes first.
+*/
+int obt_link_cmp_by_name(const void *a, const void *b);
+
 
 G_END_DECLS