QString commentifyDocString( const QString & docString, int indentLevel = 0 ) { QString indentString; for( int i=0; i<indentLevel; i++ ) indentString += "\t"; QString outString(indentString +"/**\n"); // Unix \n, windows \n\r, mac \r QStringList lines = docString.split( QRegExp( "[\n\r]\r?" ) ); QString commentStart("/*"), commentEnd("*/"); foreach( QString line, lines ) { line = line.replace( commentStart, "" ); line = line.replace( commentEnd, "" ); outString += indentString + " * " + line + "\n"; }
void XMLPreferences::savePreferences(const QString& filename, PrefSectionDict& dict) { // open the existing preference file QDomDocument doc; QFile f(filename); bool loaded = false; if (f.open(IO_ReadOnly)) { QString errorMsg; int errorLine = 0; int errorColumn = 0; if (doc.setContent(&f, false, &errorMsg, &errorLine, &errorColumn)) loaded = true; else { qWarning("Error processing file: %s!\n\t %s on line %d in column %d!", (const char*)filename, (const char*)errorMsg, errorLine, errorColumn); } // close the file f.close(); } // if no file was loaded, use the template document if (!loaded) { QString errorMsg; int errorLine = 0; int errorColumn = 0; if (doc.setContent(m_templateDoc, false, &errorMsg, &errorLine, &errorColumn)) loaded = true; } // if there was an existing file, rename it QFileInfo fileInfo(filename); if (fileInfo.exists()) { QDir dir(fileInfo.dirPath(true)); dir.rename(filename, filename + QString(".bak")); } // do more processing here QDomElement docElem = doc.documentElement(); DomConvenience conv(doc); QDomNodeList sectionList, propertyList; PreferenceDict* sectionDict; QString sectionName; QString propertyName; QVariant* propertyValue; QDomElement e; QDomNode n; sectionList = docElem.elementsByTagName("section"); QDictIterator<PreferenceDict> sdit(dict); for (; sdit.current(); ++sdit) { QDomElement section; sectionName = sdit.currentKey(); sectionDict = sdit.current(); // iterate over all the sections in the document for (uint i = 0; i < sectionList.length(); i++) { e = sectionList.item(i).toElement(); if (!e.hasAttribute("name")) { qWarning("section without name!"); continue; } // printf("found section: %s\n", (const char*)section.attribute("name")); // is this the section? if (sectionName == e.attribute("name")) { // yes, done section = e; break; } } // if no section was found, create a new one if (section.isNull()) { // create the section element section = doc.createElement("section"); // set the name attribute of the section element section.setAttribute("name", sectionName); // append the new section to the document docElem.appendChild(section); } // iterate over all the properties in the section QDictIterator<QVariant> pdit(*sectionDict); for (; pdit.current(); ++pdit) { QDomElement property; propertyName = pdit.currentKey(); propertyValue = pdit.current(); // get all the property elements in the section propertyList = section.elementsByTagName("property"); // iterate over all the property elements until a match is found for (uint j = 0; j < propertyList.length(); j++) { e = propertyList.item(j).toElement(); if (!e.hasAttribute("name")) { qWarning("property in section '%s' without name! Ignoring!", (const char*)sectionName); continue; } // is this the property being searched for? if (propertyName == e.attribute("name")) { // yes, done property = e; break; } } // if no property was found, create a new one if (property.isNull()) { // create the property element property = doc.createElement("property"); // set the name attribute of the property element property.setAttribute("name", propertyName); // append the new property to the section section.appendChild(property); } QDomElement value; // iterate over the children for (n = property.firstChild(); !n.isNull(); n = n.nextSibling()) { if (!n.isElement()) continue; // don't replace comments if (n.toElement().tagName() == "comment") continue; // convert it to an element value = n.toElement(); break; } // if no value element was found, create a new one if (value.isNull()) { // create the value element, bogus type, will be filled in later value = doc.createElement("bogus"); // append the new value to the property property.appendChild(value); } if (!conv.variantToElement(*propertyValue, value)) { qWarning("Unable to set value element in section '%s' property '%s'!", (const char*)propertyName, (const char*)propertyName); } } } // write the modified DOM to disk if (!f.open(IO_WriteOnly)) { qWarning("Unable to open file for writing: %s!", (const char*)filename); } // open a Text Stream on the file QTextStream out(&f); // make sure stream is UTF8 encoded out.setEncoding(QTextStream::UnicodeUTF8); // save the document to the text stream QString docText; QTextStream docTextStream(&docText, IO_WriteOnly); doc.save(docTextStream, 4); // put newlines after comments (which unfortunately Qt's DOM doesn't track) QRegExp commentEnd("-->"); docText.replace(commentEnd, "-->\n"); // write the fixed document out to the file out << docText; // close the file f.close(); printf("Finished saving preferences to file: %s\n", (const char*)filename); }