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