projectsession.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include <qdom.h>
00019 #include <qptrlist.h>
00020 #include <qfile.h>
00021 #include <qtimer.h>
00022
00023 #include <kparts/part.h>
00024 #include <kurl.h>
00025 #include <kmessagebox.h>
00026 #include <klocale.h>
00027 #include <kinstance.h>
00028 #include "ktexteditor/viewcursorinterface.h"
00029 #include "ktexteditor/document.h"
00030
00031 #include "api.h"
00032 #include "partcontroller.h"
00033 #include "domutil.h"
00034 #include "documentationpart.h"
00035 #include "toplevel.h"
00036 #include "kdevplugin.h"
00037
00038 #include "projectsession.h"
00039 #include "projectsession.moc"
00040
00041 ProjectSession::ProjectSession()
00042 {
00043 initXMLTree();
00044 }
00045
00046
00047 ProjectSession::~ProjectSession()
00048 {
00049 }
00050
00051
00052 void ProjectSession::initXMLTree()
00053 {
00054
00055
00056
00057
00058
00059 domdoc.clear();
00060 QDomDocument doc("KDevPrjSession");
00061 domdoc=doc;
00062 domdoc.appendChild( domdoc.createProcessingInstruction( "xml", "version=\"1.0\" encoding=\"UTF-8\"" ) );
00063
00064 QDomElement session = domdoc.documentElement();
00065 session = domdoc.createElement("KDevPrjSession");
00066 domdoc.appendChild( session);
00067 }
00068
00069
00070 bool ProjectSession::restoreFromFile( const QString & sessionFileName, const QValueList< KDevPlugin * > plugins )
00071 {
00072 bool bFileOpenOK = true;
00073
00074 QFile f(sessionFileName);
00075 if ( f.open(IO_ReadOnly) ) {
00076 bool ok = domdoc.setContent( &f);
00077 f.close();
00078 if (!ok) {
00079 KMessageBox::sorry(0L,
00080 i18n("The file %1 does not contain valid XML.\n"
00081 "The loading of the session failed.").arg(sessionFileName));
00082 initXMLTree();
00083 return false;
00084 }
00085 }
00086 else {
00087 bFileOpenOK = false;
00088 }
00089
00090
00091 if (domdoc.doctype().name() != "KDevPrjSession") {
00092 KMessageBox::sorry(0L,
00093 i18n("The file %1 does not contain a valid KDevelop project session ('KDevPrjSession').\n").arg(sessionFileName)
00094 + i18n("The document type seems to be: '%1'.").arg(domdoc.doctype().name()));
00095 return false;
00096 }
00097
00098 QDomElement session = domdoc.documentElement();
00099
00100
00101 if (bFileOpenOK) {
00102 recreateDocs(session);
00103 }
00104
00105
00106 QDomElement pluginListEl = session.namedItem("pluginList").toElement();
00107 QValueList<KDevPlugin*>::ConstIterator it = plugins.begin();
00108 while( it != plugins.end() )
00109 {
00110 KDevPlugin* pPlugin = (*it);
00111 QString pluginName = pPlugin->instance()->instanceName();
00112 QDomElement pluginEl = pluginListEl.namedItem(pluginName).toElement();
00113 if (!pluginEl.isNull()) {
00114
00115 pPlugin->restorePartialProjectSession(&pluginEl);
00116 }
00117 ++it;
00118 }
00119
00120 QTimer::singleShot( 0, this, SLOT(loadDocument()) );
00121
00122 return true;
00123 }
00124
00125
00126 void ProjectSession::recreateDocs(QDomElement& el)
00127 {
00134
00135
00136 QDomElement docsAndViewsEl = el.namedItem("DocsAndViews").toElement();
00137 int nNrOfDocs = docsAndViewsEl.attribute("NumberOfDocuments", "0").toInt();
00138
00139 int nDoc = 0;
00140 QDomElement docEl;
00141 for (docEl = docsAndViewsEl.firstChild().toElement(), nDoc = 0;
00142 nDoc < nNrOfDocs;
00143 nDoc++, docEl = docEl.nextSibling().toElement())
00144 {
00145
00146 QString docName = docEl.attribute( "URL", "");
00147 if (!docName.isEmpty() ) {
00148 KURL url(docName);
00149
00150 kdDebug() << k_funcinfo << "Doc to be activated? " << (nDoc == nNrOfDocs - 1) << endl;
00151 recreateViews(url, docEl, (nDoc == nNrOfDocs - 1));
00152 }
00153 }
00154
00155 if (nNrOfDocs > 0) {
00156 API::getInstance()->mainWindow()->callCommand("qextmdi-UI: do hack on session loading finished");
00157 }
00161 }
00162
00163
00164 void ProjectSession::recreateViews(KURL& url, QDomElement docEl, bool activate)
00165 {
00166
00167 int nNrOfViews = docEl.attribute( "NumberOfViews", "0").toInt();
00168
00169
00170
00171
00172 if ( nNrOfViews > 0 )
00173 {
00174 QDomElement viewEl = docEl.firstChild().toElement();
00175 DocumentData dd;
00176 dd.type = viewEl.attribute("Type");
00177 dd.line = viewEl.attribute("line", "0").toInt();
00178 dd.url = url;
00179 dd.activate = activate;
00180
00181 _docDataList << dd;
00182 }
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00226
00227 }
00228
00229
00230 bool ProjectSession::saveToFile( const QString & sessionFileName, const QValueList< KDevPlugin * > plugins )
00231 {
00232
00233 QString section, keyword;
00234 QDomElement session = domdoc.documentElement();
00235
00236
00237 int nDocs = 0;
00238 QString docIdStr;
00239
00248
00249
00250
00251 QDomElement docsAndViewsEl = session.namedItem("DocsAndViews").toElement();
00252 if (docsAndViewsEl.isNull()) {
00253 docsAndViewsEl = domdoc.createElement("DocsAndViews");
00254 session.appendChild( docsAndViewsEl);
00255 }
00256 else {
00257
00258 QDomNode n = docsAndViewsEl.firstChild();
00259 while ( !n.isNull() ) {
00260 QDomNode toBeRemoved = n;
00261 n = n.nextSibling();
00262 docsAndViewsEl.removeChild(toBeRemoved);
00263 }
00264 }
00265
00266 QPtrListIterator<KParts::Part> it( *PartController::getInstance()->parts() );
00267 for ( ; it.current(); ++it )
00268 {
00269
00270 KParts::ReadOnlyPart* pReadOnlyPart = dynamic_cast<KParts::ReadOnlyPart*>(it.current());
00271 if (!pReadOnlyPart)
00272 continue;
00273
00274 QString url = pReadOnlyPart->url().url();
00275
00276 docIdStr.setNum(nDocs);
00277 QDomElement docEl = domdoc.createElement("Doc" + docIdStr);
00278 docEl.setAttribute( "URL", url);
00279 docsAndViewsEl.appendChild( docEl);
00280 nDocs++;
00281 docEl.setAttribute( "NumberOfViews", 1);
00282
00283 QDomElement viewEl = domdoc.createElement( "View0");
00284 docEl.appendChild( viewEl);
00285
00286 if ( dynamic_cast<DocumentationPart*>(pReadOnlyPart) )
00287 {
00288 viewEl.setAttribute("Type", "Documentation");
00289 }
00290 else if ( pReadOnlyPart->inherits("KTextEditor::Document") )
00291 {
00292 viewEl.setAttribute("Type", "Source");
00293 KTextEditor::ViewCursorInterface *iface = dynamic_cast<KTextEditor::ViewCursorInterface*>(pReadOnlyPart->widget());
00294 if (iface) {
00295 unsigned int line, col;
00296 iface->cursorPosition(&line, &col);
00297 viewEl.setAttribute( "line", line );
00298 }
00299 }
00300 else
00301 {
00302 viewEl.setAttribute("Type", "Other");
00303 }
00304 }
00305
00306
00307
00308
00311
00312
00313
00314
00315
00316
00317
00319
00320
00321
00322
00323
00324
00325
00331
00332
00333
00335
00339
00340
00341
00342
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364 docsAndViewsEl.setAttribute("NumberOfDocuments", nDocs);
00365
00366
00367
00368
00369 QDomElement pluginListEl = session.namedItem("pluginList").toElement();
00370 if (pluginListEl.isNull()) {
00371 pluginListEl = domdoc.createElement("pluginList");
00372 session.appendChild( pluginListEl);
00373 }
00374 else {
00375
00376 QDomNode n = pluginListEl.firstChild();
00377 while ( !n.isNull() ) {
00378 QDomNode toBeRemoved = n;
00379 n = n.nextSibling();
00380 pluginListEl.removeChild(toBeRemoved);
00381 }
00382 }
00383
00384 QValueList<KDevPlugin*>::ConstIterator itt = plugins.begin();
00385 while( itt != plugins.end() )
00386 {
00387 KDevPlugin* pPlugin = (*itt);
00388 QString pluginName = pPlugin->instance()->instanceName();
00389 QDomElement pluginEl = domdoc.createElement(pluginName);
00390
00391
00392 pPlugin->savePartialProjectSession(&pluginEl);
00393
00394
00395 if (pluginEl.hasChildNodes() || pluginEl.hasAttributes())
00396 {
00397 pluginListEl.appendChild(pluginEl);
00398 }
00399 ++itt;
00400 }
00401
00402
00403 QFile f(sessionFileName);
00404 if ( f.open(IO_WriteOnly) ) {
00405 QTextStream t( &f );
00406 t << domdoc.toCString();
00407 f.close();
00408 }
00409 initXMLTree();
00410
00411 return true;
00412 }
00413
00414
00415 void ProjectSession::loadDocument( )
00416 {
00417 if ( !_docDataList.isEmpty() )
00418 {
00419 DocumentData & dd = _docDataList.first();
00420 if ( dd.type == "Source" )
00421 {
00422 PartController::getInstance()->editDocumentInternal( dd.url, dd.line, -1, dd.activate );
00423 }
00424 else if ( dd.type == "Documentation" )
00425 {
00426
00427 PartController::getInstance()->showDocument( dd.url, true );
00428 }
00429 else
00430 {
00431
00432 PartController::getInstance()->editDocument( dd.url );
00433 }
00434 _docDataList.pop_front();
00435
00436 loadDocument();
00437
00438 }
00439 }
00440
This file is part of the documentation for KDevelop Version 3.1.2.