exports.cpp
branchqt4-port
changeset 2 608f976aa7bb
parent 0 7a96bd401351
child 9 f94317a94db1
     1.1 --- a/exports.cpp	Sun Jan 30 12:58:47 2005 +0000
     1.2 +++ b/exports.cpp	Tue Jun 06 14:58:11 2006 +0000
     1.3 @@ -1,87 +1,103 @@
     1.4 +#include <q3filedialog.h>
     1.5 +#include <qmessagebox.h>
     1.6 +#include <q3process.h>
     1.7 +//Added by qt3to4:
     1.8 +#include <QTextStream>
     1.9 +
    1.10  #include "exports.h"
    1.11 +#include "file.h"
    1.12 +#include "linkablemapobj.h"
    1.13 +#include "misc.h"
    1.14 +#include "mainwindow.h"
    1.15 +#include "warningdialog.h"
    1.16 +#include "xsltproc.h"
    1.17  
    1.18 -#include "linkablemapobj.h"
    1.19 +extern Main *mainWindow;
    1.20 +extern QDir vymBaseDir;
    1.21  
    1.22  
    1.23 -Export::Export()
    1.24 +ExportBase::ExportBase()
    1.25  {
    1.26  	indentPerDepth="  ";
    1.27 +	// Create tmpdir
    1.28 +	tmpDir.setPath (makeUniqueDir("/tmp/vym-XXXXXX"));
    1.29  }
    1.30  
    1.31 -bool Export::setOutputDir(QString dirname)
    1.32 +ExportBase::~ExportBase()
    1.33  {
    1.34 -	outdir.setPath (dirname);
    1.35 -	if ( outdir.exists() )
    1.36 -	{
    1.37 -		// FIXME
    1.38 -		// ask for confirmation
    1.39 -		// then delete outdir
    1.40 -		return true;
    1.41 -	} else
    1.42 -	{
    1.43 -		// try to create directory
    1.44 -		//return outdir.mkdir (outdir.absPath());
    1.45 -		// FIXME
    1.46 -		return true;
    1.47 -	}
    1.48 +	// Remove tmpdir
    1.49 +	removeDir (tmpDir);
    1.50  }
    1.51  
    1.52 -void Export::setPath (const QString &p)
    1.53 +void ExportBase::setDir(const QString &p)
    1.54  {
    1.55 -	filepath=p;
    1.56 +	outputDir=p;
    1.57  }
    1.58  
    1.59 -void Export::setMapCenter(MapCenterObj *mc)
    1.60 +void ExportBase::setFile (const QString &p)
    1.61 +{
    1.62 +	outputFile=p;
    1.63 +}
    1.64 +
    1.65 +void ExportBase::setMapCenter(MapCenterObj *mc)
    1.66  {
    1.67  	mapCenter=mc;
    1.68  }
    1.69  
    1.70 -void Export::exportMap()
    1.71 +void ExportBase::setCaption (const QString &s)
    1.72  {
    1.73 -	QFile file (filepath);
    1.74 -	if ( !file.open( IO_WriteOnly ) )
    1.75 -	{
    1.76 -		// FIXME
    1.77 -		cout << "Export::exportMap  couldn't open "<<filepath<<endl;
    1.78 -		return;
    1.79 -	}
    1.80 -	QTextStream ts( &file );	// use LANG decoding here...
    1.81 -
    1.82 -	// Main loop over all branches
    1.83 -	QString s;
    1.84 -	QString actIndent("");
    1.85 -	int i;
    1.86 -	BranchObj *bo;
    1.87 -	bo=mapCenter->first();
    1.88 -	while (bo) 
    1.89 -	{
    1.90 -		// Make indentstring
    1.91 -		for (i=0;i<bo->getDepth();i++) actIndent+= indentPerDepth;
    1.92 -
    1.93 -		// Write heading
    1.94 -		//	write (actIndent + getSectionString(bo) + bo->getHeading()+ "\n");
    1.95 -		if (bo->getDepth()==1)
    1.96 -			ts << (getSectionString(bo) + bo->getHeading()+ "\n");
    1.97 -		else	
    1.98 -			ts << (actIndent + " - " + bo->getHeading()+ "\n");
    1.99 -		
   1.100 -		// If necessary, write note
   1.101 -		if (!bo->getNote().isEmpty())
   1.102 -		{
   1.103 -			ts << ("-------------------Begin of Note-----------------\n");
   1.104 -			ts << (bo->getNote());
   1.105 -			ts << ("\n");
   1.106 -			ts << ("-------------------End of Note-------------------\n");
   1.107 -		}
   1.108 -		
   1.109 -		bo=bo->next();
   1.110 -		actIndent="";
   1.111 -	}
   1.112 -	file.close();
   1.113 +	caption=s;
   1.114  }
   1.115  
   1.116 -QString Export::getSectionString(BranchObj *bostart)
   1.117 +void ExportBase::addFilter(const QString &s)
   1.118  {
   1.119 +	filter=s;
   1.120 +}
   1.121 +
   1.122 +bool ExportBase::execDialog()
   1.123 +{
   1.124 +	if (mapCenter && mapCenter->getMapEditor())
   1.125 +	{
   1.126 +		Q3FileDialog *fd=new Q3FileDialog( mapCenter->getMapEditor(), caption);
   1.127 +		fd->addFilter (filter);
   1.128 +		fd->setCaption(caption);
   1.129 +		fd->setMode( Q3FileDialog::AnyFile );
   1.130 +		fd->show();
   1.131 +
   1.132 +		if ( fd->exec() == QDialog::Accepted )
   1.133 +		{
   1.134 +			if (QFile (fd->selectedFile()).exists() )
   1.135 +			{
   1.136 +				QMessageBox mb( __VYM,
   1.137 +					QObject::tr("The file %1 exists already.\nDo you want to overwrite it?").arg(fd->selectedFile()), 
   1.138 +				QMessageBox::Warning,
   1.139 +				QMessageBox::Yes | QMessageBox::Default,
   1.140 +				QMessageBox::Cancel | QMessageBox::Escape,
   1.141 +				Qt::NoButton );
   1.142 +				mb.setButtonText( QMessageBox::Yes, QObject::tr("Overwrite") );
   1.143 +				mb.setButtonText( QMessageBox::No, QObject::tr("Cancel"));
   1.144 +				ExportBase ex;
   1.145 +				switch( mb.exec() ) 
   1.146 +				{
   1.147 +					case QMessageBox::Yes:
   1.148 +						// save 
   1.149 +						break;;
   1.150 +					case QMessageBox::Cancel:
   1.151 +						// return, do nothing
   1.152 +						return false;
   1.153 +						break;
   1.154 +				}
   1.155 +			}
   1.156 +			outputFile=fd->selectedFile();
   1.157 +			return true;
   1.158 +		}
   1.159 +	}
   1.160 +	return false;
   1.161 +}
   1.162 +
   1.163 +QString ExportBase::getSectionString(BranchObj *bostart)
   1.164 +{
   1.165 +	// Make prefix like "2.5.3" for "bo:2,bo:5,bo:3"
   1.166  	QString r;
   1.167  	BranchObj *bo=bostart;
   1.168  	int depth=bo->getDepth();
   1.169 @@ -97,13 +113,24 @@
   1.170  		return r + " ";
   1.171  }
   1.172  
   1.173 -void Export::exportAsHTML()
   1.174 +
   1.175 +////////////////////////////////////////////////////////////////////////
   1.176 +void ExportASCII::doExport()
   1.177  {
   1.178 -	// FIXME  just testing...
   1.179 +	QFile file (outputFile);
   1.180 +	if ( !file.open( QIODevice::WriteOnly ) )
   1.181 +	{
   1.182 +		// FIXME experimental, testing
   1.183 +		qWarning ("ExportBase::exportXML  couldn't open "+outputFile);
   1.184 +		return;
   1.185 +	}
   1.186 +	QTextStream ts( &file );	// use LANG decoding here...
   1.187 +
   1.188  	// Main loop over all branches
   1.189  	QString s;
   1.190  	QString actIndent("");
   1.191  	int i;
   1.192 +	uint j;
   1.193  	BranchObj *bo;
   1.194  	bo=mapCenter->first();
   1.195  	while (bo) 
   1.196 @@ -111,22 +138,338 @@
   1.197  		// Make indentstring
   1.198  		for (i=0;i<bo->getDepth();i++) actIndent+= indentPerDepth;
   1.199  
   1.200 -		// Write heading
   1.201 -		write (actIndent + getSectionString(bo) + bo->getHeading()+ "\n");
   1.202 +		if (bo->getDepth()==0)
   1.203 +		{
   1.204 +			ts << (bo->getHeading()+ "\n");
   1.205 +			for (j=0;j<bo->getHeading().length();j++) ts<<"=";
   1.206 +			ts << "\n";
   1.207 +		} else 	if (bo->getDepth()==1)
   1.208 +			ts << ("\n"+getSectionString(bo) + bo->getHeading()+ "\n");
   1.209 +		else	if (bo->getDepth()==2)
   1.210 +			ts << (actIndent + " o " + bo->getHeading()+ "\n");
   1.211 +		else	
   1.212 +			ts << (actIndent + " - " + bo->getHeading()+ "\n");
   1.213  		
   1.214  		// If necessary, write note
   1.215  		if (!bo->getNote().isEmpty())
   1.216  		{
   1.217 -			write (bo->getNote());
   1.218 +			s =bo->getNoteASCII();
   1.219 +			s=s.replace ("\n","\n"+actIndent);
   1.220 +			ts << (s+"\n\n");
   1.221  		}
   1.222 -		
   1.223  		bo=bo->next();
   1.224  		actIndent="";
   1.225  	}
   1.226 +	file.close();
   1.227  }
   1.228  
   1.229 -void Export::write(QString s)
   1.230 +////////////////////////////////////////////////////////////////////////
   1.231 +void ExportKDEBookmarks::doExport() 
   1.232  {
   1.233 -	cout << s;
   1.234 +	MapEditor *me=NULL;
   1.235 +	if (mapCenter) me=mapCenter->getMapEditor();
   1.236 +	if (me)
   1.237 +	{
   1.238 +		WarningDialog dia;
   1.239 +		dia.setCancelButton (true);
   1.240 +		dia.setText(QObject::tr("Exporting the %1 bookmarks will overwrite\nyour existing bookmarks file.").arg("KDE"));
   1.241 +		dia.setCaption(QObject::tr("Warning: Overwriting %1 bookmarks").arg("KDE"));
   1.242 +		dia.setShowAgainName("/vym/warnings/overwriteKDEBookmarks");
   1.243 +		if (dia.exec()==QDialog::Accepted)
   1.244 +		{
   1.245 +			me->exportXML(tmpDir.path());
   1.246 +
   1.247 +			XSLTProc p;
   1.248 +			p.setInputFile (tmpDir.path()+"/"+me->getMapName()+".xml");
   1.249 +			p.setOutputFile (tmpDir.home().path()+"/.kde/share/apps/konqueror/bookmarks.xml");
   1.250 +			p.setXSLFile (vymBaseDir.path()+"/styles/vym2kdebookmarks.xsl");
   1.251 +			p.process();
   1.252 +
   1.253 +			QString ub=vymBaseDir.path()+"/scripts/update-bookmarks";
   1.254 +			Q3Process *proc = new Q3Process( );
   1.255 +			proc->addArgument(ub);
   1.256 +
   1.257 +			if ( !proc->start() ) 
   1.258 +			{
   1.259 +				QMessageBox::warning(0, 
   1.260 +					QObject::tr("Warning"),
   1.261 +					QObject::tr("Couldn't find script %1\nto notifiy Browsers of changed bookmarks.").arg(ub));
   1.262 +			}	
   1.263 +
   1.264 +
   1.265 +		}
   1.266 +	}
   1.267 +
   1.268  }
   1.269  
   1.270 +////////////////////////////////////////////////////////////////////////
   1.271 +void ExportFirefoxBookmarks::doExport() 
   1.272 +{
   1.273 +	MapEditor *me=NULL;
   1.274 +	if (mapCenter) me=mapCenter->getMapEditor();
   1.275 +	if (me)
   1.276 +	{
   1.277 +		WarningDialog dia;
   1.278 +		dia.setCancelButton (true);
   1.279 +		dia.setText(QObject::tr("Exporting the %1 bookmarks will overwrite\nyour existing bookmarks file.").arg("Firefox"));
   1.280 +		dia.setCaption(QObject::tr("Warning: Overwriting %1 bookmarks").arg("Firefox"));
   1.281 +		dia.setShowAgainName("/vym/warnings/overwriteImportBookmarks");
   1.282 +		if (dia.exec()==QDialog::Accepted)
   1.283 +		{
   1.284 +			me->exportXML(tmpDir.path());
   1.285 +
   1.286 +/*
   1.287 +			XSLTProc p;
   1.288 +			p.setInputFile (tmpDir.path()+"/"+me->getMapName()+".xml");
   1.289 +			p.setOutputFile (tmpDir.home().path()+"/.kde/share/apps/konqueror/bookmarks.xml");
   1.290 +			p.setXSLFile (vymBaseDir.path()+"/styles/vym2kdebookmarks.xsl");
   1.291 +			p.process();
   1.292 +
   1.293 +			QString ub=vymBaseDir.path()+"/scripts/update-bookmarks";
   1.294 +			QProcess *proc = new QProcess( );
   1.295 +			proc->addArgument(ub);
   1.296 +
   1.297 +			if ( !proc->start() ) 
   1.298 +			{
   1.299 +				QMessageBox::warning(0, 
   1.300 +					QObject::tr("Warning"),
   1.301 +					QObject::tr("Couldn't find script %1\nto notifiy Browsers of changed bookmarks.").arg(ub));
   1.302 +			}	
   1.303 +
   1.304 +*/
   1.305 +
   1.306 +		}
   1.307 +	}
   1.308 +}
   1.309 +
   1.310 +////////////////////////////////////////////////////////////////////////
   1.311 +void ExportTaskjuggler::doExport() 
   1.312 +{
   1.313 +	MapEditor *me=NULL;
   1.314 +	if (mapCenter) me=mapCenter->getMapEditor();
   1.315 +	if (me)
   1.316 +	{
   1.317 +		me->exportXML(tmpDir.path());
   1.318 +
   1.319 +		XSLTProc p;
   1.320 +		p.setInputFile (tmpDir.path()+"/"+me->getMapName()+".xml");
   1.321 +		p.setOutputFile (outputFile);
   1.322 +		p.setXSLFile (vymBaseDir.path()+"/styles/vym2taskjuggler.xsl");
   1.323 +		p.process();
   1.324 +	}
   1.325 +
   1.326 +}
   1.327 +
   1.328 +////////////////////////////////////////////////////////////////////////
   1.329 +void ExportLaTeX::doExport() 
   1.330 +{
   1.331 +	// Exports a map to a LaTex file.  
   1.332 +	// This file needs to be included 
   1.333 +	// or inported into a LaTex document
   1.334 +	// it will not add a preamble, or anything 
   1.335 +	// that makes a full LaTex document.
   1.336 +  QFile file (outputFile);
   1.337 +  if ( !file.open( QIODevice::WriteOnly ) ) {
   1.338 +	QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not write %1").arg(outputFile));
   1.339 +	mainWindow->statusMessage(QString(QObject::tr("Export failed.")));
   1.340 +    return;
   1.341 +  }
   1.342 +  QTextStream ts( &file );	// use LANG decoding here...
   1.343 +  ts.setEncoding (QTextStream::UnicodeUTF8); // Force UTF8
   1.344 +  
   1.345 +  // Main loop over all branches
   1.346 +  QString s;
   1.347 +  // QString actIndent("");
   1.348 +  // int i;
   1.349 +  BranchObj *bo;
   1.350 +  bo=mapCenter->first();
   1.351 +  while (bo) {
   1.352 +	if (bo->getDepth()==0);
   1.353 +	else if (bo->getDepth()==1) {
   1.354 +	  ts << ("\\chapter{" + bo->getHeading()+ "}\n");
   1.355 +	}
   1.356 +	else if (bo->getDepth()==2) {
   1.357 +	  ts << ("\\section{" + bo->getHeading()+ "}\n");
   1.358 +	}
   1.359 +	else if (bo->getDepth()==3) {
   1.360 +	  ts << ("\\subsection{" + bo->getHeading()+ "}\n");
   1.361 +	}
   1.362 +	else if (bo->getDepth()==4) {
   1.363 +	  ts << ("\\subsubsection{" + bo->getHeading()+ "}\n");
   1.364 +	}
   1.365 +	else {
   1.366 +	  ts << ("\\paragraph*{" + bo->getHeading()+ "}\n");
   1.367 +	}
   1.368 +	
   1.369 +	// If necessary, write note
   1.370 +	if (!bo->getNote().isEmpty()) {
   1.371 +	  ts << (bo->getNoteASCII());
   1.372 +	  ts << ("\n");
   1.373 +	}
   1.374 +    bo=bo->next();
   1.375 +   }
   1.376 +  file.close();
   1.377 +}
   1.378 +
   1.379 +////////////////////////////////////////////////////////////////////////
   1.380 +ExportOO::ExportOO()
   1.381 +{
   1.382 +	useSections=false;
   1.383 +}
   1.384 +
   1.385 +ExportOO::~ExportOO()
   1.386 +{
   1.387 +}	
   1.388 +
   1.389 +QString ExportOO::buildList (BranchObj *current)
   1.390 +{
   1.391 +    QString r;
   1.392 +    BranchObj *bo;
   1.393 +
   1.394 +    uint i=0;
   1.395 +    bo=current->getFirstBranch();
   1.396 +    if (bo)
   1.397 +    {
   1.398 +        // Start list
   1.399 +        r+="<text:list text:style-name=\"vym-list\">\n";
   1.400 +        while (bo)
   1.401 +        {
   1.402 +			r+="<text:list-item><text:p >";
   1.403 +			r+=quotemeta(bo->getHeading());
   1.404 +			// If necessary, write note
   1.405 +			if (!bo->getNote().isEmpty())
   1.406 +				r+=bo->getNoteOpenDoc();
   1.407 +			r+="</text:p>";
   1.408 +			r+=buildList (bo);	// recursivly add deeper branches
   1.409 +			r+="</text:list-item>\n";
   1.410 +			i++;
   1.411 +			bo=current->getBranchNum(i);
   1.412 +        }
   1.413 +        r+="</text:list>\n";
   1.414 +    }
   1.415 +    return r;
   1.416 +}
   1.417 +
   1.418 +
   1.419 +void ExportOO::exportPresentation()
   1.420 +{
   1.421 +	QString allPages;
   1.422 +
   1.423 +	// Insert new content
   1.424 +	content.replace ("<!-- INSERT TITLE -->",quotemeta(mapCenter->getHeading()));
   1.425 +	content.replace ("<!-- INSERT AUTHOR -->",quotemeta(mapCenter->getAuthor()));
   1.426 +
   1.427 +	QString	onePage;
   1.428 +	QString list;
   1.429 +	
   1.430 +	BranchObj *sectionBO=mapCenter->getFirstBranch();
   1.431 +    int i=0;
   1.432 +	BranchObj *pagesBO;
   1.433 +    int j=0;
   1.434 +
   1.435 +	// Walk sections
   1.436 +	while (sectionBO)
   1.437 +	{
   1.438 +		if (useSections)
   1.439 +		{
   1.440 +			// Add page with section title
   1.441 +			onePage=sectionTemplate;
   1.442 +			onePage.replace ("<!-- INSERT PAGE HEADING -->", quotemeta(sectionBO->getHeading() ) );
   1.443 +			allPages+=onePage;
   1.444 +		} else
   1.445 +		{
   1.446 +			i=-2;	// only use inner loop to 
   1.447 +			        // turn mainbranches into pages
   1.448 +			sectionBO=mapCenter;
   1.449 +		}
   1.450 +
   1.451 +		// Walk mainpages
   1.452 +		pagesBO=sectionBO->getFirstBranch();
   1.453 +		j=0;
   1.454 +		while (pagesBO)
   1.455 +		{
   1.456 +			// Add page with list of items
   1.457 +			onePage=pageTemplate;
   1.458 +			onePage.replace ("<!-- INSERT PAGE HEADING -->", quotemeta (pagesBO->getHeading() ) );
   1.459 +			list=buildList (pagesBO);
   1.460 +			onePage.replace ("<!-- INSERT LIST -->", list);
   1.461 +			allPages+=onePage;
   1.462 +			j++;
   1.463 +			pagesBO=sectionBO->getBranchNum(j);
   1.464 +		}
   1.465 +		i++;
   1.466 +		sectionBO=mapCenter->getBranchNum(i);
   1.467 +	}
   1.468 +	
   1.469 +	content.replace ("<!-- INSERT PAGES -->",allPages);
   1.470 +
   1.471 +	// Write modified content
   1.472 +	QFile f (contentFile);
   1.473 +    if ( !f.open( QIODevice::WriteOnly ) ) 
   1.474 +	{
   1.475 +		QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not write %1").arg(contentFile));
   1.476 +		mainWindow->statusMessage(QString(QObject::tr("Export failed.")));
   1.477 +		return;
   1.478 +    }
   1.479 +
   1.480 +    QTextStream t( &f );
   1.481 +    t << content;
   1.482 +    f.close();
   1.483 +
   1.484 +	// zip tmpdir to destination
   1.485 +	zipDir (tmpDir,outputFile);	
   1.486 +}
   1.487 +
   1.488 +bool ExportOO::setConfigFile (const QString &cf)
   1.489 +{
   1.490 +	configFile=cf;
   1.491 +	int i=cf.findRev ("/");
   1.492 +	if (i>=0) configDir=cf.left(i);
   1.493 +	SimpleSettings set;
   1.494 +	set.readSettings(configFile);
   1.495 +
   1.496 +	// set paths
   1.497 +	templateDir=configDir+"/"+set.readEntry ("Template");
   1.498 +
   1.499 +	QDir d (templateDir);
   1.500 +	if (!d.exists())
   1.501 +	{
   1.502 +		QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Check \"%1\" in\n%2").arg("Template="+set.readEntry ("Template")).arg(configFile));
   1.503 +		return false;
   1.504 +
   1.505 +	}
   1.506 +
   1.507 +	contentTemplateFile=templateDir+"content-template.xml";
   1.508 +	contentFile=tmpDir.path()+"/content.xml";
   1.509 +	pageTemplateFile=templateDir+"page-template.xml";
   1.510 +	sectionTemplateFile=templateDir+"section-template.xml";
   1.511 +
   1.512 +	if (set.readEntry("useSections").contains("yes"))
   1.513 +		useSections=true;
   1.514 +
   1.515 +	// Copy template to tmpdir
   1.516 +	system ("cp -r "+templateDir+"* "+tmpDir.path());
   1.517 +
   1.518 +	// Read content-template
   1.519 +	if (!loadStringFromDisk (contentTemplateFile,content))
   1.520 +	{
   1.521 +		QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not read %1").arg(contentTemplateFile));
   1.522 +		return false;
   1.523 +	}
   1.524 +
   1.525 +	// Read page-template
   1.526 +	if (!loadStringFromDisk (pageTemplateFile,pageTemplate))
   1.527 +	{
   1.528 +		QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not read %1").arg(pageTemplateFile));
   1.529 +		return false;
   1.530 +	}
   1.531 +	
   1.532 +	// Read section-template
   1.533 +	if (useSections && !loadStringFromDisk (sectionTemplateFile,sectionTemplate))
   1.534 +	{
   1.535 +		QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not read %1").arg(sectionTemplateFile));
   1.536 +		return false;
   1.537 +	}
   1.538 +	return true;
   1.539 +}
   1.540 +