~cairo-dock-team/cairo-dock-plug-ins/experimental

« back to all changes in this revision

Viewing changes to Doncky/src/applet-xml.c

  • Committer: nochka85
  • Date: 2010-01-31 14:38:19 UTC
  • mto: This revision was merged to the branch mainline in revision 1490.
  • Revision ID: nochka85@nochka85-desktop-20100131143819-j6nno0cl7yxssexq
Doncky - 1st release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
* This file is a part of the Cairo-Dock project
 
3
*
 
4
* Copyright : (C) see the 'copyright' file.
 
5
* E-mail    : see the 'copyright' file.
 
6
*
 
7
* This program is free software; you can redistribute it and/or
 
8
* modify it under the terms of the GNU General Public License
 
9
* as published by the Free Software Foundation; either version 3
 
10
* of the License, or (at your option) any later version.
 
11
*
 
12
* This program is distributed in the hope that it will be useful,
 
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
* GNU General Public License for more details.
 
16
* You should have received a copy of the GNU General Public License
 
17
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
18
*/
 
19
 
 
20
 
 
21
#include <string.h>
 
22
#include <math.h>
 
23
#include <libxml/tree.h>
 
24
#include <libxml/parser.h>
 
25
 
 
26
#include <cairo-dock.h>
 
27
#include "applet-struct.h"
 
28
#include "applet-xml.h"
 
29
#include "applet-draw.h"
 
30
 
 
31
 
 
32
 
 
33
 
 
34
void cd_doncky_free_item (TextZone *pTextZone)
 
35
{
 
36
        if (pTextZone == NULL)
 
37
                return;
 
38
        
 
39
        //g_free (pTextZone->cFont);  // Pourquoi çà plante ??
 
40
        g_free (pTextZone->cText);
 
41
        g_free (pTextZone->cCommand);
 
42
        g_free (pTextZone);     
 
43
}
 
44
 
 
45
 
 
46
void cd_doncky_free_item_list (CairoDockModuleInstance *myApplet)
 
47
{
 
48
        if (myData.pTextZoneList == NULL)
 
49
                return;
 
50
        
 
51
        
 
52
        TextZone *pTextZone;    
 
53
        GList *t;
 
54
        for (t = myData.pTextZoneList; t != NULL; t = t->next)
 
55
        {
 
56
                pTextZone=  t->data;
 
57
                cd_doncky_free_item (pTextZone);
 
58
        }
 
59
        g_list_free (myData.pTextZoneList);
 
60
        myData.pTextZoneList = NULL;
 
61
}
 
62
 
 
63
gboolean cd_doncky_readxml (CairoDockModuleInstance *myApplet)
 
