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