-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.c
261 lines (225 loc) · 9.48 KB
/
parser.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/* AUTHOR: Naveen Kumar Molleti
* EMAIL: [email protected]
* GIT Read-only URL: git://github.com/NOLFXceptMe/pcmanfm2k10.git
*
* Parser module for .desktop files
* To be integrated into PCManFM
* Work as a part of GSoC 2010
*
*/
#include <stdio.h>
#include <glib.h>
#include "parser.h"
FmDesktopEntry* parse(gchar* file_name)
{
if(file_name == NULL)
return NULL;
gchar *base_name = g_path_get_basename(file_name);
if(g_str_has_suffix(base_name, ".desktop") != TRUE){
fprintf(stderr, "PARSER:: Invalid file type\n");
return NULL;
}
gchar *desktop_file_id = g_strndup(base_name, strlen(base_name) - strlen(".desktop"));
g_free(base_name);
GKeyFile *keyfile;
GKeyFileFlags flags;
GError *error = NULL;
gsize n_groups;
gchar **group_names;
gsize i;
gchar *type_string;
gchar *profile_id;
guint type;
FmDesktopEntry *fmDesktopEntry = NULL;
FmActionEntry *fmActionEntry;
FmProfileEntry *fmProfileEntry;
FmMenuEntry *fmMenuEntry;
keyfile = g_key_file_new();
flags = G_KEY_FILE_KEEP_COMMENTS|G_KEY_FILE_KEEP_TRANSLATIONS;
if(!g_key_file_load_from_file(keyfile, file_name, flags, &error)){
fprintf(stderr, "PARSER:: %s\n", file_name);
fprintf(stderr, "PARSER:: %s\n", error->message);
return NULL;
}
/* Initialize desktop entry structure */
fmDesktopEntry = g_slice_new0(FmDesktopEntry);
fmDesktopEntry->desktop_file_id = g_strdup(desktop_file_id);
fmDesktopEntry->n_action_entries = 0;
fmDesktopEntry->n_profile_entries = 0;
fmDesktopEntry->n_menu_entries = 0;
fmDesktopEntry->fmActionEntries = g_ptr_array_new();
fmDesktopEntry->fmProfileEntries = g_ptr_array_new();
fmDesktopEntry->fmMenuEntries = g_ptr_array_new();
#ifndef NDEBUG
//printf("Extracted file_id is %s\n", fmDesktopEntry->desktop_file_id);
#endif
group_names = g_key_file_get_groups(keyfile, &n_groups);
/* Store action entries, profile entries, and menu entries in respective data structures */
for(i=0;i<n_groups;++i){
#ifndef NDEBUG
//printf("Name of the group is %s\n", group_names[i]);
#endif
/* Action entry or Menu entry */
if(g_strcmp0(group_names[i], "Desktop Entry") == 0){
type_string = g_key_file_get_string(keyfile, group_names[i], "Type", NULL);
if(type_string == NULL || g_strcmp0(type_string, "Action") == 0){
type = ACTION_ENTRY;
fmActionEntry = parse_action_entry(keyfile, group_names[i]);
if(fmActionEntry != NULL){
fmActionEntry->id = g_strdup(desktop_file_id);
fmDesktopEntry->n_action_entries++;
g_ptr_array_add(fmDesktopEntry->fmActionEntries, (gpointer) fmActionEntry);
}
}
else if(g_strcmp0(type_string, "Menu") == 0){
type = MENU_ENTRY;
fmMenuEntry = parse_menu_entry(keyfile, group_names[i]);
if(fmMenuEntry != NULL){
fmMenuEntry->id = g_strdup(desktop_file_id);
fmDesktopEntry->n_menu_entries++;
g_ptr_array_add(fmDesktopEntry->fmMenuEntries, (gpointer) fmMenuEntry);
}
}
else if(g_strcmp0(type_string, "Application") == 0){
type = APPLICATION_ENTRY;
}
else if(g_strcmp0(type_string, "Link") == 0){
type = LINK_ENTRY;
}
else if(g_strcmp0(type_string, "Directory") == 0){
type = DIRECTORY_ENTRY;
}
else {
type = UNKNOWN_ENTRY;
}
}
else if(g_str_has_prefix(group_names[i], "X-Action-Profile ") == TRUE){
type = PROFILE_ENTRY;
profile_id = g_strdup(group_names[i]+ 17); /* "X-Action-Profile " is 17 characters long */
fmProfileEntry = parse_profile_entry(keyfile, group_names[i]);
if(fmProfileEntry != NULL){
fmProfileEntry->id = g_strdup(profile_id);
fmDesktopEntry->n_profile_entries++;
g_ptr_array_add(fmDesktopEntry->fmProfileEntries, (gpointer) fmProfileEntry);
}
g_free(profile_id);
}
else {
type = UNKNOWN_ENTRY;
}
}
g_free(desktop_file_id);
//printf("Parsed %s\n", file_name);
return fmDesktopEntry;
}
FmActionEntry* parse_action_entry(GKeyFile *keyfile, gchar *group_name)
{
FmActionEntry *ae = g_slice_new0(FmActionEntry);
GError *error = NULL;
/* TODO: We have a problem here with parsing locale strings */
ae->name = g_key_file_get_locale_string(keyfile, group_name, "Name", "en_US.UTF-8", &error);
if(ae->name == NULL){
fprintf(stderr, "Can't find name for Action Entry\n");
#ifndef NDEBUG
fprintf(stderr, "%s\n", error->message);
#endif
return NULL;
}
ae->type = g_key_file_get_string(keyfile, group_name, "Type", NULL);
if(ae->type == NULL){
ae->type = "Action";
}
ae->tooltip = g_key_file_get_locale_string(keyfile, group_name, "Tooltip", NULL, NULL);
ae->icon = g_key_file_get_locale_string(keyfile, group_name, "Icon", NULL, NULL);
ae->description = g_key_file_get_locale_string(keyfile, group_name, "Description", NULL, NULL);
ae->suggestedshortcut = g_key_file_get_locale_string(keyfile, group_name, "SuggestedShortcut", NULL, NULL);
ae->enabled = g_key_file_get_boolean(keyfile, group_name, "Enabled", NULL);
ae->hidden = g_key_file_get_boolean(keyfile, group_name, "Hidden", NULL);
ae->targetcontext = g_key_file_get_boolean(keyfile, group_name, "TargetContext", NULL);
ae->targetlocation = g_key_file_get_boolean(keyfile, group_name, "TargetLocation", NULL);
ae->targettoolbar = g_key_file_get_boolean(keyfile, group_name, "TargetToolbar", NULL);
ae->toolbarlabel = g_key_file_get_locale_string(keyfile, group_name, "ToolbarLabel", NULL, NULL);
ae->profiles = g_key_file_get_string_list(keyfile, group_name, "Profiles", &ae->n_profiles, NULL);
/* Parse the conditions associated with this entry */
ae->conditions = parse_conditions(keyfile, group_name);
return ae;
}
FmProfileEntry* parse_profile_entry(GKeyFile *keyfile, gchar *group_name)
{
FmProfileEntry *pe = g_slice_new0(FmProfileEntry);
GError *error;
pe->exec = g_key_file_get_string(keyfile, group_name, "Exec", &error);
if(pe->exec == NULL){
fprintf(stderr, "Cannot find Exec key for Profile Entry\n");
#ifndef NDEBUG
fprintf(stderr, "%s\n", error->message);
#endif
return NULL;
}
pe->name = g_key_file_get_locale_string(keyfile, group_name, "Name", NULL, NULL);
pe->path = g_key_file_get_string(keyfile, group_name, "Path", NULL);
pe->executionmode = g_key_file_get_string(keyfile, group_name, "ExecutionMode", NULL);
pe->startupnotify = g_key_file_get_boolean(keyfile, group_name, "StartupNotify", NULL);
pe->startupwmclass = g_key_file_get_string(keyfile, group_name, "StartupWMClass", NULL);
pe->executeas = g_key_file_get_string(keyfile, group_name, "ExecuteAs", NULL);
pe->conditions = parse_conditions(keyfile, group_name);
return pe;
}
FmMenuEntry* parse_menu_entry(GKeyFile *keyfile, gchar *group_name)
{
FmMenuEntry *me = g_slice_new0(FmMenuEntry);
GError *error;
me->type = g_key_file_get_string(keyfile, group_name, "Type", &error);
if(me->type == NULL || g_ascii_strcasecmp(me->type, "Menu") != 0){
fprintf(stderr, "Cannot find valid type for Menu\n");
#ifndef NDEBUG
fprintf(stderr, "%s\n", error->message);
#endif
g_error_free(error);
return NULL;
}
me->name = g_key_file_get_locale_string(keyfile, group_name, "Name", NULL, &error);
if(me->name == NULL){
fprintf(stderr, "Cannot find name for Menu\n");
#ifndef NDEBUG
fprintf(stderr, "%s\n", error->message);
#endif
g_error_free(error);
return NULL;
}
me->tooltip = g_key_file_get_locale_string(keyfile, group_name, "Tooltip", NULL, NULL);
me->icon = g_key_file_get_locale_string(keyfile, group_name, "Icon", NULL, NULL);
me->description = g_key_file_get_locale_string(keyfile, group_name, "Description", NULL, NULL);
me->suggestedshortcut = g_key_file_get_string(keyfile, group_name, "SuggestedShortcut", NULL);
me->enabled = g_key_file_get_boolean(keyfile, group_name, "Enabled", NULL);
me->hidden = g_key_file_get_boolean(keyfile, group_name, "Hidden", NULL);
me->itemslist = g_key_file_get_string_list(keyfile, group_name, "ItemsList", &me->n_itemslist, NULL);
me->conditions = parse_conditions(keyfile, group_name);
return me;
}
FmConditions* parse_conditions(GKeyFile *keyfile, gchar *group_name)
{
FmConditions *conditions = g_slice_new0(FmConditions);
GError *error = NULL;
conditions->onlyshowin = g_key_file_get_string_list(keyfile, group_name, "OnlyShowIn", &conditions->n_onlyshowin, NULL);
conditions->notshowin = g_key_file_get_string_list(keyfile, group_name, "NotShowIn", &conditions->n_notshowin, NULL);
conditions->tryexec = g_key_file_get_string(keyfile, group_name, "TryExec", NULL);
conditions->showifregistered = g_key_file_get_string(keyfile, group_name, "ShowIfRegistered", NULL);
conditions->showiftrue = g_key_file_get_string(keyfile, group_name, "ShowIfTrue", NULL);
conditions->showifrunning = g_key_file_get_string(keyfile, group_name, "ShowIfRunning", NULL);
conditions->mimetypes = g_key_file_get_string_list(keyfile, group_name, "MimeTypes", &conditions->n_mimetypes, NULL);
conditions->basenames = g_key_file_get_string_list(keyfile, group_name, "Basenames", &conditions->n_basenames, NULL);
conditions->matchcase = g_key_file_get_boolean(keyfile, group_name, "Matchcase", &error);
if(error){
if(error->code == G_KEY_FILE_ERROR_KEY_NOT_FOUND|| error->code == G_KEY_FILE_ERROR_INVALID_VALUE){
conditions->matchcase = TRUE;
g_error_free(error);
error = NULL;
}
}
conditions->selectioncount = g_key_file_get_string(keyfile, group_name, "SelectionCount", NULL);
conditions->schemes = g_key_file_get_string_list(keyfile, group_name, "Schemes", &conditions->n_schemes, NULL);
conditions->folderlist = g_key_file_get_string_list(keyfile, group_name, "Folders", &conditions->n_folderlist, NULL);
conditions->capabilities = g_key_file_get_string_list(keyfile, group_name, "Capabilities", &conditions->n_capabilities, NULL);
return conditions;
}