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