exports.cpp
author insilmaril
Mon, 18 May 2009 09:41:31 +0000
changeset 770 57ce1ba6d1cb
parent 753 25a77484ec72
child 773 340bc29da9a0
permissions -rw-r--r--
Fixed Standard Flags finally
     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 	indentPerDepth="  ";
    18 	bool ok;
    19     tmpDir.setPath (makeTmpDir(ok,"vym-export"));
    20 	if (!tmpDir.exists() || !ok)
    21 		QMessageBox::critical( 0, QObject::tr( "Error" ),
    22 					   QObject::tr("Couldn't access temporary directory\n"));
    23 	cancelFlag=false;				   
    24 }
    25 
    26 ExportBase::~ExportBase()
    27 {
    28 	// Cleanup tmpdir
    29 	removeDir (tmpDir);
    30 }
    31 
    32 void ExportBase::setDir(const QDir &d)
    33 {
    34 	outDir=d;
    35 }
    36 
    37 void ExportBase::setFile (const QString &p)
    38 {
    39 	outputFile=p;
    40 }
    41 
    42 QString ExportBase::getFile ()
    43 {
    44 	return outputFile;
    45 }
    46 
    47 void ExportBase::setModel(VymModel *m)
    48 {
    49 	model=m;
    50 }
    51 
    52 void ExportBase::setCaption (const QString &s)
    53 {
    54 	caption=s;
    55 }
    56 
    57 void ExportBase::addFilter(const QString &s)
    58 {
    59 	filter=s;
    60 }
    61 
    62 bool ExportBase::execDialog()
    63 {
    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 	TreeItem *ti=bostart->getTreeItem();
   114 	int depth=ti->depth();
   115 	while (depth>0)
   116 	{
   117 		r=QString("%1").arg(1+ti->num(),0,10)+"." + r;
   118 		ti=ti->parent(); 
   119 		depth=ti->depth();
   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()	//FIXME-1 segfaults...
   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 	BranchItem *cur=NULL;
   149 	BranchItem *prev=NULL;
   150 	int d;
   151 
   152 	BranchObj *bo;	//FIXME-3 still needed?
   153 	cur=model->next (cur,prev,d);
   154 	while (cur) 
   155 	{
   156 		if (cur->getType()==TreeItem::Branch || cur->getType()==TreeItem::MapCenter)
   157 		{
   158 			bo=(BranchObj*)(cur->getLMO());
   159 			std::cout << "ExportASCII::  "<<cur->getHeading().toStdString()<<std::endl;
   160 
   161 			// Make indentstring
   162 			curIndent="";
   163 			for (i=0;i<cur->depth()-1;i++) curIndent+= indentPerDepth;
   164 
   165 			if (!cur->hasHiddenExportParent() )
   166 			{
   167 				switch (cur->depth())
   168 				{
   169 					case 0:
   170 						ts << underline (cur->getHeading(),QString("="));
   171 						ts << "\n";
   172 						break;
   173 					case 1:
   174 						ts << "\n";
   175 						ts << (underline (getSectionString(bo) + cur->getHeading(), QString("-") ) );
   176 						ts << "\n";
   177 						break;
   178 					case 2:
   179 						ts << "\n";
   180 						ts << (curIndent + "* " + cur->getHeading());
   181 						ts << "\n";
   182 						break;
   183 					case 3:
   184 						ts << (curIndent + "- " + cur->getHeading());
   185 						ts << "\n";
   186 						break;
   187 					default:
   188 						ts << (curIndent + "- " + cur->getHeading());
   189 						ts << "\n";
   190 						break;
   191 				}
   192 
   193 				// If necessary, write note
   194 				if (!cur->getNoteObj().isEmpty())
   195 				{
   196 					curIndent +="  | ";
   197 					s=cur->getNoteASCII( curIndent, 80);
   198 					ts << s;
   199 				}
   200 			}
   201 		}
   202 		cur=model->next(cur,prev,d);
   203 	}
   204 	file.close();
   205 }
   206 
   207 QString ExportASCII::underline (const QString &text, const QString &line)
   208 {
   209 	QString r=text + "\n";
   210 	for (int j=0;j<text.length();j++) r+=line;
   211 	return r;
   212 }
   213 
   214 
   215 ////////////////////////////////////////////////////////////////////////
   216 void ExportCSV::doExport()
   217 {
   218 	QFile file (outputFile);
   219 	if ( !file.open( QIODevice::WriteOnly ) )
   220 	{
   221 		qWarning ("ExportBase::exportXML  couldn't open "+outputFile);
   222 		return;
   223 	}
   224 	QTextStream ts( &file );	// use LANG decoding here...
   225 
   226 	// Write header
   227 	ts << "\"Note\""  <<endl;
   228 
   229 	// Main loop over all branches
   230 	QString s;
   231 	QString curIndent("");
   232 	int i;
   233 	BranchObj *bo;  //FIXME-3 still needed?
   234 	BranchItem *cur=NULL;
   235 	BranchItem *prev=NULL;
   236 	int d;
   237 	cur=model->next (cur,prev,d);
   238 	while (cur) 
   239 	{
   240 		bo=(BranchObj*)(cur->getLMO());
   241 
   242 		if (!cur->hasHiddenExportParent() )
   243 		{
   244 			// If necessary, write note
   245 			if (!cur->getNoteObj().isEmpty())
   246 			{
   247 				s =cur->getNoteASCII();
   248 				s=s.replace ("\n","\n"+curIndent);
   249 				ts << ("\""+s+"\",");
   250 			} else
   251 				ts <<"\"\",";
   252 
   253 			// Make indentstring
   254 			for (i=0;i<cur->depth();i++) curIndent+= "\"\",";
   255 
   256 			// Write heading
   257 			ts << curIndent << "\"" << cur->getHeading()<<"\""<<endl;
   258 		}
   259 		
   260 		cur=model->next(cur,prev,d);
   261 		curIndent="";
   262 	}
   263 	file.close();
   264 }
   265 
   266 ////////////////////////////////////////////////////////////////////////
   267 void ExportKDE3Bookmarks::doExport() 
   268 {
   269 	WarningDialog dia;
   270 	dia.showCancelButton (true);
   271 	dia.setText(QObject::tr("Exporting the %1 bookmarks will overwrite\nyour existing bookmarks file.").arg("KDE"));
   272 	dia.setCaption(QObject::tr("Warning: Overwriting %1 bookmarks").arg("KDE 3"));
   273 	dia.setShowAgainName("/exports/KDE/overwriteKDEBookmarks");
   274 	if (dia.exec()==QDialog::Accepted)
   275 	{
   276 		model->exportXML(tmpDir.path(),false);
   277 
   278 		XSLTProc p;
   279 		p.setInputFile (tmpDir.path()+"/"+model->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->start( ub);
   287 		if (!proc->waitForStarted())
   288 		{
   289 			QMessageBox::warning(0, 
   290 				QObject::tr("Warning"),
   291 				QObject::tr("Couldn't find script %1\nto notifiy Browsers of changed bookmarks.").arg(ub));
   292 		}	
   293 	}
   294 }
   295 
   296 ////////////////////////////////////////////////////////////////////////
   297 void ExportKDE4Bookmarks::doExport() 
   298 {
   299 	WarningDialog dia;
   300 	dia.showCancelButton (true);
   301 	dia.setText(QObject::tr("Exporting the %1 bookmarks will overwrite\nyour existing bookmarks file.").arg("KDE 4"));
   302 	dia.setCaption(QObject::tr("Warning: Overwriting %1 bookmarks").arg("KDE"));
   303 	dia.setShowAgainName("/exports/KDE/overwriteKDEBookmarks");
   304 	if (dia.exec()==QDialog::Accepted)
   305 	{
   306 		model->exportXML(tmpDir.path(),false);
   307 
   308 		XSLTProc p;
   309 		p.setInputFile (tmpDir.path()+"/"+model->getMapName()+".xml");
   310 		p.setOutputFile (tmpDir.home().path()+"/.kde4/share/apps/konqueror/bookmarks.xml");
   311 		p.setXSLFile (vymBaseDir.path()+"/styles/vym2kdebookmarks.xsl");
   312 		p.process();
   313 
   314 		QString ub=vymBaseDir.path()+"/scripts/update-bookmarks";
   315 		QProcess *proc= new QProcess ;
   316 		proc->start( ub);
   317 		if (!proc->waitForStarted())
   318 		{
   319 			QMessageBox::warning(0, 
   320 				QObject::tr("Warning"),
   321 				QObject::tr("Couldn't find script %1\nto notifiy Browsers of changed bookmarks.").arg(ub));
   322 		}	
   323 	}
   324 }
   325 
   326 ////////////////////////////////////////////////////////////////////////
   327 void ExportFirefoxBookmarks::doExport() 
   328 {
   329 	WarningDialog dia;
   330 	dia.showCancelButton (true);
   331 	dia.setText(QObject::tr("Exporting the %1 bookmarks will overwrite\nyour existing bookmarks file.").arg("Firefox"));
   332 	dia.setCaption(QObject::tr("Warning: Overwriting %1 bookmarks").arg("Firefox"));
   333 	dia.setShowAgainName("/vym/warnings/overwriteImportBookmarks");
   334 	if (dia.exec()==QDialog::Accepted)
   335 	{
   336 		model->exportXML(tmpDir.path(),false);
   337 
   338 /*
   339 		XSLTProc p;
   340 		p.setInputFile (tmpDir.path()+"/"+me->getMapName()+".xml");
   341 		p.setOutputFile (tmpDir.home().path()+"/.kde/share/apps/konqueror/bookmarks.xml");
   342 		p.setXSLFile (vymBaseDir.path()+"/styles/vym2kdebookmarks.xsl");
   343 		p.process();
   344 
   345 		QString ub=vymBaseDir.path()+"/scripts/update-bookmarks";
   346 		QProcess *proc = new QProcess( );
   347 		proc->addArgument(ub);
   348 
   349 		if ( !proc->start() ) 
   350 		{
   351 			QMessageBox::warning(0, 
   352 				QObject::tr("Warning"),
   353 				QObject::tr("Couldn't find script %1\nto notifiy Browsers of changed bookmarks.").arg(ub));
   354 		}	
   355 
   356 */
   357 	}
   358 }
   359 
   360 ////////////////////////////////////////////////////////////////////////
   361 void ExportTaskjuggler::doExport() 
   362 {
   363 	model->exportXML(tmpDir.path(),false);
   364 
   365 	XSLTProc p;
   366 	p.setInputFile (tmpDir.path()+"/"+model->getMapName()+".xml");
   367 	p.setOutputFile (outputFile);
   368 	p.setXSLFile (vymBaseDir.path()+"/styles/vym2taskjuggler.xsl");
   369 	p.process();
   370 }
   371 
   372 ////////////////////////////////////////////////////////////////////////
   373 void ExportLaTeX::doExport() 
   374 {
   375 	// Exports a map to a LaTex file.  
   376 	// This file needs to be included 
   377 	// or inported into a LaTex document
   378 	// it will not add a preamble, or anything 
   379 	// that makes a full LaTex document.
   380   QFile file (outputFile);
   381   if ( !file.open( QIODevice::WriteOnly ) ) {
   382 	QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not write %1").arg(outputFile));
   383 	mainWindow->statusMessage(QString(QObject::tr("Export failed.")));
   384     return;
   385   }
   386   QTextStream ts( &file );	// use LANG decoding here...
   387   ts.setEncoding (QTextStream::UnicodeUTF8); // Force UTF8
   388   
   389   // Main loop over all branches
   390   QString s;
   391   // QString curIndent("");
   392   // int i;
   393   BranchObj *bo;
   394   BranchItem *cur=NULL;
   395   BranchItem *prev=NULL;
   396   int d;  //FIXME-3 still needed?
   397   model->next(cur,prev,d);
   398   while (cur) 
   399   {
   400 	bo=(BranchObj*)(cur->getLMO());
   401 
   402 	if (!cur->hasHiddenExportParent() )
   403 	{
   404 		switch (cur->depth() ) 
   405 		{
   406 			case 0: break;
   407 			case 1: 
   408 			  ts << ("\\chapter{" + cur->getHeading()+ "}\n");
   409 			  break;
   410 			case 2: 
   411 			  ts << ("\\section{" + cur->getHeading()+ "}\n");
   412 			  break;
   413 			case 3: 
   414 			  ts << ("\\subsection{" + cur->getHeading()+ "}\n");
   415 			  break;
   416 			case 4: 
   417 			  ts << ("\\subsubsection{" + cur->getHeading()+ "}\n");
   418 			  break;
   419 			default:
   420 			  ts << ("\\paragraph*{" + cur->getHeading()+ "}\n");
   421 			
   422 		}
   423 		// If necessary, write note
   424 		if (!cur->getNoteObj().isEmpty()) {
   425 		  ts << (cur->getNoteASCII());
   426 		  ts << ("\n");
   427 		}
   428 	}
   429     cur=model->next(cur,prev,d);
   430    }
   431   file.close();
   432 }
   433 
   434 ////////////////////////////////////////////////////////////////////////
   435 ExportOO::ExportOO()
   436 {
   437 	useSections=false;
   438 }
   439 
   440 ExportOO::~ExportOO()
   441 {
   442 }	
   443 
   444 QString ExportOO::buildList (TreeItem *current)
   445 {
   446     QString r;
   447 
   448     uint i=0;
   449 	BranchItem *bi=current->getFirstBranch();
   450 	if (bi)
   451     {
   452 		if (true) //if (!bo->hasHiddenExportParent() )	// FIXME-2 use BranchItem...
   453 		{
   454 			// Start list
   455 			r+="<text:list text:style-name=\"vym-list\">\n";
   456 			while (bi)
   457 			{
   458 				r+="<text:list-item><text:p >";
   459 				r+=quotemeta(bi->getHeading());
   460 				// If necessary, write note
   461 				if (!bi->getNoteObj().isEmpty())
   462 					r+=bi->getNoteOpenDoc();
   463 				r+="</text:p>";
   464 				r+=buildList (bi);	// recursivly add deeper branches
   465 				r+="</text:list-item>\n";
   466 				i++;
   467 				bi=current->getBranchNum(i);
   468 			}
   469 			r+="</text:list>\n";
   470 		}
   471     }
   472     return r;
   473 }
   474 
   475 
   476 void ExportOO::exportPresentation()
   477 {
   478 	QString allPages;
   479 
   480 /* FIXME-2 not adapted to multiple mapCenters yet, see patch already done in 1.12.2...
   481 	// Insert new content
   482 	content.replace ("<!-- INSERT TITLE -->",quotemeta(mapCenter->getHeading()));
   483 	content.replace ("<!-- INSERT AUTHOR -->",quotemeta(mapCenter->getAuthor()));
   484 
   485 	QString	onePage;
   486 	QString list;
   487 	
   488 	BranchObj *sectionBO=mapCenter->getFirstBranch();
   489     int i=0;
   490 	BranchObj *pagesBO;
   491     int j=0;
   492 
   493 	// Walk sections
   494 	while (sectionBO && !sectionBO->hasHiddenExportParent() )
   495 	{
   496 		if (useSections)
   497 		{
   498 			// Add page with section title
   499 			onePage=sectionTemplate;
   500 			onePage.replace ("<!-- INSERT PAGE HEADING -->", quotemeta(sectionBO->getHeading() ) );
   501 			allPages+=onePage;
   502 		} else
   503 		{
   504 			i=-2;	// only use inner loop to 
   505 			        // turn mainbranches into pages
   506 			sectionBO=mapCenter;
   507 		}
   508 
   509 		// Walk mainpages
   510 		pagesBO=sectionBO->getFirstBranch();
   511 		j=0;
   512 		while (pagesBO && !pagesBO->hasHiddenExportParent() )
   513 		{
   514 			// Add page with list of items
   515 			onePage=pageTemplate;
   516 			onePage.replace ("<!-- INSERT PAGE HEADING -->", quotemeta (pagesBO->getHeading() ) );
   517 			list=buildList (pagesBO->getTreeItem() );  
   518 			onePage.replace ("<!-- INSERT LIST -->", list);
   519 			allPages+=onePage;
   520 			j++;
   521 			pagesBO=sectionBO->getBranchNum(j);
   522 		}
   523 		i++;
   524 		sectionBO=mapCenter->getBranchNum(i);
   525 	}
   526 	
   527 	content.replace ("<!-- INSERT PAGES -->",allPages);
   528 
   529 	// Write modified content
   530 	QFile f (contentFile);
   531     if ( !f.open( QIODevice::WriteOnly ) ) 
   532 	{
   533 		QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not write %1").arg(contentFile));
   534 		mainWindow->statusMessage(QString(QObject::tr("Export failed.")));
   535 		return;
   536     }
   537 
   538     QTextStream t( &f );
   539     t << content;
   540     f.close();
   541 
   542 	// zip tmpdir to destination
   543 	zipDir (tmpDir,outputFile);	
   544 */
   545 }
   546 
   547 bool ExportOO::setConfigFile (const QString &cf)
   548 {
   549 	configFile=cf;
   550 	int i=cf.findRev ("/");
   551 	if (i>=0) configDir=cf.left(i);
   552 	SimpleSettings set;
   553 	set.readSettings(configFile);
   554 
   555 	// set paths
   556 	templateDir=configDir+"/"+set.readEntry ("Template");
   557 
   558 	QDir d (templateDir);
   559 	if (!d.exists())
   560 	{
   561 		QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Check \"%1\" in\n%2").arg("Template="+set.readEntry ("Template")).arg(configFile));
   562 		return false;
   563 
   564 	}
   565 
   566 	contentTemplateFile=templateDir+"content-template.xml";
   567 	contentFile=tmpDir.path()+"/content.xml";
   568 	pageTemplateFile=templateDir+"page-template.xml";
   569 	sectionTemplateFile=templateDir+"section-template.xml";
   570 
   571 	if (set.readEntry("useSections").contains("yes"))
   572 		useSections=true;
   573 
   574 	// Copy template to tmpdir
   575 	system ("cp -r "+templateDir+"* "+tmpDir.path());
   576 
   577 	// Read content-template
   578 	if (!loadStringFromDisk (contentTemplateFile,content))
   579 	{
   580 		QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not read %1").arg(contentTemplateFile));
   581 		return false;
   582 	}
   583 
   584 	// Read page-template
   585 	if (!loadStringFromDisk (pageTemplateFile,pageTemplate))
   586 	{
   587 		QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not read %1").arg(pageTemplateFile));
   588 		return false;
   589 	}
   590 	
   591 	// Read section-template
   592 	if (useSections && !loadStringFromDisk (sectionTemplateFile,sectionTemplate))
   593 	{
   594 		QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not read %1").arg(sectionTemplateFile));
   595 		return false;
   596 	}
   597 	return true;
   598 }
   599