424ff85285fce566a9806fda8d2547f2f640fae4
[platform/upstream/syncevolution.git] / src / core / FileConfigNode.h
1 /*
2  * Copyright (C) 2003-2007 Funambol, Inc
3  * Copyright (C) 2008 Patrick Ohly
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY, TITLE, NONINFRINGEMENT or FITNESS FOR A PARTICULAR
12  * PURPOSE.  See the GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17  * 02111-1307  USA
18  */
19
20 #ifndef INCL_EVOLUTION_FILE_CONFIG_NODE
21 # define INCL_EVOLUTION_FILE_CONFIG_NODE
22
23 #include "ConfigNode.h"
24
25 #include <string>
26 #include <list>
27 using namespace std;
28
29 /**
30  * This class started its life as the Posix implementation of the
31  * ManagementNode in the Funambol C++ client library. Nowadays it is
32  * part of the SyncEvoluition ConfigTree (see there for details).
33  *
34  * Each node is mapped to one file whose location is determined by
35  * the ConfigTree when the node gets created. Each node represents
36  * one .ini file with entries of the type
37  * <property>\s*=\s*<value>\s*\n
38  *
39  * Comments look like:
40  * \s*# <comment>
41  *
42  * @todo rewrite with standard C++ containers
43  */
44 class FileConfigNode : public ConfigNode {
45     string m_path;
46     string m_fileName;
47
48     list<string> m_lines;
49     bool m_modified;
50     bool m_exists;
51
52     void read();
53
54  public:
55     /**
56      * Open or create a new file. The file will be physically created
57      * right away whereas changes to its content will not be written
58      * immediately.
59      *
60      * @param path      node name, maps to directory
61      * @param fileName  name of file inside that directory
62      */
63     FileConfigNode(const string &path, const string &fileName);
64
65     virtual string getName() const { return m_path + "/" + m_fileName; }
66
67     virtual void flush();
68     virtual string readProperty(const string &property) const;
69     virtual void setProperty(const string &property,
70                              const string &value,
71                              const string &comment = "",
72                              const string *defValue = NULL);
73     virtual void readProperties(map<string, string> &props) const;
74     virtual void removeProperty(const string &property);
75     virtual bool exists() const { return m_exists; }
76 };
77
78 #endif