Filter links in a ObtLinkbase by environments.
authorDana Jansens <danakj@orodu.net>
Mon, 24 Jan 2011 20:13:27 +0000 (15:13 -0500)
committerDana Jansens <danakj@orodu.net>
Sun, 16 Oct 2011 22:54:04 +0000 (18:54 -0400)
Store a set of active environments in a linkbase and only include links in the
linkbase that 1) want to be displayed, 2) pass TryExec, 3) match environment
requirements and restrictions.

obt/link.c
obt/link.h
obt/linkbase.c
obt/linkbase.h

index c54b782675033efe85d146879dd16d8ed05d1496..de665f073e52d9cf5f29fc74fe85f6cc8c9ac3f4 100644 (file)
@@ -252,3 +252,16 @@ gchar* obt_link_id_from_ddfile(const gchar *filename)
 {
     return obt_ddparse_file_to_id(filename);
 }
+
+gboolean obt_link_display(ObtLink *e, const guint environments)
+{
+    return
+        /* display if the link wants to be displayed and TryExec passed */
+        e->display &&
+        /* display if no environment is required, or we match at least one of
+           the requirements */
+        (!e->env_required || (e->env_required & environments)) &&
+        /* display if no environment is restricted, or we do not match any of
+           the restrictions */
+        (!e->env_restricted || !(e->env_restricted & environments));
+}
index 749514de5ff71b7fda4d9e873566eeb69bc9918f..973f1132c4648fa62200b5d13ddee125bd01dc26 100644 (file)
@@ -97,10 +97,9 @@ ObtLinkType obt_link_type (ObtLink *e);
 /*! Returns TRUE if the .desktop file should be displayed to users, given the
     current    environment.  If FALSE, the .desktop file should not be showed.
        This also uses the TryExec option if it is present.
-    @env A semicolon-deliminated list of environemnts.  Can be one or more of:
-         GNOME, KDE, ROX, XFCE.  Other environments not listed here may also
-         be supported.  This can be null also if not listing any environment. */
-gboolean obt_link_display(ObtLink *e, const gchar *env);
+    @param environments A bitflags of values from ObtLinkEnvFlags indicating
+      the active environments. */
+gboolean obt_link_display(ObtLink *e, const guint environments);
 
 const gchar* obt_link_name           (ObtLink *e);
 const gchar* obt_link_generic_name   (ObtLink *e);
index 702a598acdfe38158021dcb878c0cfe7dbae186d..af4c29a6d4a8c660623bda26b31d1ec5cf24f76c 100644 (file)
@@ -38,6 +38,10 @@ struct _ObtLinkBaseEntry {
 struct _ObtLinkBase {
     gint ref;
 
+    /*! A bitflag of values from ObtLinkEnvFlags indicating which environments
+      are to be considered active. */
+    guint environments;
+
     const gchar *language;
     const gchar *country;
     const gchar *modifier;
@@ -142,22 +146,31 @@ static void update(ObtWatch *w, const gchar *base_path,
     }
 
     if (add) {
-        ObtLinkBaseEntry *e = g_slice_new(ObtLinkBaseEntry);
-        e->priority = *priority;
-        e->link = obt_link_from_ddfile(full_path, self->paths,
-                                       self->language, self->country,
-                                       self->modifier);
-        list = g_slist_insert_before(list, it, e);
+        ObtLink *link;
 
-        /* this will free 'id' */
-        g_hash_table_insert(self->base, id, list);
-        id = NULL;
+        link = obt_link_from_ddfile(full_path, self->paths,
+                                    self->language, self->country,
+                                    self->modifier);
+        if (!obt_link_display(link, self->environments)) {
+            obt_link_unref(link);
+        }
+        else {
+            ObtLinkBaseEntry *e = g_slice_new(ObtLinkBaseEntry);
+            e->priority = *priority;
+            e->link = link;
+            list = g_slist_insert_before(list, it, e);
+
+            /* this will free 'id' */
+            g_hash_table_insert(self->base, id, list);
+            id = NULL;
+        }
     }
 
     g_free(id);
 }
 
-ObtLinkBase* obt_linkbase_new(ObtPaths *paths, const gchar *locale)
+ObtLinkBase* obt_linkbase_new(ObtPaths *paths, const gchar *locale,
+                              guint environments)
 {
     ObtLinkBase *self;
     GSList *it;
@@ -165,6 +178,7 @@ ObtLinkBase* obt_linkbase_new(ObtPaths *paths, const gchar *locale)
     gint i;
     
     self = g_slice_new0(ObtLinkBase);
+    self->environments = environments;
     self->watch = obt_watch_new();
     self->base = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
                                     (GDestroyNotify)base_entry_list_free);
index 57602788afd42dc0ecc0f32e868c899f1129172d..4d2b7c4433bd8dcc8824a4481735abdfdb19ebf3 100644 (file)
@@ -30,8 +30,11 @@ typedef struct _ObtLinkBase ObtLinkBase;
 /*! Create a new database of ObtLinks.
   @param paths An ObtPaths structure.
   @param locale The value of LC_MESSAGES.
+  @param environments A bitflag of values from ObtLinkEnvFlags indicating
+    which environments are to be considered active.
 */
-ObtLinkBase* obt_linkbase_new(struct _ObtPaths *paths, const gchar *locale);
+ObtLinkBase* obt_linkbase_new(struct _ObtPaths *paths, const gchar *locale,
+                              guint environments);
 void obt_linkbase_ref(ObtLinkBase *lb);
 void obt_linkbase_unref(ObtLinkBase *lb);