exports.cpp
author insilmaril
Mon, 16 Oct 2006 12:42:54 +0000
changeset 391 59106708b5b5
parent 384 73bfe41502d9
child 393 053b8645e3e9
permissions -rw-r--r--
Code simplifications
     1 #include <q3filedialog.h>
     2 #include <QMessageBox>
     3 #include <QProcess>
     4 #include <QTextStream>
     5 
     6 #include "exports.h"
     7 #include "file.h"
     8 #include "linkablemapobj.h"
     9 #include "misc.h"
    10 #include "mainwindow.h"
    11 #include "warningdialog.h"
    12 #include "xsltproc.h"
    13 
    14 extern Main *mainWindow;
    15 extern QDir vymBaseDir;
    16 
    17 
    18 ExportBase::ExportBase()
    19 {
    20 	indentPerDepth="  ";
    21 	bool ok;
    22     tmpDir.setPath (makeUniqueDir(ok,"/tmp/vym-XXXXXX"));
    23 	if (!tmpDir.exists() || !ok)
    24 		QMessageBox::critical( 0, QObject::tr( "Error" ),
    25 					   QObject::tr("Couldn't access temporary directory\n"));
    26 }
    27 
    28 ExportBase::~ExportBase()
    29 {
    30 	// Cleanup tmpdir
    31 	removeDir (tmpDir);
    32 }
    33 
    34 void ExportBase::setDir(const QString &p)
    35 {
    36 	outputDir=p;
    37 }
    38 
    39 void ExportBase::setFile (const QString &p)
    40 {
    41 	outputFile=p;
    42 }
    43 
    44 void ExportBase::setMapCenter(MapCenterObj *mc)
    45 {
    46 	mapCenter=mc;
    47 }
    48 
    49 void ExportBase::setCaption (const QString &s)
    50 {
    51 	caption=s;
    52 }
    53 
    54 void ExportBase::addFilter(const QString &s)
    55 {
    56 	filter=s;
    57 }
    58 
    59 bool ExportBase::execDialog()
    60 {
    61 	if (mapCenter && mapCenter->getMapEditor())
    62 	{
    63 		QFileDialog *fd=new QFileDialog( mapCenter->getMapEditor(), caption);
    64 		fd->setFilter (filter);
    65 		fd->setCaption(caption);
    66 		fd->setMode( QFileDialog::AnyFile );
    67 		fd->show();
    68 
    69 		if ( fd->exec() == QDialog::Accepted )
    70 		{
    71 			if (QFile (fd->selectedFile()).exists() )
    72 			{
    73 				QMessageBox mb( __VYM,
    74 					QObject::tr("The file %1 exists already.\nDo you want to overwrite it?").arg(fd->selectedFile()), 
    75 				QMessageBox::Warning,
    76 				QMessageBox::Yes | QMessageBox::Default,
    77 				QMessageBox::Cancel | QMessageBox::Escape,
    78 				Qt::NoButton );
    79 				mb.setButtonText( QMessageBox::Yes, QObject::tr("Overwrite") );
    80 				mb.setButtonText( QMessageBox::No, QObject::tr("Cancel"));
    81 				ExportBase ex;
    82 				switch( mb.exec() ) 
    83 				{
    84 					case QMessageBox::Yes:
    85 						// save 
    86 						break;;
    87 					case QMessageBox::Cancel:
    88 						// return, do nothing
    89 						return false;
    90 						break;
    91 				}
    92 			}
    93 			outputFile=fd->selectedFile();
    94 			return true;
    95 		}
    96 	}
    97 	return false;
    98 }
    99 
   100 QString ExportBase::getSectionString(BranchObj *bostart)
   101 {
   102 	// Make prefix like "2.5.3" for "bo:2,bo:5,bo:3"
   103 	QString r;
   104 	BranchObj *bo=bostart;
   105 	int depth=bo->getDepth();
   106 	while (depth>0)
   107 	{
   108 		r=QString("%1").arg(1+bo->getNum(),0,10)+"." + r;
   109 		bo=(BranchObj*)(bo->getParObj());
   110 		depth=bo->getDepth();
   111 	}	
   112 	if (r.isEmpty())
   113 		return r;
   114 	else	
   115 		return r + " ";
   116 }
   117 
   118 
   119 ////////////////////////////////////////////////////////////////////////
   120 void ExportASCII::doExport()
   121 {
   122 	QFile file (outputFile);
   123 	if ( !file.open( QIODevice::WriteOnly ) )
   124 	{
   125 		qWarning ("ExportBase::exportXML  couldn't open "+outputFile);
   126 		return;
   127 	}
   128 	QTextStream ts( &file );	// use LANG decoding here...
   129 
   130 	// Main loop over all branches
   131 	QString s;
   132 	QString actIndent("");
   133 	int i,j;
   134 	BranchObj *bo;
   135 	bo=mapCenter->first();
   136 	while (bo) 
   137 	{
   138 		// Make indentstring
   139 		for (i=0;i<bo->getDepth();i++) actIndent+= indentPerDepth;
   140 
   141 		if (bo->getDepth()==0)
   142 		{
   143 			ts << (bo->getHeading()+ "\n");
   144 			for (j=0;j<bo->getHeading().length();j++) ts<<"=";
   145 			ts << "\n";
   146 		} else 	if (bo->getDepth()==1)
   147 			ts << ("\n"+getSectionString(bo) + bo->getHeading()+ "\n");
   148 		else	if (bo->getDepth()==2)
   149 			ts << (actIndent + " o " + bo->getHeading()+ "\n");
   150 		else	
   151 			ts << (actIndent + " - " + bo->getHeading()+ "\n");
   152 		
   153 		// If necessary, write note
   154 		if (!bo->getNote().isEmpty())
   155 		{
   156 			s =bo->getNoteASCII();
   157 			s=s.replace ("\n","\n"+actIndent);
   158 			ts << (s+"\n\n");
   159 		}
   160 		bo=bo->next();
   161 		actIndent="";
   162 	}
   163 	file.close();
   164 }
   165 
   166 ////////////////////////////////////////////////////////////////////////
   167 void ExportKDEBookmarks::doExport() 
   168 {
   169 	MapEditor *me=NULL;
   170 	if (mapCenter) me=mapCenter->getMapEditor();
   171 	if (me)
   172 	{
   173 		WarningDialog dia;
   174 		dia.showCancelButton (true);
   175 		dia.setText(QObject::tr("Exporting the %1 bookmarks will overwrite\nyour existing bookmarks file.").arg("KDE"));
   176 		dia.setCaption(QObject::tr("Warning: Overwriting %1 bookmarks").arg("KDE"));
   177 		dia.setShowAgainName("/vym/warnings/overwriteKDEBookmarks");
   178 		if (dia.exec()==QDialog::Accepted)
   179 		{
   180 			me->exportXML(tmpDir.path());
   181 
   182 			XSLTProc p;
   183 			p.setInputFile (tmpDir.path()+"/"+me->getMapName()+".xml");
   184 			p.setOutputFile (tmpDir.home().path()+"/.kde/share/apps/konqueror/bookmarks.xml");
   185 			p.setXSLFile (vymBaseDir.path()+"/styles/vym2kdebookmarks.xsl");
   186 			p.process();
   187 
   188 			QString ub=vymBaseDir.path()+"/scripts/update-bookmarks";
   189 			QProcess *proc= new QProcess ;
   190 			proc->start( ub);
   191 			if (!proc->waitForStarted())
   192 			{
   193 				QMessageBox::warning(0, 
   194 					QObject::tr("Warning"),
   195 					QObject::tr("Couldn't find script %1\nto notifiy Browsers of changed bookmarks.").arg(ub));
   196 			}	
   197 		}
   198 	}
   199 
   200 }
   201 
   202 ////////////////////////////////////////////////////////////////////////
   203 void ExportFirefoxBookmarks::doExport() 
   204 {
   205 	MapEditor *me=NULL;
   206 	if (mapCenter) me=mapCenter->getMapEditor();
   207 	if (me)
   208 	{
   209 		WarningDialog dia;
   210 		dia.showCancelButton (true);
   211 		dia.setText(QObject::tr("Exporting the %1 bookmarks will overwrite\nyour existing bookmarks file.").arg("Firefox"));
   212 		dia.setCaption(QObject::tr("Warning: Overwriting %1 bookmarks").arg("Firefox"));
   213 		dia.setShowAgainName("/vym/warnings/overwriteImportBookmarks");
   214 		if (dia.exec()==QDialog::Accepted)
   215 		{
   216 			me->exportXML(tmpDir.path());
   217 
   218 /*
   219 			XSLTProc p;
   220 			p.setInputFile (tmpDir.path()+"/"+me->getMapName()+".xml");
   221 			p.setOutputFile (tmpDir.home().path()+"/.kde/share/apps/konqueror/bookmarks.xml");
   222 			p.setXSLFile (vymBaseDir.path()+"/styles/vym2kdebookmarks.xsl");
   223 			p.process();
   224 
   225 			QString ub=vymBaseDir.path()+"/scripts/update-bookmarks";
   226 			QProcess *proc = new QProcess( );
   227 			proc->addArgument(ub);
   228 
   229 			if ( !proc->start() ) 
   230 			{
   231 				QMessageBox::warning(0, 
   232 					QObject::tr("Warning"),
   233 					QObject::tr("Couldn't find script %1\nto notifiy Browsers of changed bookmarks.").arg(ub));
   234 			}	
   235 
   236 */
   237 
   238 		}
   239 	}
   240 }
   241 
   242 ////////////////////////////////////////////////////////////////////////
   243 void ExportTaskjuggler::doExport() 
   244 {
   245 	MapEditor *me=NULL;
   246 	if (mapCenter) me=mapCenter->getMapEditor();
   247 	if (me)
   248 	{
   249 		me->exportXML(tmpDir.path());
   250 
   251 		XSLTProc p;
   252 		p.setInputFile (tmpDir.path()+"/"+me->getMapName()+".xml");
   253 		p.setOutputFile (outputFile);
   254 		p.setXSLFile (vymBaseDir.path()+"/styles/vym2taskjuggler.xsl");
   255 		p.process();
   256 	}
   257 
   258 }
   259 
   260 ////////////////////////////////////////////////////////////////////////
   261 void ExportLaTeX::doExport() 
   262 {
   263 	// Exports a map to a LaTex file.  
   264 	// This file needs to be included 
   265 	// or inported into a LaTex document
   266 	// it will not add a preamble, or anything 
   267 	// that makes a full LaTex document.
   268   QFile file (outputFile);
   269   if ( !file.open( QIODevice::WriteOnly ) ) {
   270 	QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not write %1").arg(outputFile));
   271 	mainWindow->statusMessage(QString(QObject::tr("Export failed.")));
   272     return;
   273   }
   274   QTextStream ts( &file );	// use LANG decoding here...
   275   ts.setEncoding (QTextStream::UnicodeUTF8); // Force UTF8
   276   
   277   // Main loop over all branches
   278   QString s;
   279   // QString actIndent("");
   280   // int i;
   281   BranchObj *bo;
   282   bo=mapCenter->first();
   283   while (bo) {
   284 	if (bo->getDepth()==0);
   285 	else if (bo->getDepth()==1) {
   286 	  ts << ("\\chapter{" + bo->getHeading()+ "}\n");
   287 	}
   288 	else if (bo->getDepth()==2) {
   289 	  ts << ("\\section{" + bo->getHeading()+ "}\n");
   290 	}
   291 	else if (bo->getDepth()==3) {
   292 	  ts << ("\\subsection{" + bo->getHeading()+ "}\n");
   293 	}
   294 	else if (bo->getDepth()==4) {
   295 	  ts << ("\\subsubsection{" + bo->getHeading()+ "}\n");
   296 	}
   297 	else {
   298 	  ts << ("\\paragraph*{" + bo->getHeading()+ "}\n");
   299 	}
   300 	
   301 	// If necessary, write note
   302 	if (!bo->getNote().isEmpty()) {
   303 	  ts << (bo->getNoteASCII());
   304 	  ts << ("\n");
   305 	}
   306     bo=bo->next();
   307    }
   308   file.close();
   309 }
   310 
   311 ////////////////////////////////////////////////////////////////////////
   312 ExportOO::ExportOO()
   313 {
   314 	useSections=false;
   315 }
   316 
   317 ExportOO::~ExportOO()
   318 {
   319 }	
   320 
   321 QString ExportOO::buildList (BranchObj *current)
   322 {
   323     QString r;
   324     BranchObj *bo;
   325 
   326     uint i=0;
   327     bo=current->getFirstBranch();
   328     if (bo)
   329     {
   330         // Start list
   331         r+="<text:list text:style-name=\"vym-list\">\n";
   332         while (bo)
   333         {
   334 			r+="<text:list-item><text:p >";
   335 			r+=quotemeta(bo->getHeading());
   336 			// If necessary, write note
   337 			if (!bo->getNote().isEmpty())
   338 				r+=bo->getNoteOpenDoc();
   339 			r+="</text:p>";
   340 			r+=buildList (bo);	// recursivly add deeper branches
   341 			r+="</text:list-item>\n";
   342 			i++;
   343 			bo=current->getBranchNum(i);
   344         }
   345         r+="</text:list>\n";
   346     }
   347     return r;
   348 }
   349 
   350 
   351 void ExportOO::exportPresentation()
   352 {
   353 	QString allPages;
   354 
   355 	// Insert new content
   356 	content.replace ("<!-- INSERT TITLE -->",quotemeta(mapCenter->getHeading()));
   357 	content.replace ("<!-- INSERT AUTHOR -->",quotemeta(mapCenter->getAuthor()));
   358 
   359 	QString	onePage;
   360 	QString list;
   361 	
   362 	BranchObj *sectionBO=mapCenter->getFirstBranch();
   363     int i=0;
   364 	BranchObj *pagesBO;
   365     int j=0;
   366 
   367 	// Walk sections
   368 	while (sectionBO)
   369 	{
   370 		if (useSections)
   371 		{
   372 			// Add page with section title
   373 			onePage=sectionTemplate;
   374 			onePage.replace ("<!-- INSERT PAGE HEADING -->", quotemeta(sectionBO->getHeading() ) );
   375 			allPages+=onePage;
   376 		} else
   377 		{
   378 			i=-2;	// only use inner loop to 
   379 			        // turn mainbranches into pages
   380 			sectionBO=mapCenter;
   381 		}
   382 
   383 		// Walk mainpages
   384 		pagesBO=sectionBO->getFirstBranch();
   385 		j=0;
   386 		while (pagesBO)
   387 		{
   388 			// Add page with list of items
   389 			onePage=pageTemplate;
   390 			onePage.replace ("<!-- INSERT PAGE HEADING -->", quotemeta (pagesBO->getHeading() ) );
   391 			list=buildList (pagesBO);
   392 			onePage.replace ("<!-- INSERT LIST -->", list);
   393 			allPages+=onePage;
   394 			j++;
   395 			pagesBO=sectionBO->getBranchNum(j);
   396 		}
   397 		i++;
   398 		sectionBO=mapCenter->getBranchNum(i);
   399 	}
   400 	
   401 	content.replace ("<!-- INSERT PAGES -->",allPages);
   402 
   403 	// Write modified content
   404 	QFile f (contentFile);
   405     if ( !f.open( QIODevice::WriteOnly ) ) 
   406 	{
   407 		QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not write %1").arg(contentFile));
   408 		mainWindow->statusMessage(QString(QObject::tr("Export failed.")));
   409 		return;
   410     }
   411 
   412     QTextStream t( &f );
   413     t << content;
   414     f.close();
   415 
   416 	// zip tmpdir to destination
   417 	zipDir (tmpDir,outputFile);	
   418 }
   419 
   420 bool ExportOO::setConfigFile (const QString &cf)
   421 {
   422 	configFile=cf;
   423 	int i=cf.findRev ("/");
   424 	if (i>=0) configDir=cf.left(i);
   425 	SimpleSettings set;
   426 	set.readSettings(configFile);
   427 
   428 	// set paths
   429 	templateDir=configDir+"/"+set.readEntry ("Template");
   430 
   431 	QDir d (templateDir);
   432 	if (!d.exists())
   433 	{
   434 		QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Check \"%1\" in\n%2").arg("Template="+set.readEntry ("Template")).arg(configFile));
   435 		return false;
   436 
   437 	}
   438 
   439 	contentTemplateFile=templateDir+"content-template.xml";
   440 	contentFile=tmpDir.path()+"/content.xml";
   441 	pageTemplateFile=templateDir+"page-template.xml";
   442 	sectionTemplateFile=templateDir+"section-template.xml";
   443 
   444 	if (set.readEntry("useSections").contains("yes"))
   445 		useSections=true;
   446 
   447 	// Copy template to tmpdir
   448 	system ("cp -r "+templateDir+"* "+tmpDir.path());
   449 
   450 	// Read content-template
   451 	if (!loadStringFromDisk (contentTemplateFile,content))
   452 	{
   453 		QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not read %1").arg(contentTemplateFile));
   454 		return false;
   455 	}
   456 
   457 	// Read page-template
   458 	if (!loadStringFromDisk (pageTemplateFile,pageTemplate))
   459 	{
   460 		QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not read %1").arg(pageTemplateFile));
   461 		return false;
   462 	}
   463 	
   464 	// Read section-template
   465 	if (useSections && !loadStringFromDisk (sectionTemplateFile,sectionTemplate))
   466 	{
   467 		QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not read %1").arg(sectionTemplateFile));
   468 		return false;
   469 	}
   470 	return true;
   471 }
   472