insilmaril@228: #include insilmaril@194: #include insilmaril@194: 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@228: #include "xsltproc.h" insilmaril@0: insilmaril@171: extern Main *mainWindow; insilmaril@228: extern QDir vymBaseDir; insilmaril@228: insilmaril@131: insilmaril@171: ExportBase::ExportBase() insilmaril@0: { insilmaril@0: indentPerDepth=" "; insilmaril@228: // Create tmpdir insilmaril@228: tmpDir.setPath (makeUniqueDir("/tmp/vym-XXXXXX")); insilmaril@228: } insilmaril@228: insilmaril@228: ExportBase::~ExportBase() insilmaril@228: { insilmaril@228: // Remove tmpdir insilmaril@228: removeDir (tmpDir); insilmaril@0: } insilmaril@0: insilmaril@199: void ExportBase::setDir(const QString &p) insilmaril@0: { insilmaril@199: outputDir=p; insilmaril@199: } insilmaril@199: insilmaril@199: void ExportBase::setFile (const QString &p) insilmaril@199: { insilmaril@199: outputFile=p; insilmaril@0: } insilmaril@0: insilmaril@171: void ExportBase::setMapCenter(MapCenterObj *mc) insilmaril@0: { insilmaril@0: mapCenter=mc; 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: if (mapCenter && mapCenter->getMapEditor()) insilmaril@228: { insilmaril@228: QFileDialog *fd=new QFileDialog( mapCenter->getMapEditor(), caption); insilmaril@228: fd->addFilter (filter); insilmaril@228: fd->setCaption(caption); insilmaril@228: fd->setMode( QFileDialog::AnyFile ); 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@228: QMessageBox mb( __VYM, 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@228: QMessageBox::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@228: // return, do nothing insilmaril@228: return false; insilmaril@228: break; insilmaril@228: } insilmaril@228: } insilmaril@228: outputFile=fd->selectedFile(); insilmaril@228: return true; insilmaril@228: } insilmaril@228: } insilmaril@228: return false; insilmaril@228: } insilmaril@228: 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@239: //////////////////////////////////////////////////////////////////////// insilmaril@228: void ExportASCII::doExport() insilmaril@0: { insilmaril@199: QFile file (outputFile); insilmaril@0: if ( !file.open( IO_WriteOnly ) ) insilmaril@0: { insilmaril@131: // FIXME experimental, testing 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@0: QString actIndent(""); insilmaril@160: int i; insilmaril@160: uint j; insilmaril@0: BranchObj *bo; insilmaril@0: bo=mapCenter->first(); insilmaril@0: while (bo) insilmaril@0: { insilmaril@254: // Make indentstring insilmaril@254: for (i=0;igetDepth();i++) actIndent+= indentPerDepth; insilmaril@254: insilmaril@254: if (bo->getDepth()==0) insilmaril@239: { insilmaril@254: ts << (bo->getHeading()+ "\n"); insilmaril@254: for (j=0;jgetHeading().length();j++) ts<<"="; insilmaril@254: ts << "\n"; insilmaril@254: } else if (bo->getDepth()==1) insilmaril@254: ts << ("\n"+getSectionString(bo) + bo->getHeading()+ "\n"); insilmaril@254: else if (bo->getDepth()==2) insilmaril@254: ts << (actIndent + " o " + bo->getHeading()+ "\n"); insilmaril@254: else insilmaril@254: ts << (actIndent + " - " + bo->getHeading()+ "\n"); insilmaril@254: insilmaril@254: // If necessary, write note insilmaril@254: if (!bo->getNote().isEmpty()) insilmaril@254: { insilmaril@254: s =bo->getNoteASCII(); insilmaril@254: s=s.replace ("\n","\n"+actIndent); insilmaril@254: ts << (s+"\n\n"); insilmaril@0: } insilmaril@0: bo=bo->next(); insilmaril@0: actIndent=""; insilmaril@0: } insilmaril@0: file.close(); insilmaril@0: } insilmaril@0: insilmaril@239: //////////////////////////////////////////////////////////////////////// insilmaril@239: void ExportKDEBookmarks::doExport() insilmaril@239: { insilmaril@239: MapEditor *me=NULL; insilmaril@239: if (mapCenter) me=mapCenter->getMapEditor(); insilmaril@239: if (me) insilmaril@239: { insilmaril@239: me->exportXML(tmpDir.path()); insilmaril@239: //FIXME testing insilmaril@239: cout << "tmpDir="<getMapName()+".xml"); insilmaril@239: p.setOutputFile (tmpDir.home().path()+"/.kde/share/apps/konqueror/bookmarks.xml"); insilmaril@239: p.setXSLFile (vymBaseDir.path()+"/styles/vym2kdebookmarks.xsl"); insilmaril@239: p.process(); insilmaril@239: } insilmaril@239: insilmaril@239: } insilmaril@239: insilmaril@239: //////////////////////////////////////////////////////////////////////// insilmaril@228: void ExportTaskjuggler::doExport() insilmaril@228: { insilmaril@228: MapEditor *me=NULL; insilmaril@228: if (mapCenter) me=mapCenter->getMapEditor(); insilmaril@228: if (me) insilmaril@228: { insilmaril@228: me->exportXML(tmpDir.path()); insilmaril@228: //FIXME testing insilmaril@228: cout << "tmpDir="<getMapName()+".xml"); insilmaril@228: p.setOutputFile (outputFile); insilmaril@228: p.setXSLFile (vymBaseDir.path()+"/styles/vym2taskjuggler.xsl"); insilmaril@228: p.process(); insilmaril@228: } insilmaril@228: 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@163: if ( !file.open( IO_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@163: // QString actIndent(""); insilmaril@163: // int i; insilmaril@163: BranchObj *bo; insilmaril@163: bo=mapCenter->first(); insilmaril@163: while (bo) { insilmaril@254: if (bo->getDepth()==0); insilmaril@254: else if (bo->getDepth()==1) { insilmaril@254: ts << ("\\chapter{" + bo->getHeading()+ "}\n"); insilmaril@254: } insilmaril@254: else if (bo->getDepth()==2) { insilmaril@254: ts << ("\\section{" + bo->getHeading()+ "}\n"); insilmaril@254: } insilmaril@254: else if (bo->getDepth()==3) { insilmaril@254: ts << ("\\subsection{" + bo->getHeading()+ "}\n"); insilmaril@254: } insilmaril@254: else if (bo->getDepth()==4) { insilmaril@254: ts << ("\\subsubsection{" + bo->getHeading()+ "}\n"); insilmaril@254: } insilmaril@254: else { insilmaril@254: ts << ("\\paragraph*{" + bo->getHeading()+ "}\n"); insilmaril@254: } insilmaril@254: insilmaril@254: // If necessary, write note insilmaril@254: if (!bo->getNote().isEmpty()) { insilmaril@254: ts << (bo->getNoteASCII()); insilmaril@254: ts << ("\n"); insilmaril@254: } insilmaril@163: bo=bo->next(); 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@171: // Start list insilmaril@234: r+="\n"; insilmaril@171: while (bo) insilmaril@171: { insilmaril@254: r+=""; insilmaril@254: r+=quotemeta(bo->getHeading()); insilmaril@254: // If necessary, write note insilmaril@254: if (!bo->getNote().isEmpty()) insilmaril@254: r+=bo->getNoteOpenDoc(); insilmaril@254: r+=""; insilmaril@254: r+=buildList (bo); // recursivly add deeper branches insilmaril@254: r+="\n"; insilmaril@183: i++; insilmaril@183: bo=current->getBranchNum(i); insilmaril@171: } insilmaril@171: r+="\n"; 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@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@194: while (sectionBO) 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@183: while (pagesBO) 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@171: if ( !f.open( IO_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@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: