📄 bookcasedoc.cpp
字号:
} // end unit loop coll->blockSignals(false); // now the groupview needs to be updated // this only works since the group view totally populates when grouping is changed app->slotChangeGrouping(); } // end collection loop //blockSignals(false); // be sure to do this to properly close out progress bar emit signalFractionDone(1.0); return true;}bool BookcaseDoc::saveModified() { bool completed = true; if(isModified()) { Bookcase* app = bookcaseParent(parent()); QString str(i18n("The current file has been modified.\n" "Do you want to save it?")); int want_save = KMessageBox::warningYesNoCancel(app, str, i18n("Warning!"), KStdGuiItem::save(), KStdGuiItem::discard()); switch(want_save) { case KMessageBox::Yes: if(m_url.fileName() == i18n("Untitled")) { app->slotFileSaveAs(); } else { saveDocument(URL()); } deleteContents(); completed = true; break; case KMessageBox::No: setModified(false); deleteContents(); completed = true; break; case KMessageBox::Cancel: completed = false; break; default: completed = false; break; } } return completed;}bool BookcaseDoc::saveDocument(const KURL& url_) { QDomDocument dom = exportXML(); bool success = writeURL(url_, dom.toString()); if(success) { // if successful, doc is no longer modified setModified(false); } return success;}bool BookcaseDoc::writeURL(const KURL& url_, const QString& text_) const { if(KIO::NetAccess::exists(url_)) { if(url_ != m_url) { Bookcase* app = bookcaseParent(parent()); QString str = i18n("A file named \"%1\" already exists. " "Are you sure you want to overwrite it?").arg(url_.fileName()); int want_continue = KMessageBox::warningContinueCancel(app, str, i18n("Overwrite File?"), i18n("Overwrite")); if(want_continue == KMessageBox::Cancel) { return false; } } KURL backup(url_); backup.setPath(backup.path() + QString::fromLatin1("~")); KIO::NetAccess::del(backup); KIO::NetAccess::copy(url_, backup); } bool success; if(url_.isLocalFile()) { QFile f(url_.path()); success = writeFile(f, text_); f.close(); } else { KTempFile tempfile; QFile f(tempfile.name()); success = writeFile(f, text_); f.close(); if(success) { bool uploaded = KIO::NetAccess::upload(tempfile.name(), url_); if(!uploaded) { Bookcase* app = bookcaseParent(parent()); QString str(i18n("Bookcase is unable to upload the file - %1.").arg(url_.url())); KMessageBox::sorry(app, str); } } tempfile.unlink(); } return success;}bool BookcaseDoc::writeFile(QFile& f_, const QString& text_) const { if(f_.open(IO_WriteOnly)) { QTextStream t(&f_); t.setEncoding(QTextStream::UnicodeUTF8);// kdDebug() << "-----------------------------" << endl// << text_ << endl// << "-----------------------------" << endl; t << text_; return true; } else { Bookcase* app = bookcaseParent(parent()); QString str(i18n("Bookcase is unable to write the file - %1.").arg(f_.name())); KMessageBox::sorry(app, str); return false; }}bool BookcaseDoc::closeDocument() { deleteContents(); return true;}void BookcaseDoc::deleteContents() { BCCollectionListIterator it(m_collList); for( ; it.current(); ++it) { slotDeleteCollection(it.current()); } m_collList.clear();}unsigned BookcaseDoc::collectionCount() const { return m_collList.count();}BCCollection* BookcaseDoc::collectionById(int id_) { BCCollection* coll = 0; BCCollectionListIterator it(m_collList); for( ; it.current(); ++it) { if(it.current()->id() == id_) { coll = it.current(); break; } } if(!coll) { kdDebug() << "BookcaseDoc::collectionById() - no collection found for id=" << id_ << endl; } return coll;}const BCCollectionList& BookcaseDoc::collectionList() const{ return m_collList;}QDomDocument BookcaseDoc::exportXML(bool format_/*=false*/) { QDomImplementation impl; QDomDocumentType doctype = impl.createDocumentType(QString::fromLatin1("bookcase"), QString::null, QString::fromLatin1("bookcase.dtd")); //default namespace QString ns = QString::fromLatin1("http://periapsis.org/bookcase/"); QDomDocument doc = impl.createDocument(ns, QString::fromLatin1("bookcase"), doctype); // createDocument creates a root node, insert the processing instruction before it // Is it truly UTF_8 encoding? Yes, QDomDocument::toCString() returns UTF-8 doc.insertBefore(doc.createProcessingInstruction(QString::fromLatin1("xml"), QString::fromLatin1("version=\"1.0\" " "encoding=\"UTF-8\"")), doc.documentElement());// QDomElement bcelem = doc.createElementNS(ns, QString::fromLatin1("bookcase")); QDomElement bcelem = doc.documentElement(); bcelem.setAttribute(QString::fromLatin1("syntaxVersion"), SYNTAX_VERSION); doc.appendChild(bcelem); BCCollectionListIterator collIt(m_collList); // need i counter for progress bar for(int i = 0; collIt.current(); ++collIt, ++i) { BCCollection* coll = collIt.current(); QDomElement collElem = doc.createElement(QString::fromLatin1("collection")); doc.documentElement().appendChild(collElem); collElem.setAttribute(QString::fromLatin1("title"), coll->title()); collElem.setAttribute(QString::fromLatin1("unit"), coll->unitName()); collElem.setAttribute(QString::fromLatin1("unitTitle"), coll->unitTitle()); // if the collection is custom, include the attributes in the doc file // TODO: fixme, this is a hack to get the attributes in the output when formatted // this is because I know that the only time I'm asking for formatted entried is // when I'm printing the document, and my printing stylesheet needs to map from the // attribute name to its title if(coll->isCustom() || format_) { QDomElement attsElem = doc.createElement(QString::fromLatin1("attributes")); collElem.appendChild(attsElem); BCAttributeListIterator attIt(coll->attributeList()); QDomElement attElem; for( ; attIt.current(); ++attIt) { attElem = doc.createElement(QString::fromLatin1("attribute")); attsElem.appendChild(attElem); attElem.setAttribute(QString::fromLatin1("name"), attIt.current()->name()); attElem.setAttribute(QString::fromLatin1("title"), attIt.current()->title()); attElem.setAttribute(QString::fromLatin1("category"), attIt.current()->category()); attElem.setAttribute(QString::fromLatin1("type"), attIt.current()->type()); if(attIt.current()->type() == BCAttribute::Choice) { attElem.setAttribute(QString::fromLatin1("allowed"), attIt.current()->allowed().join(QString::fromLatin1(";"))); } attElem.setAttribute(QString::fromLatin1("flags"), attIt.current()->flags()); if(!attIt.current()->description().isEmpty()) { attElem.setAttribute(QString::fromLatin1("description"), attIt.current()->description()); } } // end attribute loop } // end attribute save // element for the whole unit QDomElement unitElem; // parent element if attribute contains multiple values, child of unitElem QDomElement attParElem; // element for attributte value, child of eith unitElem or attParElem QDomElement attElem; // iterate through every attribute for each unit, go ahead and create iterator here BCAttributeListIterator attIt(coll->attributeList()); // iterate over every unit in collecction QPtrListIterator<BCUnit> unitIt(coll->unitList()); // the j counter is for progress for(int j = 0; unitIt.current(); ++unitIt, ++j) { unitElem = doc.createElement(coll->unitName()); collElem.appendChild(unitElem); // reset attribtue iterator, and loop over all for(attIt.toFirst(); attIt.current(); ++attIt) { QString attName = attIt.current()->name(); QString attValue; if(format_) { attValue = unitIt.current()->attributeFormatted(attName, attIt.current()->flags()); } else { attValue = unitIt.current()->attribute(attName); } // if empty, then no element is added if(!attValue.isEmpty()) { // if multiple version are allowed, split them into separate elements if(attIt.current()->flags() & BCAttribute::AllowMultiple) { // who cares about grammar, just add an 's' to the name attParElem = doc.createElement(attName + QString::fromLatin1("s")); unitElem.appendChild(attParElem); // the space after the semi-colon is enforced when the attribute is set for the unit QStringList atts = QStringList::split(QString::fromLatin1("; "), attValue); QStringList::Iterator it; for(it = atts.begin(); it != atts.end(); ++it) { attElem = doc.createElement(attName); // never going to be BCAttribute::Bool, so don't bother to check attElem.appendChild(doc.createTextNode(*it)); attParElem.appendChild(attElem); } } else { attElem = doc.createElement(attName); unitElem.appendChild(attElem); if(attIt.current()->type() != BCAttribute::Bool) { attElem.appendChild(doc.createTextNode(attValue)); } } } } // end attribute loop// if(j%SAVE_SIGNAL_STEP_SIZE == 0) {// // allocate equal time to each collection in document, leave 20% for later file write// // i is number of collection// // j is number of unit// float f = i+static_cast<float>(j)/static_cast<float>(coll->unitCount());// f *= 0.8/static_cast<float>(collectionCount());// emit signalFractionDone(f);// } } // end unit loop } // end collection loop // QFile f("/tmp/test.xml");// if(f.open(IO_WriteOnly)) {// QTextStream t(&f);// t << doc.toCString().data();// }// f.close(); return doc;}QDomDocument BookcaseDoc::exportXML(const QString& dictName_, bool format_) { QDomImplementation impl; QDomDocumentType doctype = impl.createDocumentType(QString::fromLatin1("bookcase"), QString::null, QString::fromLatin1("bookcase.dtd")); //default namespace QString ns = QString::fromLatin1("http://periapsis.org/bookcase/"); QDomDocument doc = impl.createDocument(ns, QString::fromLatin1("bookcase"), doctype); // createDocument creates a root node, insert the processing instruction before it // Is it truly UTF_8 encoding? Yes, QDomDocument::toCString() returns UTF-8 doc.insertBefore(doc.createProcessingInstruction(QString::fromLatin1("xml"), QString::fromLatin1("version=\"1.0\" " "encoding=\"UTF-8\"")), doc.documentElement());// QDomElement bcelem = doc.createElementNS(ns, QString::fromLatin1("bookcase")); QDomElement bcelem = doc.documentElement(); bcelem.setAttribute(QString::fromLatin1("syntaxVersion"), SYNTAX_VERSION); doc.appendChild(bcelem); BCCollectionListIterator collIt(m_collList); for( ; collIt.current(); ++collIt) { QDomElement collElem = doc.createElement(QString::fromLatin1("collection")); doc.documentElement().appendChild(collElem);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -