gwenhywfar  4.6.0beta
plugindescr.c
Go to the documentation of this file.
1 /***************************************************************************
2  begin : Thu Apr 03 2003
3  copyright : (C) 2003-2010 by Martin Preuss
4  email : martin@libchipcard.de
5 
6  ***************************************************************************
7  * *
8  * This library is free software; you can redistribute it and/or *
9  * modify it under the terms of the GNU Lesser General Public *
10  * License as published by the Free Software Foundation; either *
11  * version 2.1 of the License, or (at your option) any later version. *
12  * *
13  * This library is distributed in the hope that it will be useful, *
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
16  * Lesser General Public License for more details. *
17  * *
18  * You should have received a copy of the GNU Lesser General Public *
19  * License along with this library; if not, write to the Free Software *
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, *
21  * MA 02111-1307 USA *
22  * *
23  ***************************************************************************/
24 
25 #ifdef HAVE_CONFIG_H
26 # include <config.h>
27 #endif
28 
29 #define DISABLE_DEBUGLOG
30 
31 
32 #include "plugindescr_p.h"
33 #include "i18n_l.h"
34 #include <gwenhywfar/buffer.h>
35 #include <gwenhywfar/debug.h>
36 #include <gwenhywfar/directory.h>
37 #include <gwenhywfar/i18n.h>
38 
39 #include <sys/types.h>
40 #include <sys/stat.h>
41 #ifdef HAVE_UNISTD_H
42 # include <unistd.h>
43 #endif
44 #include <errno.h>
45 #include <string.h>
46 #ifdef HAVE_STRINGS_H
47 # include <strings.h>
48 #endif
49 
50 
51 
52 GWEN_LIST_FUNCTIONS(GWEN_PLUGIN_DESCRIPTION, GWEN_PluginDescription)
53 GWEN_LIST2_FUNCTIONS(GWEN_PLUGIN_DESCRIPTION, GWEN_PluginDescription)
54 
55 
56 
57 GWEN_PLUGIN_DESCRIPTION *GWEN_PluginDescription_new(GWEN_XMLNODE *node){
58  GWEN_PLUGIN_DESCRIPTION *pd;
59  const char *p;
60 
61  GWEN_NEW_OBJECT(GWEN_PLUGIN_DESCRIPTION, pd);
62  pd->refCount=1;
63  DBG_MEM_INC("GWEN_PLUGIN_DESCRIPTION", 0);
64  GWEN_LIST_INIT(GWEN_PLUGIN_DESCRIPTION, pd);
65  p=GWEN_XMLNode_GetProperty(node, "name", 0);
66  if (!p) {
67  DBG_ERROR(GWEN_LOGDOMAIN, "Unnamed plugin");
69  return 0;
70  }
71  pd->name=strdup(p);
72  pd->xmlNode=GWEN_XMLNode_dup(node);
73 
74  p=GWEN_XMLNode_GetProperty(node, "i18n", NULL);
75  if (!p) {
76  DBG_WARN(GWEN_LOGDOMAIN, "Plugin has no I18N domain, using GWEN");
77  p="gwenhywfar";
78  }
79  pd->langDomain=strdup(p);
80 
81  p=GWEN_XMLNode_GetProperty(node, "type", 0);
82  if (!p) {
83  DBG_ERROR(GWEN_LOGDOMAIN, "Plugin has no type");
85  return 0;
86  }
87  pd->type=strdup(p);
88 
89  p=GWEN_XMLNode_GetCharValue(node, "version", 0);
90  if (p)
91  pd->version=strdup(p);
92  p=GWEN_XMLNode_GetCharValue(node, "author", 0);
93  if (p)
94  pd->author=strdup(p);
95  p=GWEN_XMLNode_GetCharValue(node, "short", 0);
96  if (p)
97  pd->shortDescr=strdup(p);
98  p=GWEN_XMLNode_GetCharValue(node, "descr", 0);
99  if (p)
100  pd->longDescr=strdup(p);
101  return pd;
102 }
103 
104 
105 
107  if (pd) {
108  assert(pd->refCount);
109  if (pd->refCount==1) {
110  DBG_MEM_DEC("GWEN_PLUGIN_DESCRIPTION");
112  free(pd->path);
113  GWEN_XMLNode_free(pd->xmlNode);
114  free(pd->fileName);
115  free(pd->longDescr);
116  free(pd->shortDescr);
117  free(pd->author);
118  free(pd->version);
119  free(pd->langDomain);
120  free(pd->type);
121  free(pd->name);
122  pd->refCount=0;
123  GWEN_FREE_OBJECT(pd);
124  }
125  else
126  pd->refCount--;
127  }
128 }
129 
130 
131 
133  assert(pd);
134  assert(pd->refCount);
135  pd->refCount++;
136 }
137 
138 
139 
143  const char *s;
144 
145  assert(pd);
147  np->refCount=1;
148  DBG_MEM_INC("GWEN_PLUGIN_DESCRIPTION", 0);
150 
151  s=pd->fileName;
152  if (s) np->fileName=strdup(s);
153 
154  s=pd->path;
155  if (s) np->path=strdup(s);
156  s=pd->name;
157  if (s) np->name=strdup(s);
158  s=pd->type;
159  if (s) np->type=strdup(s);
160  s=pd->langDomain;
161  if (s) np->langDomain=strdup(s);
162  s=pd->shortDescr;
163  if (s) np->shortDescr=strdup(s);
164  s=pd->author;
165  if (s) np->author=strdup(s);
166  s=pd->version;
167  if (s) np->version=strdup(s);
168  s=pd->longDescr;
169  if (s) np->longDescr=strdup(s);
170  np->isActive=pd->isActive;
171  if (pd->xmlNode)
172  np->xmlNode=GWEN_XMLNode_dup(pd->xmlNode);
173 
174  return np;
175 }
176 
177 
178 
181  __attribute__((unused)) void *user_data) {
183  return 0;
184 }
185 
186 
187 
188 void GWEN_PluginDescription_List2_freeAll(GWEN_PLUGIN_DESCRIPTION_LIST2 *pdl){
189  GWEN_PluginDescription_List2_ForEach
190  (pdl,
192  0);
193  GWEN_PluginDescription_List2_free(pdl);
194 }
195 
196 
197 
199  assert(pd);
200  return pd->path;
201 }
202 
203 
204 
206  const char *s){
207  assert(pd);
208  free(pd->path);
209  if (s) pd->path=strdup(s);
210  else pd->path=0;
211 }
212 
213 
214 
216  assert(pd);
217  return pd->name;
218 }
219 
220 
221 
223  assert(pd);
224  return pd->type;
225 }
226 
227 
228 
229 const char*
231  assert(pd);
232  return GWEN_I18N_Translate(pd->langDomain, pd->shortDescr);
233 }
234 
235 
236 
237 const char*
239  assert(pd);
240  return pd->author;
241 }
242 
243 
244 
245 const char*
247  assert(pd);
248  return pd->version;
249 }
250 
251 
252 
253 const char*
255  assert(pd);
256  return GWEN_I18N_Translate(pd->langDomain, pd->longDescr);
257 }
258 
259 
260 
261 int
263  const char *s,
264  GWEN_BUFFER *buf){
265  GWEN_XMLNODE *n;
266 
267  assert(pd);
268  assert(pd->xmlNode);
269 
270  n=GWEN_XMLNode_FindFirstTag(pd->xmlNode, "descr", 0, 0);
271  if (n) {
272  n=GWEN_XMLNode_FindFirstTag(n, "text", "format", s);
273  while (n) {
274  if (0==GWEN_XMLNode_GetProperty(n, "lang", 0)) {
275  int rv;
276 
278  if (rv) {
279  DBG_INFO(GWEN_LOGDOMAIN, "here (%d)", rv);
280  return rv;
281  }
282  return 0;
283  }
284  n=GWEN_XMLNode_FindNextTag(n, "text", "format", s);
285  } /* while */
286  }
287 
288  return -1;
289 }
290 
291 
292 
293 int
295  const char *s,
296  const char *lang,
297  GWEN_BUFFER *buf){
298  GWEN_XMLNODE *n;
299 
300  assert(pd);
301  assert(pd->xmlNode);
302 
303  n=GWEN_XMLNode_FindFirstTag(pd->xmlNode, "descr", 0, 0);
304  if (n) {
305  n=GWEN_XMLNode_FindFirstTag(n, "text", "lang", lang);
306  while (n) {
307  const char *fmt;
308 
309  fmt=GWEN_XMLNode_GetProperty(n, "format", 0);
310  if (fmt && strcasecmp(fmt, s)==0) {
311  int rv;
312 
314  if (rv) {
315  DBG_INFO(GWEN_LOGDOMAIN, "here (%d)", rv);
316  return rv;
317  }
318  return 0;
319  }
320  n=GWEN_XMLNode_FindNextTag(n, "text", "lang", lang);
321  } /* while */
322  }
323 
324  return -1;
325 }
326 
327 
328 
329 int
331  const char *s,
332  GWEN_BUFFER *buf){
333  GWEN_STRINGLIST *langl;
334  int rv;
335 
337  if (langl) {
339 
340  se=GWEN_StringList_FirstEntry(langl);
341  while(se) {
342  const char *l;
343 
345  DBG_NOTICE(GWEN_LOGDOMAIN, "Trying locale \"%s\"", l);
346  assert(l);
347 
349  s,
350  l,
351  buf);
352  if (rv==0)
353  return rv;
354 
356  } /* while */
357  } /* if language list available */
358 
359  /* no localized version found, return text for default language */
361  if (rv) {
362  DBG_INFO(GWEN_LOGDOMAIN, "here (%d)", rv);
363  return rv;
364  }
365 
366  return 0;
367 }
368 
369 
370 const char*
372  assert(pd);
373  return pd->fileName;
374 }
375 
376 
377 
379  const char *s){
380  assert(pd);
381  free(pd->fileName);
382  if (s) pd->fileName=strdup(s);
383  else pd->fileName=0;
384 }
385 
386 
387 
390  assert(pd);
391  return pd->xmlNode;
392 }
393 
394 
395 
396 GWEN_PLUGIN_DESCRIPTION_LIST2 *GWEN_LoadPluginDescrs(const char *path) {
397  GWEN_PLUGIN_DESCRIPTION_LIST2 *pl;
398 
399  pl=GWEN_PluginDescription_List2_new();
400 
401  GWEN_LoadPluginDescrsByType(path, 0, pl);
402  if (GWEN_PluginDescription_List2_GetSize(pl)==0) {
403  GWEN_PluginDescription_List2_free(pl);
404  return 0;
405  }
406  return pl;
407 }
408 
409 
410 
412  assert(pd);
413  return pd->isActive;
414 }
415 
416 
417 
419  assert(pd);
420  pd->isActive=i;
421 }
422 
423 
424 
425 int GWEN_LoadPluginDescrsByType(const char *path,
426  const char *type,
427  GWEN_PLUGIN_DESCRIPTION_LIST2 *pdl){
428  GWEN_DIRECTORY *d;
429  GWEN_BUFFER *nbuf;
430  char nbuffer[64];
431  unsigned int pathLen;
432 
433  if (!path)
434  path="";
435 
436  /* create path */
437  nbuf=GWEN_Buffer_new(0, 256, 0, 1);
438  GWEN_Buffer_AppendString(nbuf, path);
439  pathLen=GWEN_Buffer_GetUsedBytes(nbuf);
440 
441  d=GWEN_Directory_new();
444  "Path \"%s\" is not available",
445  GWEN_Buffer_GetStart(nbuf));
446  GWEN_Buffer_free(nbuf);
448  return -1;
449  }
450 
451  while(!GWEN_Directory_Read(d,
452  nbuffer,
453  sizeof(nbuffer))) {
454  if (strcmp(nbuffer, ".") &&
455  strcmp(nbuffer, "..")) {
456  int nlen;
457 
458  nlen=strlen(nbuffer);
459  if (nlen>3) {
460  if (strcasecmp(nbuffer+nlen-4, ".xml")==0) {
461  struct stat st;
462 
463  GWEN_Buffer_Crop(nbuf, 0, pathLen);
464  GWEN_Buffer_SetPos(nbuf, pathLen);
466  GWEN_Buffer_AppendString(nbuf, nbuffer);
467 
468  if (stat(GWEN_Buffer_GetStart(nbuf), &st)) {
469  DBG_ERROR(GWEN_LOGDOMAIN, "stat(%s): %s",
470  GWEN_Buffer_GetStart(nbuf),
471  strerror(errno));
472  }
473  else {
474  if (!S_ISDIR(st.st_mode)) {
475  GWEN_XMLNODE *fileNode;
476 
477  fileNode=GWEN_XMLNode_new(GWEN_XMLNodeTypeTag, "root");
478  if (GWEN_XML_ReadFile(fileNode,
479  GWEN_Buffer_GetStart(nbuf),
484  "Bad file \"%s\"", GWEN_Buffer_GetStart(nbuf));
485  }
486  else {
487  GWEN_XMLNODE *node;
488  GWEN_XMLNODE *n;
489  GWEN_STRINGLIST *langl;
490 
491  n=0;
492  node=GWEN_XMLNode_FindFirstTag(fileNode, "PluginDescr", 0, 0);
493  if (!node)
494  node=fileNode;
496  if (langl) {
498 
499  se=GWEN_StringList_FirstEntry(langl);
500  while(se) {
501  const char *l;
502 
504  DBG_DEBUG(GWEN_LOGDOMAIN, "Trying locale \"%s\"", l);
505  assert(l);
506  n=GWEN_XMLNode_FindFirstTag(node, "plugin", "lang", l);
507  if (n)
508  break;
510  } /* while */
511  } /* if language list available */
512 
513  if (!n)
514  n=GWEN_XMLNode_FindFirstTag(node, "plugin", 0, 0);
515  if (n) {
517  int loadIt;
518 
519  loadIt=1;
520  if (type) {
521  const char *ft;
522 
523  ft=GWEN_XMLNode_GetProperty(n, "type", 0);
524  if (!ft)
525  loadIt=0;
526  else if (strcasecmp(ft, type)!=0){
527  loadIt=0;
528  }
529  } /* if type specified */
530  if (loadIt) {
532  if (!pd) {
533  DBG_WARN(GWEN_LOGDOMAIN, "Bad plugin description");
534  }
535  else {
537  (pd, GWEN_Buffer_GetStart(nbuf));
538  GWEN_Buffer_Crop(nbuf, 0, pathLen);
539  GWEN_Buffer_SetPos(nbuf, pathLen);
541  (pd, GWEN_Buffer_GetStart(nbuf));
542  GWEN_PluginDescription_List2_PushBack(pdl, pd);
543  }
544  } /* if loadIt */
545  else {
547  "Ignoring file \"%s\" (bad/missing type)",
548  GWEN_Buffer_GetStart(nbuf));
549  }
550  }
551  else {
553  "File \"%s\" does not contain a plugin "
554  "description",
555  GWEN_Buffer_GetStart(nbuf));
556  }
557  }
558  GWEN_XMLNode_free(fileNode);
559  } /* if !dir */
560  } /* if stat was ok */
561  } /* if XML */
562  } /* if name has more than 3 chars */
563  } /* if not "." and not ".." */
564  } /* while */
567  GWEN_Buffer_free(nbuf);
568 
569  return 0;
570 }
571 
572 
573 
574 
575 
576 
577 
578 
579 
580 
581 
582