KDevelop API Documentation

src/projectsession.cpp

Go to the documentation of this file.
00001 /*************************************************************************** 00002 projectsession.cpp - description 00003 ------------------- 00004 begin : 30 Nov 2002 00005 copyright : (C) 2002 by Falk Brettschneider 00006 email : falk@kdevelop.org 00007 ***************************************************************************/ 00008 00009 /*************************************************************************** 00010 * * 00011 * This program is free software; you can redistribute it and/or modify * 00012 * it under the terms of the GNU General Public License as published by * 00013 * the Free Software Foundation; either version 2 of the License, or * 00014 * (at your option) any later version. * 00015 * * 00016 ***************************************************************************/ 00017 00018 #include <qdom.h> 00019 #include <qptrlist.h> 00020 #include <qfile.h> 00021 00022 #include <kparts/part.h> 00023 #include <kurl.h> 00024 #include <kmessagebox.h> 00025 #include <klocale.h> 00026 #include <kinstance.h> 00027 #include "ktexteditor/viewcursorinterface.h" 00028 #include "ktexteditor/document.h" 00029 00030 #include "api.h" 00031 #include "partcontroller.h" 00032 #include "domutil.h" 00033 #include "documentationpart.h" 00034 #include "toplevel.h" 00035 #include "kdevplugin.h" 00036 00037 #include "projectsession.h" 00038 #include "projectsession.moc" 00039 //--------------------------------------------------------------------------- 00040 ProjectSession::ProjectSession() 00041 { 00042 initXMLTree(); 00043 } 00044 00045 //--------------------------------------------------------------------------- 00046 ProjectSession::~ProjectSession() 00047 { 00048 } 00049 00050 //--------------------------------------------------------------------------- 00051 void ProjectSession::initXMLTree() 00052 { 00053 // initializes the XML tree on startup of kdevelop and when a project 00054 // has been closed to ensure that the XML tree exists already including 00055 // doctype when a project gets opened that doesn't have a kdevses file 00056 // or a new project gets generated (which doesn't have a kdevses file 00057 // either as the project has never been closed before opening it). 00058 domdoc.clear(); 00059 QDomDocument doc("KDevPrjSession"); 00060 domdoc=doc; 00061 domdoc.appendChild( domdoc.createProcessingInstruction( "xml", "version=\"1.0\" encoding=\"UTF-8\"" ) ); 00062 // KDevPrjSession is the root element of the XML file 00063 QDomElement session = domdoc.documentElement(); 00064 session = domdoc.createElement("KDevPrjSession"); 00065 domdoc.appendChild( session); 00066 } 00067 00068 //--------------------------------------------------------------------------- 00069 bool ProjectSession::restoreFromFile(const QString& sessionFileName, const QDict<KDevPlugin>& projectPlugins) 00070 { 00071 bool bFileOpenOK = true; 00072 00073 QFile f(sessionFileName); 00074 if ( f.open(IO_ReadOnly) ) { // file opened successfully 00075 bool ok = domdoc.setContent( &f); 00076 f.close(); 00077 if (!ok) { 00078 KMessageBox::sorry(0L, 00079 i18n("The file %1 does not contain valid XML.\n" 00080 "The loading of the session failed.").arg(sessionFileName)); 00081 initXMLTree(); // because it was now broken after failed setContent() 00082 return false; 00083 } 00084 } 00085 else { 00086 bFileOpenOK = false; 00087 } 00088 00089 // Check for proper document type. 00090 if (domdoc.doctype().name() != "KDevPrjSession") { 00091 KMessageBox::sorry(0L, 00092 i18n("The file %1 does not contain a valid KDevelop project session ('KDevPrjSession').\n").arg(sessionFileName) 00093 + i18n("The document type seems to be: '%1'.").arg(domdoc.doctype().name())); 00094 return false; 00095 } 00096 00097 QDomElement session = domdoc.documentElement(); 00098 00099 // read the information about the mainframe widget 00100 if (bFileOpenOK) { 00101 recreateDocs(session); 00102 } 00103 00104 // now also let the project-related plugins load their session stuff 00105 QDomElement pluginListEl = session.namedItem("pluginList").toElement(); 00106 QDictIterator<KDevPlugin> it(projectPlugins); 00107 for ( ; it.current(); ++it) { 00108 KDevPlugin* pPlugin = it.current(); 00109 Q_ASSERT(pPlugin->instance()); 00110 QString pluginName = pPlugin->instance()->instanceName(); 00111 QDomElement pluginEl = pluginListEl.namedItem(pluginName).toElement(); 00112 if (!pluginEl.isNull()) { 00113 // now plugin, load what you find! 00114 pPlugin->restorePartialProjectSession(&pluginEl); 00115 } 00116 } 00117 00118 return true; 00119 } 00120 00121 //--------------------------------------------------------------------------- 00122 void ProjectSession::recreateDocs(QDomElement& el) 00123 { 00130 00131 // read the information about the documents 00132 QDomElement docsAndViewsEl = el.namedItem("DocsAndViews").toElement(); 00133 int nNrOfDocs = docsAndViewsEl.attribute("NumberOfDocuments", "0").toInt(); 00134 // loop over all docs 00135 int nDoc = 0; 00136 QDomElement docEl; 00137 for (docEl = docsAndViewsEl.firstChild().toElement(), nDoc = 0; 00138 nDoc < nNrOfDocs; 00139 nDoc++, docEl = docEl.nextSibling().toElement()) 00140 { 00141 // read the document name and type 00142 QString docName = docEl.attribute( "URL", ""); 00143 if (!docName.isEmpty() /* && URL::exists(docName)*/) { 00144 KURL url(docName); 00145 // create the views of this document, the first view creation will also create the document 00146 recreateViews(url, docEl); 00147 } 00148 } 00149 00150 if (nNrOfDocs > 0) { 00151 API::getInstance()->mainWindow()->callCommand("qextmdi-UI: do hack on session loading finished"); 00152 } 00156 } 00157 00158 //--------------------------------------------------------------------------- 00159 void ProjectSession::recreateViews(KURL& url, QDomElement docEl) 00160 { 00161 // read information about the views 00162 int nNrOfViews = docEl.attribute( "NumberOfViews", "0").toInt(); 00163 // loop over all views of this document 00164 int nView = 0; 00165 00166 QDomElement viewEl; 00167 QString viewType; 00168 QString context; 00169 if (docEl.hasAttribute("context")) { 00170 context = docEl.attribute("context"); 00171 } 00172 00173 for (viewEl = docEl.firstChild().toElement(), nView = 0; nView < nNrOfViews; nView++, viewEl = viewEl.nextSibling().toElement()) { 00174 00180 00181 if (context.isEmpty()) { 00182 int line = 0; 00183 if (viewEl.hasAttribute("line")) { 00184 line = viewEl.attribute("line", "0").toInt(); 00185 } 00186 PartController::getInstance()->editDocument(url, line); 00187 } 00188 else { 00189 PartController::getInstance()->showDocument(url, context); 00190 } 00191 QDomElement viewPropertiesEl = viewEl.namedItem("AdditionalSettings").toElement(); 00192 if (!viewPropertiesEl.isNull()) { 00193 emit sig_restoreAdditionalViewProperties(url.url(), &viewPropertiesEl); 00194 } 00195 00196 } 00204 00205 } 00206 00207 //--------------------------------------------------------------------------- 00208 bool ProjectSession::saveToFile(const QString& sessionFileName, const QDict<KDevPlugin>& projectPlugins) 00209 { 00210 00211 QString section, keyword; 00212 QDomElement session = domdoc.documentElement(); 00213 00214 00215 int nDocs = 0; 00216 QString docIdStr; 00217 00226 00227 00228 // read the information about the documents 00229 QDomElement docsAndViewsEl = session.namedItem("DocsAndViews").toElement(); 00230 if (docsAndViewsEl.isNull()) { 00231 docsAndViewsEl = domdoc.createElement("DocsAndViews"); 00232 session.appendChild( docsAndViewsEl); 00233 } 00234 else { 00235 // we need to remove the old ones before memorizing the current ones (to avoid merging) 00236 QDomNode n = docsAndViewsEl.firstChild(); 00237 while ( !n.isNull() ) { 00238 QDomNode toBeRemoved = n; 00239 n = n.nextSibling(); 00240 docsAndViewsEl.removeChild(toBeRemoved); 00241 } 00242 } 00243 00244 QPtrListIterator<KParts::Part> it( *PartController::getInstance()->parts() ); 00245 for ( ; it.current(); ++it ) { 00248 00249 KParts::ReadOnlyPart* pReadOnlyPart = dynamic_cast<KParts::ReadOnlyPart*>(it.current()); 00250 if (!pReadOnlyPart) 00251 continue; // note: read-write parts are also a read-only part, they inherit from it 00252 00253 DocumentationPart* pDocuPart = dynamic_cast<DocumentationPart*>(pReadOnlyPart); 00254 00256 QString url = pReadOnlyPart->url().url(); 00257 00258 docIdStr.setNum(nDocs); 00259 QDomElement docEl = domdoc.createElement("Doc" + docIdStr); 00260 docEl.setAttribute( "URL", url); 00261 docsAndViewsEl.appendChild( docEl); 00262 nDocs++; 00268 docEl.setAttribute( "NumberOfViews", 1); 00269 // loop over all views of this document 00270 int nView = 0; 00272 QString viewIdStr; 00276 viewIdStr.setNum( nView); 00277 QDomElement viewEl = domdoc.createElement( "View"+viewIdStr); 00278 docEl.appendChild( viewEl); 00279 // focus? 00281 viewEl.setAttribute("Type", "???"); 00282 00283 QDomElement viewPropertiesEl = domdoc.createElement("AdditionalSettings"); 00284 viewEl.appendChild(viewPropertiesEl); 00285 emit sig_saveAdditionalViewProperties(url, &viewPropertiesEl); 00286 00287 if (pReadOnlyPart->inherits("KTextEditor::Document")) { 00288 KTextEditor::ViewCursorInterface *iface = dynamic_cast<KTextEditor::ViewCursorInterface*>(pReadOnlyPart->widget()); 00289 if (iface) { 00290 unsigned int line, col; 00291 iface->cursorPosition(&line, &col); 00292 viewEl.setAttribute( "line", line ); 00293 } 00294 } 00295 00296 if (pDocuPart) { 00297 docEl.setAttribute( "context", pDocuPart->context() ); 00298 } 00299 } 00300 00301 docsAndViewsEl.setAttribute("NumberOfDocuments", nDocs); 00302 00303 00304 // now also let the project-related plugins save their session stuff 00305 // read the information about the documents 00306 QDomElement pluginListEl = session.namedItem("pluginList").toElement(); 00307 if (pluginListEl.isNull()) { 00308 pluginListEl = domdoc.createElement("pluginList"); 00309 session.appendChild( pluginListEl); 00310 } 00311 else { 00312 // we need to remove the old ones before memorizing the current ones (to avoid merging) 00313 QDomNode n = pluginListEl.firstChild(); 00314 while ( !n.isNull() ) { 00315 QDomNode toBeRemoved = n; 00316 n = n.nextSibling(); 00317 pluginListEl.removeChild(toBeRemoved); 00318 } 00319 } 00320 00321 QDictIterator<KDevPlugin> pluginIter(projectPlugins); 00322 for ( ; pluginIter.current(); ++pluginIter) { 00323 KDevPlugin* pPlugin = pluginIter.current(); 00324 Q_ASSERT(pPlugin->instance()); 00325 QString pluginName = pPlugin->instance()->instanceName(); 00326 QDomElement pluginEl = domdoc.createElement(pluginName); 00327 // now plugin, save what you have! 00328 pPlugin->savePartialProjectSession(&pluginEl); 00329 // if the plugin wrote anything, accept it for the session, otherwise forget it 00330 if (pluginEl.hasChildNodes() || pluginEl.hasAttributes()) { 00331 pluginListEl.appendChild(pluginEl); 00332 } 00333 } 00334 00335 // Write it out to the session file on disc 00336 QFile f(sessionFileName); 00337 if ( f.open(IO_WriteOnly) ) { // file opened successfully 00338 QTextStream t( &f ); // use a text stream 00339 t << domdoc.toCString(); 00340 f.close(); 00341 } 00342 initXMLTree(); // clear and initialize the tree again 00343 00344 return true; 00345 }
KDE Logo
This file is part of the documentation for KDevelop Version 3.0.4.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Oct 6 17:39:14 2004 by doxygen 1.3.7 written by Dimitri van Heesch, © 1997-2003