further speedup during load. positions of branches are saved (again)
3 #include "branchitem.h"
5 #include "linkablemapobj.h"
7 #include "mainwindow.h"
8 #include "warningdialog.h"
11 extern Main *mainWindow;
12 extern QDir vymBaseDir;
13 extern QString vymName;
15 ExportBase::ExportBase()
19 tmpDir.setPath (makeTmpDir(ok,"vym-export"));
20 if (!tmpDir.exists() || !ok)
21 QMessageBox::critical( 0, QObject::tr( "Error" ),
22 QObject::tr("Couldn't access temporary directory\n"));
26 ExportBase::~ExportBase()
32 void ExportBase::setDir(const QDir &d)
37 void ExportBase::setFile (const QString &p)
42 QString ExportBase::getFile ()
47 void ExportBase::setModel(VymModel *m)
52 void ExportBase::setCaption (const QString &s)
57 void ExportBase::addFilter(const QString &s)
62 bool ExportBase::execDialog()
65 QFileDialog *fd=new QFileDialog( 0, caption);
66 fd->setFilter (filter);
67 fd->setCaption(caption);
68 fd->setMode( QFileDialog::AnyFile );
72 if ( fd->exec() == QDialog::Accepted )
74 if (QFile (fd->selectedFile()).exists() )
76 QMessageBox mb( vymName,
77 QObject::tr("The file %1 exists already.\nDo you want to overwrite it?").arg(fd->selectedFile()),
79 QMessageBox::Yes | QMessageBox::Default,
80 QMessageBox::Cancel | QMessageBox::Escape,
82 mb.setButtonText( QMessageBox::Yes, QObject::tr("Overwrite") );
83 mb.setButtonText( QMessageBox::No, QObject::tr("Cancel"));
87 case QMessageBox::Yes:
90 case QMessageBox::Cancel:
96 outputFile=fd->selectedFile();
104 bool ExportBase::canceled()
109 QString ExportBase::getSectionString(BranchObj *bostart)
111 // Make prefix like "2.5.3" for "bo:2,bo:5,bo:3"
113 TreeItem *ti=bostart->getTreeItem();
114 int depth=ti->depth();
117 r=QString("%1").arg(1+ti->num(),0,10)+"." + r;
127 ////////////////////////////////////////////////////////////////////////
128 ExportASCII::ExportASCII()
130 filter="TXT (*.txt)";
131 caption=vymName+ " -" +QObject::tr("Export as ASCII")+" "+QObject::tr("(still experimental)");
134 void ExportASCII::doExport() //FIXME-1 segfaults...
136 QFile file (outputFile);
137 if ( !file.open( QIODevice::WriteOnly ) )
139 qWarning ("ExportBase::exportXML couldn't open "+outputFile);
142 QTextStream ts( &file ); // use LANG decoding here...
144 // Main loop over all branches
148 BranchItem *cur=NULL;
149 BranchItem *prev=NULL;
152 BranchObj *bo; //FIXME-3 still needed?
153 cur=model->next (cur,prev,d);
156 if (cur->getType()==TreeItem::Branch || cur->getType()==TreeItem::MapCenter)
158 bo=(BranchObj*)(cur->getLMO());
159 std::cout << "ExportASCII:: "<<cur->getHeading().toStdString()<<std::endl;
163 for (i=0;i<cur->depth()-1;i++) curIndent+= indentPerDepth;
165 if (!cur->hasHiddenExportParent() )
167 switch (cur->depth())
170 ts << underline (cur->getHeading(),QString("="));
175 ts << (underline (getSectionString(bo) + cur->getHeading(), QString("-") ) );
180 ts << (curIndent + "* " + cur->getHeading());
184 ts << (curIndent + "- " + cur->getHeading());
188 ts << (curIndent + "- " + cur->getHeading());
193 // If necessary, write note
194 if (!cur->getNoteObj().isEmpty())
197 s=cur->getNoteASCII( curIndent, 80);
202 cur=model->next(cur,prev,d);
207 QString ExportASCII::underline (const QString &text, const QString &line)
209 QString r=text + "\n";
210 for (int j=0;j<text.length();j++) r+=line;
215 ////////////////////////////////////////////////////////////////////////
216 void ExportCSV::doExport()
218 QFile file (outputFile);
219 if ( !file.open( QIODevice::WriteOnly ) )
221 qWarning ("ExportBase::exportXML couldn't open "+outputFile);
224 QTextStream ts( &file ); // use LANG decoding here...
227 ts << "\"Note\"" <<endl;
229 // Main loop over all branches
231 QString curIndent("");
233 BranchObj *bo; //FIXME-3 still needed?
234 BranchItem *cur=NULL;
235 BranchItem *prev=NULL;
237 cur=model->next (cur,prev,d);
240 bo=(BranchObj*)(cur->getLMO());
242 if (!cur->hasHiddenExportParent() )
244 // If necessary, write note
245 if (!cur->getNoteObj().isEmpty())
247 s =cur->getNoteASCII();
248 s=s.replace ("\n","\n"+curIndent);
249 ts << ("\""+s+"\",");
254 for (i=0;i<cur->depth();i++) curIndent+= "\"\",";
257 ts << curIndent << "\"" << cur->getHeading()<<"\""<<endl;
260 cur=model->next(cur,prev,d);
266 ////////////////////////////////////////////////////////////////////////
267 void ExportKDE3Bookmarks::doExport()
270 dia.showCancelButton (true);
271 dia.setText(QObject::tr("Exporting the %1 bookmarks will overwrite\nyour existing bookmarks file.").arg("KDE"));
272 dia.setCaption(QObject::tr("Warning: Overwriting %1 bookmarks").arg("KDE 3"));
273 dia.setShowAgainName("/exports/KDE/overwriteKDEBookmarks");
274 if (dia.exec()==QDialog::Accepted)
276 model->exportXML(tmpDir.path(),false);
279 p.setInputFile (tmpDir.path()+"/"+model->getMapName()+".xml");
280 p.setOutputFile (tmpDir.home().path()+"/.kde/share/apps/konqueror/bookmarks.xml");
281 p.setXSLFile (vymBaseDir.path()+"/styles/vym2kdebookmarks.xsl");
284 QString ub=vymBaseDir.path()+"/scripts/update-bookmarks";
285 QProcess *proc= new QProcess ;
287 if (!proc->waitForStarted())
289 QMessageBox::warning(0,
290 QObject::tr("Warning"),
291 QObject::tr("Couldn't find script %1\nto notifiy Browsers of changed bookmarks.").arg(ub));
296 ////////////////////////////////////////////////////////////////////////
297 void ExportKDE4Bookmarks::doExport()
300 dia.showCancelButton (true);
301 dia.setText(QObject::tr("Exporting the %1 bookmarks will overwrite\nyour existing bookmarks file.").arg("KDE 4"));
302 dia.setCaption(QObject::tr("Warning: Overwriting %1 bookmarks").arg("KDE"));
303 dia.setShowAgainName("/exports/KDE/overwriteKDEBookmarks");
304 if (dia.exec()==QDialog::Accepted)
306 model->exportXML(tmpDir.path(),false);
309 p.setInputFile (tmpDir.path()+"/"+model->getMapName()+".xml");
310 p.setOutputFile (tmpDir.home().path()+"/.kde4/share/apps/konqueror/bookmarks.xml");
311 p.setXSLFile (vymBaseDir.path()+"/styles/vym2kdebookmarks.xsl");
314 QString ub=vymBaseDir.path()+"/scripts/update-bookmarks";
315 QProcess *proc= new QProcess ;
317 if (!proc->waitForStarted())
319 QMessageBox::warning(0,
320 QObject::tr("Warning"),
321 QObject::tr("Couldn't find script %1\nto notifiy Browsers of changed bookmarks.").arg(ub));
326 ////////////////////////////////////////////////////////////////////////
327 void ExportFirefoxBookmarks::doExport()
330 dia.showCancelButton (true);
331 dia.setText(QObject::tr("Exporting the %1 bookmarks will overwrite\nyour existing bookmarks file.").arg("Firefox"));
332 dia.setCaption(QObject::tr("Warning: Overwriting %1 bookmarks").arg("Firefox"));
333 dia.setShowAgainName("/vym/warnings/overwriteImportBookmarks");
334 if (dia.exec()==QDialog::Accepted)
336 model->exportXML(tmpDir.path(),false);
340 p.setInputFile (tmpDir.path()+"/"+me->getMapName()+".xml");
341 p.setOutputFile (tmpDir.home().path()+"/.kde/share/apps/konqueror/bookmarks.xml");
342 p.setXSLFile (vymBaseDir.path()+"/styles/vym2kdebookmarks.xsl");
345 QString ub=vymBaseDir.path()+"/scripts/update-bookmarks";
346 QProcess *proc = new QProcess( );
347 proc->addArgument(ub);
349 if ( !proc->start() )
351 QMessageBox::warning(0,
352 QObject::tr("Warning"),
353 QObject::tr("Couldn't find script %1\nto notifiy Browsers of changed bookmarks.").arg(ub));
360 ////////////////////////////////////////////////////////////////////////
361 void ExportTaskjuggler::doExport()
363 model->exportXML(tmpDir.path(),false);
366 p.setInputFile (tmpDir.path()+"/"+model->getMapName()+".xml");
367 p.setOutputFile (outputFile);
368 p.setXSLFile (vymBaseDir.path()+"/styles/vym2taskjuggler.xsl");
372 ////////////////////////////////////////////////////////////////////////
373 void ExportLaTeX::doExport()
375 // Exports a map to a LaTex file.
376 // This file needs to be included
377 // or inported into a LaTex document
378 // it will not add a preamble, or anything
379 // that makes a full LaTex document.
380 QFile file (outputFile);
381 if ( !file.open( QIODevice::WriteOnly ) ) {
382 QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not write %1").arg(outputFile));
383 mainWindow->statusMessage(QString(QObject::tr("Export failed.")));
386 QTextStream ts( &file ); // use LANG decoding here...
387 ts.setEncoding (QTextStream::UnicodeUTF8); // Force UTF8
389 // Main loop over all branches
391 // QString curIndent("");
394 BranchItem *cur=NULL;
395 BranchItem *prev=NULL;
396 int d; //FIXME-3 still needed?
397 model->next(cur,prev,d);
400 bo=(BranchObj*)(cur->getLMO());
402 if (!cur->hasHiddenExportParent() )
404 switch (cur->depth() )
408 ts << ("\\chapter{" + cur->getHeading()+ "}\n");
411 ts << ("\\section{" + cur->getHeading()+ "}\n");
414 ts << ("\\subsection{" + cur->getHeading()+ "}\n");
417 ts << ("\\subsubsection{" + cur->getHeading()+ "}\n");
420 ts << ("\\paragraph*{" + cur->getHeading()+ "}\n");
423 // If necessary, write note
424 if (!cur->getNoteObj().isEmpty()) {
425 ts << (cur->getNoteASCII());
429 cur=model->next(cur,prev,d);
434 ////////////////////////////////////////////////////////////////////////
440 ExportOO::~ExportOO()
444 QString ExportOO::buildList (TreeItem *current)
449 BranchItem *bi=current->getFirstBranch();
452 if (true) //if (!bo->hasHiddenExportParent() ) // FIXME-2 use BranchItem...
455 r+="<text:list text:style-name=\"vym-list\">\n";
458 r+="<text:list-item><text:p >";
459 r+=quotemeta(bi->getHeading());
460 // If necessary, write note
461 if (!bi->getNoteObj().isEmpty())
462 r+=bi->getNoteOpenDoc();
464 r+=buildList (bi); // recursivly add deeper branches
465 r+="</text:list-item>\n";
467 bi=current->getBranchNum(i);
476 void ExportOO::exportPresentation()
480 /* FIXME-2 not adapted to multiple mapCenters yet, see patch already done in 1.12.2...
481 // Insert new content
482 content.replace ("<!-- INSERT TITLE -->",quotemeta(mapCenter->getHeading()));
483 content.replace ("<!-- INSERT AUTHOR -->",quotemeta(mapCenter->getAuthor()));
488 BranchObj *sectionBO=mapCenter->getFirstBranch();
494 while (sectionBO && !sectionBO->hasHiddenExportParent() )
498 // Add page with section title
499 onePage=sectionTemplate;
500 onePage.replace ("<!-- INSERT PAGE HEADING -->", quotemeta(sectionBO->getHeading() ) );
504 i=-2; // only use inner loop to
505 // turn mainbranches into pages
510 pagesBO=sectionBO->getFirstBranch();
512 while (pagesBO && !pagesBO->hasHiddenExportParent() )
514 // Add page with list of items
515 onePage=pageTemplate;
516 onePage.replace ("<!-- INSERT PAGE HEADING -->", quotemeta (pagesBO->getHeading() ) );
517 list=buildList (pagesBO->getTreeItem() );
518 onePage.replace ("<!-- INSERT LIST -->", list);
521 pagesBO=sectionBO->getBranchNum(j);
524 sectionBO=mapCenter->getBranchNum(i);
527 content.replace ("<!-- INSERT PAGES -->",allPages);
529 // Write modified content
530 QFile f (contentFile);
531 if ( !f.open( QIODevice::WriteOnly ) )
533 QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not write %1").arg(contentFile));
534 mainWindow->statusMessage(QString(QObject::tr("Export failed.")));
542 // zip tmpdir to destination
543 zipDir (tmpDir,outputFile);
547 bool ExportOO::setConfigFile (const QString &cf)
550 int i=cf.findRev ("/");
551 if (i>=0) configDir=cf.left(i);
553 set.readSettings(configFile);
556 templateDir=configDir+"/"+set.readEntry ("Template");
558 QDir d (templateDir);
561 QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Check \"%1\" in\n%2").arg("Template="+set.readEntry ("Template")).arg(configFile));
566 contentTemplateFile=templateDir+"content-template.xml";
567 contentFile=tmpDir.path()+"/content.xml";
568 pageTemplateFile=templateDir+"page-template.xml";
569 sectionTemplateFile=templateDir+"section-template.xml";
571 if (set.readEntry("useSections").contains("yes"))
574 // Copy template to tmpdir
575 system ("cp -r "+templateDir+"* "+tmpDir.path());
577 // Read content-template
578 if (!loadStringFromDisk (contentTemplateFile,content))
580 QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not read %1").arg(contentTemplateFile));
584 // Read page-template
585 if (!loadStringFromDisk (pageTemplateFile,pageTemplate))
587 QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not read %1").arg(pageTemplateFile));
591 // Read section-template
592 if (useSections && !loadStringFromDisk (sectionTemplateFile,sectionTemplate))
594 QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not read %1").arg(sectionTemplateFile));