3 #include "linkablemapobj.h"
5 #include "mainwindow.h"
6 #include "warningdialog.h"
10 extern Main *mainWindow;
11 extern QDir vymBaseDir;
12 extern QString vymName;
14 ExportBase::ExportBase()
18 tmpDir.setPath (makeTmpDir(ok,"vym-export"));
19 if (!tmpDir.exists() || !ok)
20 QMessageBox::critical( 0, QObject::tr( "Error" ),
21 QObject::tr("Couldn't access temporary directory\n"));
25 ExportBase::~ExportBase()
31 void ExportBase::setDir(const QDir &d)
36 void ExportBase::setFile (const QString &p)
41 QString ExportBase::getFile ()
46 void ExportBase::setModel(VymModel *m)
51 void ExportBase::setCaption (const QString &s)
56 void ExportBase::addFilter(const QString &s)
61 bool ExportBase::execDialog()
63 //MapEditor *me=model.getMapEditor(); FIXME needed?
64 // if (model->mapCenters.count() && me)
66 QFileDialog *fd=new QFileDialog( 0, caption);
67 fd->setFilter (filter);
68 fd->setCaption(caption);
69 fd->setMode( QFileDialog::AnyFile );
73 if ( fd->exec() == QDialog::Accepted )
75 if (QFile (fd->selectedFile()).exists() )
77 QMessageBox mb( vymName,
78 QObject::tr("The file %1 exists already.\nDo you want to overwrite it?").arg(fd->selectedFile()),
80 QMessageBox::Yes | QMessageBox::Default,
81 QMessageBox::Cancel | QMessageBox::Escape,
83 mb.setButtonText( QMessageBox::Yes, QObject::tr("Overwrite") );
84 mb.setButtonText( QMessageBox::No, QObject::tr("Cancel"));
88 case QMessageBox::Yes:
91 case QMessageBox::Cancel:
97 outputFile=fd->selectedFile();
105 bool ExportBase::canceled()
110 QString ExportBase::getSectionString(BranchObj *bostart)
112 // Make prefix like "2.5.3" for "bo:2,bo:5,bo:3"
114 BranchObj *bo=bostart;
115 int depth=bo->getDepth();
118 r=QString("%1").arg(1+bo->getNum(),0,10)+"." + r;
119 bo=(BranchObj*)(bo->getParObj());
120 depth=bo->getDepth();
128 ////////////////////////////////////////////////////////////////////////
129 ExportASCII::ExportASCII()
131 filter="TXT (*.txt)";
132 caption=vymName+ " -" +QObject::tr("Export as ASCII")+" "+QObject::tr("(still experimental)");
135 void ExportASCII::doExport()
137 QFile file (outputFile);
138 if ( !file.open( QIODevice::WriteOnly ) )
140 qWarning ("ExportBase::exportXML couldn't open "+outputFile);
143 QTextStream ts( &file ); // use LANG decoding here...
145 // Main loop over all branches
155 for (i=0;i<bo->getDepth()-1;i++) curIndent+= indentPerDepth;
157 if (!bo->hasHiddenExportParent() )
159 switch (bo->getDepth())
162 ts << underline (bo->getHeading(),QString("="));
167 ts << (underline (getSectionString(bo) + bo->getHeading(), QString("-") ) );
172 ts << (curIndent + "* " + bo->getHeading());
176 ts << (curIndent + "- " + bo->getHeading());
180 ts << (curIndent + "- " + bo->getHeading());
185 // If necessary, write note
186 if (!bo->getNote().isEmpty())
189 s=bo->getNoteASCII( curIndent, 80);
198 QString ExportASCII::underline (const QString &text, const QString &line)
200 QString r=text + "\n";
201 for (int j=0;j<text.length();j++) r+=line;
206 ////////////////////////////////////////////////////////////////////////
207 void ExportCSV::doExport()
209 QFile file (outputFile);
210 if ( !file.open( QIODevice::WriteOnly ) )
212 qWarning ("ExportBase::exportXML couldn't open "+outputFile);
215 QTextStream ts( &file ); // use LANG decoding here...
218 ts << "\"Note\"" <<endl;
220 // Main loop over all branches
222 QString curIndent("");
228 if (!bo->hasHiddenExportParent() )
230 // If necessary, write note
231 if (!bo->getNote().isEmpty())
233 s =bo->getNoteASCII();
234 s=s.replace ("\n","\n"+curIndent);
235 ts << ("\""+s+"\",");
240 for (i=0;i<bo->getDepth();i++) curIndent+= "\"\",";
243 ts << curIndent << "\"" << bo->getHeading()<<"\""<<endl;
252 ////////////////////////////////////////////////////////////////////////
253 void ExportKDE3Bookmarks::doExport()
255 MapEditor *me=model->getMapEditor();
259 dia.showCancelButton (true);
260 dia.setText(QObject::tr("Exporting the %1 bookmarks will overwrite\nyour existing bookmarks file.").arg("KDE"));
261 dia.setCaption(QObject::tr("Warning: Overwriting %1 bookmarks").arg("KDE 3"));
262 dia.setShowAgainName("/exports/KDE/overwriteKDEBookmarks");
263 if (dia.exec()==QDialog::Accepted)
265 me->exportXML(tmpDir.path(),false);
268 p.setInputFile (tmpDir.path()+"/"+me->getMapName()+".xml");
269 p.setOutputFile (tmpDir.home().path()+"/.kde/share/apps/konqueror/bookmarks.xml");
270 p.setXSLFile (vymBaseDir.path()+"/styles/vym2kdebookmarks.xsl");
273 QString ub=vymBaseDir.path()+"/scripts/update-bookmarks";
274 QProcess *proc= new QProcess ;
276 if (!proc->waitForStarted())
278 QMessageBox::warning(0,
279 QObject::tr("Warning"),
280 QObject::tr("Couldn't find script %1\nto notifiy Browsers of changed bookmarks.").arg(ub));
286 ////////////////////////////////////////////////////////////////////////
287 void ExportKDE4Bookmarks::doExport()
289 MapEditor *me=model->getMapEditor();
293 dia.showCancelButton (true);
294 dia.setText(QObject::tr("Exporting the %1 bookmarks will overwrite\nyour existing bookmarks file.").arg("KDE"));
295 dia.setCaption(QObject::tr("Warning: Overwriting %1 bookmarks").arg("KDE 4"));
296 dia.setShowAgainName("/exports/KDE/overwriteKDEBookmarks");
297 if (dia.exec()==QDialog::Accepted)
299 me->exportXML(tmpDir.path(),false);
302 p.setInputFile (tmpDir.path()+"/"+me->getMapName()+".xml");
303 p.setOutputFile (tmpDir.home().path()+"/.kde4/share/apps/konqueror/bookmarks.xml");
304 p.setXSLFile (vymBaseDir.path()+"/styles/vym2kdebookmarks.xsl");
307 QString ub=vymBaseDir.path()+"/scripts/update-bookmarks";
308 QProcess *proc= new QProcess ;
310 if (!proc->waitForStarted())
312 QMessageBox::warning(0,
313 QObject::tr("Warning"),
314 QObject::tr("Couldn't find script %1\nto notifiy Browsers of changed bookmarks.").arg(ub));
321 ////////////////////////////////////////////////////////////////////////
322 void ExportFirefoxBookmarks::doExport()
324 MapEditor *me=model->getMapEditor();
328 dia.showCancelButton (true);
329 dia.setText(QObject::tr("Exporting the %1 bookmarks will overwrite\nyour existing bookmarks file.").arg("Firefox"));
330 dia.setCaption(QObject::tr("Warning: Overwriting %1 bookmarks").arg("Firefox"));
331 dia.setShowAgainName("/vym/warnings/overwriteImportBookmarks");
332 if (dia.exec()==QDialog::Accepted)
334 me->exportXML(tmpDir.path(),false);
338 p.setInputFile (tmpDir.path()+"/"+me->getMapName()+".xml");
339 p.setOutputFile (tmpDir.home().path()+"/.kde/share/apps/konqueror/bookmarks.xml");
340 p.setXSLFile (vymBaseDir.path()+"/styles/vym2kdebookmarks.xsl");
343 QString ub=vymBaseDir.path()+"/scripts/update-bookmarks";
344 QProcess *proc = new QProcess( );
345 proc->addArgument(ub);
347 if ( !proc->start() )
349 QMessageBox::warning(0,
350 QObject::tr("Warning"),
351 QObject::tr("Couldn't find script %1\nto notifiy Browsers of changed bookmarks.").arg(ub));
360 ////////////////////////////////////////////////////////////////////////
361 void ExportTaskjuggler::doExport()
363 MapEditor *me=model->getMapEditor();
366 me->exportXML(tmpDir.path(),false);
369 p.setInputFile (tmpDir.path()+"/"+me->getMapName()+".xml");
370 p.setOutputFile (outputFile);
371 p.setXSLFile (vymBaseDir.path()+"/styles/vym2taskjuggler.xsl");
377 ////////////////////////////////////////////////////////////////////////
378 void ExportLaTeX::doExport()
380 // Exports a map to a LaTex file.
381 // This file needs to be included
382 // or inported into a LaTex document
383 // it will not add a preamble, or anything
384 // that makes a full LaTex document.
385 QFile file (outputFile);
386 if ( !file.open( QIODevice::WriteOnly ) ) {
387 QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not write %1").arg(outputFile));
388 mainWindow->statusMessage(QString(QObject::tr("Export failed.")));
391 QTextStream ts( &file ); // use LANG decoding here...
392 ts.setEncoding (QTextStream::UnicodeUTF8); // Force UTF8
394 // Main loop over all branches
396 // QString curIndent("");
401 if (!bo->hasHiddenExportParent() )
403 if (bo->getDepth()==0);
404 else if (bo->getDepth()==1) {
405 ts << ("\\chapter{" + bo->getHeading()+ "}\n");
407 else if (bo->getDepth()==2) {
408 ts << ("\\section{" + bo->getHeading()+ "}\n");
410 else if (bo->getDepth()==3) {
411 ts << ("\\subsection{" + bo->getHeading()+ "}\n");
413 else if (bo->getDepth()==4) {
414 ts << ("\\subsubsection{" + bo->getHeading()+ "}\n");
417 ts << ("\\paragraph*{" + bo->getHeading()+ "}\n");
420 // If necessary, write note
421 if (!bo->getNote().isEmpty()) {
422 ts << (bo->getNoteASCII());
431 ////////////////////////////////////////////////////////////////////////
437 ExportOO::~ExportOO()
441 QString ExportOO::buildList (BranchObj *current)
447 bo=current->getFirstBranch();
450 if (!bo->hasHiddenExportParent() )
453 r+="<text:list text:style-name=\"vym-list\">\n";
456 r+="<text:list-item><text:p >";
457 r+=quotemeta(bo->getHeading());
458 // If necessary, write note
459 if (!bo->getNote().isEmpty())
460 r+=bo->getNoteOpenDoc();
462 r+=buildList (bo); // recursivly add deeper branches
463 r+="</text:list-item>\n";
465 bo=current->getBranchNum(i);
474 void ExportOO::exportPresentation()
478 MapCenterObj *firstMCO=(MapCenterObj*)model->first();
481 QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("No objects in map!"));
485 // Insert new content
486 // FIXME add extra title in mapinfo for vym 1.13.x
487 content.replace ("<!-- INSERT TITLE -->",quotemeta(firstMCO->getHeading()));
488 content.replace ("<!-- INSERT AUTHOR -->",quotemeta(model->getAuthor()));
493 BranchObj *sectionBO;
498 int mapcenters=model->countMapCenters();
500 // useSections already has been set in setConfigFile
504 sectionBO=firstMCO->getFirstBranch();
507 while (sectionBO && !sectionBO->hasHiddenExportParent() )
511 // Add page with section title
512 onePage=sectionTemplate;
513 onePage.replace ("<!-- INSERT PAGE HEADING -->", quotemeta(sectionBO->getHeading() ) );
515 pagesBO=sectionBO->getFirstBranch();
518 //i=-2; // only use inner loop to
519 // turn mainbranches into pages
520 //sectionBO=firstMCO;
525 while (pagesBO && !pagesBO->hasHiddenExportParent() )
527 // Add page with list of items
528 onePage=pageTemplate;
529 onePage.replace ("<!-- INSERT PAGE HEADING -->", quotemeta (pagesBO->getHeading() ) );
530 list=buildList (pagesBO);
531 onePage.replace ("<!-- INSERT LIST -->", list);
533 if (pagesBO!=sectionBO)
536 pagesBO=((BranchObj*)pagesBO->getParObj())->getBranchNum(j);
538 pagesBO=NULL; // We are already iterating over the sectionBOs
542 sectionBO=model->getMapCenterNum (i);
544 sectionBO=firstMCO->getBranchNum (i);
547 content.replace ("<!-- INSERT PAGES -->",allPages);
549 // Write modified content
550 QFile f (contentFile);
551 if ( !f.open( QIODevice::WriteOnly ) )
553 QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not write %1").arg(contentFile));
554 mainWindow->statusMessage(QString(QObject::tr("Export failed.")));
562 // zip tmpdir to destination
563 zipDir (tmpDir,outputFile);
566 bool ExportOO::setConfigFile (const QString &cf)
569 int i=cf.findRev ("/");
570 if (i>=0) configDir=cf.left(i);
572 set.readSettings(configFile);
575 templateDir=configDir+"/"+set.readEntry ("Template");
577 QDir d (templateDir);
580 QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Check \"%1\" in\n%2").arg("Template="+set.readEntry ("Template")).arg(configFile));
585 contentTemplateFile=templateDir+"content-template.xml";
586 contentFile=tmpDir.path()+"/content.xml";
587 pageTemplateFile=templateDir+"page-template.xml";
588 sectionTemplateFile=templateDir+"section-template.xml";
590 if (model->countMapCenters()>1 ||set.readEntry("useSections").contains("yes"))
593 // Copy template to tmpdir
594 system ("cp -r "+templateDir+"* "+tmpDir.path());
596 // Read content-template
597 if (!loadStringFromDisk (contentTemplateFile,content))
599 QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not read %1").arg(contentTemplateFile));
603 // Read page-template
604 if (!loadStringFromDisk (pageTemplateFile,pageTemplate))
606 QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not read %1").arg(pageTemplateFile));
610 // Read section-template
611 if (useSections && !loadStringFromDisk (sectionTemplateFile,sectionTemplate))
613 QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not read %1").arg(sectionTemplateFile));