Code

Translations. French translation minor update.
[inkscape.git] / src / pedro / pedroconfig.cpp
1 /*
2  * Implementation the Pedro mini-XMPP client
3  *
4  * Authors:
5  *   Bob Jamison
6  *
7  * Copyright (C) 2005-2007 Bob Jamison
8  *
9  *  This library is free software; you can redistribute it and/or
10  *  modify it under the terms of the GNU Lesser General Public
11  *  License as published by the Free Software Foundation; either
12  *  version 2.1 of the License, or (at your option) any later version.
13  *
14  *  This library is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  *  Lesser General Public License for more details.
18  *
19  *  You should have received a copy of the GNU Lesser General Public
20  *  License along with this library; if not, write to the Free Software
21  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
22  */
25 /*
26 ====================================================
27 We are expecting an xml file with this format:
29 <pedro>
31     <!-- zero to many of these -->
32     <account>
33         <name>Jabber's Main Server</name>
34         <host>jabber.org</host>
35         <port>5222</port>
36         <username>myname</username>
37         <password>mypassword</password>
38     </account>
40 </pedro>
43 ====================================================
44 */
48 #include "pedroconfig.h"
49 #include "pedrodom.h"
51 #include <stdio.h>
52 #include <cstring>
53 #include <string>
54 #include <cstdlib>
55 #include <stdlib.h>
56 #include <string.h>
58 namespace Pedro
59 {
62 static long  getInt(const DOMString &s)
63 {
64     char *start = (char *) s.c_str();
65     char *end;
66     long val = strtol(start, &end, 10);
67     if (end == start) // did we read more than 1 digit?
68         val = 0L;
69     return val;
70 }
74 bool XmppConfig::read(const DOMString &buffer)
75 {
76     Parser parser;
78     Element *root = parser.parse(buffer);
80     if (!root)
81         {
82         error("Error in configuration syntax");
83         return false;
84         }
86     accounts.clear();
88     std::vector<Element *> mucElems = root->findElements("muc");
89     if (mucElems.size() > 0)
90         {
91         Element *elem = mucElems[0];
92         mucGroup    = elem->getTagValue("group");
93         mucHost     = elem->getTagValue("host");
94         mucNick     = elem->getTagValue("nick");
95         mucPassword = elem->getTagValue("password");
96         }
98     std::vector<Element *> accountElems = root->findElements("account");
100     for (unsigned int i=0 ; i<accountElems .size() ; i++)
101         {
102         XmppAccount account;
103         Element *elem = accountElems [i];
105         DOMString str = elem->getTagValue("name");
106         if (str.size()==0)
107             str = "unnamed account";
108         account.setName(str);
110         str = elem->getTagValue("host");
111         if (str.size()==0)
112             str = "jabber.org";
113         account.setHost(str);
115         str = elem->getTagValue("port");
116         int port = (int) getInt(str);
117         if (port == 0)
118             port = 5222;
119         account.setPort(port);
121         str = elem->getTagValue("username");
122         if (str.size()==0)
123             str = "noname";
124         account.setUsername(str);
126         str = elem->getTagValue("password");
127         if (str.size()==0)
128             str = "nopass";
129         account.setPassword(str);
131         accounts.push_back(account);
132         }
135     delete root;
137     return true;
145 bool XmppConfig::readFile(const DOMString &fileName)
148     FILE *f = fopen(fileName.c_str(), "rb");
149     if (!f)
150         {
151         error("Could not open configuration file '%s' for reading",
152               fileName.c_str());
153         return false;
154         }
156     DOMString buffer;
157     while (!feof(f))
158         {
159         char ch = (char) fgetc(f);
160         buffer.push_back(ch);
161         }
162     fclose(f);
164     if (!read(buffer))
165         return false;
167     return true;
171 DOMString XmppConfig::toXmlBuffer()
174     DOMString buf;
176     char fmtbuf[32];
178     buf.append("<pedro>\n");
179     buf.append("    <muc>\n");
180     buf.append("        <group>");
181     buf.append(mucGroup);
182     buf.append("</group>\n");
183     buf.append("        <host>");
184     buf.append(mucHost);
185     buf.append("</host>\n");
186     buf.append("        <nick>");
187     buf.append(mucNick);
188     buf.append("</nick>\n");
189     buf.append("        <password>");
190     buf.append(mucPassword);
191     buf.append("</password>\n");
192     buf.append("    </muc>\n");
194     for (unsigned int i = 0 ; i<accounts.size() ; i++)
195         {
196         XmppAccount acc = accounts[i];
197         buf.append("    <account>\n");
198         buf.append("        <name>");
199         buf.append(acc.getName());
200         buf.append("</name>\n");
201         buf.append("        <host>");
202         buf.append(acc.getHost());
203         buf.append("</host>\n");
204         buf.append("        <port>");
205         snprintf(fmtbuf, 31, "%d", acc.getPort());
206         buf.append(fmtbuf);
207         buf.append("</port>\n");
208         buf.append("        <username>");
209         buf.append(acc.getUsername());
210         buf.append("</username>\n");
211         buf.append("        <password>");
212         buf.append(acc.getPassword());
213         buf.append("</password>\n");
214         buf.append("    </account>\n");
215         }
217     buf.append("</pedro>\n");
219     return buf;
225 bool XmppConfig::writeFile(const DOMString &fileName)
228     FILE *f = fopen(fileName.c_str(), "wb");
229     if (!f)
230         {
231         error("Could not open configuration file '%s' for writing",
232               fileName.c_str());
233         return false;
234         }
236     DOMString buffer = toXmlBuffer();
237     char *s = (char *)buffer.c_str();
238     size_t len = (size_t) strlen(s);  //in case we have wide chars
240     if (fwrite(s, 1, len, f) != len)
241         {
242         return false;
243         }
244     fclose(f);
246     if (!read(buffer))
247         return false;
249     return true;
253 /**
254  *
255  */
256 DOMString XmppConfig::getMucGroup()
258     return mucGroup;
261 /**
262  *
263  */
264 void XmppConfig::setMucGroup(const DOMString &val)
266     mucGroup = val;
269 /**
270  *
271  */
272 DOMString XmppConfig::getMucHost()
274     return mucHost;
277 /**
278  *
279  */
280 void XmppConfig::setMucHost(const DOMString &val)
282     mucHost = val;
285 /**
286  *
287  */
288 DOMString XmppConfig::getMucNick()
290     return mucNick;
293 /**
294  *
295  */
296 void XmppConfig::setMucNick(const DOMString &val)
298     mucNick = val;
301 /**
302  *
303  */
304 DOMString XmppConfig::getMucPassword()
306     return mucPassword;
309 /**
310  *
311  */
312 void XmppConfig::setMucPassword(const DOMString &val)
314     mucPassword = val;
319 /**
320  *
321  */
322 std::vector<XmppAccount> &XmppConfig::getAccounts()
324     return accounts;
328 /**
329  *
330  */
331 bool XmppConfig::accountAdd(const XmppAccount &account)
333     DOMString name = account.getName();
334     if (name.size() < 1)
335         return false;
336     if (accountExists(name))
337         return false;
338     accounts.push_back(account);
339     return true;
343 /**
344  *
345  */
346 bool XmppConfig::accountExists(const DOMString &accountName)
348     if (accountName.size() < 1)
349         return false;
350     std::vector<XmppAccount>::iterator iter;
351     for (iter = accounts.begin() ; iter!= accounts.end() ; iter++)
352         {
353         if (iter->getName() == accountName)
354             return true;
355         }
356     return false;
361 /**
362  *
363  */
364 void XmppConfig::accountRemove(const DOMString &accountName)
366     if (accountName.size() < 1)
367         return;
368     std::vector<XmppAccount>::iterator iter;
369     for (iter = accounts.begin() ; iter!= accounts.end() ; )
370         {
371         if (iter->getName() == accountName)
372             iter = accounts.erase(iter);
373         else
374             iter++;
375         }        
379 /**
380  *
381  */
382 bool XmppConfig::accountFind(const DOMString &accountName,
383                              XmppAccount &retVal)
385     if (accountName.size() < 1)
386         return false;
387     std::vector<XmppAccount>::iterator iter;
388     for (iter = accounts.begin() ; iter!= accounts.end() ; iter++)
389         {
390         if (iter->getName() == accountName)
391             {
392             retVal = (*iter);
393             return true;
394             }
395         }        
396     return false;
403 } //namespace Pedro
404 //########################################################################
405 //# E N D    O F     F I L E
406 //########################################################################