exports.cpp
author insilmaril
Thu, 26 Mar 2009 07:50:32 +0000
changeset 747 008e72977ab8
parent 746 ee6b0f3a4c2f
child 749 9ff332964015
permissions -rw-r--r--
Notes work again (to some degree)
     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::setModel(VymModel *m)
    46 {
    47 	model=m;
    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 	{
    63 		QFileDialog *fd=new QFileDialog( 0, caption);
    64 		fd->setFilter (filter);
    65 		fd->setCaption(caption);
    66 		fd->setMode( QFileDialog::AnyFile );
    67 		fd->setDir (outDir);
    68 		fd->show();
    69 
    70 		if ( fd->exec() == QDialog::Accepted )
    71 		{
    72 			if (QFile (fd->selectedFile()).exists() )
    73 			{
    74 				QMessageBox mb( vymName,
    75 					QObject::tr("The file %1 exists already.\nDo you want to overwrite it?").arg(fd->selectedFile()), 
    76 				QMessageBox::Warning,
    77 				QMessageBox::Yes | QMessageBox::Default,
    78 				QMessageBox::Cancel | QMessageBox::Escape,
    79 				Qt::NoButton );
    80 				mb.setButtonText( QMessageBox::Yes, QObject::tr("Overwrite") );
    81 				mb.setButtonText( QMessageBox::No, QObject::tr("Cancel"));
    82 				ExportBase ex;
    83 				switch( mb.exec() ) 
    84 				{
    85 					case QMessageBox::Yes:
    86 						// save 
    87 						break;;
    88 					case QMessageBox::Cancel:
    89 						cancelFlag=true;
    90 						return false;
    91 						break;
    92 				}
    93 			}
    94 			outputFile=fd->selectedFile();
    95 			cancelFlag=false;
    96 			return true;
    97 		}
    98 	}
    99 	return false;
   100 }
   101 
   102 bool ExportBase::canceled()
   103 {
   104 	return cancelFlag;
   105 }
   106 
   107 QString ExportBase::getSectionString(BranchObj *bostart)
   108 {
   109 	// Make prefix like "2.5.3" for "bo:2,bo:5,bo:3"
   110 	QString r;
   111 	BranchObj *bo=bostart;
   112 	int depth=bo->getDepth();
   113 	while (depth>0)
   114 	{
   115 		r=QString("%1").arg(1+bo->getNum(),0,10)+"." + r;
   116 		bo=(BranchObj*)(bo->getParObj());
   117 		depth=bo->getDepth();
   118 	}	
   119 	if (r.isEmpty())
   120 		return r;
   121 	else	
   122 		return r + " ";
   123 }
   124 
   125 ////////////////////////////////////////////////////////////////////////
   126 ExportASCII::ExportASCII()
   127 {
   128 	filter="TXT (*.txt)";
   129 	caption=vymName+ " -" +QObject::tr("Export as ASCII")+" "+QObject::tr("(still experimental)");
   130 }
   131 
   132 void ExportASCII::doExport()	//FIXME-1 segfaults...
   133 {
   134 	QFile file (outputFile);
   135 	if ( !file.open( QIODevice::WriteOnly ) )
   136 	{
   137 		qWarning ("ExportBase::exportXML  couldn't open "+outputFile);
   138 		return;
   139 	}
   140 	QTextStream ts( &file );	// use LANG decoding here...
   141 
   142 	// Main loop over all branches
   143 	QString s;
   144 	QString curIndent;
   145 	int i;
   146 	TreeItem *cur=NULL;
   147 	TreeItem *prev=NULL;
   148 	int d;
   149 
   150 	BranchObj *bo;
   151 	cur=model->next (cur,prev,d);
   152 	while (cur) 
   153 	{
   154 		if (cur->getType()==TreeItem::Branch || cur->getType()==TreeItem::MapCenter)
   155 		{
   156 			bo=(BranchObj*)(cur->getLMO());
   157 			std::cout << "ExportASCII::  "<<cur->getHeading().toStdString()<<std::endl;
   158 
   159 			// Make indentstring
   160 			curIndent="";
   161 			for (i=0;i<cur->depth()-1;i++) curIndent+= indentPerDepth;
   162 
   163 			if (!bo->hasHiddenExportParent() )
   164 			{
   165 				switch (cur->depth())
   166 				{
   167 					case 0:
   168 						ts << underline (cur->getHeading(),QString("="));
   169 						ts << "\n";
   170 						break;
   171 					case 1:
   172 						ts << "\n";
   173 						ts << (underline (getSectionString(bo) + cur->getHeading(), QString("-") ) );
   174 						ts << "\n";
   175 						break;
   176 					case 2:
   177 						ts << "\n";
   178 						ts << (curIndent + "* " + cur->getHeading());
   179 						ts << "\n";
   180 						break;
   181 					case 3:
   182 						ts << (curIndent + "- " + cur->getHeading());
   183 						ts << "\n";
   184 						break;
   185 					default:
   186 						ts << (curIndent + "- " + cur->getHeading());
   187 						ts << "\n";
   188 						break;
   189 				}
   190 
   191 				// If necessary, write note
   192 				if (!cur->getNoteObj().isEmpty())
   193 				{
   194 					curIndent +="  | ";
   195 					s=cur->getNoteASCII( curIndent, 80);
   196 					ts << s;
   197 				}
   198 			}
   199 		}
   200 		cur=model->next(cur,prev,d);
   201 	}
   202 	file.close();
   203 }
   204 
   205 QString ExportASCII::underline (const QString &text, const QString &line)
   206 {
   207 	QString r=text + "\n";
   208 	for (int j=0;j<text.length();j++) r+=line;
   209 	return r;
   210 }
   211 
   212 
   213 ////////////////////////////////////////////////////////////////////////
   214 void ExportCSV::doExport()
   215 {
   216 	QFile file (outputFile);
   217 	if ( !file.open( QIODevice::WriteOnly ) )
   218 	{
   219 		qWarning ("ExportBase::exportXML  couldn't open "+outputFile);
   220 		return;
   221 	}
   222 	QTextStream ts( &file );	// use LANG decoding here...
   223 
   224 	// Write header
   225 	ts << "\"Note\""  <<endl;
   226 
   227 	// Main loop over all branches
   228 	QString s;
   229 	QString curIndent("");
   230 	int i;
   231 	BranchObj *bo;
   232 	TreeItem *cur=NULL;
   233 	TreeItem *prev=NULL;
   234 	int d;
   235 	cur=model->next (cur,prev,d);
   236 	while (cur) 
   237 	{
   238 		bo=(BranchObj*)(cur->getLMO());
   239 
   240 		if (!bo->hasHiddenExportParent() )
   241 		{
   242 			// If necessary, write note
   243 			if (!cur->getNoteObj().isEmpty())
   244 			{
   245 				s =cur->getNoteASCII();
   246 				s=s.replace ("\n","\n"+curIndent);
   247 				ts << ("\""+s+"\",");
   248 			} else
   249 				ts <<"\"\",";
   250 
   251 			// Make indentstring
   252 			for (i=0;i<cur->depth();i++) curIndent+= "\"\",";
   253 
   254 			// Write heading
   255 			ts << curIndent << "\"" << cur->getHeading()<<"\""<<endl;
   256 		}
   257 		
   258 		cur=model->next(cur,prev,d);
   259 		curIndent="";
   260 	}
   261 	file.close();
   262 }
   263 
   264 ////////////////////////////////////////////////////////////////////////
   265 void ExportKDE3Bookmarks::doExport() 
   266 {
   267 	WarningDialog dia;
   268 	dia.showCancelButton (true);
   269 	dia.setText(QObject::tr("Exporting the %1 bookmarks will overwrite\nyour existing bookmarks file.").arg("KDE"));
   270 	dia.setCaption(QObject::tr("Warning: Overwriting %1 bookmarks").arg("KDE 3"));
   271 	dia.setShowAgainName("/exports/KDE/overwriteKDEBookmarks");
   272 	if (dia.exec()==QDialog::Accepted)
   273 	{
   274 		model->exportXML(tmpDir.path(),false);
   275 
   276 		XSLTProc p;
   277 		p.setInputFile (tmpDir.path()+"/"+model->getMapName()+".xml");
   278 		p.setOutputFile (tmpDir.home().path()+"/.kde/share/apps/konqueror/bookmarks.xml");
   279 		p.setXSLFile (vymBaseDir.path()+"/styles/vym2kdebookmarks.xsl");
   280 		p.process();
   281 
   282 		QString ub=vymBaseDir.path()+"/scripts/update-bookmarks";
   283 		QProcess *proc= new QProcess ;
   284 		proc->start( ub);
   285 		if (!proc->waitForStarted())
   286 		{
   287 			QMessageBox::warning(0, 
   288 				QObject::tr("Warning"),
   289 				QObject::tr("Couldn't find script %1\nto notifiy Browsers of changed bookmarks.").arg(ub));
   290 		}	
   291 	}
   292 }
   293 
   294 ////////////////////////////////////////////////////////////////////////
   295 void ExportKDE4Bookmarks::doExport() 
   296 {
   297 	WarningDialog dia;
   298 	dia.showCancelButton (true);
   299 	dia.setText(QObject::tr("Exporting the %1 bookmarks will overwrite\nyour existing bookmarks file.").arg("KDE 4"));
   300 	dia.setCaption(QObject::tr("Warning: Overwriting %1 bookmarks").arg("KDE"));
   301 	dia.setShowAgainName("/exports/KDE/overwriteKDEBookmarks");
   302 	if (dia.exec()==QDialog::Accepted)
   303 	{
   304 		model->exportXML(tmpDir.path(),false);
   305 
   306 		XSLTProc p;
   307 		p.setInputFile (tmpDir.path()+"/"+model->getMapName()+".xml");
   308 		p.setOutputFile (tmpDir.home().path()+"/.kde4/share/apps/konqueror/bookmarks.xml");
   309 		p.setXSLFile (vymBaseDir.path()+"/styles/vym2kdebookmarks.xsl");
   310 		p.process();
   311 
   312 		QString ub=vymBaseDir.path()+"/scripts/update-bookmarks";
   313 		QProcess *proc= new QProcess ;
   314 		proc->start( ub);
   315 		if (!proc->waitForStarted())
   316 		{
   317 			QMessageBox::warning(0, 
   318 				QObject::tr("Warning"),
   319 				QObject::tr("Couldn't find script %1\nto notifiy Browsers of changed bookmarks.").arg(ub));
   320 		}	
   321 	}
   322 }
   323 
   324 ////////////////////////////////////////////////////////////////////////
   325 void ExportFirefoxBookmarks::doExport() 
   326 {
   327 	WarningDialog dia;
   328 	dia.showCancelButton (true);
   329 	dia.setText(QObject::tr("Exporting the %1 bookmarks will overwrite\nyour existing bookmarks file.").arg("Firefox"));
   330 	dia.setCaption(QObject::tr("Warning: Overwriting %1 bookmarks").arg("Firefox"));
   331 	dia.setShowAgainName("/vym/warnings/overwriteImportBookmarks");
   332 	if (dia.exec()==QDialog::Accepted)
   333 	{
   334 		model->exportXML(tmpDir.path(),false);
   335 
   336 /*
   337 		XSLTProc p;
   338 		p.setInputFile (tmpDir.path()+"/"+me->getMapName()+".xml");
   339 		p.setOutputFile (tmpDir.home().path()+"/.kde/share/apps/konqueror/bookmarks.xml");
   340 		p.setXSLFile (vymBaseDir.path()+"/styles/vym2kdebookmarks.xsl");
   341 		p.process();
   342 
   343 		QString ub=vymBaseDir.path()+"/scripts/update-bookmarks";
   344 		QProcess *proc = new QProcess( );
   345 		proc->addArgument(ub);
   346 
   347 		if ( !proc->start() ) 
   348 		{
   349 			QMessageBox::warning(0, 
   350 				QObject::tr("Warning"),
   351 				QObject::tr("Couldn't find script %1\nto notifiy Browsers of changed bookmarks.").arg(ub));
   352 		}	
   353 
   354 */
   355 	}
   356 }
   357 
   358 ////////////////////////////////////////////////////////////////////////
   359 void ExportTaskjuggler::doExport() 
   360 {
   361 	model->exportXML(tmpDir.path(),false);
   362 
   363 	XSLTProc p;
   364 	p.setInputFile (tmpDir.path()+"/"+model->getMapName()+".xml");
   365 	p.setOutputFile (outputFile);
   366 	p.setXSLFile (vymBaseDir.path()+"/styles/vym2taskjuggler.xsl");
   367 	p.process();
   368 }
   369 
   370 ////////////////////////////////////////////////////////////////////////
   371 void ExportLaTeX::doExport() 
   372 {
   373 	// Exports a map to a LaTex file.  
   374 	// This file needs to be included 
   375 	// or inported into a LaTex document
   376 	// it will not add a preamble, or anything 
   377 	// that makes a full LaTex document.
   378   QFile file (outputFile);
   379   if ( !file.open( QIODevice::WriteOnly ) ) {
   380 	QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not write %1").arg(outputFile));
   381 	mainWindow->statusMessage(QString(QObject::tr("Export failed.")));
   382     return;
   383   }
   384   QTextStream ts( &file );	// use LANG decoding here...
   385   ts.setEncoding (QTextStream::UnicodeUTF8); // Force UTF8
   386   
   387   // Main loop over all branches
   388   QString s;
   389   // QString curIndent("");
   390   // int i;
   391   BranchObj *bo;
   392   TreeItem *cur=NULL;
   393   TreeItem *prev=NULL;
   394   int d;
   395   model->next(cur,prev,d);
   396   while (cur) 
   397   {
   398 	bo=(BranchObj*)(cur->getLMO());
   399 
   400 	if (!bo->hasHiddenExportParent() )
   401 	{
   402 		switch (cur->depth() ) 
   403 		{
   404 			case 0: break;
   405 			case 1: 
   406 			  ts << ("\\chapter{" + bo->getHeading()+ "}\n");
   407 			  break;
   408 			case 2: 
   409 			  ts << ("\\section{" + bo->getHeading()+ "}\n");
   410 			  break;
   411 			case 3: 
   412 			  ts << ("\\subsection{" + bo->getHeading()+ "}\n");
   413 			  break;
   414 			case 4: 
   415 			  ts << ("\\subsubsection{" + bo->getHeading()+ "}\n");
   416 			  break;
   417 			default:
   418 			  ts << ("\\paragraph*{" + bo->getHeading()+ "}\n");
   419 			
   420 		}
   421 		// If necessary, write note
   422 		if (!cur->getNoteObj().isEmpty()) {
   423 		  ts << (cur->getNoteASCII());
   424 		  ts << ("\n");
   425 		}
   426 	}
   427     cur=model->next(cur,prev,d);
   428    }
   429   file.close();
   430 }
   431 
   432 ////////////////////////////////////////////////////////////////////////
   433 ExportOO::ExportOO()
   434 {
   435 	useSections=false;
   436 }
   437 
   438 ExportOO::~ExportOO()
   439 {
   440 }	
   441 
   442 QString ExportOO::buildList (BranchObj *current)
   443 {
   444     QString r;
   445     BranchObj *bo;
   446 
   447     uint i=0;
   448     bo=current->getFirstBranch();
   449 	TreeItem *ti=bo->getTreeItem();
   450     if (bo)
   451     {
   452 		if (!bo->hasHiddenExportParent() )
   453 		{
   454 			// Start list
   455 			r+="<text:list text:style-name=\"vym-list\">\n";
   456 			while (bo)
   457 			{
   458 				r+="<text:list-item><text:p >";
   459 				r+=quotemeta(bo->getHeading());
   460 				// If necessary, write note
   461 				if (!ti->getNoteObj().isEmpty())
   462 					r+=ti->getNoteOpenDoc();
   463 				r+="</text:p>";
   464 				r+=buildList (bo);	// recursivly add deeper branches
   465 				r+="</text:list-item>\n";
   466 				i++;
   467 				bo=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);
   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