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