]> Dogcows Code - chaz/openbox/blob - obt/link.c
parse Categories tag and save them as an array of GQuarks
[chaz/openbox] / obt / link.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3 obt/link.c for the Openbox window manager
4 Copyright (c) 2009 Dana Jansens
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 See the COPYING file for a copy of the GNU General Public License.
17 */
18
19 #include "obt/link.h"
20 #include "obt/ddparse.h"
21 #include "obt/paths.h"
22 #include <glib.h>
23
24 struct _ObtLink {
25 guint ref;
26
27 ObtLinkType type;
28 gchar *name; /*!< Specific name for the object (eg Firefox) */
29 gboolean display; /*<! When false, do not display this link in menus or
30 launchers, etc */
31 gboolean deleted; /*<! When true, the Link could exist but is deleted
32 for the current user */
33 gchar *generic; /*!< Generic name for the object (eg Web Browser) */
34 gchar *comment; /*!< Comment/description to display for the object */
35 gchar *icon; /*!< Name/path for an icon for the object */
36 guint env_required; /*!< The environments that must be present to use this
37 link. */
38 guint env_restricted; /*!< The environments that must _not_ be present to
39 use this link. */
40
41 union _ObtLinkData {
42 struct _ObtLinkApp {
43 gchar *exec; /*!< Executable to run for the app */
44 gchar *wdir; /*!< Working dir to run the app in */
45 gboolean term; /*!< Run the app in a terminal or not */
46 ObtLinkAppOpen open;
47
48 /* XXX gchar**? or something better, a mime struct.. maybe
49 glib has something i can use. */
50 gchar **mime; /*!< Mime types the app can open */
51
52 GQuark *categories; /*!< Array of quarks representing the
53 application's categories */
54
55 ObtLinkAppStartup startup;
56 gchar *startup_wmclass;
57 } app;
58 struct _ObtLinkLink {
59 gchar *addr;
60 } url;
61 struct _ObtLinkDir {
62 } dir;
63 } d;
64 };
65
66 ObtLink* obt_link_from_ddfile(const gchar *ddname, GSList *paths,
67 ObtPaths *p)
68 {
69 ObtLink *link;
70 GHashTable *groups, *keys;
71 ObtDDParseGroup *g;
72 ObtDDParseValue *v;
73
74 /* parse the file, and get a hash table of the groups */
75 groups = obt_ddparse_file(ddname, paths);
76 if (!groups) return NULL; /* parsing failed */
77 /* grab the Desktop Entry group */
78 g = g_hash_table_lookup(groups, "Desktop Entry");
79 g_assert(g != NULL);
80 /* grab the keys that appeared in the Desktop Entry group */
81 keys = obt_ddparse_group_keys(g);
82
83 /* build the ObtLink (we steal all strings from the parser) */
84 link = g_slice_new0(ObtLink);
85 link->ref = 1;
86 link->display = TRUE;
87
88 v = g_hash_table_lookup(keys, "Type");
89 g_assert(v);
90 link->type = v->value.enumerable;
91
92 if ((v = g_hash_table_lookup(keys, "Hidden")))
93 link->deleted = v->value.boolean;
94
95 if ((v = g_hash_table_lookup(keys, "NoDisplay")))
96 link->display = !v->value.boolean;
97
98 if ((v = g_hash_table_lookup(keys, "GenericName")))
99 link->generic = v->value.string, v->value.string = NULL;
100
101 if ((v = g_hash_table_lookup(keys, "Comment")))
102 link->comment = v->value.string, v->value.string = NULL;
103
104 if ((v = g_hash_table_lookup(keys, "Icon")))
105 link->icon = v->value.string, v->value.string = NULL;
106
107 if ((v = g_hash_table_lookup(keys, "OnlyShowIn")))
108 link->env_required = v->value.environments;
109 else
110 link->env_required = 0;
111
112 if ((v = g_hash_table_lookup(keys, "NotShowIn")))
113 link->env_restricted = v->value.environments;
114 else
115 link->env_restricted = 0;
116
117 /* type-specific keys */
118
119 if (link->type == OBT_LINK_TYPE_APPLICATION) {
120 gchar *c;
121 gboolean percent;
122
123 v = g_hash_table_lookup(keys, "Exec");
124 g_assert(v);
125 link->d.app.exec = v->value.string;
126 v->value.string = NULL;
127
128 /* parse link->d.app.exec to determine link->d.app.open */
129 percent = FALSE;
130 for (c = link->d.app.exec; *c; ++c) {
131 if (*c == '%') percent = !percent;
132 if (percent) {
133 switch (*c) {
134 case 'f': link->d.app.open = OBT_LINK_APP_SINGLE_LOCAL; break;
135 case 'F': link->d.app.open = OBT_LINK_APP_MULTI_LOCAL; break;
136 case 'u': link->d.app.open = OBT_LINK_APP_SINGLE_URL; break;
137 case 'U': link->d.app.open = OBT_LINK_APP_MULTI_URL; break;
138 }
139 }
140 }
141
142 if ((v = g_hash_table_lookup(keys, "TryExec"))) {
143 /* XXX spawn a thread to check TryExec? */
144 link->display = link->display &&
145 obt_paths_try_exec(p, v->value.string);
146 }
147
148 if ((v = g_hash_table_lookup(keys, "Path"))) {
149 /* steal the string */
150 link->d.app.wdir = v->value.string;
151 v->value.string = NULL;
152 }
153
154 if ((v = g_hash_table_lookup(keys, "Terminal")))
155 link->d.app.term = v->value.boolean;
156
157 if ((v = g_hash_table_lookup(keys, "StartupNotify")))
158 link->d.app.startup = v->value.boolean ?
159 OBT_LINK_APP_STARTUP_PROTOCOL_SUPPORT :
160 OBT_LINK_APP_STARTUP_NO_SUPPORT;
161 else {
162 link->d.app.startup = OBT_LINK_APP_STARTUP_LEGACY_SUPPORT;
163 if ((v = g_hash_table_lookup(keys, "StartupWMClass"))) {
164 /* steal the string */
165 link->d.app.startup_wmclass = v->value.string;
166 v->value.string = NULL;
167 }
168 }
169
170 if ((v = g_hash_table_lookup(keys, "Categories"))) {
171 gulong i;
172 gchar *end;
173
174 link->d.app.categories = g_new(GQuark, v->value.strings.n);
175
176 c = end = v->value.strings.s;
177 for (i = 0; i < v->value.strings.n; ++i) {
178 while (*end) ++end;
179 link->d.app.categories[i] = g_quark_from_string(c);
180 c = end = end+1; /* next */
181 }
182 }
183
184 /* XXX do something with mime types */
185 }
186 else if (link->type == OBT_LINK_TYPE_URL) {
187 v = g_hash_table_lookup(keys, "URL");
188 g_assert(v);
189 link->d.url.addr = v->value.string;
190 v->value.string = NULL;
191 }
192
193 /* destroy the parsing info */
194 g_hash_table_destroy(groups);
195
196 return link;
197 }
198
199 void obt_link_ref(ObtLink *dd)
200 {
201 ++dd->ref;
202 }
203
204 void obt_link_unref(ObtLink *dd)
205 {
206 if (--dd->ref < 1) {
207 g_free(dd->name);
208 g_free(dd->generic);
209 g_free(dd->comment);
210 g_free(dd->icon);
211 if (dd->type == OBT_LINK_TYPE_APPLICATION) {
212 g_free(dd->d.app.exec);
213 g_free(dd->d.app.wdir);
214 g_free(dd->d.app.categories);
215 g_free(dd->d.app.startup_wmclass);
216 }
217 else if (dd->type == OBT_LINK_TYPE_URL)
218 g_free(dd->d.url.addr);
219 g_slice_free(ObtLink, dd);
220 }
221 }
This page took 0.042417 seconds and 4 git commands to generate.