exports.cpp
author insilmaril
Wed, 22 Jul 2009 11:48:23 +0000
changeset 781 6cbf3c9cbd21
parent 777 8acac4fade1b
child 791 f1006de05c54
permissions -rw-r--r--
Empty branches are always unscrolled after import, relinking to scrolled branch doesn't open branch, starting DBUS implementation
     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()	
   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 	BranchObj *bo;	//FIXME-3 still needed?
   152 	cur=model->next (cur,prev);
   153 	while (cur) 
   154 	{
   155 		if (cur->getType()==TreeItem::Branch || cur->getType()==TreeItem::MapCenter)
   156 		{
   157 			bo=(BranchObj*)(cur->getLMO());
   158 			std::cout << "ExportASCII::  "<<cur->getHeading().toStdString()<<std::endl;
   159 
   160 			// Make indentstring
   161 			curIndent="";
   162 			for (i=0;i<cur->depth()-1;i++) curIndent+= indentPerDepth;
   163 
   164 			if (!cur->hasHiddenExportParent() )
   165 			{
   166 				switch (cur->depth())
   167 				{
   168 					case 0:
   169 						ts << underline (cur->getHeading(),QString("="));
   170 						ts << "\n";
   171 						break;
   172 					case 1:
   173 						ts << "\n";
   174 						ts << (underline (getSectionString(bo) + cur->getHeading(), QString("-") ) );
   175 						ts << "\n";
   176 						break;
   177 					case 2:
   178 						ts << "\n";
   179 						ts << (curIndent + "* " + cur->getHeading());
   180 						ts << "\n";
   181 						break;
   182 					case 3:
   183 						ts << (curIndent + "- " + cur->getHeading());
   184 						ts << "\n";
   185 						break;
   186 					default:
   187 						ts << (curIndent + "- " + cur->getHeading());
   188 						ts << "\n";
   189 						break;
   190 				}
   191 
   192 				// If necessary, write note
   193 				if (!cur->getNoteObj().isEmpty())
   194 				{
   195 					curIndent +="  | ";
   196 					s=cur->getNoteASCII( curIndent, 80);
   197 					ts << s;
   198 				}
   199 			}
   200 		}
   201 		cur=model->next(cur,prev);
   202 	}
   203 	file.close();
   204 }
   205 
   206 QString ExportASCII::underline (const QString &text, const QString &line)
   207 {
   208 	QString r=text + "\n";
   209 	for (int j=0;j<text.length();j++) r+=line;
   210 	return r;
   211 }
   212 
   213 
   214 ////////////////////////////////////////////////////////////////////////
   215 void ExportCSV::doExport()
   216 {
   217 	QFile file (outputFile);
   218 	if ( !file.open( QIODevice::WriteOnly ) )
   219 	{
   220 		qWarning ("ExportBase::exportXML  couldn't open "+outputFile);
   221 		return;
   222 	}
   223 	QTextStream ts( &file );	// use LANG decoding here...
   224 
   225 	// Write header
   226 	ts << "\"Note\""  <<endl;
   227 
   228 	// Main loop over all branches
   229 	QString s;
   230 	QString curIndent("");
   231 	int i;
   232 	BranchObj *bo;  //FIXME-3 still needed?
   233 	BranchItem *cur=NULL;
   234 	BranchItem *prev=NULL;
   235 	cur=model->next (cur,prev);
   236 	while (cur) 
   237 	{
   238 		bo=(BranchObj*)(cur->getLMO());
   239 
   240 		if (!cur->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);
   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   BranchItem *cur=NULL;
   393   BranchItem *prev=NULL;
   394   model->next(cur,prev);
   395   while (cur) 
   396   {
   397 	bo=(BranchObj*)(cur->getLMO());
   398 
   399 	if (!cur->hasHiddenExportParent() )
   400 	{
   401 		switch (cur->depth() ) 
   402 		{
   403 			case 0: break;
   404 			case 1: 
   405 			  ts << ("\\chapter{" + cur->getHeading()+ "}\n");
   406 			  break;
   407 			case 2: 
   408 			  ts << ("\\section{" + cur->getHeading()+ "}\n");
   409 			  break;
   410 			case 3: 
   411 			  ts << ("\\subsection{" + cur->getHeading()+ "}\n");
   412 			  break;
   413 			case 4: 
   414 			  ts << ("\\subsubsection{" + cur->getHeading()+ "}\n");
   415 			  break;
   416 			default:
   417 			  ts << ("\\paragraph*{" + cur->getHeading()+ "}\n");
   418 			
   419 		}
   420 		// If necessary, write note
   421 		if (!cur->getNoteObj().isEmpty()) {
   422 		  ts << (cur->getNoteASCII());
   423 		  ts << ("\n");
   424 		}
   425 	}
   426     cur=model->next(cur,prev);
   427    }
   428   file.close();
   429 }
   430 
   431 ////////////////////////////////////////////////////////////////////////
   432 ExportOO::ExportOO()
   433 {
   434 	useSections=false;
   435 }
   436 
   437 ExportOO::~ExportOO()
   438 {
   439 }	
   440 
   441 QString ExportOO::buildList (TreeItem *current)
   442 {
   443     QString r;
   444 
   445     uint i=0;
   446 	BranchItem *bi=current->getFirstBranch();
   447 	if (bi)
   448     {
   449 		if (true) //if (!bo->hasHiddenExportParent() )	// FIXME-2 use BranchItem...
   450 		{
   451 			// Start list
   452 			r+="<text:list text:style-name=\"vym-list\">\n";
   453 			while (bi)
   454 			{
   455 				r+="<text:list-item><text:p >";
   456 				r+=quotemeta(bi->getHeading());
   457 				// If necessary, write note
   458 				if (!bi->getNoteObj().isEmpty())
   459 					r+=bi->getNoteOpenDoc();
   460 				r+="</text:p>";
   461 				r+=buildList (bi);	// recursivly add deeper branches
   462 				r+="</text:list-item>\n";
   463 				i++;
   464 				bi=current->getBranchNum(i);
   465 			}
   466 			r+="</text:list>\n";
   467 		}
   468     }
   469     return r;
   470 }
   471 
   472 
   473 void ExportOO::exportPresentation()
   474 {
   475 	QString allPages;
   476 
   477 /* FIXME-2 not adapted to multiple mapCenters yet, see patch already done in 1.12.2...
   478 	// Insert new content
   479 	content.replace ("<!-- INSERT TITLE -->",quotemeta(mapCenter->getHeading()));
   480 	content.replace ("<!-- INSERT AUTHOR -->",quotemeta(mapCenter->getAuthor()));
   481 
   482 	QString	onePage;
   483 	QString list;
   484 	
   485 	BranchObj *sectionBO=mapCenter->getFirstBranch();
   486     int i=0;
   487 	BranchObj *pagesBO;
   488     int j=0;
   489 
   490 	// Walk sections
   491 	while (sectionBO && !sectionBO->hasHiddenExportParent() )
   492 	{
   493 		if (useSections)
   494 		{
   495 			// Add page with section title
   496 			onePage=sectionTemplate;
   497 			onePage.replace ("<!-- INSERT PAGE HEADING -->", quotemeta(sectionBO->getHeading() ) );
   498 			allPages+=onePage;
   499 		} else
   500 		{
   501 			i=-2;	// only use inner loop to 
   502 			        // turn mainbranches into pages
   503 			sectionBO=mapCenter;
   504 		}
   505 
   506 		// Walk mainpages
   507 		pagesBO=sectionBO->getFirstBranch();
   508 		j=0;
   509 		while (pagesBO && !pagesBO->hasHiddenExportParent() )
   510 		{
   511 			// Add page with list of items
   512 			onePage=pageTemplate;
   513 			onePage.replace ("<!-- INSERT PAGE HEADING -->", quotemeta (pagesBO->getHeading() ) );
   514 			list=buildList (pagesBO->getTreeItem() );  
   515 			onePage.replace ("<!-- INSERT LIST -->", list);
   516 			allPages+=onePage;
   517 			j++;
   518 			pagesBO=sectionBO->getBranchNum(j);
   519 		}
   520 		i++;
   521 		sectionBO=mapCenter->getBranchNum(i);
   522 	}
   523 	
   524 	content.replace ("<!-- INSERT PAGES -->",allPages);
   525 
   526 	// Write modified content
   527 	QFile f (contentFile);
   528     if ( !f.open( QIODevice::WriteOnly ) ) 
   529 	{
   530 		QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not write %1").arg(contentFile));
   531 		mainWindow->statusMessage(QString(QObject::tr("Export failed.")));
   532 		return;
   533     }
   534 
   535     QTextStream t( &f );
   536     t << content;
   537     f.close();
   538 
   539 	// zip tmpdir to destination
   540 	zipDir (tmpDir,outputFile);	
   541 */
   542 }
   543 
   544 bool ExportOO::setConfigFile (const QString &cf)
   545 {
   546 	configFile=cf;
   547 	int i=cf.findRev ("/");
   548 	if (i>=0) configDir=cf.left(i);
   549 	SimpleSettings set;
   550 	set.readSettings(configFile);
   551 
   552 	// set paths
   553 	templateDir=configDir+"/"+set.readEntry ("Template");
   554 
   555 	QDir d (templateDir);
   556 	if (!d.exists())
   557 	{
   558 		QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Check \"%1\" in\n%2").arg("Template="+set.readEntry ("Template")).arg(configFile));
   559 		return false;
   560 
   561 	}
   562 
   563 	contentTemplateFile=templateDir+"content-template.xml";
   564 	contentFile=tmpDir.path()+"/content.xml";
   565 	pageTemplateFile=templateDir+"page-template.xml";
   566 	sectionTemplateFile=templateDir+"section-template.xml";
   567 
   568 	if (set.readEntry("useSections").contains("yes"))
   569 		useSections=true;
   570 
   571 	// Copy template to tmpdir
   572 	system ("cp -r "+templateDir+"* "+tmpDir.path());
   573 
   574 	// Read content-template
   575 	if (!loadStringFromDisk (contentTemplateFile,content))
   576 	{
   577 		QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not read %1").arg(contentTemplateFile));
   578 		return false;
   579 	}
   580 
   581 	// Read page-template
   582 	if (!loadStringFromDisk (pageTemplateFile,pageTemplate))
   583 	{
   584 		QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not read %1").arg(pageTemplateFile));
   585 		return false;
   586 	}
   587 	
   588 	// Read section-template
   589 	if (useSections && !loadStringFromDisk (sectionTemplateFile,sectionTemplate))
   590 	{
   591 		QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not read %1").arg(sectionTemplateFile));
   592 		return false;
   593 	}
   594 	return true;
   595 }
   596