• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • KDevelop Generic Shell
 

KDevelop Generic Shell

  • src
projectsession.cpp
1 /***************************************************************************
2  projectsession.cpp - description
3  -------------------
4  begin : 30 Nov 2002
5  copyright : (C) 2002 by Falk Brettschneider
6  email : falk@kdevelop.org
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #include <tqdom.h>
19 #include <tqptrlist.h>
20 #include <tqfile.h>
21 #include <tqtimer.h>
22 
23 #include <tdeparts/part.h>
24 #include <kurl.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>
31 
32 #include "api.h"
33 #include "partcontroller.h"
34 #include "domutil.h"
35 #include "documentationpart.h"
36 #include "toplevel.h"
37 #include "kdevplugin.h"
38 
39 #include "projectsession.h"
40 #include "projectsession.moc"
41 //---------------------------------------------------------------------------
42 ProjectSession::ProjectSession()
43 {
44  initXMLTree();
45 }
46 
47 //---------------------------------------------------------------------------
48 ProjectSession::~ProjectSession()
49 {
50 }
51 
52 //---------------------------------------------------------------------------
53 void ProjectSession::initXMLTree()
54 {
55  // initializes the XML tree on startup of tdevelop and when a project
56  // has been closed to ensure that the XML tree exists already including
57  // doctype when a project gets opened that doesn't have a kdevses file
58  // or a new project gets generated (which doesn't have a kdevses file
59  // either as the project has never been closed before opening it).
60  domdoc.clear();
61  TQDomDocument doc("KDevPrjSession");
62  domdoc=doc;
63  domdoc.appendChild( domdoc.createProcessingInstruction( "xml", "version=\"1.0\" encoding=\"UTF-8\"" ) );
64  // KDevPrjSession is the root element of the XML file
65  TQDomElement session = domdoc.documentElement();
66  session = domdoc.createElement("KDevPrjSession");
67  domdoc.appendChild( session);
68 }
69 
70 //---------------------------------------------------------------------------
71 bool ProjectSession::restoreFromFile( const TQString & sessionFileName, const TQValueList< KDevPlugin * > plugins )
72 {
73  bool bFileOpenOK = true;
74 
75  TQFile f(sessionFileName);
76  if ( f.open(IO_ReadOnly) ) { // file opened successfully
77  bool ok = domdoc.setContent( &f);
78  f.close();
79  if (!ok) {
80  KMessageBox::sorry(0L,
81  i18n("The file %1 does not contain valid XML.\n"
82  "The loading of the session failed.").arg(sessionFileName));
83  initXMLTree(); // because it was now broken after failed setContent()
84  return false;
85  }
86  }
87  else {
88  bFileOpenOK = false;
89  }
90 
91  // Check for proper document type.
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()));
96  return false;
97  }
98 
99  TQDomElement session = domdoc.documentElement();
100 
101  // read the information about the mainframe widget
102  if (bFileOpenOK) {
103  recreateDocs(session);
104  }
105 
106  // now also let the plugins load their session stuff
107  TQDomElement pluginListEl = session.namedItem("pluginList").toElement();
108  TQValueList<KDevPlugin*>::ConstIterator it = plugins.begin();
109  while( it != plugins.end() )
110  {
111  KDevPlugin* pPlugin = (*it);
112  TQString pluginName = pPlugin->instance()->instanceName();
113  TQDomElement pluginEl = pluginListEl.namedItem(pluginName).toElement();
114  if (!pluginEl.isNull()) {
115  // now plugin, load what you find!
116  pPlugin->restorePartialProjectSession(&pluginEl);
117  }
118  ++it;
119  }
120 
121  TQTimer::singleShot( 0, this, TQT_SLOT(loadDocument()) );
122 
123  return true;
124 }
125 
126 //---------------------------------------------------------------------------
127 void ProjectSession::recreateDocs(TQDomElement& el)
128 {
135 
136  // read the information about the documents
137  TQDomElement docsAndViewsEl = el.namedItem("DocsAndViews").toElement();
138  int nNrOfDocs = docsAndViewsEl.attribute("NumberOfDocuments", "0").toInt();
139  // loop over all docs
140  int nDoc = 0;
141  TQDomElement docEl;
142  for (docEl = docsAndViewsEl.firstChild().toElement(), nDoc = 0;
143  nDoc < nNrOfDocs;
144  nDoc++, docEl = docEl.nextSibling().toElement())
145  {
146  // read the document name and type
147  TQString docName = docEl.attribute( "URL", "");
148  if (!docName.isEmpty() /* && URL::exists(docName)*/) {
149  KURL url(docName);
150  // create the views of this document, the first view creation will also create the document
151  kdDebug() << k_funcinfo << "Doc to be activated? " << (nDoc == nNrOfDocs - 1) << endl;
152  recreateViews(url, docEl, (nDoc == nNrOfDocs - 1));
153  }
154  }
155 
156  //FIXME: God, I hate KMDI. What the hell is this?
157 /* if (nNrOfDocs > 0) {
158  API::getInstance()->mainWindow()->callCommand("qextmdi-UI: do hack on session loading finished");
159  }*/
163 }
164 
165 //---------------------------------------------------------------------------
166 void ProjectSession::recreateViews(KURL& url, TQDomElement docEl, bool activate)
167 {
168  // read information about the views
169  int nNrOfViews = docEl.attribute( "NumberOfViews", "0").toInt();
170 
171  // we should restore every view, but right now we only support a single view per document
172  // so use this simple method for now
173 
174  if ( nNrOfViews > 0 )
175  {
176  TQDomElement viewEl = docEl.firstChild().toElement();
177  DocumentData dd;
178  dd.type = viewEl.attribute("Type");
179  dd.line = viewEl.attribute("line", "0").toInt();
180  dd.url = url;
181  dd.activate = activate;
182  dd.encoding = viewEl.attribute( "Encoding" );
183 
184  _docDataList << dd;
185  }
186 
187 /*
188  // loop over all views of this document
189  int nView = 0;
190 
191  TQDomElement viewEl;
192  TQString viewType;
193  TQString context;
194  if (docEl.hasAttribute("context")) {
195  context = docEl.attribute("context");
196  }
197 
198  for (viewEl = docEl.firstChild().toElement(), nView = 0; nView < nNrOfViews; nView++, viewEl = viewEl.nextSibling().toElement()) {
199 
205 
206  if (context.isEmpty()) {
207  int line = 0;
208  if (viewEl.hasAttribute("line")) {
209  line = viewEl.attribute("line", "0").toInt();
210  }
211  PartController::getInstance()->editDocument(url, line);
212  }
213  else {
214  PartController::getInstance()->showDocument(url);
215  }
216  TQDomElement viewPropertiesEl = viewEl.namedItem("AdditionalSettings").toElement();
217  if (!viewPropertiesEl.isNull()) {
218  emit sig_restoreAdditionalViewProperties(url.url(), &viewPropertiesEl);
219  }
220 
221  }
229 */
230 }
231 
232 //---------------------------------------------------------------------------
233 bool ProjectSession::saveToFile( const TQString & sessionFileName, const TQValueList< KDevPlugin * > plugins )
234 {
235  TQString section, keyword;
236  TQDomElement session = domdoc.documentElement();
237 
238  int nDocs = 0;
239  TQString docIdStr;
240 
249 
250 
251  // read the information about the documents
252  TQDomElement docsAndViewsEl = session.namedItem("DocsAndViews").toElement();
253  if (docsAndViewsEl.isNull()) {
254  docsAndViewsEl = domdoc.createElement("DocsAndViews");
255  session.appendChild( docsAndViewsEl);
256  }
257  else {
258  // we need to remove the old ones before memorizing the current ones (to avoid merging)
259  TQDomNode n = docsAndViewsEl.firstChild();
260  while ( !n.isNull() ) {
261  TQDomNode toBeRemoved = n;
262  n = n.nextSibling();
263  docsAndViewsEl.removeChild(toBeRemoved);
264  }
265  }
266 
267  TQPtrListIterator<KParts::Part> it( *PartController::getInstance()->parts() );
268  for ( ; it.current(); ++it )
269  {
270 
271  KParts::ReadOnlyPart* pReadOnlyPart = dynamic_cast<KParts::ReadOnlyPart*>(it.current());
272  if (!pReadOnlyPart)
273  continue;
274 
275  TQString url = pReadOnlyPart->url().url();
276 
277  docIdStr.setNum(nDocs);
278  TQDomElement docEl = domdoc.createElement("Doc" + docIdStr);
279  docEl.setAttribute( "URL", url);
280  docsAndViewsEl.appendChild( docEl);
281  nDocs++;
282  docEl.setAttribute( "NumberOfViews", 1);
283 
284  TQDomElement viewEl = domdoc.createElement( "View0");
285  docEl.appendChild( viewEl);
286 
287  if ( dynamic_cast<HTMLDocumentationPart*>(pReadOnlyPart) )
288  {
289  viewEl.setAttribute("Type", "Documentation");
290  }
291  else if ( pReadOnlyPart->inherits("KTextEditor::Document") )
292  {
293  viewEl.setAttribute("Type", "Source");
294  KTextEditor::ViewCursorInterface *iface = dynamic_cast<KTextEditor::ViewCursorInterface*>(pReadOnlyPart->widget());
295  if (iface) {
296  unsigned int line, col;
297  iface->cursorPosition(&line, &col);
298  viewEl.setAttribute( "line", line );
299  }
300  if ( KTextEditor::EncodingInterface * ei = dynamic_cast<KTextEditor::EncodingInterface*>( pReadOnlyPart ) )
301  {
302  TQString encoding = ei->encoding();
303  if ( !encoding.isNull() )
304  {
305  viewEl.setAttribute( "Encoding", encoding );
306  }
307  }
308  }
309  else
310  {
311  viewEl.setAttribute("Type", "Other");
312  }
313  }
314 
315 /*
316  TQPtrListIterator<KParts::Part> it( *PartController::getInstance()->parts() );
317  for ( ; it.current(); ++it ) {
320 
321  KParts::ReadOnlyPart* pReadOnlyPart = dynamic_cast<KParts::ReadOnlyPart*>(it.current());
322  if (!pReadOnlyPart)
323  continue; // note: read-write parts are also a read-only part, they inherit from it
324 
325  HTMLDocumentationPart* pDocuPart = dynamic_cast<HTMLDocumentationPart*>(pReadOnlyPart);
326 
328  TQString url = pReadOnlyPart->url().url();
329 
330  docIdStr.setNum(nDocs);
331  TQDomElement docEl = domdoc.createElement("Doc" + docIdStr);
332  docEl.setAttribute( "URL", url);
333  docsAndViewsEl.appendChild( docEl);
334  nDocs++;
340  docEl.setAttribute( "NumberOfViews", 1);
341  // loop over all views of this document
342  int nView = 0;
344  TQString viewIdStr;
348  viewIdStr.setNum( nView);
349  TQDomElement viewEl = domdoc.createElement( "View"+viewIdStr);
350  docEl.appendChild( viewEl);
351  // focus?
353  viewEl.setAttribute("Type", "???");
354 
355  TQDomElement viewPropertiesEl = domdoc.createElement("AdditionalSettings");
356  viewEl.appendChild(viewPropertiesEl);
357  emit sig_saveAdditionalViewProperties(url, &viewPropertiesEl);
358 
359  if (pReadOnlyPart->inherits("KTextEditor::Document")) {
360  KTextEditor::ViewCursorInterface *iface = dynamic_cast<KTextEditor::ViewCursorInterface*>(pReadOnlyPart->widget());
361  if (iface) {
362  unsigned int line, col;
363  iface->cursorPosition(&line, &col);
364  viewEl.setAttribute( "line", line );
365  }
366  }
367 
368  if (pDocuPart) {
369  docEl.setAttribute( "context", pDocuPart->context() );
370  }
371  }
372 */
373  docsAndViewsEl.setAttribute("NumberOfDocuments", nDocs);
374 
375 
376  // now also let the project-related plugins save their session stuff
377  // read the information about the documents
378  TQDomElement pluginListEl = session.namedItem("pluginList").toElement();
379  if (pluginListEl.isNull()) {
380  pluginListEl = domdoc.createElement("pluginList");
381  session.appendChild( pluginListEl);
382  }
383  else {
384  // we need to remove the old ones before memorizing the current ones (to avoid merging)
385  TQDomNode n = pluginListEl.firstChild();
386  while ( !n.isNull() ) {
387  TQDomNode toBeRemoved = n;
388  n = n.nextSibling();
389  pluginListEl.removeChild(toBeRemoved);
390  }
391  }
392 
393  TQValueList<KDevPlugin*>::ConstIterator itt = plugins.begin();
394  while( itt != plugins.end() )
395  {
396  KDevPlugin* pPlugin = (*itt);
397  TQString pluginName = pPlugin->instance()->instanceName();
398  TQDomElement pluginEl = domdoc.createElement(pluginName);
399 
400  // now plugin, save what you have!
401  pPlugin->savePartialProjectSession(&pluginEl);
402 
403  // if the plugin wrote anything, accept itt for the session, otherwise forget itt
404  if (pluginEl.hasChildNodes() || pluginEl.hasAttributes())
405  {
406  pluginListEl.appendChild(pluginEl);
407  }
408  ++itt;
409  }
410 
411  // Write it out to the session file on disc
412  TQFile f(sessionFileName);
413  if ( f.open(IO_WriteOnly) ) { // file opened successfully
414  TQTextStream t( &f ); // use a text stream
415  t << domdoc.toCString();
416  f.close();
417  }
418  initXMLTree(); // clear and initialize the tree again
419 
420  return true;
421 }
422 
423 
424 void ProjectSession::loadDocument( )
425 {
426  while ( !_docDataList.isEmpty() )
427  {
428  DocumentData & dd = _docDataList.first();
429  if ( dd.type == "Source" )
430  {
431  PartController::getInstance()->setEncoding( dd.encoding );
432  PartController::getInstance()->editDocumentInternal( dd.url, dd.line, -1, dd.activate );
433  }
434  else if ( dd.type == "Documentation" )
435  {
436  // FIXME needs to be deferred if !activate ?
437  PartController::getInstance()->showDocument( dd.url, true );
438  }
439  else
440  {
441  // FIXME needs to be deferred if !activate ?
442  PartController::getInstance()->editDocument( dd.url );
443  }
444  _docDataList.pop_front();
445 
446  loadDocument();
447  //TQTimer::singleShot( 0, this, TQT_SLOT(loadDocument()) );
448  }
449 }
450 
ProjectSession::saveToFile
bool saveToFile(const TQString &fileName, const TQValueList< KDevPlugin *> plugins)
Opens the .kdevses file and saves the project session in XML format to it.
Definition: projectsession.cpp:233
ProjectSession::restoreFromFile
bool restoreFromFile(const TQString &fileName, const TQValueList< KDevPlugin *> plugins)
Opens the .kdevses file and loads the project session from it.
Definition: projectsession.cpp:71

KDevelop Generic Shell

Skip menu "KDevelop Generic Shell"
  • Main Page
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

KDevelop Generic Shell

Skip menu "KDevelop Generic Shell"
  • buildtools
  •   lib
  •     base
  •     parsers
  •       autotools
  •       qmake
  •     widgets
  •   api
  • languages
  •   lib
  •     debugger
  •     designer_integration
  •     interfaces
  • lib
  •   catalog
  •   interfaces
  •     extensions
  •     external
  •     extras
  •   util
  •   widgets
  •     propeditor
  • parts
  •   documentation
  •     interfaces
  • src
  •   profileengine
  •     lib
Generated for KDevelop Generic Shell by doxygen 1.8.13
This website is maintained by Timothy Pearson.