4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
8 http://www.apache.org/licenses/LICENSE-2.0
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
24 #include <sys/types.h>
34 struct json_object *applications;
37 struct appfwk *appfwk_create()
39 struct appfwk *appfwk = malloc(sizeof * appfwk);
46 appfwk->applications = NULL;
51 void appfwk_addref(struct appfwk *appfwk)
57 void appfwk_unref(struct appfwk *appfwk)
60 if (!--appfwk->refcount) {
61 while (appfwk->nrroots)
62 free(appfwk->roots[--appfwk->nrroots]);
68 int appfwk_add_root(struct appfwk *appfwk, const char *path)
75 /* don't depend on the cwd and unique name */
76 r = realpath(path, NULL);
80 /* avoiding duplications */
82 roots = appfwk->roots;
83 for (i = 0 ; i < n ; i++) {
84 if (!strcmp(r, roots[i])) {
91 roots = realloc(roots, (n + 1) * sizeof(roots[0]));
98 appfwk->roots = roots;
104 static int json_add(struct json_object *obj, const char *key, struct json_object *val)
106 json_object_object_add(obj, key, val);
110 static int json_add_str(struct json_object *obj, const char *key, const char *val)
112 struct json_object *str = json_object_new_string (val ? val : "");
113 return str ? json_add(obj, key, str) : -1;
116 static int json_add_int(struct json_object *obj, const char *key, int val)
118 struct json_object *v = json_object_new_int (val);
119 return v ? json_add(obj, key, v) : -1;
122 static struct json_object *read_app_desc(const char *path)
124 struct wgt_info *info;
125 const struct wgt_desc *desc;
126 struct json_object *result;
129 result = json_object_new_object();
133 info = wgt_info_createat(AT_FDCWD, path, 0, 0, 0);
136 desc = wgt_info_desc(info);
138 appid = alloca(2 + strlen(desc->id) + strlen(desc->version));
139 end = stpcpy(appid, desc->id);
141 strcpy(end, desc->version);
143 if(json_add_str(result, "appid", appid)
144 || json_add_str(result, "id", desc->id)
145 || json_add_str(result, "version", desc->version)
146 || json_add_str(result, "path", path)
147 || json_add_int(result, "width", desc->width)
148 || json_add_int(result, "height", desc->height)
149 || json_add_str(result, "name", desc->name)
150 || json_add_str(result, "description", desc->description)
151 || json_add_str(result, "shortname", desc->name_short)
152 || json_add_str(result, "author", desc->author))
155 wgt_info_unref(info);
159 wgt_info_unref(info);
161 json_object_put(result);
166 static int add_appdesc(struct json_object *appset, struct json_object *app)
168 struct json_object *appid;
170 if (!json_object_object_get_ex(app, "appid", &appid)) {
175 return json_add(appset, json_object_get_string(appid), app);
181 struct json_object *apps;
184 static int enumentries(struct enumdata *data, int (*callto)(struct enumdata *))
189 struct dirent entry, *e;
191 /* opens the directory */
192 dir = opendir(data->path);
196 /* prepare appending entry names */
197 beg = data->path + data->length;
200 /* enumerate entries */
201 rc = readdir_r(dir, &entry, &e);
203 if (entry.d_name[0] != '.' || (entry.d_name[1] && (entry.d_name[1] != '.' || entry.d_name[2]))) {
205 end = stpcpy(beg, entry.d_name);
206 data->length = end - data->path;
207 /* call the function */
212 rc = readdir_r(dir, &entry, &e);
218 static int recordapp(struct enumdata *data)
220 struct json_object *app;
222 app = read_app_desc(data->path);
224 if (!add_appdesc(data->apps, app))
226 json_object_put(app);
231 /* enumerate the versions */
232 static int enumvers(struct enumdata *data)
234 int rc = enumentries(data, recordapp);
235 return !rc || errno != ENOTDIR ? 0 : rc;
238 /* regenerate the list of applications */
239 int appfwk_update_applications(struct appfwk *af)
242 struct enumdata edata;
243 struct json_object *oldapps;
245 /* create the result */
246 edata.apps = json_object_new_object();
247 if (edata.apps == NULL) {
252 for (iroot = 0 ; iroot < af->nrroots ; iroot++) {
253 edata.length = stpcpy(edata.path, af->roots[iroot]) - edata.path;
254 assert(edata.length < sizeof edata.path);
255 /* enumerate the applications */
256 rc = enumentries(&edata, enumvers);
258 json_object_put(edata.apps);
262 /* commit the result */
263 oldapps = af->applications;
264 af->applications = edata.apps;
266 json_object_put(oldapps);
272 struct appfwk *af = appfwk_create();
273 appfwk_add_root(af,"af/apps/");
274 appfwk_update_applications(af);
275 json_object_to_file("/dev/stdout", af->applications);