exports.cpp
author insilmaril
Mon Jul 23 12:42:02 2007 +0000 (2007-07-23)
changeset 566 ebebedffba50
parent 512 96680eb33a79
child 600 a9fe976e01a6
permissions -rw-r--r--
Added import filter for Freemind
     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 (makeUniqueDir(ok,"/tmp/vym-export-XXXXXX"));
    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->getDepth()==0)
   155 		{
   156 			ts << (bo->getHeading()+ "\n");
   157 			for (j=0;j<bo->getHeading().length();j++) ts<<"=";
   158 			ts << "\n";
   159 		} else 	if (bo->getDepth()==1)
   160 			ts << ("\n"+getSectionString(bo) + bo->getHeading()+ "\n");
   161 		else	if (bo->getDepth()==2)
   162 			ts << (actIndent + " * " + bo->getHeading()+ "\n");
   163 		else	if (bo->getDepth()==3)
   164 			ts << (actIndent + " o " + bo->getHeading()+ "\n");
   165 		else	
   166 			ts << (actIndent + " - " + bo->getHeading()+ "\n");
   167 		
   168 		// If necessary, write note
   169 		if (!bo->getNote().isEmpty())
   170 		{
   171 			s =bo->getNoteASCII();
   172 			s=s.replace ("\n","\n"+actIndent);
   173 			ts << (s+"\n\n");
   174 		}
   175 		bo=bo->next();
   176 		actIndent="";
   177 	}
   178 	file.close();
   179 }
   180 
   181 
   182 ////////////////////////////////////////////////////////////////////////
   183 void ExportCSV::doExport()
   184 {
   185 	QFile file (outputFile);
   186 	if ( !file.open( QIODevice::WriteOnly ) )
   187 	{
   188 		qWarning ("ExportBase::exportXML  couldn't open "+outputFile);
   189 		return;
   190 	}
   191 	QTextStream ts( &file );	// use LANG decoding here...
   192 
   193 	// Write header
   194 	ts << "\"Note\""  <<endl;
   195 
   196 	// Main loop over all branches
   197 	QString s;
   198 	QString actIndent("");
   199 	int i;
   200 	BranchObj *bo;
   201 	bo=mapCenter->first();
   202 	while (bo) 
   203 	{
   204 		// If necessary, write note
   205 		if (!bo->getNote().isEmpty())
   206 		{
   207 			s =bo->getNoteASCII();
   208 			s=s.replace ("\n","\n"+actIndent);
   209 			ts << ("\""+s+"\",");
   210 		} else
   211 			ts <<"\"\",";
   212 
   213 		// Make indentstring
   214 		for (i=0;i<bo->getDepth();i++) actIndent+= "\"\",";
   215 
   216 		// Write heading
   217 		ts << actIndent << "\"" << bo->getHeading()<<"\""<<endl;
   218 		
   219 		bo=bo->next();
   220 		actIndent="";
   221 	}
   222 	file.close();
   223 }
   224 
   225 ////////////////////////////////////////////////////////////////////////
   226 void ExportKDEBookmarks::doExport() 
   227 {
   228 	MapEditor *me=NULL;
   229 	if (mapCenter) me=mapCenter->getMapEditor();
   230 	if (me)
   231 	{
   232 		WarningDialog dia;
   233 		dia.showCancelButton (true);
   234 		dia.setText(QObject::tr("Exporting the %1 bookmarks will overwrite\nyour existing bookmarks file.").arg("KDE"));
   235 		dia.setCaption(QObject::tr("Warning: Overwriting %1 bookmarks").arg("KDE"));
   236 		dia.setShowAgainName("/exports/KDE/overwriteKDEBookmarks");
   237 		if (dia.exec()==QDialog::Accepted)
   238 		{
   239 			me->exportXML(tmpDir.path(),false);
   240 
   241 			XSLTProc p;
   242 			p.setInputFile (tmpDir.path()+"/"+me->getMapName()+".xml");
   243 			p.setOutputFile (tmpDir.home().path()+"/.kde/share/apps/konqueror/bookmarks.xml");
   244 			p.setXSLFile (vymBaseDir.path()+"/styles/vym2kdebookmarks.xsl");
   245 			p.process();
   246 
   247 			QString ub=vymBaseDir.path()+"/scripts/update-bookmarks";
   248 			QProcess *proc= new QProcess ;
   249 			proc->start( ub);
   250 			if (!proc->waitForStarted())
   251 			{
   252 				QMessageBox::warning(0, 
   253 					QObject::tr("Warning"),
   254 					QObject::tr("Couldn't find script %1\nto notifiy Browsers of changed bookmarks.").arg(ub));
   255 			}	
   256 		}
   257 	}
   258 
   259 }
   260 
   261 ////////////////////////////////////////////////////////////////////////
   262 void ExportFirefoxBookmarks::doExport() 
   263 {
   264 	MapEditor *me=NULL;
   265 	if (mapCenter) me=mapCenter->getMapEditor();
   266 	if (me)
   267 	{
   268 		WarningDialog dia;
   269 		dia.showCancelButton (true);
   270 		dia.setText(QObject::tr("Exporting the %1 bookmarks will overwrite\nyour existing bookmarks file.").arg("Firefox"));
   271 		dia.setCaption(QObject::tr("Warning: Overwriting %1 bookmarks").arg("Firefox"));
   272 		dia.setShowAgainName("/vym/warnings/overwriteImportBookmarks");
   273 		if (dia.exec()==QDialog::Accepted)
   274 		{
   275 			me->exportXML(tmpDir.path(),false);
   276 
   277 /*
   278 			XSLTProc p;
   279 			p.setInputFile (tmpDir.path()+"/"+me->getMapName()+".xml");
   280 			p.setOutputFile (tmpDir.home().path()+"/.kde/share/apps/konqueror/bookmarks.xml");
   281 			p.setXSLFile (vymBaseDir.path()+"/styles/vym2kdebookmarks.xsl");
   282 			p.process();
   283 
   284 			QString ub=vymBaseDir.path()+"/scripts/update-bookmarks";
   285 			QProcess *proc = new QProcess( );
   286 			proc->addArgument(ub);
   287 
   288 			if ( !proc->start() ) 
   289 			{
   290 				QMessageBox::warning(0, 
   291 					QObject::tr("Warning"),
   292 					QObject::tr("Couldn't find script %1\nto notifiy Browsers of changed bookmarks.").arg(ub));
   293 			}	
   294 
   295 */
   296 
   297 		}
   298 	}
   299 }
   300 
   301 ////////////////////////////////////////////////////////////////////////
   302 void ExportTaskjuggler::doExport() 
   303 {
   304 	MapEditor *me=NULL;
   305 	if (mapCenter) me=mapCenter->getMapEditor();
   306 	if (me)
   307 	{
   308 		me->exportXML(tmpDir.path(),false);
   309 
   310 		XSLTProc p;
   311 		p.setInputFile (tmpDir.path()+"/"+me->getMapName()+".xml");
   312 		p.setOutputFile (outputFile);
   313 		p.setXSLFile (vymBaseDir.path()+"/styles/vym2taskjuggler.xsl");
   314 		p.process();
   315 	}
   316 
   317 }
   318 
   319 ////////////////////////////////////////////////////////////////////////
   320 void ExportLaTeX::doExport() 
   321 {
   322 	// Exports a map to a LaTex file.  
   323 	// This file needs to be included 
   324 	// or inported into a LaTex document
   325 	// it will not add a preamble, or anything 
   326 	// that makes a full LaTex document.
   327   QFile file (outputFile);
   328   if ( !file.open( QIODevice::WriteOnly ) ) {
   329 	QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not write %1").arg(outputFile));
   330 	mainWindow->statusMessage(QString(QObject::tr("Export failed.")));
   331     return;
   332   }
   333   QTextStream ts( &file );	// use LANG decoding here...
   334   ts.setEncoding (QTextStream::UnicodeUTF8); // Force UTF8
   335   
   336   // Main loop over all branches
   337   QString s;
   338   // QString actIndent("");
   339   // int i;
   340   BranchObj *bo;
   341   bo=mapCenter->first();
   342   while (bo) {
   343 	if (bo->getDepth()==0);
   344 	else if (bo->getDepth()==1) {
   345 	  ts << ("\\chapter{" + bo->getHeading()+ "}\n");
   346 	}
   347 	else if (bo->getDepth()==2) {
   348 	  ts << ("\\section{" + bo->getHeading()+ "}\n");
   349 	}
   350 	else if (bo->getDepth()==3) {
   351 	  ts << ("\\subsection{" + bo->getHeading()+ "}\n");
   352 	}
   353 	else if (bo->getDepth()==4) {
   354 	  ts << ("\\subsubsection{" + bo->getHeading()+ "}\n");
   355 	}
   356 	else {
   357 	  ts << ("\\paragraph*{" + bo->getHeading()+ "}\n");
   358 	}
   359 	
   360 	// If necessary, write note
   361 	if (!bo->getNote().isEmpty()) {
   362 	  ts << (bo->getNoteASCII());
   363 	  ts << ("\n");
   364 	}
   365     bo=bo->next();
   366    }
   367   file.close();
   368 }
   369 
   370 ////////////////////////////////////////////////////////////////////////
   371 ExportOO::ExportOO()
   372 {
   373 	useSections=false;
   374 }
   375 
   376 ExportOO::~ExportOO()
   377 {
   378 }	
   379 
   380 QString ExportOO::buildList (BranchObj *current)
   381 {
   382     QString r;
   383     BranchObj *bo;
   384 
   385     uint i=0;
   386     bo=current->getFirstBranch();
   387     if (bo)
   388     {
   389         // Start list
   390         r+="<text:list text:style-name=\"vym-list\">\n";
   391         while (bo)
   392         {
   393 			r+="<text:list-item><text:p >";
   394 			r+=quotemeta(bo->getHeading());
   395 			// If necessary, write note
   396 			if (!bo->getNote().isEmpty())
   397 				r+=bo->getNoteOpenDoc();
   398 			r+="</text:p>";
   399 			r+=buildList (bo);	// recursivly add deeper branches
   400 			r+="</text:list-item>\n";
   401 			i++;
   402 			bo=current->getBranchNum(i);
   403         }
   404         r+="</text:list>\n";
   405     }
   406     return r;
   407 }
   408 
   409 
   410 void ExportOO::exportPresentation()
   411 {
   412 	QString allPages;
   413 
   414 	// Insert new content
   415 	content.replace ("<!-- INSERT TITLE -->",quotemeta(mapCenter->getHeading()));
   416 	content.replace ("<!-- INSERT AUTHOR -->",quotemeta(mapCenter->getAuthor()));
   417 
   418 	QString	onePage;
   419 	QString list;
   420 	
   421 	BranchObj *sectionBO=mapCenter->getFirstBranch();
   422     int i=0;
   423 	BranchObj *pagesBO;
   424     int j=0;
   425 
   426 	// Walk sections
   427 	while (sectionBO)
   428 	{
   429 		if (useSections)
   430 		{
   431 			// Add page with section title
   432 			onePage=sectionTemplate;
   433 			onePage.replace ("<!-- INSERT PAGE HEADING -->", quotemeta(sectionBO->getHeading() ) );
   434 			allPages+=onePage;
   435 		} else
   436 		{
   437 			i=-2;	// only use inner loop to 
   438 			        // turn mainbranches into pages
   439 			sectionBO=mapCenter;
   440 		}
   441 
   442 		// Walk mainpages
   443 		pagesBO=sectionBO->getFirstBranch();
   444 		j=0;
   445 		while (pagesBO)
   446 		{
   447 			// Add page with list of items
   448 			onePage=pageTemplate;
   449 			onePage.replace ("<!-- INSERT PAGE HEADING -->", quotemeta (pagesBO->getHeading() ) );
   450 			list=buildList (pagesBO);
   451 			onePage.replace ("<!-- INSERT LIST -->", list);
   452 			allPages+=onePage;
   453 			j++;
   454 			pagesBO=sectionBO->getBranchNum(j);
   455 		}
   456 		i++;
   457 		sectionBO=mapCenter->getBranchNum(i);
   458 	}
   459 	
   460 	content.replace ("<!-- INSERT PAGES -->",allPages);
   461 
   462 	// Write modified content
   463 	QFile f (contentFile);
   464     if ( !f.open( QIODevice::WriteOnly ) ) 
   465 	{
   466 		QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not write %1").arg(contentFile));
   467 		mainWindow->statusMessage(QString(QObject::tr("Export failed.")));
   468 		return;
   469     }
   470 
   471     QTextStream t( &f );
   472     t << content;
   473     f.close();
   474 
   475 	// zip tmpdir to destination
   476 	zipDir (tmpDir,outputFile);	
   477 }
   478 
   479 bool ExportOO::setConfigFile (const QString &cf)
   480 {
   481 	configFile=cf;
   482 	int i=cf.findRev ("/");
   483 	if (i>=0) configDir=cf.left(i);
   484 	SimpleSettings set;
   485 	set.readSettings(configFile);
   486 
   487 	// set paths
   488 	templateDir=configDir+"/"+set.readEntry ("Template");
   489 
   490 	QDir d (templateDir);
   491 	if (!d.exists())
   492 	{
   493 		QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Check \"%1\" in\n%2").arg("Template="+set.readEntry ("Template")).arg(configFile));
   494 		return false;
   495 
   496 	}
   497 
   498 	contentTemplateFile=templateDir+"content-template.xml";
   499 	contentFile=tmpDir.path()+"/content.xml";
   500 	pageTemplateFile=templateDir+"page-template.xml";
   501 	sectionTemplateFile=templateDir+"section-template.xml";
   502 
   503 	if (set.readEntry("useSections").contains("yes"))
   504 		useSections=true;
   505 
   506 	// Copy template to tmpdir
   507 	system ("cp -r "+templateDir+"* "+tmpDir.path());
   508 
   509 	// Read content-template
   510 	if (!loadStringFromDisk (contentTemplateFile,content))
   511 	{
   512 		QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not read %1").arg(contentTemplateFile));
   513 		return false;
   514 	}
   515 
   516 	// Read page-template
   517 	if (!loadStringFromDisk (pageTemplateFile,pageTemplate))
   518 	{
   519 		QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not read %1").arg(pageTemplateFile));
   520 		return false;
   521 	}
   522 	
   523 	// Read section-template
   524 	if (useSections && !loadStringFromDisk (sectionTemplateFile,sectionTemplate))
   525 	{
   526 		QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not read %1").arg(sectionTemplateFile));
   527 		return false;
   528 	}
   529 	return true;
   530 }
   531