exports.cpp
author insilmaril
Tue, 23 Mar 2010 11:54:30 +0000
branchrelease-1-12-maintained
changeset 81 876eed30ba3b
parent 62 85683324f94a
permissions -rw-r--r--
Patch from Xavier Oswald to compile with older compilers
     1 #include "exports.h"
     2 #include "file.h"
     3 #include "linkablemapobj.h"
     4 #include "misc.h"
     5 #include "mainwindow.h"
     6 #include "warningdialog.h"
     7 #include "xsltproc.h"
     8 #include <cstdlib>
     9 
    10 extern Main *mainWindow;
    11 extern QDir vymBaseDir;
    12 extern QString vymName;
    13 
    14 ExportBase::ExportBase()
    15 {
    16 	indentPerDepth="  ";
    17 	bool ok;
    18     tmpDir.setPath (makeTmpDir(ok,"vym-export"));
    19 	if (!tmpDir.exists() || !ok)
    20 		QMessageBox::critical( 0, QObject::tr( "Error" ),
    21 					   QObject::tr("Couldn't access temporary directory\n"));
    22 	cancelFlag=false;				   
    23 }
    24 
    25 ExportBase::~ExportBase()
    26 {
    27 	// Cleanup tmpdir
    28 	removeDir (tmpDir);
    29 }
    30 
    31 void ExportBase::setDir(const QDir &d)
    32 {
    33 	outDir=d;
    34 }
    35 
    36 void ExportBase::setFile (const QString &p)
    37 {
    38 	outputFile=p;
    39 }
    40 
    41 QString ExportBase::getFile ()
    42 {
    43 	return outputFile;
    44 }
    45 
    46 void ExportBase::setModel(VymModel *m)
    47 {
    48 	model=m;
    49 }
    50 
    51 void ExportBase::setCaption (const QString &s)
    52 {
    53 	caption=s;
    54 }
    55 
    56 void ExportBase::addFilter(const QString &s)
    57 {
    58 	filter=s;
    59 }
    60 
    61 bool ExportBase::execDialog()
    62 {
    63 	//MapEditor *me=model.getMapEditor(); FIXME needed?
    64 	// if (model->mapCenters.count() && me)
    65 	{
    66 		QFileDialog *fd=new QFileDialog( 0, caption);
    67 		fd->setFilter (filter);
    68 		fd->setCaption(caption);
    69 		fd->setMode( QFileDialog::AnyFile );
    70 		fd->setDir (outDir);
    71 		fd->show();
    72 
    73 		if ( fd->exec() == QDialog::Accepted )
    74 		{
    75 			if (QFile (fd->selectedFile()).exists() )
    76 			{
    77 				QMessageBox mb( vymName,
    78 					QObject::tr("The file %1 exists already.\nDo you want to overwrite it?").arg(fd->selectedFile()), 
    79 				QMessageBox::Warning,
    80 				QMessageBox::Yes | QMessageBox::Default,
    81 				QMessageBox::Cancel | QMessageBox::Escape,
    82 				Qt::NoButton );
    83 				mb.setButtonText( QMessageBox::Yes, QObject::tr("Overwrite") );
    84 				mb.setButtonText( QMessageBox::No, QObject::tr("Cancel"));
    85 				ExportBase ex;
    86 				switch( mb.exec() ) 
    87 				{
    88 					case QMessageBox::Yes:
    89 						// save 
    90 						break;;
    91 					case QMessageBox::Cancel:
    92 						cancelFlag=true;
    93 						return false;
    94 						break;
    95 				}
    96 			}
    97 			outputFile=fd->selectedFile();
    98 			cancelFlag=false;
    99 			return true;
   100 		}
   101 	}
   102 	return false;
   103 }
   104 
   105 bool ExportBase::canceled()
   106 {
   107 	return cancelFlag;
   108 }
   109 
   110 QString ExportBase::getSectionString(BranchObj *bostart)
   111 {
   112 	// Make prefix like "2.5.3" for "bo:2,bo:5,bo:3"
   113 	QString r;
   114 	BranchObj *bo=bostart;
   115 	int depth=bo->getDepth();
   116 	while (depth>0)
   117 	{
   118 		r=QString("%1").arg(1+bo->getNum(),0,10)+"." + r;
   119 		bo=(BranchObj*)(bo->getParObj());
   120 		depth=bo->getDepth();
   121 	}	
   122 	if (r.isEmpty())
   123 		return r;
   124 	else	
   125 		return r + " ";
   126 }
   127 
   128 ////////////////////////////////////////////////////////////////////////
   129 ExportASCII::ExportASCII()
   130 {
   131 	filter="TXT (*.txt)";
   132 	caption=vymName+ " -" +QObject::tr("Export as ASCII")+" "+QObject::tr("(still experimental)");
   133 }
   134 
   135 void ExportASCII::doExport()
   136 {
   137 	QFile file (outputFile);
   138 	if ( !file.open( QIODevice::WriteOnly ) )
   139 	{
   140 		qWarning ("ExportBase::exportXML  couldn't open "+outputFile);
   141 		return;
   142 	}
   143 	QTextStream ts( &file );	// use LANG decoding here...
   144 
   145 	// Main loop over all branches
   146 	QString s;
   147 	QString curIndent;
   148 	int i;
   149 	BranchObj *bo;
   150 	bo=model->first();
   151 	while (bo) 
   152 	{
   153 		// Make indentstring
   154 		curIndent="";
   155 		for (i=0;i<bo->getDepth()-1;i++) curIndent+= indentPerDepth;
   156 
   157 		if (!bo->hasHiddenExportParent() )
   158 		{
   159 			switch (bo->getDepth())
   160 			{
   161 				case 0:
   162 					ts << underline (bo->getHeading(),QString("="));
   163 					ts << "\n";
   164 					break;
   165 				case 1:
   166 					ts << "\n";
   167 					ts << (underline (getSectionString(bo) + bo->getHeading(), QString("-") ) );
   168 					ts << "\n";
   169 					break;
   170 				case 2:
   171 					ts << "\n";
   172 					ts << (curIndent + "* " + bo->getHeading());
   173 					ts << "\n";
   174 					break;
   175 				case 3:
   176 					ts << (curIndent + "- " + bo->getHeading());
   177 					ts << "\n";
   178 					break;
   179 				default:
   180 					ts << (curIndent + "- " + bo->getHeading());
   181 					ts << "\n";
   182 					break;
   183 			}
   184 
   185 			// If necessary, write note
   186 			if (!bo->getNote().isEmpty())
   187 			{
   188 				curIndent +="  | ";
   189 				s=bo->getNoteASCII( curIndent, 80);
   190 				ts << s;
   191 			}
   192 		}
   193 		bo=model->next(bo);
   194 	}
   195 	file.close();
   196 }
   197 
   198 QString ExportASCII::underline (const QString &text, const QString &line)
   199 {
   200 	QString r=text + "\n";
   201 	for (int j=0;j<text.length();j++) r+=line;
   202 	return r;
   203 }
   204 
   205 
   206 ////////////////////////////////////////////////////////////////////////
   207 void ExportCSV::doExport()
   208 {
   209 	QFile file (outputFile);
   210 	if ( !file.open( QIODevice::WriteOnly ) )
   211 	{
   212 		qWarning ("ExportBase::exportXML  couldn't open "+outputFile);
   213 		return;
   214 	}
   215 	QTextStream ts( &file );	// use LANG decoding here...
   216 
   217 	// Write header
   218 	ts << "\"Note\""  <<endl;
   219 
   220 	// Main loop over all branches
   221 	QString s;
   222 	QString curIndent("");
   223 	int i;
   224 	BranchObj *bo;
   225 	bo=model->first();
   226 	while (bo) 
   227 	{
   228 		if (!bo->hasHiddenExportParent() )
   229 		{
   230 			// If necessary, write note
   231 			if (!bo->getNote().isEmpty())
   232 			{
   233 				s =bo->getNoteASCII();
   234 				s=s.replace ("\n","\n"+curIndent);
   235 				ts << ("\""+s+"\",");
   236 			} else
   237 				ts <<"\"\",";
   238 
   239 			// Make indentstring
   240 			for (i=0;i<bo->getDepth();i++) curIndent+= "\"\",";
   241 
   242 			// Write heading
   243 			ts << curIndent << "\"" << bo->getHeading()<<"\""<<endl;
   244 		}
   245 		
   246 		bo=model->next(bo);
   247 		curIndent="";
   248 	}
   249 	file.close();
   250 }
   251 
   252 ////////////////////////////////////////////////////////////////////////
   253 void ExportKDE3Bookmarks::doExport() 
   254 {
   255 	MapEditor *me=model->getMapEditor();
   256 	if (me)
   257 	{
   258 		WarningDialog dia;
   259 		dia.showCancelButton (true);
   260 		dia.setText(QObject::tr("Exporting the %1 bookmarks will overwrite\nyour existing bookmarks file.").arg("KDE"));
   261 		dia.setCaption(QObject::tr("Warning: Overwriting %1 bookmarks").arg("KDE 3"));
   262 		dia.setShowAgainName("/exports/KDE/overwriteKDEBookmarks");
   263 		if (dia.exec()==QDialog::Accepted)
   264 		{
   265 			me->exportXML(tmpDir.path(),false);
   266 
   267 			XSLTProc p;
   268 			p.setInputFile (tmpDir.path()+"/"+me->getMapName()+".xml");
   269 			p.setOutputFile (tmpDir.home().path()+"/.kde/share/apps/konqueror/bookmarks.xml");
   270 			p.setXSLFile (vymBaseDir.path()+"/styles/vym2kdebookmarks.xsl");
   271 			p.process();
   272 
   273 			QString ub=vymBaseDir.path()+"/scripts/update-bookmarks";
   274 			QProcess *proc= new QProcess ;
   275 			proc->start( ub);
   276 			if (!proc->waitForStarted())
   277 			{
   278 				QMessageBox::warning(0, 
   279 					QObject::tr("Warning"),
   280 					QObject::tr("Couldn't find script %1\nto notifiy Browsers of changed bookmarks.").arg(ub));
   281 			}	
   282 		}
   283 	}
   284 }
   285 
   286 ////////////////////////////////////////////////////////////////////////
   287 void ExportKDE4Bookmarks::doExport() 
   288 {
   289 	MapEditor *me=model->getMapEditor();
   290 	if (me)
   291 	{
   292 		WarningDialog dia;
   293 		dia.showCancelButton (true);
   294 		dia.setText(QObject::tr("Exporting the %1 bookmarks will overwrite\nyour existing bookmarks file.").arg("KDE"));
   295 		dia.setCaption(QObject::tr("Warning: Overwriting %1 bookmarks").arg("KDE 4"));
   296 		dia.setShowAgainName("/exports/KDE/overwriteKDEBookmarks");
   297 		if (dia.exec()==QDialog::Accepted)
   298 		{
   299 			me->exportXML(tmpDir.path(),false);
   300 
   301 			XSLTProc p;
   302 			p.setInputFile (tmpDir.path()+"/"+me->getMapName()+".xml");
   303 			p.setOutputFile (tmpDir.home().path()+"/.kde4/share/apps/konqueror/bookmarks.xml");
   304 			p.setXSLFile (vymBaseDir.path()+"/styles/vym2kdebookmarks.xsl");
   305 			p.process();
   306 
   307 			QString ub=vymBaseDir.path()+"/scripts/update-bookmarks";
   308 			QProcess *proc= new QProcess ;
   309 			proc->start( ub);
   310 			if (!proc->waitForStarted())
   311 			{
   312 				QMessageBox::warning(0, 
   313 					QObject::tr("Warning"),
   314 					QObject::tr("Couldn't find script %1\nto notifiy Browsers of changed bookmarks.").arg(ub));
   315 			}	
   316 		}
   317 	}
   318 
   319 }
   320 
   321 ////////////////////////////////////////////////////////////////////////
   322 void ExportFirefoxBookmarks::doExport() 
   323 {
   324 	MapEditor *me=model->getMapEditor();
   325 	if (me)
   326 	{
   327 		WarningDialog dia;
   328 		dia.showCancelButton (true);
   329 		dia.setText(QObject::tr("Exporting the %1 bookmarks will overwrite\nyour existing bookmarks file.").arg("Firefox"));
   330 		dia.setCaption(QObject::tr("Warning: Overwriting %1 bookmarks").arg("Firefox"));
   331 		dia.setShowAgainName("/vym/warnings/overwriteImportBookmarks");
   332 		if (dia.exec()==QDialog::Accepted)
   333 		{
   334 			me->exportXML(tmpDir.path(),false);
   335 
   336 /*
   337 			XSLTProc p;
   338 			p.setInputFile (tmpDir.path()+"/"+me->getMapName()+".xml");
   339 			p.setOutputFile (tmpDir.home().path()+"/.kde/share/apps/konqueror/bookmarks.xml");
   340 			p.setXSLFile (vymBaseDir.path()+"/styles/vym2kdebookmarks.xsl");
   341 			p.process();
   342 
   343 			QString ub=vymBaseDir.path()+"/scripts/update-bookmarks";
   344 			QProcess *proc = new QProcess( );
   345 			proc->addArgument(ub);
   346 
   347 			if ( !proc->start() ) 
   348 			{
   349 				QMessageBox::warning(0, 
   350 					QObject::tr("Warning"),
   351 					QObject::tr("Couldn't find script %1\nto notifiy Browsers of changed bookmarks.").arg(ub));
   352 			}	
   353 
   354 */
   355 
   356 		}
   357 	}
   358 }
   359 
   360 ////////////////////////////////////////////////////////////////////////
   361 void ExportTaskjuggler::doExport() 
   362 {
   363 	MapEditor *me=model->getMapEditor();
   364 	if (me)
   365 	{
   366 		me->exportXML(tmpDir.path(),false);
   367 
   368 		XSLTProc p;
   369 		p.setInputFile (tmpDir.path()+"/"+me->getMapName()+".xml");
   370 		p.setOutputFile (outputFile);
   371 		p.setXSLFile (vymBaseDir.path()+"/styles/vym2taskjuggler.xsl");
   372 		p.process();
   373 	}
   374 
   375 }
   376 
   377 ////////////////////////////////////////////////////////////////////////
   378 void ExportLaTeX::doExport() 
   379 {
   380 	// Exports a map to a LaTex file.  
   381 	// This file needs to be included 
   382 	// or inported into a LaTex document
   383 	// it will not add a preamble, or anything 
   384 	// that makes a full LaTex document.
   385   QFile file (outputFile);
   386   if ( !file.open( QIODevice::WriteOnly ) ) {
   387 	QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not write %1").arg(outputFile));
   388 	mainWindow->statusMessage(QString(QObject::tr("Export failed.")));
   389     return;
   390   }
   391   QTextStream ts( &file );	// use LANG decoding here...
   392   ts.setEncoding (QTextStream::UnicodeUTF8); // Force UTF8
   393   
   394   // Main loop over all branches
   395   QString s;
   396   // QString curIndent("");
   397   // int i;
   398   BranchObj *bo;
   399   bo=model->first();
   400   while (bo) {
   401 	if (!bo->hasHiddenExportParent() )
   402 	{
   403 		if (bo->getDepth()==0);
   404 		else if (bo->getDepth()==1) {
   405 		  ts << ("\\chapter{" + bo->getHeading()+ "}\n");
   406 		}
   407 		else if (bo->getDepth()==2) {
   408 		  ts << ("\\section{" + bo->getHeading()+ "}\n");
   409 		}
   410 		else if (bo->getDepth()==3) {
   411 		  ts << ("\\subsection{" + bo->getHeading()+ "}\n");
   412 		}
   413 		else if (bo->getDepth()==4) {
   414 		  ts << ("\\subsubsection{" + bo->getHeading()+ "}\n");
   415 		}
   416 		else {
   417 		  ts << ("\\paragraph*{" + bo->getHeading()+ "}\n");
   418 		}
   419 		
   420 		// If necessary, write note
   421 		if (!bo->getNote().isEmpty()) {
   422 		  ts << (bo->getNoteASCII());
   423 		  ts << ("\n");
   424 		}
   425 	}
   426     bo=model->next(bo);
   427    }
   428   file.close();
   429 }
   430 
   431 ////////////////////////////////////////////////////////////////////////
   432 ExportOO::ExportOO()
   433 {
   434 	useSections=false;
   435 }
   436 
   437 ExportOO::~ExportOO()
   438 {
   439 }	
   440 
   441 QString ExportOO::buildList (BranchObj *current)
   442 {
   443     QString r;
   444     BranchObj *bo;
   445 
   446     uint i=0;
   447     bo=current->getFirstBranch();
   448     if (bo)
   449     {
   450 		if (!bo->hasHiddenExportParent() )
   451 		{
   452 			// Start list
   453 			r+="<text:list text:style-name=\"vym-list\">\n";
   454 			while (bo)
   455 			{
   456 				r+="<text:list-item><text:p >";
   457 				r+=quotemeta(bo->getHeading());
   458 				// If necessary, write note
   459 				if (!bo->getNote().isEmpty())
   460 					r+=bo->getNoteOpenDoc();
   461 				r+="</text:p>";
   462 				r+=buildList (bo);	// recursivly add deeper branches
   463 				r+="</text:list-item>\n";
   464 				i++;
   465 				bo=current->getBranchNum(i);
   466 			}
   467 			r+="</text:list>\n";
   468 		}
   469     }
   470     return r;
   471 }
   472 
   473 
   474 void ExportOO::exportPresentation()
   475 {
   476 	QString allPages;
   477 
   478 	MapCenterObj *firstMCO=(MapCenterObj*)model->first();
   479 	if (!firstMCO) 
   480 	{
   481 		QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("No objects in map!"));
   482 		return;
   483 	}
   484 
   485 	// Insert new content
   486 	// FIXME add extra title in mapinfo for vym 1.13.x
   487 	content.replace ("<!-- INSERT TITLE -->",quotemeta(firstMCO->getHeading()));
   488 	content.replace ("<!-- INSERT AUTHOR -->",quotemeta(model->getAuthor()));
   489 
   490 	QString	onePage;
   491 	QString list;
   492 	
   493 	BranchObj *sectionBO;	
   494     int i=0;
   495 	BranchObj *pagesBO;
   496     int j=0;
   497 
   498 	int mapcenters=model->countMapCenters();
   499 
   500 	// useSections already has been set in setConfigFile 
   501 	if (mapcenters>1)	
   502 		sectionBO=firstMCO;
   503 	else
   504 		sectionBO=firstMCO->getFirstBranch();
   505 
   506 	// Walk sections
   507 	while (sectionBO && !sectionBO->hasHiddenExportParent() )
   508 	{
   509 		if (useSections)
   510 		{
   511 			// Add page with section title
   512 			onePage=sectionTemplate;
   513 			onePage.replace ("<!-- INSERT PAGE HEADING -->", quotemeta(sectionBO->getHeading() ) );
   514 			allPages+=onePage;
   515 			pagesBO=sectionBO->getFirstBranch();
   516 		} else
   517 		{
   518 			//i=-2;	// only use inner loop to 
   519 			        // turn mainbranches into pages
   520 			//sectionBO=firstMCO;
   521 			pagesBO=sectionBO;
   522 		}
   523 
   524 		j=0;
   525 		while (pagesBO && !pagesBO->hasHiddenExportParent() )
   526 		{
   527 			// Add page with list of items
   528 			onePage=pageTemplate;
   529 			onePage.replace ("<!-- INSERT PAGE HEADING -->", quotemeta (pagesBO->getHeading() ) );
   530 			list=buildList (pagesBO);
   531 			onePage.replace ("<!-- INSERT LIST -->", list);
   532 			allPages+=onePage;
   533 			if (pagesBO!=sectionBO)
   534 			{
   535 				j++;
   536 				pagesBO=((BranchObj*)pagesBO->getParObj())->getBranchNum(j);
   537 			} else
   538 				pagesBO=NULL;	// We are already iterating over the sectionBOs
   539 		}
   540 		i++;
   541 		if (mapcenters>1 )
   542 			sectionBO=model->getMapCenterNum (i);
   543 		else
   544 			sectionBO=firstMCO->getBranchNum (i);
   545 	}
   546 	
   547 	content.replace ("<!-- INSERT PAGES -->",allPages);
   548 
   549 	// Write modified content
   550 	QFile f (contentFile);
   551     if ( !f.open( QIODevice::WriteOnly ) ) 
   552 	{
   553 		QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not write %1").arg(contentFile));
   554 		mainWindow->statusMessage(QString(QObject::tr("Export failed.")));
   555 		return;
   556     }
   557 
   558     QTextStream t( &f );
   559     t << content;
   560     f.close();
   561 
   562 	// zip tmpdir to destination
   563 	zipDir (tmpDir,outputFile);	
   564 }
   565 
   566 bool ExportOO::setConfigFile (const QString &cf)
   567 {
   568 	configFile=cf;
   569 	int i=cf.findRev ("/");
   570 	if (i>=0) configDir=cf.left(i);
   571 	SimpleSettings set;
   572 	set.readSettings(configFile);
   573 
   574 	// set paths
   575 	templateDir=configDir+"/"+set.readEntry ("Template");
   576 
   577 	QDir d (templateDir);
   578 	if (!d.exists())
   579 	{
   580 		QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Check \"%1\" in\n%2").arg("Template="+set.readEntry ("Template")).arg(configFile));
   581 		return false;
   582 
   583 	}
   584 
   585 	contentTemplateFile=templateDir+"content-template.xml";
   586 	contentFile=tmpDir.path()+"/content.xml";
   587 	pageTemplateFile=templateDir+"page-template.xml";
   588 	sectionTemplateFile=templateDir+"section-template.xml";
   589 
   590 	if (model->countMapCenters()>1 ||set.readEntry("useSections").contains("yes"))
   591 		useSections=true;
   592 
   593 	// Copy template to tmpdir
   594 	system ("cp -r "+templateDir+"* "+tmpDir.path());
   595 
   596 	// Read content-template
   597 	if (!loadStringFromDisk (contentTemplateFile,content))
   598 	{
   599 		QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not read %1").arg(contentTemplateFile));
   600 		return false;
   601 	}
   602 
   603 	// Read page-template
   604 	if (!loadStringFromDisk (pageTemplateFile,pageTemplate))
   605 	{
   606 		QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not read %1").arg(pageTemplateFile));
   607 		return false;
   608 	}
   609 	
   610 	// Read section-template
   611 	if (useSections && !loadStringFromDisk (sectionTemplateFile,sectionTemplate))
   612 	{
   613 		QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not read %1").arg(sectionTemplateFile));
   614 		return false;
   615 	}
   616 	return true;
   617 }
   618