19 #include <tqptrlist.h> 23 #include <tdeparts/part.h> 25 #include <tdemessagebox.h> 26 #include <tdelocale.h> 27 #include <kinstance.h> 28 #include <tdetexteditor/viewcursorinterface.h> 29 #include <tdetexteditor/document.h> 30 #include <tdetexteditor/encodinginterface.h> 33 #include "partcontroller.h" 35 #include "documentationpart.h" 37 #include "kdevplugin.h" 39 #include "projectsession.h" 40 #include "projectsession.moc" 42 ProjectSession::ProjectSession()
48 ProjectSession::~ProjectSession()
53 void ProjectSession::initXMLTree()
61 TQDomDocument doc(
"KDevPrjSession");
63 domdoc.appendChild( domdoc.createProcessingInstruction(
"xml",
"version=\"1.0\" encoding=\"UTF-8\"" ) );
65 TQDomElement session = domdoc.documentElement();
66 session = domdoc.createElement(
"KDevPrjSession");
67 domdoc.appendChild( session);
73 bool bFileOpenOK =
true;
75 TQFile f(sessionFileName);
76 if ( f.open(IO_ReadOnly) ) {
77 bool ok = domdoc.setContent( &f);
80 KMessageBox::sorry(0L,
81 i18n(
"The file %1 does not contain valid XML.\n" 82 "The loading of the session failed.").arg(sessionFileName));
92 if (domdoc.doctype().name() !=
"KDevPrjSession") {
93 KMessageBox::sorry(0L,
94 i18n(
"The file %1 does not contain a valid TDevelop project session ('KDevPrjSession').\n").arg(sessionFileName)
95 + i18n(
"The document type seems to be: '%1'.").arg(domdoc.doctype().name()));
99 TQDomElement session = domdoc.documentElement();
103 recreateDocs(session);
107 TQDomElement pluginListEl = session.namedItem(
"pluginList").toElement();
108 TQValueList<KDevPlugin*>::ConstIterator it = plugins.begin();
109 while( it != plugins.end() )
111 KDevPlugin* pPlugin = (*it);
112 TQString pluginName = pPlugin->instance()->instanceName();
113 TQDomElement pluginEl = pluginListEl.namedItem(pluginName).toElement();
114 if (!pluginEl.isNull()) {
116 pPlugin->restorePartialProjectSession(&pluginEl);
121 TQTimer::singleShot( 0,
this, TQT_SLOT(loadDocument()) );
127 void ProjectSession::recreateDocs(TQDomElement& el)
137 TQDomElement docsAndViewsEl = el.namedItem(
"DocsAndViews").toElement();
138 int nNrOfDocs = docsAndViewsEl.attribute(
"NumberOfDocuments",
"0").toInt();
142 for (docEl = docsAndViewsEl.firstChild().toElement(), nDoc = 0;
144 nDoc++, docEl = docEl.nextSibling().toElement())
147 TQString docName = docEl.attribute(
"URL",
"");
148 if (!docName.isEmpty() ) {
151 kdDebug() << k_funcinfo <<
"Doc to be activated? " << (nDoc == nNrOfDocs - 1) << endl;
152 recreateViews(url, docEl, (nDoc == nNrOfDocs - 1));
166 void ProjectSession::recreateViews(KURL& url, TQDomElement docEl,
bool activate)
169 int nNrOfViews = docEl.attribute(
"NumberOfViews",
"0").toInt();
174 if ( nNrOfViews > 0 )
176 TQDomElement viewEl = docEl.firstChild().toElement();
178 dd.type = viewEl.attribute(
"Type");
179 dd.line = viewEl.attribute(
"line",
"0").toInt();
181 dd.activate = activate;
182 dd.encoding = viewEl.attribute(
"Encoding" );
235 TQString section, keyword;
236 TQDomElement session = domdoc.documentElement();
252 TQDomElement docsAndViewsEl = session.namedItem(
"DocsAndViews").toElement();
253 if (docsAndViewsEl.isNull()) {
254 docsAndViewsEl = domdoc.createElement(
"DocsAndViews");
255 session.appendChild( docsAndViewsEl);
259 TQDomNode n = docsAndViewsEl.firstChild();
260 while ( !n.isNull() ) {
261 TQDomNode toBeRemoved = n;
263 docsAndViewsEl.removeChild(toBeRemoved);
267 TQPtrListIterator<KParts::Part> it( *PartController::getInstance()->parts() );
268 for ( ; it.current(); ++it )
271 KParts::ReadOnlyPart* pReadOnlyPart =
dynamic_cast<KParts::ReadOnlyPart*
>(it.current());
275 TQString url = pReadOnlyPart->url().url();
277 docIdStr.setNum(nDocs);
278 TQDomElement docEl = domdoc.createElement(
"Doc" + docIdStr);
279 docEl.setAttribute(
"URL", url);
280 docsAndViewsEl.appendChild( docEl);
282 docEl.setAttribute(
"NumberOfViews", 1);
284 TQDomElement viewEl = domdoc.createElement(
"View0");
285 docEl.appendChild( viewEl);
287 if ( dynamic_cast<HTMLDocumentationPart*>(pReadOnlyPart) )
289 viewEl.setAttribute(
"Type",
"Documentation");
291 else if ( pReadOnlyPart->inherits(
"KTextEditor::Document") )
293 viewEl.setAttribute(
"Type",
"Source");
294 KTextEditor::ViewCursorInterface *iface =
dynamic_cast<KTextEditor::ViewCursorInterface*
>(pReadOnlyPart->widget());
296 unsigned int line, col;
297 iface->cursorPosition(&line, &col);
298 viewEl.setAttribute(
"line", line );
300 if ( KTextEditor::EncodingInterface * ei = dynamic_cast<KTextEditor::EncodingInterface*>( pReadOnlyPart ) )
302 TQString encoding = ei->encoding();
303 if ( !encoding.isNull() )
305 viewEl.setAttribute(
"Encoding", encoding );
311 viewEl.setAttribute(
"Type",
"Other");
373 docsAndViewsEl.setAttribute(
"NumberOfDocuments", nDocs);
378 TQDomElement pluginListEl = session.namedItem(
"pluginList").toElement();
379 if (pluginListEl.isNull()) {
380 pluginListEl = domdoc.createElement(
"pluginList");
381 session.appendChild( pluginListEl);
385 TQDomNode n = pluginListEl.firstChild();
386 while ( !n.isNull() ) {
387 TQDomNode toBeRemoved = n;
389 pluginListEl.removeChild(toBeRemoved);
393 TQValueList<KDevPlugin*>::ConstIterator itt = plugins.begin();
394 while( itt != plugins.end() )
396 KDevPlugin* pPlugin = (*itt);
397 TQString pluginName = pPlugin->instance()->instanceName();
398 TQDomElement pluginEl = domdoc.createElement(pluginName);
401 pPlugin->savePartialProjectSession(&pluginEl);
404 if (pluginEl.hasChildNodes() || pluginEl.hasAttributes())
406 pluginListEl.appendChild(pluginEl);
412 TQFile f(sessionFileName);
413 if ( f.open(IO_WriteOnly) ) {
414 TQTextStream t( &f );
415 t << domdoc.toCString();
424 void ProjectSession::loadDocument( )
426 while ( !_docDataList.isEmpty() )
428 DocumentData & dd = _docDataList.first();
429 if ( dd.type ==
"Source" )
431 PartController::getInstance()->setEncoding( dd.encoding );
432 PartController::getInstance()->editDocumentInternal( dd.url, dd.line, -1, dd.activate );
434 else if ( dd.type ==
"Documentation" )
437 PartController::getInstance()->showDocument( dd.url,
true );
442 PartController::getInstance()->editDocument( dd.url );
444 _docDataList.pop_front();
bool saveToFile(const TQString &fileName, const TQValueList< KDevPlugin *> plugins)
Opens the .kdevses file and saves the project session in XML format to it.
bool restoreFromFile(const TQString &fileName, const TQValueList< KDevPlugin *> plugins)
Opens the .kdevses file and loads the project session from it.