exports.cpp
author insilmaril
Wed, 05 Dec 2007 14:58:03 +0000
changeset 630 4998bb082c73
parent 613 8fb5b3956b3e
child 643 7eb4e5529c82
permissions -rw-r--r--
Added files from Andrew Ng for Windows
     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 curIndent;
   146 	int i;
   147 	BranchObj *bo;
   148 	bo=mapCenter->first();
   149 	while (bo) 
   150 	{
   151 		// Make indentstring
   152 		curIndent="";
   153 		for (i=0;i<bo->getDepth()-1;i++) curIndent+= indentPerDepth;
   154 
   155 		if (!bo->hasHiddenExportParent() )
   156 		{
   157 			switch (bo->getDepth())
   158 			{
   159 				case 0:
   160 					ts << underline (bo->getHeading(),QString("="));
   161 					ts << "\n";
   162 					break;
   163 				case 1:
   164 					ts << "\n";
   165 					ts << (underline (getSectionString(bo) + bo->getHeading(), QString("-") ) );
   166 					ts << "\n";
   167 					break;
   168 				case 2:
   169 					ts << "\n";
   170 					ts << (curIndent + "* " + bo->getHeading());
   171 					ts << "\n";
   172 					break;
   173 				case 3:
   174 					ts << (curIndent + "- " + bo->getHeading());
   175 					ts << "\n";
   176 					break;
   177 				default:
   178 					ts << (curIndent + "- " + bo->getHeading());
   179 					ts << "\n";
   180 					break;
   181 			}
   182 
   183 			// If necessary, write note
   184 			if (!bo->getNote().isEmpty())
   185 			{
   186 				curIndent +="  | ";
   187 				s =curIndent + bo->getNoteASCII( curIndent, 80);
   188 				s=s.replace ("\n","\n"+curIndent);
   189 				ts << QString (s+"\n");
   190 			}
   191 		}
   192 		bo=bo->next();
   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=mapCenter->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=bo->next();
   246 		curIndent="";
   247 	}
   248 	file.close();
   249 }
   250 
   251 ////////////////////////////////////////////////////////////////////////
   252 void ExportKDEBookmarks::doExport() 
   253 {
   254 	MapEditor *me=NULL;
   255 	if (mapCenter) me=mapCenter->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"));
   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 ////////////////////////////////////////////////////////////////////////
   288 void ExportFirefoxBookmarks::doExport() 
   289 {
   290 	MapEditor *me=NULL;
   291 	if (mapCenter) me=mapCenter->getMapEditor();
   292 	if (me)
   293 	{
   294 		WarningDialog dia;
   295 		dia.showCancelButton (true);
   296 		dia.setText(QObject::tr("Exporting the %1 bookmarks will overwrite\nyour existing bookmarks file.").arg("Firefox"));
   297 		dia.setCaption(QObject::tr("Warning: Overwriting %1 bookmarks").arg("Firefox"));
   298 		dia.setShowAgainName("/vym/warnings/overwriteImportBookmarks");
   299 		if (dia.exec()==QDialog::Accepted)
   300 		{
   301 			me->exportXML(tmpDir.path(),false);
   302 
   303 /*
   304 			XSLTProc p;
   305 			p.setInputFile (tmpDir.path()+"/"+me->getMapName()+".xml");
   306 			p.setOutputFile (tmpDir.home().path()+"/.kde/share/apps/konqueror/bookmarks.xml");
   307 			p.setXSLFile (vymBaseDir.path()+"/styles/vym2kdebookmarks.xsl");
   308 			p.process();
   309 
   310 			QString ub=vymBaseDir.path()+"/scripts/update-bookmarks";
   311 			QProcess *proc = new QProcess( );
   312 			proc->addArgument(ub);
   313 
   314 			if ( !proc->start() ) 
   315 			{
   316 				QMessageBox::warning(0, 
   317 					QObject::tr("Warning"),
   318 					QObject::tr("Couldn't find script %1\nto notifiy Browsers of changed bookmarks.").arg(ub));
   319 			}	
   320 
   321 */
   322 
   323 		}
   324 	}
   325 }
   326 
   327 ////////////////////////////////////////////////////////////////////////
   328 void ExportTaskjuggler::doExport() 
   329 {
   330 	MapEditor *me=NULL;
   331 	if (mapCenter) me=mapCenter->getMapEditor();
   332 	if (me)
   333 	{
   334 		me->exportXML(tmpDir.path(),false);
   335 
   336 		XSLTProc p;
   337 		p.setInputFile (tmpDir.path()+"/"+me->getMapName()+".xml");
   338 		p.setOutputFile (outputFile);
   339 		p.setXSLFile (vymBaseDir.path()+"/styles/vym2taskjuggler.xsl");
   340 		p.process();
   341 	}
   342 
   343 }
   344 
   345 ////////////////////////////////////////////////////////////////////////
   346 void ExportLaTeX::doExport() 
   347 {
   348 	// Exports a map to a LaTex file.  
   349 	// This file needs to be included 
   350 	// or inported into a LaTex document
   351 	// it will not add a preamble, or anything 
   352 	// that makes a full LaTex document.
   353   QFile file (outputFile);
   354   if ( !file.open( QIODevice::WriteOnly ) ) {
   355 	QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not write %1").arg(outputFile));
   356 	mainWindow->statusMessage(QString(QObject::tr("Export failed.")));
   357     return;
   358   }
   359   QTextStream ts( &file );	// use LANG decoding here...
   360   ts.setEncoding (QTextStream::UnicodeUTF8); // Force UTF8
   361   
   362   // Main loop over all branches
   363   QString s;
   364   // QString curIndent("");
   365   // int i;
   366   BranchObj *bo;
   367   bo=mapCenter->first();
   368   while (bo) {
   369 	if (!bo->hasHiddenExportParent() )
   370 	{
   371 		if (bo->getDepth()==0);
   372 		else if (bo->getDepth()==1) {
   373 		  ts << ("\\chapter{" + bo->getHeading()+ "}\n");
   374 		}
   375 		else if (bo->getDepth()==2) {
   376 		  ts << ("\\section{" + bo->getHeading()+ "}\n");
   377 		}
   378 		else if (bo->getDepth()==3) {
   379 		  ts << ("\\subsection{" + bo->getHeading()+ "}\n");
   380 		}
   381 		else if (bo->getDepth()==4) {
   382 		  ts << ("\\subsubsection{" + bo->getHeading()+ "}\n");
   383 		}
   384 		else {
   385 		  ts << ("\\paragraph*{" + bo->getHeading()+ "}\n");
   386 		}
   387 		
   388 		// If necessary, write note
   389 		if (!bo->getNote().isEmpty()) {
   390 		  ts << (bo->getNoteASCII());
   391 		  ts << ("\n");
   392 		}
   393 	}
   394     bo=bo->next();
   395    }
   396   file.close();
   397 }
   398 
   399 ////////////////////////////////////////////////////////////////////////
   400 ExportOO::ExportOO()
   401 {
   402 	useSections=false;
   403 }
   404 
   405 ExportOO::~ExportOO()
   406 {
   407 }	
   408 
   409 QString ExportOO::buildList (BranchObj *current)
   410 {
   411     QString r;
   412     BranchObj *bo;
   413 
   414     uint i=0;
   415     bo=current->getFirstBranch();
   416     if (bo)
   417     {
   418 		if (!bo->hasHiddenExportParent() )
   419 		{
   420 			// Start list
   421 			r+="<text:list text:style-name=\"vym-list\">\n";
   422 			while (bo)
   423 			{
   424 				r+="<text:list-item><text:p >";
   425 				r+=quotemeta(bo->getHeading());
   426 				// If necessary, write note
   427 				if (!bo->getNote().isEmpty())
   428 					r+=bo->getNoteOpenDoc();
   429 				r+="</text:p>";
   430 				r+=buildList (bo);	// recursivly add deeper branches
   431 				r+="</text:list-item>\n";
   432 				i++;
   433 				bo=current->getBranchNum(i);
   434 			}
   435 			r+="</text:list>\n";
   436 		}
   437     }
   438     return r;
   439 }
   440 
   441 
   442 void ExportOO::exportPresentation()
   443 {
   444 	QString allPages;
   445 
   446 	// Insert new content
   447 	content.replace ("<!-- INSERT TITLE -->",quotemeta(mapCenter->getHeading()));
   448 	content.replace ("<!-- INSERT AUTHOR -->",quotemeta(mapCenter->getAuthor()));
   449 
   450 	QString	onePage;
   451 	QString list;
   452 	
   453 	BranchObj *sectionBO=mapCenter->getFirstBranch();
   454     int i=0;
   455 	BranchObj *pagesBO;
   456     int j=0;
   457 
   458 	// Walk sections
   459 	while (sectionBO && !sectionBO->hasHiddenExportParent() )
   460 	{
   461 		if (useSections)
   462 		{
   463 			// Add page with section title
   464 			onePage=sectionTemplate;
   465 			onePage.replace ("<!-- INSERT PAGE HEADING -->", quotemeta(sectionBO->getHeading() ) );
   466 			allPages+=onePage;
   467 		} else
   468 		{
   469 			i=-2;	// only use inner loop to 
   470 			        // turn mainbranches into pages
   471 			sectionBO=mapCenter;
   472 		}
   473 
   474 		// Walk mainpages
   475 		pagesBO=sectionBO->getFirstBranch();
   476 		j=0;
   477 		while (pagesBO && !pagesBO->hasHiddenExportParent() )
   478 		{
   479 			// Add page with list of items
   480 			onePage=pageTemplate;
   481 			onePage.replace ("<!-- INSERT PAGE HEADING -->", quotemeta (pagesBO->getHeading() ) );
   482 			list=buildList (pagesBO);
   483 			onePage.replace ("<!-- INSERT LIST -->", list);
   484 			allPages+=onePage;
   485 			j++;
   486 			pagesBO=sectionBO->getBranchNum(j);
   487 		}
   488 		i++;
   489 		sectionBO=mapCenter->getBranchNum(i);
   490 	}
   491 	
   492 	content.replace ("<!-- INSERT PAGES -->",allPages);
   493 
   494 	// Write modified content
   495 	QFile f (contentFile);
   496     if ( !f.open( QIODevice::WriteOnly ) ) 
   497 	{
   498 		QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not write %1").arg(contentFile));
   499 		mainWindow->statusMessage(QString(QObject::tr("Export failed.")));
   500 		return;
   501     }
   502 
   503     QTextStream t( &f );
   504     t << content;
   505     f.close();
   506 
   507 	// zip tmpdir to destination
   508 	zipDir (tmpDir,outputFile);	
   509 }
   510 
   511 bool ExportOO::setConfigFile (const QString &cf)
   512 {
   513 	configFile=cf;
   514 	int i=cf.findRev ("/");
   515 	if (i>=0) configDir=cf.left(i);
   516 	SimpleSettings set;
   517 	set.readSettings(configFile);
   518 
   519 	// set paths
   520 	templateDir=configDir+"/"+set.readEntry ("Template");
   521 
   522 	QDir d (templateDir);
   523 	if (!d.exists())
   524 	{
   525 		QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Check \"%1\" in\n%2").arg("Template="+set.readEntry ("Template")).arg(configFile));
   526 		return false;
   527 
   528 	}
   529 
   530 	contentTemplateFile=templateDir+"content-template.xml";
   531 	contentFile=tmpDir.path()+"/content.xml";
   532 	pageTemplateFile=templateDir+"page-template.xml";
   533 	sectionTemplateFile=templateDir+"section-template.xml";
   534 
   535 	if (set.readEntry("useSections").contains("yes"))
   536 		useSections=true;
   537 
   538 	// Copy template to tmpdir
   539 	system ("cp -r "+templateDir+"* "+tmpDir.path());
   540 
   541 	// Read content-template
   542 	if (!loadStringFromDisk (contentTemplateFile,content))
   543 	{
   544 		QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not read %1").arg(contentTemplateFile));
   545 		return false;
   546 	}
   547 
   548 	// Read page-template
   549 	if (!loadStringFromDisk (pageTemplateFile,pageTemplate))
   550 	{
   551 		QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not read %1").arg(pageTemplateFile));
   552 		return false;
   553 	}
   554 	
   555 	// Read section-template
   556 	if (useSections && !loadStringFromDisk (sectionTemplateFile,sectionTemplate))
   557 	{
   558 		QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not read %1").arg(sectionTemplateFile));
   559 		return false;
   560 	}
   561 	return true;
   562 }
   563