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
13 Config::Config(QObject *parent) :
\r
16 confFile.setFileName(CONFIG_FILE);
\r
18 profileList.clear();
\r
21 bool Config::openConfig()
\r
24 qDebug() << "Opening the Config file";
\r
27 bool bOpenResult = confFile.open(QIODevice::ReadWrite | QIODevice::Text);
\r
31 int errorLine, errorColumn;
\r
32 bOpenResult = domDoc.setContent(&confFile, &errorMsg,
\r
33 &errorLine, &errorColumn);
\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
49 QDomNode node = domDoc.namedItem(PROFILE_TAG);
\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
59 // Now load the whole config file in memory!
\r
60 qDebug() << "Trying to load the whole config now...";
\r
61 bOpenResult = readAllProfiles();
\r
66 bool Config::closeConfig()
\r
68 bool bResult = confFile.flush();
\r
73 int Config::addProfile(const Profile &p)
\r
76 QDomElement el = domDoc.namedItem(PROFILE_TAG).toElement();
\r
77 Xmlutil::generateProfileXML(p, &error, &domDoc, &el);
\r
81 updateNoOfProfiles();
\r
82 profileList.append(p);
\r
84 qDebug() << "AddProfile(): " << error << "\tXMLString is: " << endl << domDoc.toString();
\r
88 void Config::updateNoOfProfiles()
\r
90 QDomElement el = domDoc.namedItem(PROFILE_TAG).toElement();
\r
91 el.setAttribute(NO_OF_PROFILE_ATTR, noOfProfiles);
\r
94 int Config::removeProfile(const Profile &p)
\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
101 QString name = el.attribute(NAME_ATTR);
\r
103 qDebug() << "Name is: " << name;
\r
104 if(p.mName == name)
\r
106 if(!el.parentNode().removeChild(el).isNull())
\r
109 updateNoOfProfiles();
\r
110 profileList.removeAt(i);
\r
111 qDebug() << "RemoveChild okay!";
\r
115 el = el.nextSiblingElement();
\r
120 void Config::flushConfig()
\r
125 bool Config::readAllProfiles()
\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
131 qDebug() << "Number of profiles in config is: " << noOfProfiles;
\r
133 if(noOfProfiles > 0)
\r
135 QDomElement childEl = el.firstChildElement();
\r
136 for(int i = 0; i < noOfProfiles; i++)
\r
139 QString dummy; dummy.clear();
\r
140 if(!Xmlutil::degenerateProfileXML(dummy, p, &childEl))
\r
142 profileList.append(p);
\r
143 qDebug() << "Profile added to ProfileList...";
\r
147 qDebug() << "Profile not added, returning false";
\r
150 childEl = childEl.nextSiblingElement();
\r
156 bool Config::writeAllProfiles()
\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
163 bResult = confFile.open(QIODevice::WriteOnly | QIODevice::Text);
\r
165 // No need to do set content since document data is always updated...
\r
166 qint64 error = confFile.write(domDoc.toByteArray());
\r
173 bool Config::removeAllProfiles()
\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