64
{
 
65
        // On va lire le contenu de myConfig.cXmlFilePath       
 
66
        cd_debug ("Doncky-debug : ---------------------->  myConfig.cXmlFilePath = \"%s\"",myConfig.cXmlFilePath);
 
67
        
 
68
        
 
69
        g_return_val_if_fail (myConfig.cXmlFilePath != NULL, FALSE);
 
70
        xmlInitParser ();
 
71
        xmlDocPtr pXmlFile;
 
72
        xmlNodePtr pXmlMainNode;
 
73
        
 
74
        
 
75
        pXmlFile = cairo_dock_open_xml_file (myConfig.cXmlFilePath, "code", &pXmlMainNode, NULL);
 
76
        
 
77
        g_return_val_if_fail (pXmlFile != NULL && pXmlMainNode != NULL, FALSE);
 
78
                
 
79
        
 
80
        xmlAttrPtr ap;
 
81
        xmlChar *cAttribute, *cNodeContent, *cTextNodeContent;
 
82
        TextZone *pTextZone = NULL;
 
83
                
 
84
        xmlNodePtr pXmlNode;
 
85
        
 
86
        int i;
 
87
        for (pXmlNode = pXmlMainNode->children, i = 0; pXmlNode != NULL; pXmlNode = pXmlNode->next, i ++)
 
88
        {
 
89
                
 
90
                if (xmlStrcmp (pXmlNode->name, (const xmlChar *) "zone") == 0)
 
91
                {
 
92
                        pTextZone = g_new0 (TextZone, 1);
 
93
                        myData.pTextZoneList = g_list_append (myData.pTextZoneList, pTextZone); 
 
94
                        
 
95
                        pTextZone->cCommand = NULL;
 
96
                        pTextZone->bRefresh = FALSE;
 
97
                        pTextZone->bEndOfLine = FALSE;  
 
98
                        pTextZone->bBar = FALSE;
 
99
                        
 
100
                        
 
101
                        xmlNodePtr pXmlSubNode;                 
 
102
                        for (pXmlSubNode = pXmlNode->children; pXmlSubNode != NULL; pXmlSubNode = pXmlSubNode->next)
 
103
                        {                               
 
104
                                cNodeContent = xmlNodeGetContent (pXmlSubNode);
 
105
                                
 
106
                                // On gère le refresh à part pour lui imposer à 0 si rien n'est renseigné
 
107
                                if (xmlStrcmp (pXmlSubNode->name, (const xmlChar *) "refresh") == 0)
 
108
                                {
 
109
                                        pTextZone->iRefresh = g_strtod (cNodeContent, NULL);
 
110
                                        pTextZone->bRefresh = TRUE;
 
111
                                                pTextZone->iTimer = pTextZone->iRefresh - 2 ;   // On triche sur le timer à la lecture du xml pour avoir un refresh                                             
 
112
                                }
 
113
                                else
 
114
                                {
 
115
                                        if (xmlStrcmp (pXmlSubNode->name, (const xmlChar *) "cmd") == 0 || xmlStrcmp (pXmlSubNode->name, (const xmlChar *) "echo") == 0 || xmlStrcmp (pXmlSubNode->name, (const xmlChar *) "internal") == 0)
 
116
                                        {
 
117
                                                pTextZone->bRefresh = TRUE;
 
118
                                                pTextZone->iRefresh = 0;
 
119
                                        }
 
120
                                }                               
 
121
                                
 
122
                                // On gère l'alignement à part pour lui imposer à 'left' si rien n'est renseigné (ou si mal renseigné)
 
123
                                if (xmlStrcmp (pXmlSubNode->name, (const xmlChar *) "align") == 0)
 
124
                                {
 
125
                                        pTextZone->cAlign = xmlNodeGetContent (pXmlSubNode);                                    
 
126
                                        if (strcmp (pTextZone->cAlign, "left") == 0)
 
127
                                                pTextZone->cAlign = g_strdup_printf("left");
 
128
                                        else if (strcmp (pTextZone->cAlign, "center") == 0)
 
129
                                                pTextZone->cAlign = g_strdup_printf("center");
 
130
                                        else if (strcmp (pTextZone->cAlign, "right") == 0)
 
131
                                                pTextZone->cAlign = g_strdup_printf("right");
 
132
                                        else
 
133
                                                pTextZone->cAlign = g_strdup_printf("left");
 
134
                                }
 
135
                                else if (pTextZone->cAlign == NULL)
 
136
                                        pTextZone->cAlign = g_strdup_printf("left");            
 
137
                                
 
138
                                
 
139
                                if (xmlStrcmp (pXmlSubNode->name, (const xmlChar *) "font") == 0)
 
140
                                {
 
141
                                        pTextZone->cFont = xmlNodeGetContent (pXmlSubNode);                                     
 
142
                                }
 
143
                                else if (xmlStrcmp (pXmlSubNode->name, (const xmlChar *) "red") == 0)
 
144
                                {
 
145
                                        pTextZone->fTextColor[0] = g_strtod (cNodeContent, NULL);                                       
 
146
                                }
 
147
                                else if (xmlStrcmp (pXmlSubNode->name, (const xmlChar *) "green") == 0)
 
148
                                {
 
149
                                        pTextZone->fTextColor[1] = g_strtod (cNodeContent, NULL);                                       
 
150
                                }
 
151
                                else if (xmlStrcmp (pXmlSubNode->name, (const xmlChar *) "blue") == 0)
 
152
                                {
 
153
                                        pTextZone->fTextColor[2] = g_strtod (cNodeContent, NULL);                                       
 
154
                                }
 
155
                                else if (xmlStrcmp (pXmlSubNode->name, (const xmlChar *) "alpha") == 0)
 
156
                                {
 
157
                                        pTextZone->fTextColor[3] = g_strtod (cNodeContent, NULL);                                       
 
158
                                }
 
159
                                else if (xmlStrcmp (pXmlSubNode->name, (const xmlChar *) "txt") == 0)
 
160
                                {
 
161
                                        pTextZone->cText = xmlNodeGetContent (pXmlSubNode);                                     
 
162
                                }
 
163
                                else if (xmlStrcmp (pXmlSubNode->name, (const xmlChar *) "echo") == 0)
 
164
                                {
 
165
                                        // On insère sh -c 'echo " AVANT la commande et "' APRES
 
166
                                        gchar *cXmlCommand;
 
167
                                        cXmlCommand = xmlNodeGetContent (pXmlSubNode);                  
 
168
                                        
 
169
                                        GString *sTemp =  g_string_new  ("");   
 
170
                                        g_string_printf (sTemp, "sh -c 'echo \"%s\"'", cXmlCommand);                                                    
 
171
                                        pTextZone->cCommand = g_strdup_printf("%s",sTemp->str) ;
 
172
                                        
 
173
                                        g_string_free (sTemp, TRUE);
 
174
                                        g_free (cXmlCommand);                                                                           
 
175
                                }
 
176
                                else if (xmlStrcmp (pXmlSubNode->name, (const xmlChar *) "cmd") == 0)
 
177
                                {
 
178
                                        pTextZone->cCommand = xmlNodeGetContent (pXmlSubNode);                                                                                                                  
 
179
                                }                       
 
180
                                else if (xmlStrcmp (pXmlSubNode->name, (const xmlChar *) "br") == 0)
 
181
                                {
 
182
                                        pTextZone->bEndOfLine = TRUE;
 
183
                                        pTextZone->bNextNewLine = TRUE;                                                                                                         
 
184
                                }
 
185
                                else if (xmlStrcmp (pXmlSubNode->name, (const xmlChar *) "nbr") == 0)
 
186
                                {
 
187
                                        pTextZone->bEndOfLine = TRUE;
 
188
                                        pTextZone->bNextNewLine = FALSE;                                                                                                                
 
189
                                }
 
190
                                else if (xmlStrcmp (pXmlSubNode->name, (const xmlChar *) "internal") == 0)
 
191
                                        pTextZone->cInternal = xmlNodeGetContent (pXmlSubNode);
 
192
                                
 
193
                                
 
194
                                
 
195
                                else if (xmlStrcmp (pXmlSubNode->name, (const xmlChar *) "lbar") == 0)
 
196
                                {
 
197
                                        gchar *cTempo = xmlNodeGetContent (pXmlSubNode);
 
198
 
 
199
                                        // On récupère le 1er champ -> = commande interne
 
200
                                        g_strreverse (cTempo);
 
201
                                        cTempo = strrchr(cTempo, ';') ;
 
202
                                        ltrim( cTempo, ";" );
 
203
                                        g_strreverse (cTempo);
 
204
                                        pTextZone->cInternal = g_strdup_printf("%s", cTempo);
 
205
                                        
 
206
                                        // On récupère le 2ème champ -> = Largeur
 
207
                                        cTempo = strchr(xmlNodeGetContent (pXmlSubNode), ';') ;
 
208
                                        ltrim( cTempo, ";" );
 
209
                                        g_strreverse (cTempo);                                  
 
210
                                        cTempo = strchr(cTempo, ';') ;                  
 
211
                                        ltrim( cTempo, ";" );
 
212
                                        g_strreverse (cTempo);                                                                          
 
213
                                        pTextZone->iWidth = atoi(cTempo); 
 
214
                                        
 
215
                                        // On récupère le 3ème champ -> = Hauteur
 
216
                                        cTempo = strrchr(xmlNodeGetContent (pXmlSubNode), ';') ;
 
217
                                        ltrim( cTempo, ";" );                                   
 
218
                                        pTextZone->iHeight = atoi(cTempo); 
 
219
                                        
 
220
                                        pTextZone->bLimitedBar = TRUE;                                  
 
221
                                }
 
222
                                
 
223
                                else if (xmlStrcmp (pXmlSubNode->name, (const xmlChar *) "bar") == 0)
 
224
                                {
 
225
                                        gchar *cTempo = xmlNodeGetContent (pXmlSubNode);
 
226
 
 
227
                                        // On récupère le 1er champ -> = commande interne
 
228
                                        g_strreverse (cTempo);
 
229
                                        cTempo = strrchr(cTempo, ';') ;
 
230
                                        ltrim( cTempo, ";" );
 
231
                                        g_strreverse (cTempo);
 
232
                                        pTextZone->cInternal = g_strdup_printf("%s", cTempo);
 
233
                                                                                
 
234
                                        // On récupère le 3ème champ -> = Hauteur
 
235
                                        cTempo = strrchr(xmlNodeGetContent (pXmlSubNode), ';') ;
 
236
                                        ltrim( cTempo, ";" );                                   
 
237
                                        pTextZone->iHeight = atoi(cTempo); 
 
238
                                        
 
239
                                        pTextZone->bBar = TRUE;                                 
 
240
                                }
 
241
                                else if (xmlStrcmp (pXmlSubNode->name, (const xmlChar *) "img") == 0)
 
242
                                {
 
243
                                        pTextZone->cImgPath = xmlNodeGetContent (pXmlSubNode);
 
244
                                        pTextZone->bImgDraw=FALSE;              
 
245
                                }
 
246
                                else if (xmlStrcmp (pXmlSubNode->name, (const xmlChar *) "imgsize") == 0)
 
247
                                {
 
248
                                        pTextZone->iImgSize = g_strtod (cNodeContent, NULL);                            
 
249
                                }
 
250
                                else if (xmlStrcmp (pXmlSubNode->name, (const xmlChar *) "imgsizeW") == 0)
 
251
                                {
 
252
                                        pTextZone->iWidth= g_strtod (cNodeContent, NULL);                               
 
253
                                }
 
254
                                else if (xmlStrcmp (pXmlSubNode->name, (const xmlChar *) "imgsizeH") == 0)
 
255
                                {
 
256
                                        pTextZone->iHeight = g_strtod (cNodeContent, NULL);                             
 
257
                                }
 
258
                                                
 
259
                                
 
260
                                xmlFree (cNodeContent);                         
 
261
                        }                       
 
262
                }
 
263
        }       
 
264
        cairo_dock_close_xml_file (pXmlFile);
 
265
                
 
266
        return TRUE;
 
267
}