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