insilmaril@2: #include insilmaril@2: #include insilmaril@2: #include insilmaril@2: //Added by qt3to4: insilmaril@2: #include insilmaril@2: insilmaril@0: #include "exports.h" insilmaril@2: #include "file.h" insilmaril@2: #include "linkablemapobj.h" insilmaril@2: #include "misc.h" insilmaril@2: #include "mainwindow.h" insilmaril@2: #include "warningdialog.h" insilmaril@2: #include "xsltproc.h" insilmaril@0: insilmaril@2: extern Main *mainWindow; insilmaril@2: extern QDir vymBaseDir; insilmaril@0: insilmaril@0: insilmaril@2: ExportBase::ExportBase() insilmaril@0: { insilmaril@0: indentPerDepth=" "; insilmaril@2: // Create tmpdir insilmaril@2: tmpDir.setPath (makeUniqueDir("/tmp/vym-XXXXXX")); insilmaril@0: } insilmaril@0: insilmaril@2: ExportBase::~ExportBase() insilmaril@0: { insilmaril@2: // Remove tmpdir insilmaril@2: removeDir (tmpDir); insilmaril@0: } insilmaril@0: insilmaril@2: void ExportBase::setDir(const QString &p) insilmaril@0: { insilmaril@2: outputDir=p; insilmaril@0: } insilmaril@0: insilmaril@2: void ExportBase::setFile (const QString &p) insilmaril@2: { insilmaril@2: outputFile=p; insilmaril@2: } insilmaril@2: insilmaril@2: void ExportBase::setMapCenter(MapCenterObj *mc) insilmaril@0: { insilmaril@0: mapCenter=mc; insilmaril@0: } insilmaril@0: insilmaril@2: void ExportBase::setCaption (const QString &s) insilmaril@0: { insilmaril@2: caption=s; insilmaril@0: } insilmaril@0: insilmaril@2: void ExportBase::addFilter(const QString &s) insilmaril@0: { insilmaril@2: filter=s; insilmaril@2: } insilmaril@2: insilmaril@2: bool ExportBase::execDialog() insilmaril@2: { insilmaril@2: if (mapCenter && mapCenter->getMapEditor()) insilmaril@2: { insilmaril@2: Q3FileDialog *fd=new Q3FileDialog( mapCenter->getMapEditor(), caption); insilmaril@2: fd->addFilter (filter); insilmaril@2: fd->setCaption(caption); insilmaril@2: fd->setMode( Q3FileDialog::AnyFile ); insilmaril@2: fd->show(); insilmaril@2: insilmaril@2: if ( fd->exec() == QDialog::Accepted ) insilmaril@2: { insilmaril@2: if (QFile (fd->selectedFile()).exists() ) insilmaril@2: { insilmaril@2: QMessageBox mb( __VYM, insilmaril@2: QObject::tr("The file %1 exists already.\nDo you want to overwrite it?").arg(fd->selectedFile()), insilmaril@2: QMessageBox::Warning, insilmaril@2: QMessageBox::Yes | QMessageBox::Default, insilmaril@2: QMessageBox::Cancel | QMessageBox::Escape, insilmaril@2: Qt::NoButton ); insilmaril@2: mb.setButtonText( QMessageBox::Yes, QObject::tr("Overwrite") ); insilmaril@2: mb.setButtonText( QMessageBox::No, QObject::tr("Cancel")); insilmaril@2: ExportBase ex; insilmaril@2: switch( mb.exec() ) insilmaril@2: { insilmaril@2: case QMessageBox::Yes: insilmaril@2: // save insilmaril@2: break;; insilmaril@2: case QMessageBox::Cancel: insilmaril@2: // return, do nothing insilmaril@2: return false; insilmaril@2: break; insilmaril@2: } insilmaril@2: } insilmaril@2: outputFile=fd->selectedFile(); insilmaril@2: return true; insilmaril@2: } insilmaril@2: } insilmaril@2: return false; insilmaril@2: } insilmaril@2: insilmaril@2: QString ExportBase::getSectionString(BranchObj *bostart) insilmaril@2: { insilmaril@2: // 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@2: insilmaril@2: //////////////////////////////////////////////////////////////////////// insilmaril@2: void ExportASCII::doExport() insilmaril@0: { insilmaril@2: QFile file (outputFile); insilmaril@2: if ( !file.open( QIODevice::WriteOnly ) ) insilmaril@2: { insilmaril@2: // FIXME experimental, testing insilmaril@2: qWarning ("ExportBase::exportXML couldn't open "+outputFile); insilmaril@2: return; insilmaril@2: } insilmaril@2: QTextStream ts( &file ); // use LANG decoding here... insilmaril@2: insilmaril@0: // Main loop over all branches insilmaril@0: QString s; insilmaril@0: QString actIndent(""); insilmaril@0: int i; insilmaril@2: uint j; insilmaril@0: BranchObj *bo; insilmaril@0: bo=mapCenter->first(); insilmaril@0: while (bo) insilmaril@0: { insilmaril@0: // Make indentstring insilmaril@0: for (i=0;igetDepth();i++) actIndent+= indentPerDepth; insilmaril@0: insilmaril@2: if (bo->getDepth()==0) insilmaril@2: { insilmaril@2: ts << (bo->getHeading()+ "\n"); insilmaril@2: for (j=0;jgetHeading().length();j++) ts<<"="; insilmaril@2: ts << "\n"; insilmaril@2: } else if (bo->getDepth()==1) insilmaril@2: ts << ("\n"+getSectionString(bo) + bo->getHeading()+ "\n"); insilmaril@2: else if (bo->getDepth()==2) insilmaril@2: ts << (actIndent + " o " + bo->getHeading()+ "\n"); insilmaril@2: else insilmaril@2: ts << (actIndent + " - " + bo->getHeading()+ "\n"); insilmaril@0: insilmaril@0: // If necessary, write note insilmaril@0: if (!bo->getNote().isEmpty()) insilmaril@0: { insilmaril@2: s =bo->getNoteASCII(); insilmaril@2: s=s.replace ("\n","\n"+actIndent); insilmaril@2: ts << (s+"\n\n"); insilmaril@0: } insilmaril@0: bo=bo->next(); insilmaril@0: actIndent=""; insilmaril@0: } insilmaril@2: file.close(); insilmaril@0: } insilmaril@0: insilmaril@2: //////////////////////////////////////////////////////////////////////// insilmaril@2: void ExportKDEBookmarks::doExport() insilmaril@0: { insilmaril@2: MapEditor *me=NULL; insilmaril@2: if (mapCenter) me=mapCenter->getMapEditor(); insilmaril@2: if (me) insilmaril@2: { insilmaril@2: WarningDialog dia; insilmaril@2: dia.setCancelButton (true); insilmaril@2: dia.setText(QObject::tr("Exporting the %1 bookmarks will overwrite\nyour existing bookmarks file.").arg("KDE")); insilmaril@2: dia.setCaption(QObject::tr("Warning: Overwriting %1 bookmarks").arg("KDE")); insilmaril@2: dia.setShowAgainName("/vym/warnings/overwriteKDEBookmarks"); insilmaril@2: if (dia.exec()==QDialog::Accepted) insilmaril@2: { insilmaril@2: me->exportXML(tmpDir.path()); insilmaril@2: insilmaril@2: XSLTProc p; insilmaril@2: p.setInputFile (tmpDir.path()+"/"+me->getMapName()+".xml"); insilmaril@2: p.setOutputFile (tmpDir.home().path()+"/.kde/share/apps/konqueror/bookmarks.xml"); insilmaril@2: p.setXSLFile (vymBaseDir.path()+"/styles/vym2kdebookmarks.xsl"); insilmaril@2: p.process(); insilmaril@2: insilmaril@2: QString ub=vymBaseDir.path()+"/scripts/update-bookmarks"; insilmaril@2: Q3Process *proc = new Q3Process( ); insilmaril@2: proc->addArgument(ub); insilmaril@2: insilmaril@2: if ( !proc->start() ) insilmaril@2: { insilmaril@2: QMessageBox::warning(0, insilmaril@2: QObject::tr("Warning"), insilmaril@2: QObject::tr("Couldn't find script %1\nto notifiy Browsers of changed bookmarks.").arg(ub)); insilmaril@2: } insilmaril@2: insilmaril@2: insilmaril@2: } insilmaril@2: } insilmaril@2: insilmaril@0: } insilmaril@0: insilmaril@2: //////////////////////////////////////////////////////////////////////// insilmaril@2: void ExportFirefoxBookmarks::doExport() insilmaril@2: { insilmaril@2: MapEditor *me=NULL; insilmaril@2: if (mapCenter) me=mapCenter->getMapEditor(); insilmaril@2: if (me) insilmaril@2: { insilmaril@2: WarningDialog dia; insilmaril@2: dia.setCancelButton (true); insilmaril@2: dia.setText(QObject::tr("Exporting the %1 bookmarks will overwrite\nyour existing bookmarks file.").arg("Firefox")); insilmaril@2: dia.setCaption(QObject::tr("Warning: Overwriting %1 bookmarks").arg("Firefox")); insilmaril@2: dia.setShowAgainName("/vym/warnings/overwriteImportBookmarks"); insilmaril@2: if (dia.exec()==QDialog::Accepted) insilmaril@2: { insilmaril@2: me->exportXML(tmpDir.path()); insilmaril@2: insilmaril@2: /* insilmaril@2: XSLTProc p; insilmaril@2: p.setInputFile (tmpDir.path()+"/"+me->getMapName()+".xml"); insilmaril@2: p.setOutputFile (tmpDir.home().path()+"/.kde/share/apps/konqueror/bookmarks.xml"); insilmaril@2: p.setXSLFile (vymBaseDir.path()+"/styles/vym2kdebookmarks.xsl"); insilmaril@2: p.process(); insilmaril@2: insilmaril@2: QString ub=vymBaseDir.path()+"/scripts/update-bookmarks"; insilmaril@2: QProcess *proc = new QProcess( ); insilmaril@2: proc->addArgument(ub); insilmaril@2: insilmaril@2: if ( !proc->start() ) insilmaril@2: { insilmaril@2: QMessageBox::warning(0, insilmaril@2: QObject::tr("Warning"), insilmaril@2: QObject::tr("Couldn't find script %1\nto notifiy Browsers of changed bookmarks.").arg(ub)); insilmaril@2: } insilmaril@2: insilmaril@2: */ insilmaril@2: insilmaril@2: } insilmaril@2: } insilmaril@2: } insilmaril@2: insilmaril@2: //////////////////////////////////////////////////////////////////////// insilmaril@2: void ExportTaskjuggler::doExport() insilmaril@2: { insilmaril@2: MapEditor *me=NULL; insilmaril@2: if (mapCenter) me=mapCenter->getMapEditor(); insilmaril@2: if (me) insilmaril@2: { insilmaril@2: me->exportXML(tmpDir.path()); insilmaril@2: insilmaril@2: XSLTProc p; insilmaril@2: p.setInputFile (tmpDir.path()+"/"+me->getMapName()+".xml"); insilmaril@2: p.setOutputFile (outputFile); insilmaril@2: p.setXSLFile (vymBaseDir.path()+"/styles/vym2taskjuggler.xsl"); insilmaril@2: p.process(); insilmaril@2: } insilmaril@2: insilmaril@2: } insilmaril@2: insilmaril@2: //////////////////////////////////////////////////////////////////////// insilmaril@2: void ExportLaTeX::doExport() insilmaril@2: { insilmaril@2: // Exports a map to a LaTex file. insilmaril@2: // This file needs to be included insilmaril@2: // or inported into a LaTex document insilmaril@2: // it will not add a preamble, or anything insilmaril@2: // that makes a full LaTex document. insilmaril@2: QFile file (outputFile); insilmaril@2: if ( !file.open( QIODevice::WriteOnly ) ) { insilmaril@2: QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not write %1").arg(outputFile)); insilmaril@2: mainWindow->statusMessage(QString(QObject::tr("Export failed."))); insilmaril@2: return; insilmaril@2: } insilmaril@2: QTextStream ts( &file ); // use LANG decoding here... insilmaril@2: ts.setEncoding (QTextStream::UnicodeUTF8); // Force UTF8 insilmaril@2: insilmaril@2: // Main loop over all branches insilmaril@2: QString s; insilmaril@2: // QString actIndent(""); insilmaril@2: // int i; insilmaril@2: BranchObj *bo; insilmaril@2: bo=mapCenter->first(); insilmaril@2: while (bo) { insilmaril@2: if (bo->getDepth()==0); insilmaril@2: else if (bo->getDepth()==1) { insilmaril@2: ts << ("\\chapter{" + bo->getHeading()+ "}\n"); insilmaril@2: } insilmaril@2: else if (bo->getDepth()==2) { insilmaril@2: ts << ("\\section{" + bo->getHeading()+ "}\n"); insilmaril@2: } insilmaril@2: else if (bo->getDepth()==3) { insilmaril@2: ts << ("\\subsection{" + bo->getHeading()+ "}\n"); insilmaril@2: } insilmaril@2: else if (bo->getDepth()==4) { insilmaril@2: ts << ("\\subsubsection{" + bo->getHeading()+ "}\n"); insilmaril@2: } insilmaril@2: else { insilmaril@2: ts << ("\\paragraph*{" + bo->getHeading()+ "}\n"); insilmaril@2: } insilmaril@2: insilmaril@2: // If necessary, write note insilmaril@2: if (!bo->getNote().isEmpty()) { insilmaril@2: ts << (bo->getNoteASCII()); insilmaril@2: ts << ("\n"); insilmaril@2: } insilmaril@2: bo=bo->next(); insilmaril@2: } insilmaril@2: file.close(); insilmaril@2: } insilmaril@2: insilmaril@2: //////////////////////////////////////////////////////////////////////// insilmaril@2: ExportOO::ExportOO() insilmaril@2: { insilmaril@2: useSections=false; insilmaril@2: } insilmaril@2: insilmaril@2: ExportOO::~ExportOO() insilmaril@2: { insilmaril@2: } insilmaril@2: insilmaril@2: QString ExportOO::buildList (BranchObj *current) insilmaril@2: { insilmaril@2: QString r; insilmaril@2: BranchObj *bo; insilmaril@2: insilmaril@2: uint i=0; insilmaril@2: bo=current->getFirstBranch(); insilmaril@2: if (bo) insilmaril@2: { insilmaril@2: // Start list insilmaril@2: r+="\n"; insilmaril@2: while (bo) insilmaril@2: { insilmaril@2: r+=""; insilmaril@2: r+=quotemeta(bo->getHeading()); insilmaril@2: // If necessary, write note insilmaril@2: if (!bo->getNote().isEmpty()) insilmaril@2: r+=bo->getNoteOpenDoc(); insilmaril@2: r+=""; insilmaril@2: r+=buildList (bo); // recursivly add deeper branches insilmaril@2: r+="\n"; insilmaril@2: i++; insilmaril@2: bo=current->getBranchNum(i); insilmaril@2: } insilmaril@2: r+="\n"; insilmaril@2: } insilmaril@2: return r; insilmaril@2: } insilmaril@2: insilmaril@2: insilmaril@2: void ExportOO::exportPresentation() insilmaril@2: { insilmaril@2: QString allPages; insilmaril@2: insilmaril@2: // Insert new content insilmaril@2: content.replace ("",quotemeta(mapCenter->getHeading())); insilmaril@2: content.replace ("",quotemeta(mapCenter->getAuthor())); insilmaril@2: insilmaril@2: QString onePage; insilmaril@2: QString list; insilmaril@2: insilmaril@2: BranchObj *sectionBO=mapCenter->getFirstBranch(); insilmaril@2: int i=0; insilmaril@2: BranchObj *pagesBO; insilmaril@2: int j=0; insilmaril@2: insilmaril@2: // Walk sections insilmaril@2: while (sectionBO) insilmaril@2: { insilmaril@2: if (useSections) insilmaril@2: { insilmaril@2: // Add page with section title insilmaril@2: onePage=sectionTemplate; insilmaril@2: onePage.replace ("", quotemeta(sectionBO->getHeading() ) ); insilmaril@2: allPages+=onePage; insilmaril@2: } else insilmaril@2: { insilmaril@2: i=-2; // only use inner loop to insilmaril@2: // turn mainbranches into pages insilmaril@2: sectionBO=mapCenter; insilmaril@2: } insilmaril@2: insilmaril@2: // Walk mainpages insilmaril@2: pagesBO=sectionBO->getFirstBranch(); insilmaril@2: j=0; insilmaril@2: while (pagesBO) insilmaril@2: { insilmaril@2: // Add page with list of items insilmaril@2: onePage=pageTemplate; insilmaril@2: onePage.replace ("", quotemeta (pagesBO->getHeading() ) ); insilmaril@2: list=buildList (pagesBO); insilmaril@2: onePage.replace ("", list); insilmaril@2: allPages+=onePage; insilmaril@2: j++; insilmaril@2: pagesBO=sectionBO->getBranchNum(j); insilmaril@2: } insilmaril@2: i++; insilmaril@2: sectionBO=mapCenter->getBranchNum(i); insilmaril@2: } insilmaril@2: insilmaril@2: content.replace ("",allPages); insilmaril@2: insilmaril@2: // Write modified content insilmaril@2: QFile f (contentFile); insilmaril@2: if ( !f.open( QIODevice::WriteOnly ) ) insilmaril@2: { insilmaril@2: QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not write %1").arg(contentFile)); insilmaril@2: mainWindow->statusMessage(QString(QObject::tr("Export failed."))); insilmaril@2: return; insilmaril@2: } insilmaril@2: insilmaril@2: QTextStream t( &f ); insilmaril@2: t << content; insilmaril@2: f.close(); insilmaril@2: insilmaril@2: // zip tmpdir to destination insilmaril@2: zipDir (tmpDir,outputFile); insilmaril@2: } insilmaril@2: insilmaril@2: bool ExportOO::setConfigFile (const QString &cf) insilmaril@2: { insilmaril@2: configFile=cf; insilmaril@2: int i=cf.findRev ("/"); insilmaril@2: if (i>=0) configDir=cf.left(i); insilmaril@2: SimpleSettings set; insilmaril@2: set.readSettings(configFile); insilmaril@2: insilmaril@2: // set paths insilmaril@2: templateDir=configDir+"/"+set.readEntry ("Template"); insilmaril@2: insilmaril@2: QDir d (templateDir); insilmaril@2: if (!d.exists()) insilmaril@2: { insilmaril@2: QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Check \"%1\" in\n%2").arg("Template="+set.readEntry ("Template")).arg(configFile)); insilmaril@2: return false; insilmaril@2: insilmaril@2: } insilmaril@2: insilmaril@2: contentTemplateFile=templateDir+"content-template.xml"; insilmaril@2: contentFile=tmpDir.path()+"/content.xml"; insilmaril@2: pageTemplateFile=templateDir+"page-template.xml"; insilmaril@2: sectionTemplateFile=templateDir+"section-template.xml"; insilmaril@2: insilmaril@2: if (set.readEntry("useSections").contains("yes")) insilmaril@2: useSections=true; insilmaril@2: insilmaril@2: // Copy template to tmpdir insilmaril@2: system ("cp -r "+templateDir+"* "+tmpDir.path()); insilmaril@2: insilmaril@2: // Read content-template insilmaril@2: if (!loadStringFromDisk (contentTemplateFile,content)) insilmaril@2: { insilmaril@2: QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not read %1").arg(contentTemplateFile)); insilmaril@2: return false; insilmaril@2: } insilmaril@2: insilmaril@2: // Read page-template insilmaril@2: if (!loadStringFromDisk (pageTemplateFile,pageTemplate)) insilmaril@2: { insilmaril@2: QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not read %1").arg(pageTemplateFile)); insilmaril@2: return false; insilmaril@2: } insilmaril@2: insilmaril@2: // Read section-template insilmaril@2: if (useSections && !loadStringFromDisk (sectionTemplateFile,sectionTemplate)) insilmaril@2: { insilmaril@2: QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not read %1").arg(sectionTemplateFile)); insilmaril@2: return false; insilmaril@2: } insilmaril@2: return true; insilmaril@2: } insilmaril@2: