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(TreeItem *start)
111 // Make prefix like "2.5.3" for "bo:2,bo:5,bo:3"
114 int depth=ti->depth();
117 r=QString("%1").arg(1+ti->num(),0,10)+"." + r;
127 ////////////////////////////////////////////////////////////////////////
130 filter="TXT (*.txt)";
131 caption=vymName+ " -" +QObject::tr("Export as ASCII")+" "+QObject::tr("(still experimental)");
134 void ExportAO::doExport()
136 QFile file (outputFile);
137 if ( !file.open( QIODevice::WriteOnly ) )
139 qWarning ("ExportAO::doExport 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;
154 cur=model->nextBranch (cur,prev);
157 if (cur->getType()==TreeItem::Branch || cur->getType()==TreeItem::MapCenter)
161 for (i=0;i<cur->depth()-1;i++) curIndent+= indentPerDepth;
163 if (!cur->hasHiddenExportParent() )
165 col=cur->getHeadingColor();
166 if (col==QColor (255,0,0))
168 else if (col==QColor (217,81,0))
170 else if (col==QColor (0,85,0))
174 switch (cur->depth())
177 //ts << underline (cur->getHeading(),QString("="));
182 //ts << (underline ( cur->getHeading(), QString("-") ) );
185 case 2: // Main heading
187 ts << underline ( cur->getHeading(), QString("=") );
190 case 3: // Achievement, Bonus, Objective ...
191 ts << underline ( cur->getHeading(), "-");
194 case 4: // That's the item we need to know
195 //ts << (curIndent + "* " + colString+" "+ cur->getHeading());
196 ts << colString+" "+ cur->getHeading();
197 if (cur->isActiveStandardFlag ("hook-green"))
199 else if (cur->isActiveStandardFlag ("wip"))
201 else if (cur->isActiveStandardFlag ("cross-red"))
202 ts << " [NOT STARTED] ";
206 ts << (curIndent + "- " + cur->getHeading());
211 // If necessary, write note
212 if (!cur->getNoteObj().isEmpty())
215 s=cur->getNoteASCII( curIndent, 80);
220 cur=model->nextBranch(cur,prev);
225 QString ExportAO::underline (const QString &text, const QString &line)
227 QString r=text + "\n";
228 for (int j=0;j<text.length();j++) r+=line;
233 ////////////////////////////////////////////////////////////////////////
234 ExportASCII::ExportASCII()
236 filter="TXT (*.txt)";
237 caption=vymName+ " -" +QObject::tr("Export as ASCII")+" "+QObject::tr("(still experimental)");
240 void ExportASCII::doExport()
242 QFile file (outputFile);
243 if ( !file.open( QIODevice::WriteOnly ) )
245 qWarning ("ExportASCII::doExport couldn't open "+outputFile);
248 QTextStream ts( &file ); // use LANG decoding here...
250 // Main loop over all branches
254 BranchItem *cur=NULL;
255 BranchItem *prev=NULL;
257 cur=model->nextBranch (cur,prev);
260 if (cur->getType()==TreeItem::Branch || cur->getType()==TreeItem::MapCenter)
264 for (i=0;i<cur->depth()-1;i++) curIndent+= indentPerDepth;
266 if (!cur->hasHiddenExportParent() )
268 //std::cout << "ExportASCII:: "<<curIndent.toStdString()<<cur->getHeading().toStdString()<<std::endl;
269 switch (cur->depth())
272 ts << underline (cur->getHeading(),QString("="));
277 ts << (underline (getSectionString(cur) + cur->getHeading(), QString("-") ) );
282 ts << (curIndent + "* " + cur->getHeading());
286 ts << (curIndent + "- " + cur->getHeading());
290 ts << (curIndent + "- " + cur->getHeading());
295 // If necessary, write note
296 if (!cur->getNoteObj().isEmpty())
299 s=cur->getNoteASCII( curIndent, 80);
304 cur=model->nextBranch(cur,prev);
309 QString ExportASCII::underline (const QString &text, const QString &line)
311 QString r=text + "\n";
312 for (int j=0;j<text.length();j++) r+=line;
317 ////////////////////////////////////////////////////////////////////////
318 void ExportCSV::doExport()
320 QFile file (outputFile);
321 if ( !file.open( QIODevice::WriteOnly ) )
323 qWarning ("ExportBase::exportXML couldn't open "+outputFile);
326 QTextStream ts( &file ); // use LANG decoding here...
329 ts << "\"Note\"" <<endl;
331 // Main loop over all branches
333 QString curIndent("");
335 BranchObj *bo; //FIXME-3 still needed?
336 BranchItem *cur=NULL;
337 BranchItem *prev=NULL;
338 cur=model->nextBranch (cur,prev);
341 bo=(BranchObj*)(cur->getLMO());
343 if (!cur->hasHiddenExportParent() )
345 // If necessary, write note
346 if (!cur->getNoteObj().isEmpty())
348 s =cur->getNoteASCII();
349 s=s.replace ("\n","\n"+curIndent);
350 ts << ("\""+s+"\",");
355 for (i=0;i<cur->depth();i++) curIndent+= "\"\",";
358 ts << curIndent << "\"" << cur->getHeading()<<"\""<<endl;
361 cur=model->nextBranch(cur,prev);
367 ////////////////////////////////////////////////////////////////////////
368 void ExportKDE3Bookmarks::doExport()
371 dia.showCancelButton (true);
372 dia.setText(QObject::tr("Exporting the %1 bookmarks will overwrite\nyour existing bookmarks file.").arg("KDE"));
373 dia.setCaption(QObject::tr("Warning: Overwriting %1 bookmarks").arg("KDE 3"));
374 dia.setShowAgainName("/exports/KDE/overwriteKDEBookmarks");
375 if (dia.exec()==QDialog::Accepted)
377 model->exportXML(tmpDir.path(),false);
380 p.setInputFile (tmpDir.path()+"/"+model->getMapName()+".xml");
381 p.setOutputFile (tmpDir.home().path()+"/.kde/share/apps/konqueror/bookmarks.xml");
382 p.setXSLFile (vymBaseDir.path()+"/styles/vym2kdebookmarks.xsl");
385 QString ub=vymBaseDir.path()+"/scripts/update-bookmarks";
386 QProcess *proc= new QProcess ;
388 if (!proc->waitForStarted())
390 QMessageBox::warning(0,
391 QObject::tr("Warning"),
392 QObject::tr("Couldn't find script %1\nto notifiy Browsers of changed bookmarks.").arg(ub));
397 ////////////////////////////////////////////////////////////////////////
398 void ExportKDE4Bookmarks::doExport()
401 dia.showCancelButton (true);
402 dia.setText(QObject::tr("Exporting the %1 bookmarks will overwrite\nyour existing bookmarks file.").arg("KDE 4"));
403 dia.setCaption(QObject::tr("Warning: Overwriting %1 bookmarks").arg("KDE"));
404 dia.setShowAgainName("/exports/KDE/overwriteKDEBookmarks");
405 if (dia.exec()==QDialog::Accepted)
407 model->exportXML(tmpDir.path(),false);
410 p.setInputFile (tmpDir.path()+"/"+model->getMapName()+".xml");
411 p.setOutputFile (tmpDir.home().path()+"/.kde4/share/apps/konqueror/bookmarks.xml");
412 p.setXSLFile (vymBaseDir.path()+"/styles/vym2kdebookmarks.xsl");
415 QString ub=vymBaseDir.path()+"/scripts/update-bookmarks";
416 QProcess *proc= new QProcess ;
418 if (!proc->waitForStarted())
420 QMessageBox::warning(0,
421 QObject::tr("Warning"),
422 QObject::tr("Couldn't find script %1\nto notifiy Browsers of changed bookmarks.").arg(ub));
427 ////////////////////////////////////////////////////////////////////////
428 void ExportFirefoxBookmarks::doExport()
431 dia.showCancelButton (true);
432 dia.setText(QObject::tr("Exporting the %1 bookmarks will overwrite\nyour existing bookmarks file.").arg("Firefox"));
433 dia.setCaption(QObject::tr("Warning: Overwriting %1 bookmarks").arg("Firefox"));
434 dia.setShowAgainName("/vym/warnings/overwriteImportBookmarks");
435 if (dia.exec()==QDialog::Accepted)
437 model->exportXML(tmpDir.path(),false);
441 p.setInputFile (tmpDir.path()+"/"+me->getMapName()+".xml");
442 p.setOutputFile (tmpDir.home().path()+"/.kde/share/apps/konqueror/bookmarks.xml");
443 p.setXSLFile (vymBaseDir.path()+"/styles/vym2kdebookmarks.xsl");
446 QString ub=vymBaseDir.path()+"/scripts/update-bookmarks";
447 QProcess *proc = new QProcess( );
448 proc->addArgument(ub);
450 if ( !proc->start() )
452 QMessageBox::warning(0,
453 QObject::tr("Warning"),
454 QObject::tr("Couldn't find script %1\nto notifiy Browsers of changed bookmarks.").arg(ub));
461 ////////////////////////////////////////////////////////////////////////
462 void ExportTaskjuggler::doExport()
464 model->exportXML(tmpDir.path(),false);
467 p.setInputFile (tmpDir.path()+"/"+model->getMapName()+".xml");
468 p.setOutputFile (outputFile);
469 p.setXSLFile (vymBaseDir.path()+"/styles/vym2taskjuggler.xsl");
473 ////////////////////////////////////////////////////////////////////////
474 void ExportLaTeX::doExport()
476 // Exports a map to a LaTex file.
477 // This file needs to be included
478 // or inported into a LaTex document
479 // it will not add a preamble, or anything
480 // that makes a full LaTex document.
481 QFile file (outputFile);
482 if ( !file.open( QIODevice::WriteOnly ) ) {
483 QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not write %1").arg(outputFile));
484 mainWindow->statusMessage(QString(QObject::tr("Export failed.")));
487 QTextStream ts( &file ); // use LANG decoding here...
488 ts.setEncoding (QTextStream::UnicodeUTF8); // Force UTF8
490 // Main loop over all branches
492 // QString curIndent("");
495 BranchItem *cur=NULL;
496 BranchItem *prev=NULL;
497 model->nextBranch(cur,prev);
500 bo=(BranchObj*)(cur->getLMO());
502 if (!cur->hasHiddenExportParent() )
504 switch (cur->depth() )
508 ts << ("\\chapter{" + cur->getHeading()+ "}\n");
511 ts << ("\\section{" + cur->getHeading()+ "}\n");
514 ts << ("\\subsection{" + cur->getHeading()+ "}\n");
517 ts << ("\\subsubsection{" + cur->getHeading()+ "}\n");
520 ts << ("\\paragraph*{" + cur->getHeading()+ "}\n");
523 // If necessary, write note
524 if (!cur->getNoteObj().isEmpty()) {
525 ts << (cur->getNoteASCII());
529 cur=model->nextBranch(cur,prev);
534 ////////////////////////////////////////////////////////////////////////
540 ExportOO::~ExportOO()
544 QString ExportOO::buildList (TreeItem *current)
549 BranchItem *bi=current->getFirstBranch();
552 if (!bi->hasHiddenExportParent() ) // FIXME-3 use BranchItem...
555 r+="<text:list text:style-name=\"vym-list\">\n";
558 r+="<text:list-item><text:p >";
559 r+=quotemeta(bi->getHeading());
560 // If necessary, write note
561 if (!bi->getNoteObj().isEmpty())
562 r+=bi->getNoteOpenDoc();
564 r+=buildList (bi); // recursivly add deeper branches
565 r+="</text:list-item>\n";
567 bi=current->getBranchNum(i);
576 void ExportOO::exportPresentation()
580 BranchItem *firstMCO=(BranchItem*)(model->getRootItem()->getFirstBranch());
583 QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("No objects in map!"));
587 // Insert new content
588 // FIXME add extra title in mapinfo for vym 1.13.x
589 content.replace ("<!-- INSERT TITLE -->",quotemeta(firstMCO->getHeading()));
590 content.replace ("<!-- INSERT AUTHOR -->",quotemeta(model->getAuthor()));
595 BranchItem *sectionBI;
600 int mapcenters=model->getRootItem()->branchCount();
602 // useSections already has been set in setConfigFile
606 sectionBI=firstMCO->getFirstBranch();
609 while (sectionBI && !sectionBI->hasHiddenExportParent() )
613 // Add page with section title
614 onePage=sectionTemplate;
615 onePage.replace ("<!-- INSERT PAGE HEADING -->", quotemeta(sectionBI->getHeading() ) );
617 pagesBI=sectionBI->getFirstBranch();
620 //i=-2; // only use inner loop to
621 // turn mainbranches into pages
622 //sectionBI=firstMCO;
627 while (pagesBI && !pagesBI->hasHiddenExportParent() )
629 // Add page with list of items
630 onePage=pageTemplate;
631 onePage.replace ("<!-- INSERT PAGE HEADING -->", quotemeta (pagesBI->getHeading() ) );
632 list=buildList (pagesBI);
633 onePage.replace ("<!-- INSERT LIST -->", list);
635 if (pagesBI!=sectionBI)
638 pagesBI=((BranchItem*)pagesBI->parent())->getBranchNum(j);
640 pagesBI=NULL; // We are already iterating over the sectionBIs
644 sectionBI=model->getRootItem()->getBranchNum (i);
646 sectionBI=firstMCO->getBranchNum (i);
649 content.replace ("<!-- INSERT PAGES -->",allPages);
651 // Write modified content
652 QFile f (contentFile);
653 if ( !f.open( QIODevice::WriteOnly ) )
655 QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not write %1").arg(contentFile));
656 mainWindow->statusMessage(QString(QObject::tr("Export failed.")));
664 // zip tmpdir to destination
665 zipDir (tmpDir,outputFile);
668 bool ExportOO::setConfigFile (const QString &cf)
671 int i=cf.findRev ("/");
672 if (i>=0) configDir=cf.left(i);
674 set.readSettings(configFile);
677 templateDir=configDir+"/"+set.readEntry ("Template");
679 QDir d (templateDir);
682 QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Check \"%1\" in\n%2").arg("Template="+set.readEntry ("Template")).arg(configFile));
687 contentTemplateFile=templateDir+"content-template.xml";
688 contentFile=tmpDir.path()+"/content.xml";
689 pageTemplateFile=templateDir+"page-template.xml";
690 sectionTemplateFile=templateDir+"section-template.xml";
692 if (set.readEntry("useSections").contains("yes"))
695 // Copy template to tmpdir
696 system ("cp -r "+templateDir+"* "+tmpDir.path());
698 // Read content-template
699 if (!loadStringFromDisk (contentTemplateFile,content))
701 QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not read %1").arg(contentTemplateFile));
705 // Read page-template
706 if (!loadStringFromDisk (pageTemplateFile,pageTemplate))
708 QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not read %1").arg(pageTemplateFile));
712 // Read section-template
713 if (useSections && !loadStringFromDisk (sectionTemplateFile,sectionTemplate))
715 QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not read %1").arg(sectionTemplateFile));