exports.cpp
author insilmaril
Wed, 10 Feb 2010 13:48:42 +0000
changeset 822 c2ce9944148c
parent 819 8f987e376035
child 823 0bba81dde1bc
permissions -rw-r--r--
More fixes and sorting lexically backwards
     1 #include "exports.h"
     2 
     3 #include "branchitem.h"
     4 #include "file.h"
     5 #include "linkablemapobj.h"
     6 #include "misc.h"
     7 #include "mainwindow.h"
     8 #include "warningdialog.h"
     9 #include "xsltproc.h"
    10 
    11 extern Main *mainWindow;
    12 extern QDir vymBaseDir;
    13 extern QString vymName;
    14 
    15 ExportBase::ExportBase()
    16 {
    17 	init();
    18 }
    19 
    20 ExportBase::ExportBase(VymModel *m)
    21 {
    22 	init();
    23 	model=m;
    24 }
    25 
    26 ExportBase::~ExportBase()
    27 {
    28 	// Cleanup tmpdir
    29 	removeDir (tmpDir);
    30 }
    31 
    32 void ExportBase::init()
    33 {
    34 	indentPerDepth="  ";
    35 	bool ok;
    36     tmpDir.setPath (makeTmpDir(ok,"vym-export"));
    37 	if (!tmpDir.exists() || !ok)
    38 		QMessageBox::critical( 0, QObject::tr( "Error" ),
    39 					   QObject::tr("Couldn't access temporary directory\n"));
    40 	cancelFlag=false;				   
    41 }
    42 
    43 void ExportBase::setDir(const QDir &d)
    44 {
    45 	outDir=d;
    46 }
    47 
    48 void ExportBase::setFile (const QString &p)
    49 {
    50 	outputFile=p;
    51 }
    52 
    53 QString ExportBase::getFile ()
    54 {
    55 	return outputFile;
    56 }
    57 
    58 void ExportBase::setModel(VymModel *m)
    59 {
    60 	model=m;
    61 }
    62 
    63 void ExportBase::setCaption (const QString &s)
    64 {
    65 	caption=s;
    66 }
    67 
    68 void ExportBase::addFilter(const QString &s)
    69 {
    70 	filter=s;
    71 }
    72 
    73 bool ExportBase::execDialog()
    74 {
    75 	{
    76 		QFileDialog *fd=new QFileDialog( 0, caption);
    77 		fd->setFilter (filter);
    78 		fd->setCaption(caption);
    79 		fd->setMode( QFileDialog::AnyFile );
    80 		fd->setDir (outDir);
    81 		fd->show();
    82 
    83 		if ( fd->exec() == QDialog::Accepted )
    84 		{
    85 			if (QFile (fd->selectedFile()).exists() )
    86 			{
    87 				QMessageBox mb( vymName,
    88 					QObject::tr("The file %1 exists already.\nDo you want to overwrite it?").arg(fd->selectedFile()), 
    89 				QMessageBox::Warning,
    90 				QMessageBox::Yes | QMessageBox::Default,
    91 				QMessageBox::Cancel | QMessageBox::Escape,
    92 				Qt::NoButton );
    93 				mb.setButtonText( QMessageBox::Yes, QObject::tr("Overwrite") );
    94 				mb.setButtonText( QMessageBox::No, QObject::tr("Cancel"));
    95 				ExportBase ex;
    96 				switch( mb.exec() ) 
    97 				{
    98 					case QMessageBox::Yes:
    99 						// save 
   100 						break;;
   101 					case QMessageBox::Cancel:
   102 						cancelFlag=true;
   103 						return false;
   104 						break;
   105 				}
   106 			}
   107 			outputFile=fd->selectedFile();
   108 			cancelFlag=false;
   109 			return true;
   110 		}
   111 	}
   112 	return false;
   113 }
   114 
   115 bool ExportBase::canceled()
   116 {
   117 	return cancelFlag;
   118 }
   119 
   120 QString ExportBase::getSectionString(TreeItem *start)
   121 {
   122 	// Make prefix like "2.5.3" for "bo:2,bo:5,bo:3"
   123 	QString r;
   124 	TreeItem *ti=start;
   125 	int depth=ti->depth();
   126 	while (depth>0)
   127 	{
   128 		r=QString("%1").arg(1+ti->num(),0,10)+"." + r;
   129 		ti=ti->parent(); 
   130 		depth=ti->depth();
   131 	}	
   132 	if (r.isEmpty())
   133 		return r;
   134 	else	
   135 		return r + " ";
   136 }
   137 
   138 ////////////////////////////////////////////////////////////////////////
   139 ExportAO::ExportAO()
   140 {
   141 	filter="TXT (*.txt)";
   142 	caption=vymName+ " -" +QObject::tr("Export as ASCII")+" "+QObject::tr("(still experimental)");
   143 }
   144 
   145 void ExportAO::doExport()	
   146 {
   147 	QFile file (outputFile);
   148 	if ( !file.open( QIODevice::WriteOnly ) )
   149 	{
   150 		qWarning ("ExportAO::doExport couldn't open "+outputFile);
   151 		return;
   152 	}
   153 	QTextStream ts( &file );	// use LANG decoding here...
   154 
   155 	// Main loop over all branches
   156 	QString s;
   157 	QString curIndent;
   158 	int i;
   159 	BranchItem *cur=NULL;
   160 	BranchItem *prev=NULL;
   161 
   162 	QString colString;
   163 	QColor col;
   164 
   165 	cur=model->nextBranch (cur,prev);
   166 	while (cur) 
   167 	{
   168 		if (cur->getType()==TreeItem::Branch || cur->getType()==TreeItem::MapCenter)
   169 		{
   170 			// Make indentstring
   171 			curIndent="";
   172 			for (i=0;i<cur->depth()-1;i++) curIndent+= indentPerDepth;
   173 
   174 			if (!cur->hasHiddenExportParent() )
   175 			{
   176 				col=cur->getHeadingColor();
   177 				if (col==QColor (255,0,0))
   178 					colString="[R]";
   179 				else if (col==QColor (217,81,0))
   180 					colString="[O]";
   181 				else if (col==QColor (0,85,0))
   182 					colString="[G]";
   183 				else  	
   184 					colString="[?]";
   185 				switch (cur->depth())
   186 				{
   187 					case 0:
   188 						//ts << underline (cur->getHeading(),QString("="));
   189 						//ts << "\n";
   190 						break;
   191 					case 1:
   192 						//ts << "\n";
   193 						//ts << (underline ( cur->getHeading(), QString("-") ) );
   194 						//ts << "\n";
   195 						break;
   196 					case 2: // Main heading
   197 						ts << "\n";
   198 						ts << underline ( cur->getHeading(), QString("=") );
   199 						ts << "\n";
   200 						break;
   201 					case 3: // Achievement, Bonus, Objective ...
   202 						ts << "\n\n";
   203 						ts << underline ( cur->getHeading(), "-");
   204 						ts << "\n\n";
   205 						break;
   206 					case 4:	// That's the item we need to know
   207 						//ts << (curIndent + "* " + colString+" "+ cur->getHeading());
   208 						ts << colString+" "+ cur->getHeading();
   209 						if (cur->isActiveStandardFlag ("hook-green"))
   210 							ts << " [DONE] ";
   211 						else	if (cur->isActiveStandardFlag ("wip"))
   212 							ts << " [WIP] ";
   213 						else	if (cur->isActiveStandardFlag ("cross-red"))
   214 							ts << " [NOT STARTED] ";
   215 						ts << "\n";
   216 						break;
   217 					default:
   218 						ts << (curIndent + "- " + cur->getHeading());
   219 						ts << "\n";
   220 						break;
   221 				}
   222 
   223 				// If necessary, write note
   224 				if (!cur->getNoteObj().isEmpty())
   225 				{
   226 					curIndent +="  | ";
   227 					s=cur->getNoteASCII( curIndent, 80);
   228 					ts << s;
   229 				}
   230 			}
   231 		}
   232 		cur=model->nextBranch(cur,prev);
   233 	}
   234 	file.close();
   235 }
   236 
   237 QString ExportAO::underline (const QString &text, const QString &line)
   238 {
   239 	QString r=text + "\n";
   240 	for (int j=0;j<text.length();j++) r+=line;
   241 	return r;
   242 }
   243 
   244 
   245 ////////////////////////////////////////////////////////////////////////
   246 ExportASCII::ExportASCII()
   247 {
   248 	filter="TXT (*.txt)";
   249 	caption=vymName+ " -" +QObject::tr("Export as ASCII")+" "+QObject::tr("(still experimental)");
   250 }
   251 
   252 void ExportASCII::doExport()	
   253 {
   254 	QFile file (outputFile);
   255 	if ( !file.open( QIODevice::WriteOnly ) )
   256 	{
   257 		qWarning ("ExportASCII::doExport couldn't open "+outputFile);
   258 		return;
   259 	}
   260 	QTextStream ts( &file );	// use LANG decoding here...
   261 
   262 	// Main loop over all branches
   263 	QString s;
   264 	QString curIndent;
   265 	int i;
   266 	BranchItem *cur=NULL;
   267 	BranchItem *prev=NULL;
   268 
   269 	cur=model->nextBranch (cur,prev);
   270 	while (cur) 
   271 	{
   272 		if (cur->getType()==TreeItem::Branch || cur->getType()==TreeItem::MapCenter)
   273 		{
   274 			// Make indentstring
   275 			curIndent="";
   276 			for (i=1;i<cur->depth()-1;i++) curIndent+= indentPerDepth;
   277 
   278 			if (!cur->hasHiddenExportParent() )
   279 			{
   280 				//std::cout << "ExportASCII::  "<<curIndent.toStdString()<<cur->getHeading().toStdString()<<std::endl;
   281 				switch (cur->depth())
   282 				{
   283 					case 0:
   284 						ts << underline (cur->getHeading(),QString("="));
   285 						ts << "\n";
   286 						break;
   287 					case 1:
   288 						ts << "\n";
   289 						ts << (underline (getSectionString(cur) + cur->getHeading(), QString("-") ) );
   290 						ts << "\n";
   291 						break;
   292 					case 2:
   293 						ts << "\n";
   294 						ts << (curIndent + "* " + cur->getHeading());
   295 						ts << "\n";
   296 						break;
   297 					case 3:
   298 						ts << (curIndent + "- " + cur->getHeading());
   299 						ts << "\n";
   300 						break;
   301 					default:
   302 						ts << (curIndent + "- " + cur->getHeading());
   303 						ts << "\n";
   304 						break;
   305 				}
   306 
   307 				// If necessary, write note
   308 				if (!cur->getNoteObj().isEmpty())
   309 				{
   310 					curIndent +="  | ";
   311 					s=cur->getNoteASCII( curIndent, 80);
   312 					ts << s;
   313 				}
   314 			}
   315 		}
   316 		cur=model->nextBranch(cur,prev);
   317 	}
   318 	file.close();
   319 }
   320 
   321 QString ExportASCII::underline (const QString &text, const QString &line)
   322 {
   323 	QString r=text + "\n";
   324 	for (int j=0;j<text.length();j++) r+=line;
   325 	return r;
   326 }
   327 
   328 
   329 ////////////////////////////////////////////////////////////////////////
   330 void ExportCSV::doExport()
   331 {
   332 	QFile file (outputFile);
   333 	if ( !file.open( QIODevice::WriteOnly ) )
   334 	{
   335 		qWarning ("ExportBase::exportXML  couldn't open "+outputFile);
   336 		return;
   337 	}
   338 	QTextStream ts( &file );	// use LANG decoding here...
   339 
   340 	// Write header
   341 	ts << "\"Note\""  <<endl;
   342 
   343 	// Main loop over all branches
   344 	QString s;
   345 	QString curIndent("");
   346 	int i;
   347 	BranchObj *bo;  //FIXME-3 still needed?
   348 	BranchItem *cur=NULL;
   349 	BranchItem *prev=NULL;
   350 	cur=model->nextBranch (cur,prev);
   351 	while (cur) 
   352 	{
   353 		bo=(BranchObj*)(cur->getLMO());
   354 
   355 		if (!cur->hasHiddenExportParent() )
   356 		{
   357 			// If necessary, write note
   358 			if (!cur->getNoteObj().isEmpty())
   359 			{
   360 				s =cur->getNoteASCII();
   361 				s=s.replace ("\n","\n"+curIndent);
   362 				ts << ("\""+s+"\",");
   363 			} else
   364 				ts <<"\"\",";
   365 
   366 			// Make indentstring
   367 			for (i=0;i<cur->depth();i++) curIndent+= "\"\",";
   368 
   369 			// Write heading
   370 			ts << curIndent << "\"" << cur->getHeading()<<"\""<<endl;
   371 		}
   372 		
   373 		cur=model->nextBranch(cur,prev);
   374 		curIndent="";
   375 	}
   376 	file.close();
   377 }
   378 
   379 ////////////////////////////////////////////////////////////////////////
   380 void ExportKDE3Bookmarks::doExport() 
   381 {
   382 	WarningDialog dia;
   383 	dia.showCancelButton (true);
   384 	dia.setText(QObject::tr("Exporting the %1 bookmarks will overwrite\nyour existing bookmarks file.").arg("KDE"));
   385 	dia.setCaption(QObject::tr("Warning: Overwriting %1 bookmarks").arg("KDE 3"));
   386 	dia.setShowAgainName("/exports/KDE/overwriteKDEBookmarks");
   387 	if (dia.exec()==QDialog::Accepted)
   388 	{
   389 		model->exportXML(tmpDir.path(),false);
   390 
   391 		XSLTProc p;
   392 		p.setInputFile (tmpDir.path()+"/"+model->getMapName()+".xml");
   393 		p.setOutputFile (tmpDir.home().path()+"/.kde/share/apps/konqueror/bookmarks.xml");
   394 		p.setXSLFile (vymBaseDir.path()+"/styles/vym2kdebookmarks.xsl");
   395 		p.process();
   396 
   397 		QString ub=vymBaseDir.path()+"/scripts/update-bookmarks";
   398 		QProcess *proc= new QProcess ;
   399 		proc->start( ub);
   400 		if (!proc->waitForStarted())
   401 		{
   402 			QMessageBox::warning(0, 
   403 				QObject::tr("Warning"),
   404 				QObject::tr("Couldn't find script %1\nto notifiy Browsers of changed bookmarks.").arg(ub));
   405 		}	
   406 	}
   407 }
   408 
   409 ////////////////////////////////////////////////////////////////////////
   410 void ExportKDE4Bookmarks::doExport() 
   411 {
   412 	WarningDialog dia;
   413 	dia.showCancelButton (true);
   414 	dia.setText(QObject::tr("Exporting the %1 bookmarks will overwrite\nyour existing bookmarks file.").arg("KDE 4"));
   415 	dia.setCaption(QObject::tr("Warning: Overwriting %1 bookmarks").arg("KDE"));
   416 	dia.setShowAgainName("/exports/KDE/overwriteKDEBookmarks");
   417 	if (dia.exec()==QDialog::Accepted)
   418 	{
   419 		model->exportXML(tmpDir.path(),false);
   420 
   421 		XSLTProc p;
   422 		p.setInputFile (tmpDir.path()+"/"+model->getMapName()+".xml");
   423 		p.setOutputFile (tmpDir.home().path()+"/.kde4/share/apps/konqueror/bookmarks.xml");
   424 		p.setXSLFile (vymBaseDir.path()+"/styles/vym2kdebookmarks.xsl");
   425 		p.process();
   426 
   427 		QString ub=vymBaseDir.path()+"/scripts/update-bookmarks";
   428 		QProcess *proc= new QProcess ;
   429 		proc->start( ub);
   430 		if (!proc->waitForStarted())
   431 		{
   432 			QMessageBox::warning(0, 
   433 				QObject::tr("Warning"),
   434 				QObject::tr("Couldn't find script %1\nto notifiy Browsers of changed bookmarks.").arg(ub));
   435 		}	
   436 	}
   437 }
   438 
   439 ////////////////////////////////////////////////////////////////////////
   440 void ExportFirefoxBookmarks::doExport() 
   441 {
   442 	WarningDialog dia;
   443 	dia.showCancelButton (true);
   444 	dia.setText(QObject::tr("Exporting the %1 bookmarks will overwrite\nyour existing bookmarks file.").arg("Firefox"));
   445 	dia.setCaption(QObject::tr("Warning: Overwriting %1 bookmarks").arg("Firefox"));
   446 	dia.setShowAgainName("/vym/warnings/overwriteImportBookmarks");
   447 	if (dia.exec()==QDialog::Accepted)
   448 	{
   449 		model->exportXML(tmpDir.path(),false);
   450 
   451 /*
   452 		XSLTProc p;
   453 		p.setInputFile (tmpDir.path()+"/"+me->getMapName()+".xml");
   454 		p.setOutputFile (tmpDir.home().path()+"/.kde/share/apps/konqueror/bookmarks.xml");
   455 		p.setXSLFile (vymBaseDir.path()+"/styles/vym2kdebookmarks.xsl");
   456 		p.process();
   457 
   458 		QString ub=vymBaseDir.path()+"/scripts/update-bookmarks";
   459 		QProcess *proc = new QProcess( );
   460 		proc->addArgument(ub);
   461 
   462 		if ( !proc->start() ) 
   463 		{
   464 			QMessageBox::warning(0, 
   465 				QObject::tr("Warning"),
   466 				QObject::tr("Couldn't find script %1\nto notifiy Browsers of changed bookmarks.").arg(ub));
   467 		}	
   468 
   469 */
   470 	}
   471 }
   472 
   473 ////////////////////////////////////////////////////////////////////////
   474 ExportHTML::ExportHTML():ExportBase()
   475 {
   476 	init();
   477 }
   478 
   479 ExportHTML::ExportHTML(VymModel *m):ExportBase(m)
   480 {
   481 	init();
   482 }
   483 
   484 void ExportHTML::init()
   485 {
   486 	singularDelimiter=": ";
   487 	noSingulars=true;	
   488 	frameURLs=true;
   489 	useMapColors=true;
   490 
   491 	if (model &&model->getMapEditor()) 
   492 		offset=model->getMapEditor()->getTotalBBox().topLeft();
   493 }
   494 
   495 QString ExportHTML::getBranchText(BranchItem *current)
   496 {
   497 	if (current)
   498 	{
   499 		bool vis=false;
   500 		QRectF hr;
   501 		LinkableMapObj *lmo=current->getLMO();
   502 		if (lmo)
   503 		{
   504 			hr=((BranchObj*)lmo)->getBBoxHeading();
   505 			if (lmo->isVisibleObj()) vis=true;
   506 		}
   507 		QString col;
   508 		QString id=model->getSelectString(current);
   509 		if (useMapColors)
   510 			col=QString("style='color:%1'").arg(current->getHeadingColor().name());
   511 		QString s=QString("<span class='vym-branch%1' %2 id='%3'>")
   512 			.arg(current->depth())
   513 			.arg(col)
   514 			.arg(id);
   515 		QString url=current->getURL();	
   516 		if (!url.isEmpty())
   517 		{
   518 			s+=QString ("<a href=\"%1\">").arg(url);
   519 			s+=QString ("<img src=\"flags/flag-url-16x16.png\">%1</a>").arg(quotemeta(current->getHeading()));
   520 			s+="</a>";
   521 			
   522 			QRectF fbox=current->getBBoxFlag ("system-url");
   523 			if (vis)
   524 				imageMap+=QString("  <area shape='rect' coords='%1,%2,%3,%4' href='%5'>\n")
   525 					.arg(fbox.left()-offset.x())
   526 					.arg(fbox.top()-offset.y())
   527 					.arg(fbox.right()-offset.x())
   528 					.arg(fbox.bottom()-offset.y())
   529 					.arg(url);
   530 		} else	
   531 			s+=quotemeta(current->getHeading());
   532 		s+="</span>";
   533 
   534 		if (vis)
   535 			imageMap+=QString("  <area shape='rect' coords='%1,%2,%3,%4' href='#%5'>\n")
   536 				.arg(hr.left()-offset.x())
   537 				.arg(hr.top()-offset.y())
   538 				.arg(hr.right()-offset.x())
   539 				.arg(hr.bottom()-offset.y())
   540 				.arg(id);
   541 
   542 		// Include note
   543 		if (!current->getNoteObj().isEmpty())
   544 			s+="<table border=1><tr><td>"+current->getNote()+"</td></tr></table>";
   545 
   546 		return s;
   547 	} 
   548 	return QString();
   549 }
   550 
   551 QString ExportHTML::buildList (BranchItem *current)
   552 {
   553     QString r;
   554 
   555     uint i=0;
   556 	BranchItem *bi=current->getFirstBranch();
   557 
   558 	// Only add itemized list, if we have more than one subitem.
   559 	// For only one subitem, just add a separator to keep page more compact
   560 	bool noSingularsHere=false;
   561 	if (current->branchCount()<2 && noSingulars) noSingularsHere=true;
   562 
   563 	if (bi)
   564     {
   565 		if (!noSingularsHere)
   566 			r+="<ul>\n";
   567 		else
   568 			r+=singularDelimiter;
   569 
   570 		while (bi)
   571 		{
   572 			if (!bi->hasHiddenExportParent() )	
   573 			{
   574 				if (!noSingularsHere) r+="<li>";
   575 				r+=getBranchText (bi);
   576 				if (!bi->getURL().isEmpty() && frameURLs && noSingularsHere)
   577 					// Add frame, if we have subitems to an URL
   578 					r+="<table border=1><tr><td>"+buildList (bi)+"</td></tr></table>";	// recursivly add deeper branches
   579 				else
   580 					r+=buildList (bi);	// recursivly add deeper branches
   581 				if (!noSingularsHere) r+="</li>";
   582 				r+="\n";
   583 			}
   584 			i++;
   585 			bi=current->getBranchNum(i);
   586 		}
   587 
   588 		if (!noSingularsHere) r+="</ul>\n";
   589 	}
   590     return r;
   591 }
   592 
   593 void ExportHTML::doExport() 
   594 {
   595 	QFile file (outputFile);
   596 	if ( !file.open( QIODevice::WriteOnly ) ) 
   597 	{
   598 		QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not write %1").arg(outputFile));
   599 		mainWindow->statusMessage(QString(QObject::tr("Export failed.")));
   600 		return;
   601 	}
   602 	QTextStream ts( &file );	// use LANG decoding here...
   603 	ts.setEncoding (QTextStream::UnicodeUTF8); // Force UTF8
   604 
   605 	// Write header
   606 	ts<<"<html><title>"+model->getMapName()<<"</title><body>";
   607 	ts<<" <link rel='stylesheet' id='css.stylesheet' href='vym.css' />\n";
   608 
   609 	// Include image
   610 	ts<<"<center><img src=\""<<model->getMapName()<<".png\" usemap='#imagemap'></center>\n";
   611 
   612 
   613 	// Main loop over all mapcenters
   614 	QString s;
   615 	TreeItem *rootItem=model->getRootItem();
   616 	BranchItem *bi;
   617 	for (int i=0; i<rootItem->branchCount(); i++)
   618 	{
   619 		bi=rootItem->getBranchNum(i);
   620 		if (!bi->hasHiddenExportParent())
   621 		{
   622 			ts<<getBranchText (bi);
   623 			ts<<buildList (bi);
   624 		}
   625 	}	
   626 
   627 	// Imagemap
   628 	ts<<"<map name='imagemap'>\n"+imageMap+"</map>\n";
   629 
   630 	// Write footer 
   631 	ts<<"<hr/>\n";
   632 	ts<<"<table class=\"vym-footer\">   \n\
   633       <tr> \n\
   634         <td class=\"vym-footerL\">"+model->getFileName()+"</td> \n\
   635         <td class=\"vym-footerC\">"+model->getDate()+"</td> \n\
   636         <td class=\"vym-footerR\"> vym "+model->getVersion()+"</td> \n\
   637       </tr> \n \
   638     </table>\n";
   639 	ts<<"</body></html>";
   640 	file.close();
   641 }
   642 
   643 ////////////////////////////////////////////////////////////////////////
   644 void ExportTaskjuggler::doExport() 
   645 {
   646 	model->exportXML(tmpDir.path(),false);
   647 
   648 	XSLTProc p;
   649 	p.setInputFile (tmpDir.path()+"/"+model->getMapName()+".xml");
   650 	p.setOutputFile (outputFile);
   651 	p.setXSLFile (vymBaseDir.path()+"/styles/vym2taskjuggler.xsl");
   652 	p.process();
   653 }
   654 
   655 ////////////////////////////////////////////////////////////////////////
   656 void ExportLaTeX::doExport() 
   657 {
   658 	// Exports a map to a LaTex file.  
   659 	// This file needs to be included 
   660 	// or inported into a LaTex document
   661 	// it will not add a preamble, or anything 
   662 	// that makes a full LaTex document.
   663   QFile file (outputFile);
   664   if ( !file.open( QIODevice::WriteOnly ) ) {
   665 	QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not write %1").arg(outputFile));
   666 	mainWindow->statusMessage(QString(QObject::tr("Export failed.")));
   667     return;
   668   }
   669   QTextStream ts( &file );	// use LANG decoding here...
   670   ts.setEncoding (QTextStream::UnicodeUTF8); // Force UTF8
   671   
   672   // Main loop over all branches
   673   QString s;
   674   // QString curIndent("");
   675   // int i;
   676   BranchObj *bo;
   677   BranchItem *cur=NULL;
   678   BranchItem *prev=NULL;
   679   model->nextBranch(cur,prev);
   680   while (cur) 
   681   {
   682 	bo=(BranchObj*)(cur->getLMO());
   683 
   684 	if (!cur->hasHiddenExportParent() )
   685 	{
   686 		switch (cur->depth() ) 
   687 		{
   688 			case 0: break;
   689 			case 1: 
   690 			  ts << ("\\chapter{" + cur->getHeading()+ "}\n");
   691 			  break;
   692 			case 2: 
   693 			  ts << ("\\section{" + cur->getHeading()+ "}\n");
   694 			  break;
   695 			case 3: 
   696 			  ts << ("\\subsection{" + cur->getHeading()+ "}\n");
   697 			  break;
   698 			case 4: 
   699 			  ts << ("\\subsubsection{" + cur->getHeading()+ "}\n");
   700 			  break;
   701 			default:
   702 			  ts << ("\\paragraph*{" + cur->getHeading()+ "}\n");
   703 			
   704 		}
   705 		// If necessary, write note
   706 		if (!cur->getNoteObj().isEmpty()) {
   707 		  ts << (cur->getNoteASCII());
   708 		  ts << ("\n");
   709 		}
   710 	}
   711     cur=model->nextBranch(cur,prev);
   712    }
   713   file.close();
   714 }
   715 
   716 ////////////////////////////////////////////////////////////////////////
   717 ExportOO::ExportOO()
   718 {
   719 	useSections=false;
   720 }
   721 
   722 ExportOO::~ExportOO()
   723 {
   724 }	
   725 
   726 QString ExportOO::buildList (TreeItem *current)
   727 {
   728     QString r;
   729 
   730     uint i=0;
   731 	BranchItem *bi=current->getFirstBranch();
   732 	if (bi)
   733     {
   734 		// Start list
   735 		r+="<text:list text:style-name=\"vym-list\">\n";
   736 		while (bi)
   737 		{
   738 			if (!bi->hasHiddenExportParent() )	
   739 			{
   740 				r+="<text:list-item><text:p >";
   741 				r+=quotemeta(bi->getHeading());
   742 				// If necessary, write note
   743 				if (!bi->getNoteObj().isEmpty())
   744 					r+=bi->getNoteOpenDoc();
   745 				r+="</text:p>";
   746 				r+=buildList (bi);	// recursivly add deeper branches
   747 				r+="</text:list-item>\n";
   748 			}
   749 			i++;
   750 			bi=current->getBranchNum(i);
   751 		}
   752 		r+="</text:list>\n";
   753     }
   754     return r;
   755 }
   756 
   757 
   758 void ExportOO::exportPresentation()
   759 {
   760 	QString allPages;
   761 
   762 	BranchItem *firstMCO=(BranchItem*)(model->getRootItem()->getFirstBranch());
   763 	if (!firstMCO) 
   764 	{
   765 		QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("No objects in map!"));
   766 		return;
   767 	}
   768 
   769 	// Insert new content
   770 	// FIXME add extra title in mapinfo for vym 1.13.x
   771 	content.replace ("<!-- INSERT TITLE -->",quotemeta(firstMCO->getHeading()));
   772 	content.replace ("<!-- INSERT AUTHOR -->",quotemeta(model->getAuthor()));
   773 
   774 	QString	onePage;
   775 	QString list;
   776 	
   777 	BranchItem *sectionBI;	
   778     int i=0;
   779 	BranchItem *pagesBI;
   780     int j=0;
   781 
   782 	int mapcenters=model->getRootItem()->branchCount();
   783 
   784 	// useSections already has been set in setConfigFile 
   785 	if (mapcenters>1)	
   786 		sectionBI=firstMCO;
   787 	else
   788 		sectionBI=firstMCO->getFirstBranch();
   789 
   790 	// Walk sections
   791 	while (sectionBI && !sectionBI->hasHiddenExportParent() )
   792 	{
   793 		if (useSections)
   794 		{
   795 			// Add page with section title
   796 			onePage=sectionTemplate;
   797 			onePage.replace ("<!-- INSERT PAGE HEADING -->", quotemeta(sectionBI->getHeading() ) );
   798 			allPages+=onePage;
   799 			pagesBI=sectionBI->getFirstBranch();
   800 		} else
   801 		{
   802 			//i=-2;	// only use inner loop to 
   803 			        // turn mainbranches into pages
   804 			//sectionBI=firstMCO;
   805 			pagesBI=sectionBI;
   806 		}
   807 
   808 		j=0;
   809 		while (pagesBI && !pagesBI->hasHiddenExportParent() )
   810 		{
   811 			// Add page with list of items
   812 			onePage=pageTemplate;
   813 			onePage.replace ("<!-- INSERT PAGE HEADING -->", quotemeta (pagesBI->getHeading() ) );
   814 			list=buildList (pagesBI);
   815 			onePage.replace ("<!-- INSERT LIST -->", list);
   816 			allPages+=onePage;
   817 			if (pagesBI!=sectionBI)
   818 			{
   819 				j++;
   820 				pagesBI=((BranchItem*)pagesBI->parent())->getBranchNum(j);
   821 			} else
   822 				pagesBI=NULL;	// We are already iterating over the sectionBIs
   823 		}
   824 		i++;
   825 		if (mapcenters>1 )
   826 			sectionBI=model->getRootItem()->getBranchNum (i);
   827 		else
   828 			sectionBI=firstMCO->getBranchNum (i);
   829 	}
   830 	
   831 	content.replace ("<!-- INSERT PAGES -->",allPages);
   832 
   833 	// Write modified content
   834 	QFile f (contentFile);
   835     if ( !f.open( QIODevice::WriteOnly ) ) 
   836 	{
   837 		QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not write %1").arg(contentFile));
   838 		mainWindow->statusMessage(QString(QObject::tr("Export failed.")));
   839 		return;
   840     }
   841 
   842     QTextStream t( &f );
   843     t << content;
   844     f.close();
   845 
   846 	// zip tmpdir to destination
   847 	zipDir (tmpDir,outputFile);	
   848 }
   849 
   850 bool ExportOO::setConfigFile (const QString &cf)
   851 {
   852 	configFile=cf;
   853 	int i=cf.findRev ("/");
   854 	if (i>=0) configDir=cf.left(i);
   855 	SimpleSettings set;
   856 	set.readSettings(configFile);
   857 
   858 	// set paths
   859 	templateDir=configDir+"/"+set.readEntry ("Template");
   860 
   861 	QDir d (templateDir);
   862 	if (!d.exists())
   863 	{
   864 		QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Check \"%1\" in\n%2").arg("Template="+set.readEntry ("Template")).arg(configFile));
   865 		return false;
   866 
   867 	}
   868 
   869 	contentTemplateFile=templateDir+"content-template.xml";
   870 	contentFile=tmpDir.path()+"/content.xml";
   871 	pageTemplateFile=templateDir+"page-template.xml";
   872 	sectionTemplateFile=templateDir+"section-template.xml";
   873 
   874 	if (set.readEntry("useSections").contains("yes"))
   875 		useSections=true;
   876 
   877 	// Copy template to tmpdir
   878 	system ("cp -r "+templateDir+"* "+tmpDir.path());
   879 
   880 	// Read content-template
   881 	if (!loadStringFromDisk (contentTemplateFile,content))
   882 	{
   883 		QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not read %1").arg(contentTemplateFile));
   884 		return false;
   885 	}
   886 
   887 	// Read page-template
   888 	if (!loadStringFromDisk (pageTemplateFile,pageTemplate))
   889 	{
   890 		QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not read %1").arg(pageTemplateFile));
   891 		return false;
   892 	}
   893 	
   894 	// Read section-template
   895 	if (useSections && !loadStringFromDisk (sectionTemplateFile,sectionTemplate))
   896 	{
   897 		QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not read %1").arg(sectionTemplateFile));
   898 		return false;
   899 	}
   900 	return true;
   901 }
   902