Proper README and changelog commits.
[confmgr] / config.cpp
1 #include "config.h"\r
2 #include "profile.h"\r
3 #include "xmlutil.h"\r
4 \r
5 #include <QDebug>\r
6 #include <QDir>\r
7 \r
8 /*\r
9  TODO: Create backup & restore functions for the backup configuration files.\r
10      Today it only writes a backup when the removeAllProfiles() is called.\r
11  */\r
12 \r
13 Config::Config(QObject *parent) :\r
14     QObject(parent)\r
15 {\r
16     confFile.setFileName(CONFIG_FILE);\r
17     noOfProfiles = 0;\r
18     profileList.clear();\r
19 }\r
20 \r
21 bool Config::openConfig()\r
22 {\r
23     int stopLoops = 0;\r
24     qDebug() << "Opening the Config file";\r
25 \r
26     processAgain:\r
27     bool bOpenResult = confFile.open(QIODevice::ReadWrite | QIODevice::Text);\r
28     if(bOpenResult)\r
29     {\r
30         QString errorMsg;\r
31         int errorLine, errorColumn;\r
32         bOpenResult = domDoc.setContent(&confFile, &errorMsg,\r
33                                         &errorLine, &errorColumn);\r
34         if(!bOpenResult)\r
35         {\r
36             qDebug() << "Error while setContent on Line: " << errorLine\r
37                     << "Column: " << errorColumn << endl\r
38                     << "Error Text: " << errorMsg;\r
39 //            return bOpenResult;\r
40             confFile.write(DEFAULT_XML);\r
41             closeConfig();\r
42             if(0 == stopLoops)\r
43             {\r
44                 stopLoops++;\r
45                 goto processAgain;\r
46             }\r
47         }\r
48 \r
49         QDomNode node = domDoc.namedItem(PROFILE_TAG);\r
50         if(node.isNull())\r
51         {\r
52             qDebug() << "Probably first use of config! Creating Profiles Tag";\r
53             QDomElement el = Xmlutil::addElement(domDoc, domDoc, PROFILE_TAG);\r
54             // noOfProfiles should be 0, initialized in CTOR...\r
55             el.setAttribute(NO_OF_PROFILE_ATTR, noOfProfiles);\r
56             return true;\r
57         }\r
58 \r
59         // Now load the whole config file in memory!\r
60         qDebug() << "Trying to load the whole config now...";\r
61         bOpenResult = readAllProfiles();\r
62     }\r
63     return bOpenResult;\r
64 }\r
65 \r
66 bool Config::closeConfig()\r
67 {\r
68     bool bResult = confFile.flush();\r
69     confFile.close();\r
70     return bResult;\r
71 }\r
72 \r
73 int Config::addProfile(const Profile &p)\r
74 {\r
75     int error = 0;\r
76     QDomElement el = domDoc.namedItem(PROFILE_TAG).toElement();\r
77     Xmlutil::generateProfileXML(p, &error, &domDoc, &el);\r
78     if(!error)\r
79     {\r
80         noOfProfiles++;\r
81         updateNoOfProfiles();\r
82         profileList.append(p);\r
83     }\r
84     qDebug() << "AddProfile(): " << error << "\tXMLString is: " << endl << domDoc.toString();\r
85     return error;\r
86 }\r
87 \r
88 void Config::updateNoOfProfiles()\r
89 {\r
90     QDomElement el = domDoc.namedItem(PROFILE_TAG).toElement();\r
91     el.setAttribute(NO_OF_PROFILE_ATTR, noOfProfiles);\r
92 }\r
93 \r
94 int Config::removeProfile(const Profile &p)\r
95 {\r
96     QDomElement elem = domDoc.namedItem(PROFILE_TAG).toElement();\r
97     QDomElement el = elem.firstChildElement();\r
98     qDebug() << el.text();\r
99     for(int i = 0; i < noOfProfiles; i++)\r
100     {\r
101         QString name = el.attribute(NAME_ATTR);\r
102 \r
103         qDebug() << "Name is: " << name;\r
104         if(p.mName == name)\r
105         {\r
106             if(!el.parentNode().removeChild(el).isNull())\r
107             {\r
108                 noOfProfiles--;\r
109                 updateNoOfProfiles();\r
110                 profileList.removeAt(i);\r
111                 qDebug() << "RemoveChild okay!";\r
112             }\r
113             break;\r
114         }\r
115         el = el.nextSiblingElement();\r
116     }\r
117     return 0;\r
118 }\r
119 \r
120 void Config::flushConfig()\r
121 {\r
122     confFile.flush();\r
123 }\r
124 \r
125 bool Config::readAllProfiles()\r
126 {\r
127     // Point to the PROFILE_TAG node->element\r
128     QDomElement el = domDoc.namedItem(PROFILE_TAG).toElement();\r
129     noOfProfiles = el.attribute(NO_OF_PROFILE_ATTR).toInt();\r
130 \r
131     qDebug() << "Number of profiles in config is: " << noOfProfiles;\r
132 \r
133     if(noOfProfiles > 0)\r
134     {\r
135         QDomElement childEl = el.firstChildElement();\r
136         for(int i = 0; i < noOfProfiles; i++)\r
137         {\r
138             Profile p;\r
139             QString dummy; dummy.clear();\r
140             if(!Xmlutil::degenerateProfileXML(dummy, p, &childEl))\r
141             {\r
142                 profileList.append(p);\r
143                 qDebug() << "Profile added to ProfileList...";\r
144             }\r
145             else\r
146             {\r
147                 qDebug() << "Profile not added, returning false";\r
148                 return false;\r
149             }\r
150             childEl = childEl.nextSiblingElement();\r
151         }\r
152     }\r
153     return true;\r
154 }\r
155 \r
156 bool Config::writeAllProfiles()\r
157 {\r
158     // Stupid hack to dump all the document data into the config file...\r
159     bool bResult = closeConfig();\r
160     QDir dir(CONFIG_DIR);\r
161     bResult = dir.remove(CONFIG_FILE);\r
162 \r
163     bResult = confFile.open(QIODevice::WriteOnly | QIODevice::Text);\r
164 \r
165     // No need to do set content since document data is always updated...\r
166     qint64 error = confFile.write(domDoc.toByteArray());\r
167     flushConfig();\r
168     if( -1 == error)\r
169         return false;\r
170     return true;\r
171 }\r
172 \r
173 bool Config::removeAllProfiles()\r
174 {\r
175     // Create a backup file just in case...\r
176     profileList.clear();\r
177     QDir dir(CONFIG_DIR);\r
178     bool bResult = dir.remove(BACKUP_CONFIG_FILE);\r
179     bResult = confFile.copy(BACKUP_CONFIG_FILE);\r
180     // Essentially just delete the whole file and re-open\r
181     bResult = closeConfig();\r
182     bResult = dir.remove(CONFIG_FILE);\r
183     bResult = openConfig();\r
184     return bResult;\r
185 }\r