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