insilmaril@0: #include "exports.h" insilmaril@171: #include "file.h" insilmaril@171: #include "linkablemapobj.h" insilmaril@171: #include "misc.h" insilmaril@171: #include "mainwindow.h" insilmaril@311: #include "warningdialog.h" insilmaril@228: #include "xsltproc.h" insilmaril@0: insilmaril@171: extern Main *mainWindow; insilmaril@228: extern QDir vymBaseDir; insilmaril@404: extern QString vymName; insilmaril@131: insilmaril@171: ExportBase::ExportBase() insilmaril@0: { insilmaril@0: indentPerDepth=" "; insilmaril@377: bool ok; insilmaril@606: tmpDir.setPath (makeTmpDir(ok,"vym-export")); insilmaril@377: if (!tmpDir.exists() || !ok) insilmaril@377: QMessageBox::critical( 0, QObject::tr( "Error" ), insilmaril@377: QObject::tr("Couldn't access temporary directory\n")); insilmaril@512: cancelFlag=false; insilmaril@228: } insilmaril@228: insilmaril@228: ExportBase::~ExportBase() insilmaril@228: { insilmaril@377: // Cleanup tmpdir insilmaril@228: removeDir (tmpDir); insilmaril@0: } insilmaril@0: insilmaril@424: void ExportBase::setDir(const QDir &d) insilmaril@0: { insilmaril@424: outDir=d; insilmaril@199: } insilmaril@199: insilmaril@199: void ExportBase::setFile (const QString &p) insilmaril@199: { insilmaril@199: outputFile=p; insilmaril@0: } insilmaril@0: insilmaril@512: QString ExportBase::getFile () insilmaril@512: { insilmaril@512: return outputFile; insilmaril@512: } insilmaril@512: insilmaril@643: void ExportBase::setModel(VymModel *m) insilmaril@0: { insilmaril@643: model=m; insilmaril@0: } insilmaril@0: insilmaril@228: void ExportBase::setCaption (const QString &s) insilmaril@228: { insilmaril@228: caption=s; insilmaril@228: } insilmaril@228: insilmaril@228: void ExportBase::addFilter(const QString &s) insilmaril@228: { insilmaril@228: filter=s; insilmaril@228: } insilmaril@228: insilmaril@228: bool ExportBase::execDialog() insilmaril@228: { insilmaril@228: { insilmaril@643: QFileDialog *fd=new QFileDialog( 0, caption); insilmaril@366: fd->setFilter (filter); insilmaril@228: fd->setCaption(caption); insilmaril@228: fd->setMode( QFileDialog::AnyFile ); insilmaril@424: fd->setDir (outDir); insilmaril@228: fd->show(); insilmaril@228: insilmaril@228: if ( fd->exec() == QDialog::Accepted ) insilmaril@228: { insilmaril@228: if (QFile (fd->selectedFile()).exists() ) insilmaril@228: { insilmaril@404: QMessageBox mb( vymName, insilmaril@228: QObject::tr("The file %1 exists already.\nDo you want to overwrite it?").arg(fd->selectedFile()), insilmaril@228: QMessageBox::Warning, insilmaril@228: QMessageBox::Yes | QMessageBox::Default, insilmaril@228: QMessageBox::Cancel | QMessageBox::Escape, insilmaril@366: Qt::NoButton ); insilmaril@228: mb.setButtonText( QMessageBox::Yes, QObject::tr("Overwrite") ); insilmaril@228: mb.setButtonText( QMessageBox::No, QObject::tr("Cancel")); insilmaril@228: ExportBase ex; insilmaril@228: switch( mb.exec() ) insilmaril@228: { insilmaril@228: case QMessageBox::Yes: insilmaril@228: // save insilmaril@228: break;; insilmaril@228: case QMessageBox::Cancel: insilmaril@512: cancelFlag=true; insilmaril@228: return false; insilmaril@228: break; insilmaril@228: } insilmaril@228: } insilmaril@228: outputFile=fd->selectedFile(); insilmaril@512: cancelFlag=false; insilmaril@228: return true; insilmaril@228: } insilmaril@228: } insilmaril@228: return false; insilmaril@228: } insilmaril@228: insilmaril@512: bool ExportBase::canceled() insilmaril@512: { insilmaril@512: return cancelFlag; insilmaril@512: } insilmaril@512: insilmaril@171: QString ExportBase::getSectionString(BranchObj *bostart) insilmaril@163: { insilmaril@205: // Make prefix like "2.5.3" for "bo:2,bo:5,bo:3" insilmaril@163: QString r; insilmaril@163: BranchObj *bo=bostart; insilmaril@163: int depth=bo->getDepth(); insilmaril@163: while (depth>0) insilmaril@163: { insilmaril@163: r=QString("%1").arg(1+bo->getNum(),0,10)+"." + r; insilmaril@163: bo=(BranchObj*)(bo->getParObj()); insilmaril@163: depth=bo->getDepth(); insilmaril@163: } insilmaril@163: if (r.isEmpty()) insilmaril@163: return r; insilmaril@163: else insilmaril@163: return r + " "; insilmaril@163: } insilmaril@163: insilmaril@239: //////////////////////////////////////////////////////////////////////// insilmaril@512: ExportASCII::ExportASCII() insilmaril@512: { insilmaril@512: filter="TXT (*.txt)"; insilmaril@512: caption=vymName+ " -" +QObject::tr("Export as ASCII")+" "+QObject::tr("(still experimental)"); insilmaril@512: } insilmaril@512: insilmaril@228: void ExportASCII::doExport() insilmaril@0: { insilmaril@199: QFile file (outputFile); insilmaril@366: if ( !file.open( QIODevice::WriteOnly ) ) insilmaril@0: { insilmaril@228: qWarning ("ExportBase::exportXML couldn't open "+outputFile); insilmaril@0: return; insilmaril@0: } insilmaril@0: QTextStream ts( &file ); // use LANG decoding here... insilmaril@0: insilmaril@0: // Main loop over all branches insilmaril@0: QString s; insilmaril@613: QString curIndent; insilmaril@613: int i; insilmaril@0: BranchObj *bo; insilmaril@643: bo=model->first(); insilmaril@0: while (bo) insilmaril@0: { insilmaril@254: // Make indentstring insilmaril@613: curIndent=""; insilmaril@613: for (i=0;igetDepth()-1;i++) curIndent+= indentPerDepth; insilmaril@254: insilmaril@600: if (!bo->hasHiddenExportParent() ) insilmaril@239: { insilmaril@613: switch (bo->getDepth()) insilmaril@600: { insilmaril@613: case 0: insilmaril@613: ts << underline (bo->getHeading(),QString("=")); insilmaril@613: ts << "\n"; insilmaril@613: break; insilmaril@613: case 1: insilmaril@613: ts << "\n"; insilmaril@613: ts << (underline (getSectionString(bo) + bo->getHeading(), QString("-") ) ); insilmaril@613: ts << "\n"; insilmaril@613: break; insilmaril@613: case 2: insilmaril@613: ts << "\n"; insilmaril@613: ts << (curIndent + "* " + bo->getHeading()); insilmaril@613: ts << "\n"; insilmaril@613: break; insilmaril@613: case 3: insilmaril@613: ts << (curIndent + "- " + bo->getHeading()); insilmaril@613: ts << "\n"; insilmaril@613: break; insilmaril@613: default: insilmaril@613: ts << (curIndent + "- " + bo->getHeading()); insilmaril@613: ts << "\n"; insilmaril@613: break; insilmaril@613: } insilmaril@613: insilmaril@600: // If necessary, write note insilmaril@600: if (!bo->getNote().isEmpty()) insilmaril@600: { insilmaril@613: curIndent +=" | "; insilmaril@688: s=bo->getNoteASCII( curIndent, 80); insilmaril@688: ts << s; insilmaril@600: } insilmaril@0: } insilmaril@643: bo=model->next(bo); insilmaril@0: } insilmaril@0: file.close(); insilmaril@0: } insilmaril@0: insilmaril@613: QString ExportASCII::underline (const QString &text, const QString &line) insilmaril@613: { insilmaril@613: QString r=text + "\n"; insilmaril@613: for (int j=0;jfirst(); insilmaril@497: while (bo) insilmaril@497: { insilmaril@600: if (!bo->hasHiddenExportParent() ) insilmaril@497: { insilmaril@600: // If necessary, write note insilmaril@600: if (!bo->getNote().isEmpty()) insilmaril@600: { insilmaril@600: s =bo->getNoteASCII(); insilmaril@613: s=s.replace ("\n","\n"+curIndent); insilmaril@600: ts << ("\""+s+"\","); insilmaril@600: } else insilmaril@600: ts <<"\"\","; insilmaril@497: insilmaril@600: // Make indentstring insilmaril@613: for (i=0;igetDepth();i++) curIndent+= "\"\","; insilmaril@497: insilmaril@600: // Write heading insilmaril@613: ts << curIndent << "\"" << bo->getHeading()<<"\""<next(bo); insilmaril@613: curIndent=""; insilmaril@497: } insilmaril@497: file.close(); insilmaril@497: } insilmaril@497: insilmaril@239: //////////////////////////////////////////////////////////////////////// insilmaril@239: void ExportKDEBookmarks::doExport() insilmaril@239: { insilmaril@721: WarningDialog dia; insilmaril@721: dia.showCancelButton (true); insilmaril@721: dia.setText(QObject::tr("Exporting the %1 bookmarks will overwrite\nyour existing bookmarks file.").arg("KDE")); insilmaril@721: dia.setCaption(QObject::tr("Warning: Overwriting %1 bookmarks").arg("KDE")); insilmaril@721: dia.setShowAgainName("/exports/KDE/overwriteKDEBookmarks"); insilmaril@721: if (dia.exec()==QDialog::Accepted) insilmaril@239: { insilmaril@721: model->exportXML(tmpDir.path(),false); insilmaril@721: insilmaril@721: XSLTProc p; insilmaril@721: p.setInputFile (tmpDir.path()+"/"+model->getMapName()+".xml"); insilmaril@721: p.setOutputFile (tmpDir.home().path()+"/.kde/share/apps/konqueror/bookmarks.xml"); insilmaril@721: p.setXSLFile (vymBaseDir.path()+"/styles/vym2kdebookmarks.xsl"); insilmaril@721: p.process(); insilmaril@721: insilmaril@721: QString ub=vymBaseDir.path()+"/scripts/update-bookmarks"; insilmaril@721: QProcess *proc= new QProcess ; insilmaril@721: proc->start( ub); insilmaril@721: if (!proc->waitForStarted()) insilmaril@311: { insilmaril@721: QMessageBox::warning(0, insilmaril@721: QObject::tr("Warning"), insilmaril@721: QObject::tr("Couldn't find script %1\nto notifiy Browsers of changed bookmarks.").arg(ub)); insilmaril@721: } insilmaril@239: } insilmaril@239: } insilmaril@239: insilmaril@239: //////////////////////////////////////////////////////////////////////// insilmaril@311: void ExportFirefoxBookmarks::doExport() insilmaril@311: { insilmaril@721: WarningDialog dia; insilmaril@721: dia.showCancelButton (true); insilmaril@721: dia.setText(QObject::tr("Exporting the %1 bookmarks will overwrite\nyour existing bookmarks file.").arg("Firefox")); insilmaril@721: dia.setCaption(QObject::tr("Warning: Overwriting %1 bookmarks").arg("Firefox")); insilmaril@721: dia.setShowAgainName("/vym/warnings/overwriteImportBookmarks"); insilmaril@721: if (dia.exec()==QDialog::Accepted) insilmaril@311: { insilmaril@721: model->exportXML(tmpDir.path(),false); insilmaril@311: insilmaril@311: /* insilmaril@721: XSLTProc p; insilmaril@721: p.setInputFile (tmpDir.path()+"/"+me->getMapName()+".xml"); insilmaril@721: p.setOutputFile (tmpDir.home().path()+"/.kde/share/apps/konqueror/bookmarks.xml"); insilmaril@721: p.setXSLFile (vymBaseDir.path()+"/styles/vym2kdebookmarks.xsl"); insilmaril@721: p.process(); insilmaril@311: insilmaril@721: QString ub=vymBaseDir.path()+"/scripts/update-bookmarks"; insilmaril@721: QProcess *proc = new QProcess( ); insilmaril@721: proc->addArgument(ub); insilmaril@311: insilmaril@721: if ( !proc->start() ) insilmaril@721: { insilmaril@721: QMessageBox::warning(0, insilmaril@721: QObject::tr("Warning"), insilmaril@721: QObject::tr("Couldn't find script %1\nto notifiy Browsers of changed bookmarks.").arg(ub)); insilmaril@721: } insilmaril@311: insilmaril@311: */ insilmaril@311: } insilmaril@311: } insilmaril@311: insilmaril@311: //////////////////////////////////////////////////////////////////////// insilmaril@228: void ExportTaskjuggler::doExport() insilmaril@228: { insilmaril@721: model->exportXML(tmpDir.path(),false); insilmaril@228: insilmaril@721: XSLTProc p; insilmaril@721: p.setInputFile (tmpDir.path()+"/"+model->getMapName()+".xml"); insilmaril@721: p.setOutputFile (outputFile); insilmaril@721: p.setXSLFile (vymBaseDir.path()+"/styles/vym2taskjuggler.xsl"); insilmaril@721: p.process(); insilmaril@228: } insilmaril@228: insilmaril@239: //////////////////////////////////////////////////////////////////////// insilmaril@228: void ExportLaTeX::doExport() insilmaril@0: { insilmaril@183: // Exports a map to a LaTex file. insilmaril@183: // This file needs to be included insilmaril@183: // or inported into a LaTex document insilmaril@183: // it will not add a preamble, or anything insilmaril@183: // that makes a full LaTex document. insilmaril@199: QFile file (outputFile); insilmaril@366: if ( !file.open( QIODevice::WriteOnly ) ) { insilmaril@205: QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not write %1").arg(outputFile)); insilmaril@205: mainWindow->statusMessage(QString(QObject::tr("Export failed."))); insilmaril@163: return; insilmaril@163: } insilmaril@163: QTextStream ts( &file ); // use LANG decoding here... insilmaril@163: ts.setEncoding (QTextStream::UnicodeUTF8); // Force UTF8 insilmaril@163: insilmaril@163: // Main loop over all branches insilmaril@163: QString s; insilmaril@613: // QString curIndent(""); insilmaril@163: // int i; insilmaril@163: BranchObj *bo; insilmaril@643: bo=model->first(); insilmaril@163: while (bo) { insilmaril@600: if (!bo->hasHiddenExportParent() ) insilmaril@600: { insilmaril@600: if (bo->getDepth()==0); insilmaril@600: else if (bo->getDepth()==1) { insilmaril@600: ts << ("\\chapter{" + bo->getHeading()+ "}\n"); insilmaril@600: } insilmaril@600: else if (bo->getDepth()==2) { insilmaril@600: ts << ("\\section{" + bo->getHeading()+ "}\n"); insilmaril@600: } insilmaril@600: else if (bo->getDepth()==3) { insilmaril@600: ts << ("\\subsection{" + bo->getHeading()+ "}\n"); insilmaril@600: } insilmaril@600: else if (bo->getDepth()==4) { insilmaril@600: ts << ("\\subsubsection{" + bo->getHeading()+ "}\n"); insilmaril@600: } insilmaril@600: else { insilmaril@600: ts << ("\\paragraph*{" + bo->getHeading()+ "}\n"); insilmaril@600: } insilmaril@600: insilmaril@600: // If necessary, write note insilmaril@600: if (!bo->getNote().isEmpty()) { insilmaril@600: ts << (bo->getNoteASCII()); insilmaril@600: ts << ("\n"); insilmaril@600: } insilmaril@254: } insilmaril@643: bo=model->next(bo); insilmaril@163: } insilmaril@163: file.close(); insilmaril@0: } insilmaril@0: insilmaril@239: //////////////////////////////////////////////////////////////////////// insilmaril@171: ExportOO::ExportOO() insilmaril@171: { insilmaril@199: useSections=false; insilmaril@171: } insilmaril@163: insilmaril@171: ExportOO::~ExportOO() insilmaril@171: { insilmaril@171: } insilmaril@163: insilmaril@171: QString ExportOO::buildList (BranchObj *current) insilmaril@171: { insilmaril@171: QString r; insilmaril@171: BranchObj *bo; insilmaril@183: insilmaril@183: uint i=0; insilmaril@171: bo=current->getFirstBranch(); insilmaril@171: if (bo) insilmaril@171: { insilmaril@600: if (!bo->hasHiddenExportParent() ) insilmaril@600: { insilmaril@600: // Start list insilmaril@600: r+="\n"; insilmaril@600: while (bo) insilmaril@600: { insilmaril@600: r+=""; insilmaril@600: r+=quotemeta(bo->getHeading()); insilmaril@600: // If necessary, write note insilmaril@600: if (!bo->getNote().isEmpty()) insilmaril@600: r+=bo->getNoteOpenDoc(); insilmaril@600: r+=""; insilmaril@600: r+=buildList (bo); // recursivly add deeper branches insilmaril@600: r+="\n"; insilmaril@600: i++; insilmaril@600: bo=current->getBranchNum(i); insilmaril@600: } insilmaril@600: r+="\n"; insilmaril@600: } insilmaril@171: } insilmaril@171: return r; insilmaril@171: } insilmaril@163: insilmaril@171: insilmaril@171: void ExportOO::exportPresentation() insilmaril@171: { insilmaril@183: QString allPages; insilmaril@171: insilmaril@643: /* FIXME not adapted to multiple mapCenters yet insilmaril@163: // Insert new content insilmaril@171: content.replace ("",quotemeta(mapCenter->getHeading())); insilmaril@171: content.replace ("",quotemeta(mapCenter->getAuthor())); insilmaril@183: insilmaril@183: QString onePage; insilmaril@183: QString list; insilmaril@183: insilmaril@194: BranchObj *sectionBO=mapCenter->getFirstBranch(); insilmaril@183: int i=0; insilmaril@183: BranchObj *pagesBO; insilmaril@183: int j=0; insilmaril@183: insilmaril@194: // Walk sections insilmaril@600: while (sectionBO && !sectionBO->hasHiddenExportParent() ) insilmaril@183: { insilmaril@194: if (useSections) insilmaril@183: { insilmaril@194: // Add page with section title insilmaril@194: onePage=sectionTemplate; insilmaril@194: onePage.replace ("", quotemeta(sectionBO->getHeading() ) ); insilmaril@183: allPages+=onePage; insilmaril@183: } else insilmaril@183: { insilmaril@183: i=-2; // only use inner loop to insilmaril@183: // turn mainbranches into pages insilmaril@194: sectionBO=mapCenter; insilmaril@183: } insilmaril@183: insilmaril@183: // Walk mainpages insilmaril@194: pagesBO=sectionBO->getFirstBranch(); insilmaril@183: j=0; insilmaril@600: while (pagesBO && !pagesBO->hasHiddenExportParent() ) insilmaril@183: { insilmaril@183: // Add page with list of items insilmaril@254: onePage=pageTemplate; insilmaril@254: onePage.replace ("", quotemeta (pagesBO->getHeading() ) ); insilmaril@254: list=buildList (pagesBO); insilmaril@254: onePage.replace ("", list); insilmaril@254: allPages+=onePage; insilmaril@183: j++; insilmaril@194: pagesBO=sectionBO->getBranchNum(j); insilmaril@183: } insilmaril@183: i++; insilmaril@194: sectionBO=mapCenter->getBranchNum(i); insilmaril@183: } insilmaril@183: insilmaril@171: content.replace ("",allPages); insilmaril@171: insilmaril@171: // Write modified content insilmaril@171: QFile f (contentFile); insilmaril@366: if ( !f.open( QIODevice::WriteOnly ) ) insilmaril@171: { insilmaril@205: QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not write %1").arg(contentFile)); insilmaril@205: mainWindow->statusMessage(QString(QObject::tr("Export failed."))); insilmaril@171: return; insilmaril@171: } insilmaril@171: insilmaril@171: QTextStream t( &f ); insilmaril@171: t << content; insilmaril@171: f.close(); insilmaril@171: insilmaril@163: // zip tmpdir to destination insilmaril@171: zipDir (tmpDir,outputFile); insilmaril@643: */ insilmaril@163: } insilmaril@163: insilmaril@194: bool ExportOO::setConfigFile (const QString &cf) insilmaril@183: { insilmaril@183: configFile=cf; insilmaril@183: int i=cf.findRev ("/"); insilmaril@183: if (i>=0) configDir=cf.left(i); insilmaril@183: SimpleSettings set; insilmaril@183: set.readSettings(configFile); insilmaril@183: insilmaril@183: // set paths insilmaril@183: templateDir=configDir+"/"+set.readEntry ("Template"); insilmaril@183: insilmaril@194: QDir d (templateDir); insilmaril@194: if (!d.exists()) insilmaril@194: { insilmaril@194: QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Check \"%1\" in\n%2").arg("Template="+set.readEntry ("Template")).arg(configFile)); insilmaril@194: return false; insilmaril@194: insilmaril@194: } insilmaril@183: insilmaril@183: contentTemplateFile=templateDir+"content-template.xml"; insilmaril@183: contentFile=tmpDir.path()+"/content.xml"; insilmaril@183: pageTemplateFile=templateDir+"page-template.xml"; insilmaril@194: sectionTemplateFile=templateDir+"section-template.xml"; insilmaril@183: insilmaril@199: if (set.readEntry("useSections").contains("yes")) insilmaril@199: useSections=true; insilmaril@199: insilmaril@183: // Copy template to tmpdir insilmaril@183: system ("cp -r "+templateDir+"* "+tmpDir.path()); insilmaril@183: insilmaril@183: // Read content-template insilmaril@183: if (!loadStringFromDisk (contentTemplateFile,content)) insilmaril@183: { insilmaril@194: QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not read %1").arg(contentTemplateFile)); insilmaril@194: return false; insilmaril@183: } insilmaril@183: insilmaril@183: // Read page-template insilmaril@183: if (!loadStringFromDisk (pageTemplateFile,pageTemplate)) insilmaril@183: { insilmaril@194: QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not read %1").arg(pageTemplateFile)); insilmaril@194: return false; insilmaril@183: } insilmaril@183: insilmaril@194: // Read section-template insilmaril@201: if (useSections && !loadStringFromDisk (sectionTemplateFile,sectionTemplate)) insilmaril@183: { insilmaril@194: QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not read %1").arg(sectionTemplateFile)); insilmaril@194: return false; insilmaril@183: } insilmaril@194: return true; insilmaril@183: } insilmaril@183: