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