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