parse Path, Terminal, and StartupNotify
[dana/openbox.git] / 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             ObtLinkAppStartup startup;
53             gchar *startup_wmclass;
54         } app;
55         struct _ObtLinkLink {
56             gchar *addr;
57         } url;
58         struct _ObtLinkDir {
59         } dir;
60     } d;
61 };
62
63 ObtLink* obt_link_from_ddfile(const gchar *ddname, GSList *paths,
64                               ObtPaths *p)
65 {
66     ObtLink *link;
67     GHashTable *groups, *keys;
68     ObtDDParseGroup *g;
69     ObtDDParseValue *v, *type, *name, *target;
70
71     groups = obt_ddparse_file(ddname, paths);
72     if (!groups) return NULL;
73     g = g_hash_table_lookup(groups, "Desktop Entry");
74     if (!g) {
75         g_hash_table_destroy(groups);
76         return NULL;
77     }
78
79     keys = obt_ddparse_group_keys(g);
80
81     /* check that required keys exist */
82
83     if (!(type = g_hash_table_lookup(keys, "Type")))
84     { g_hash_table_destroy(groups); return NULL; }
85     if (!(name = g_hash_table_lookup(keys, "Name")))
86     { g_hash_table_destroy(groups); return NULL; }
87
88     if (type->value.enumerable == OBT_LINK_TYPE_APPLICATION) {
89         if (!(target = g_hash_table_lookup(keys, "Exec")))
90         { g_hash_table_destroy(groups); return NULL; }
91     }
92     else if (type->value.enumerable == OBT_LINK_TYPE_URL) {
93         if (!(target = g_hash_table_lookup(keys, "URL")))
94         { g_hash_table_destroy(groups); return NULL; }
95     }
96     else
97         target = NULL;
98
99     /* parse all the optional keys and build ObtLink (steal the strings) */
100     link = g_slice_new0(ObtLink);
101     link->ref = 1;
102     link->type = type->value.enumerable;
103     if (link->type == OBT_LINK_TYPE_APPLICATION)
104         link->d.app.exec = target->value.string, target->value.string = NULL;
105     else if (link->type == OBT_LINK_TYPE_URL)
106         link->d.url.addr = target->value.string, target->value.string = NULL;
107     link->display = TRUE;
108
109     if ((v = g_hash_table_lookup(keys, "Hidden")))
110         link->deleted = v->value.boolean;
111
112     if ((v = g_hash_table_lookup(keys, "NoDisplay")))
113         link->display = !v->value.boolean;
114
115     if ((v = g_hash_table_lookup(keys, "GenericName")))
116         link->generic = v->value.string, v->value.string = NULL;
117
118     if ((v = g_hash_table_lookup(keys, "Comment")))
119         link->comment = v->value.string, v->value.string = NULL;
120
121     if ((v = g_hash_table_lookup(keys, "Icon")))
122         link->icon = v->value.string, v->value.string = NULL;
123
124     if ((v = g_hash_table_lookup(keys, "OnlyShowIn")))
125         link->env_required = v->value.environments;
126     else
127         link->env_required = 0;
128
129     if ((v = g_hash_table_lookup(keys, "NotShowIn")))
130         link->env_restricted = v->value.environments;
131     else
132         link->env_restricted = 0;
133
134     if (link->type == OBT_LINK_TYPE_APPLICATION) {
135         if ((v = g_hash_table_lookup(keys, "TryExec"))) {
136             /* XXX spawn a thread to check TryExec? */
137             link->display = link->display &&
138                 obt_paths_try_exec(p, v->value.string);
139         }
140
141         if ((v = g_hash_table_lookup(keys, "Path"))) {
142             /* steal the string */
143             link->d.app.wdir = v->value.string;
144             v->value.string = NULL;
145         }
146
147         if ((v = g_hash_table_lookup(keys, "Terminal")))
148             link->d.app.term = v->value.boolean;
149
150         if ((v = g_hash_table_lookup(keys, "StartupNotify")))
151             link->d.app.startup = v->value.boolean ?
152                 OBT_LINK_APP_STARTUP_PROTOCOL_SUPPORT :
153                 OBT_LINK_APP_STARTUP_NO_SUPPORT;
154         else
155             link->d.app.startup = OBT_LINK_APP_STARTUP_LEGACY_SUPPORT;
156
157         /* XXX parse link->d.app.exec to determine link->d.app.open */
158
159         /* XXX there's more app specific stuff */
160     }
161
162     else if (link->type == OBT_LINK_TYPE_URL) {
163         /* XXX there's URL specific stuff */
164     }
165
166     return link;
167 }
168
169 void obt_link_ref(ObtLink *dd)
170 {
171     ++dd->ref;
172 }
173
174 void obt_link_unref(ObtLink *dd)
175 {
176     if (--dd->ref < 1) {
177         g_slice_free(ObtLink, dd);
178     }
179 }