--- /dev/null
+#include "config.h"\r
+#include "profile.h"\r
+#include "xmlutil.h"\r
+\r
+#include <QDebug>\r
+#include <QDir>\r
+\r
+/*\r
+ TODO: Create backup & restore functions for the backup configuration files.\r
+ Today it only writes a backup when the removeAllProfiles() is called.\r
+ */\r
+\r
+Config::Config(QObject *parent) :\r
+ QObject(parent)\r
+{\r
+ confFile.setFileName(CONFIG_FILE);\r
+ noOfProfiles = 0;\r
+ profileList.clear();\r
+}\r
+\r
+bool Config::openConfig()\r
+{\r
+ int stopLoops = 0;\r
+ qDebug() << "Opening the Config file";\r
+\r
+ processAgain:\r
+ bool bOpenResult = confFile.open(QIODevice::ReadWrite | QIODevice::Text);\r
+ if(bOpenResult)\r
+ {\r
+ QString errorMsg;\r
+ int errorLine, errorColumn;\r
+ bOpenResult = domDoc.setContent(&confFile, &errorMsg,\r
+ &errorLine, &errorColumn);\r
+ if(!bOpenResult)\r
+ {\r
+ qDebug() << "Error while setContent on Line: " << errorLine\r
+ << "Column: " << errorColumn << endl\r
+ << "Error Text: " << errorMsg;\r
+// return bOpenResult;\r
+ confFile.write(DEFAULT_XML);\r
+ closeConfig();\r
+ if(0 == stopLoops)\r
+ {\r
+ stopLoops++;\r
+ goto processAgain;\r
+ }\r
+ }\r
+\r
+ QDomNode node = domDoc.namedItem(PROFILE_TAG);\r
+ if(node.isNull())\r
+ {\r
+ qDebug() << "Probably first use of config! Creating Profiles Tag";\r
+ QDomElement el = Xmlutil::addElement(domDoc, domDoc, PROFILE_TAG);\r
+ // noOfProfiles should be 0, initialized in CTOR...\r
+ el.setAttribute(NO_OF_PROFILE_ATTR, noOfProfiles);\r
+ return true;\r
+ }\r
+\r
+ // Now load the whole config file in memory!\r
+ qDebug() << "Trying to load the whole config now...";\r
+ bOpenResult = readAllProfiles();\r
+ }\r
+ return bOpenResult;\r
+}\r
+\r
+bool Config::closeConfig()\r
+{\r
+ bool bResult = confFile.flush();\r
+ confFile.close();\r
+ return bResult;\r
+}\r
+\r
+int Config::addProfile(const Profile &p)\r
+{\r
+ int error = 0;\r
+ QDomElement el = domDoc.namedItem(PROFILE_TAG).toElement();\r
+ Xmlutil::generateProfileXML(p, &error, &domDoc, &el);\r
+ if(!error)\r
+ {\r
+ noOfProfiles++;\r
+ updateNoOfProfiles();\r
+ profileList.append(p);\r
+ }\r
+ qDebug() << "AddProfile(): " << error << "\tXMLString is: " << endl << domDoc.toString();\r
+ return error;\r
+}\r
+\r
+void Config::updateNoOfProfiles()\r
+{\r
+ QDomElement el = domDoc.namedItem(PROFILE_TAG).toElement();\r
+ el.setAttribute(NO_OF_PROFILE_ATTR, noOfProfiles);\r
+}\r
+\r
+int Config::removeProfile(const Profile &p)\r
+{\r
+ QDomElement elem = domDoc.namedItem(PROFILE_TAG).toElement();\r
+ QDomElement el = elem.firstChildElement();\r
+ qDebug() << el.text();\r
+ for(int i = 0; i < noOfProfiles; i++)\r
+ {\r
+ QString name = el.attribute(NAME_ATTR);\r
+\r
+ qDebug() << "Name is: " << name;\r
+ if(p.mName == name)\r
+ {\r
+ if(!el.parentNode().removeChild(el).isNull())\r
+ {\r
+ noOfProfiles--;\r
+ updateNoOfProfiles();\r
+ profileList.removeAt(i);\r
+ qDebug() << "RemoveChild okay!";\r
+ }\r
+ break;\r
+ }\r
+ el = el.nextSiblingElement();\r
+ }\r
+ return 0;\r
+}\r
+\r
+void Config::flushConfig()\r
+{\r
+ confFile.flush();\r
+}\r
+\r
+bool Config::readAllProfiles()\r
+{\r
+ // Point to the PROFILE_TAG node->element\r
+ QDomElement el = domDoc.namedItem(PROFILE_TAG).toElement();\r
+ noOfProfiles = el.attribute(NO_OF_PROFILE_ATTR).toInt();\r
+\r
+ qDebug() << "Number of profiles in config is: " << noOfProfiles;\r
+\r
+ if(noOfProfiles > 0)\r
+ {\r
+ QDomElement childEl = el.firstChildElement();\r
+ for(int i = 0; i < noOfProfiles; i++)\r
+ {\r
+ Profile p;\r
+ QString dummy; dummy.clear();\r
+ if(!Xmlutil::degenerateProfileXML(dummy, p, &childEl))\r
+ {\r
+ profileList.append(p);\r
+ qDebug() << "Profile added to ProfileList...";\r
+ }\r
+ else\r
+ {\r
+ qDebug() << "Profile not added, returning false";\r
+ return false;\r
+ }\r
+ childEl = childEl.nextSiblingElement();\r
+ }\r
+ }\r
+ return true;\r
+}\r
+\r
+bool Config::writeAllProfiles()\r
+{\r
+ // Stupid hack to dump all the document data into the config file...\r
+ bool bResult = closeConfig();\r
+ QDir dir(CONFIG_DIR);\r
+ bResult = dir.remove(CONFIG_FILE);\r
+\r
+ bResult = confFile.open(QIODevice::WriteOnly | QIODevice::Text);\r
+\r
+ // No need to do set content since document data is always updated...\r
+ qint64 error = confFile.write(domDoc.toByteArray());\r
+ flushConfig();\r
+ if( -1 == error)\r
+ return false;\r
+ return true;\r
+}\r
+\r
+bool Config::removeAllProfiles()\r
+{\r
+ // Create a backup file just in case...\r
+ profileList.clear();\r
+ QDir dir(CONFIG_DIR);\r
+ bool bResult = dir.remove(BACKUP_CONFIG_FILE);\r
+ bResult = confFile.copy(BACKUP_CONFIG_FILE);\r
+ // Essentially just delete the whole file and re-open\r
+ bResult = closeConfig();\r
+ bResult = dir.remove(CONFIG_FILE);\r
+ bResult = openConfig();\r
+ return bResult;\r
+}\r