exports.cpp
author insilmaril
Thu, 14 Jun 2007 10:21:46 +0000
changeset 504 731728086341
parent 497 ab118b86bc54
child 512 96680eb33a79
permissions -rw-r--r--
Fixed undo/redo of copy/paste
     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 (makeUniqueDir(ok,"/tmp/vym-export-XXXXXX"));
    18 	if (!tmpDir.exists() || !ok)
    19 		QMessageBox::critical( 0, QObject::tr( "Error" ),
    20 					   QObject::tr("Couldn't access temporary directory\n"));
    21 }
    22 
    23 ExportBase::~ExportBase()
    24 {
    25 	// Cleanup tmpdir
    26 	removeDir (tmpDir);
    27 }
    28 
    29 void ExportBase::setDir(const QDir &d)
    30 {
    31 	outDir=d;
    32 }
    33 
    34 void ExportBase::setFile (const QString &p)
    35 {
    36 	outputFile=p;
    37 }
    38 
    39 void ExportBase::setMapCenter(MapCenterObj *mc)
    40 {
    41 	mapCenter=mc;
    42 }
    43 
    44 void ExportBase::setCaption (const QString &s)
    45 {
    46 	caption=s;
    47 }
    48 
    49 void ExportBase::addFilter(const QString &s)
    50 {
    51 	filter=s;
    52 }
    53 
    54 bool ExportBase::execDialog()
    55 {
    56 	if (mapCenter && mapCenter->getMapEditor())
    57 	{
    58 		QFileDialog *fd=new QFileDialog( mapCenter->getMapEditor(), caption);
    59 		fd->setFilter (filter);
    60 		fd->setCaption(caption);
    61 		fd->setMode( QFileDialog::AnyFile );
    62 		fd->setDir (outDir);
    63 		fd->show();
    64 
    65 		if ( fd->exec() == QDialog::Accepted )
    66 		{
    67 			if (QFile (fd->selectedFile()).exists() )
    68 			{
    69 				QMessageBox mb( vymName,
    70 					QObject::tr("The file %1 exists already.\nDo you want to overwrite it?").arg(fd->selectedFile()), 
    71 				QMessageBox::Warning,
    72 				QMessageBox::Yes | QMessageBox::Default,
    73 				QMessageBox::Cancel | QMessageBox::Escape,
    74 				Qt::NoButton );
    75 				mb.setButtonText( QMessageBox::Yes, QObject::tr("Overwrite") );
    76 				mb.setButtonText( QMessageBox::No, QObject::tr("Cancel"));
    77 				ExportBase ex;
    78 				switch( mb.exec() ) 
    79 				{
    80 					case QMessageBox::Yes:
    81 						// save 
    82 						break;;
    83 					case QMessageBox::Cancel:
    84 						// return, do nothing
    85 						return false;
    86 						break;
    87 				}
    88 			}
    89 			outputFile=fd->selectedFile();
    90 			return true;
    91 		}
    92 	}
    93 	return false;
    94 }
    95 
    96 QString ExportBase::getSectionString(BranchObj *bostart)
    97 {
    98 	// Make prefix like "2.5.3" for "bo:2,bo:5,bo:3"
    99 	QString r;
   100 	BranchObj *bo=bostart;
   101 	int depth=bo->getDepth();
   102 	while (depth>0)
   103 	{
   104 		r=QString("%1").arg(1+bo->getNum(),0,10)+"." + r;
   105 		bo=(BranchObj*)(bo->getParObj());
   106 		depth=bo->getDepth();
   107 	}	
   108 	if (r.isEmpty())
   109 		return r;
   110 	else	
   111 		return r + " ";
   112 }
   113 
   114 ////////////////////////////////////////////////////////////////////////
   115 void ExportASCII::doExport()
   116 {
   117 	QFile file (outputFile);
   118 	if ( !file.open( QIODevice::WriteOnly ) )
   119 	{
   120 		qWarning ("ExportBase::exportXML  couldn't open "+outputFile);
   121 		return;
   122 	}
   123 	QTextStream ts( &file );	// use LANG decoding here...
   124 
   125 	// Main loop over all branches
   126 	QString s;
   127 	QString actIndent("");
   128 	int i,j;
   129 	BranchObj *bo;
   130 	bo=mapCenter->first();
   131 	while (bo) 
   132 	{
   133 		// Make indentstring
   134 		for (i=0;i<bo->getDepth();i++) actIndent+= indentPerDepth;
   135 
   136 		if (bo->getDepth()==0)
   137 		{
   138 			ts << (bo->getHeading()+ "\n");
   139 			for (j=0;j<bo->getHeading().length();j++) ts<<"=";
   140 			ts << "\n";
   141 		} else 	if (bo->getDepth()==1)
   142 			ts << ("\n"+getSectionString(bo) + bo->getHeading()+ "\n");
   143 		else	if (bo->getDepth()==2)
   144 			ts << (actIndent + " * " + bo->getHeading()+ "\n");
   145 		else	if (bo->getDepth()==3)
   146 			ts << (actIndent + " o " + bo->getHeading()+ "\n");
   147 		else	
   148 			ts << (actIndent + " - " + bo->getHeading()+ "\n");
   149 		
   150 		// If necessary, write note
   151 		if (!bo->getNote().isEmpty())
   152 		{
   153 			s =bo->getNoteASCII();
   154 			s=s.replace ("\n","\n"+actIndent);
   155 			ts << (s+"\n\n");
   156 		}
   157 		bo=bo->next();
   158 		actIndent="";
   159 	}
   160 	file.close();
   161 }
   162 
   163 
   164 ////////////////////////////////////////////////////////////////////////
   165 void ExportCSV::doExport()
   166 {
   167 	QFile file (outputFile);
   168 	if ( !file.open( QIODevice::WriteOnly ) )
   169 	{
   170 		qWarning ("ExportBase::exportXML  couldn't open "+outputFile);
   171 		return;
   172 	}
   173 	QTextStream ts( &file );	// use LANG decoding here...
   174 
   175 	// Write header
   176 	ts << "\"Note\""  <<endl;
   177 
   178 	// Main loop over all branches
   179 	QString s;
   180 	QString actIndent("");
   181 	int i,j;
   182 	BranchObj *bo;
   183 	bo=mapCenter->first();
   184 	while (bo) 
   185 	{
   186 		// If necessary, write note
   187 		if (!bo->getNote().isEmpty())
   188 		{
   189 			s =bo->getNoteASCII();
   190 			s=s.replace ("\n","\n"+actIndent);
   191 			ts << ("\""+s+"\",");
   192 		} else
   193 			ts <<"\"\",";
   194 
   195 		// Make indentstring
   196 		for (i=0;i<bo->getDepth();i++) actIndent+= "\"\",";
   197 
   198 		// Write heading
   199 		ts << actIndent << "\"" << bo->getHeading()<<"\""<<endl;
   200 		
   201 		bo=bo->next();
   202 		actIndent="";
   203 	}
   204 	file.close();
   205 }
   206 
   207 ////////////////////////////////////////////////////////////////////////
   208 void ExportKDEBookmarks::doExport() 
   209 {
   210 	MapEditor *me=NULL;
   211 	if (mapCenter) me=mapCenter->getMapEditor();
   212 	if (me)
   213 	{
   214 		WarningDialog dia;
   215 		dia.showCancelButton (true);
   216 		dia.setText(QObject::tr("Exporting the %1 bookmarks will overwrite\nyour existing bookmarks file.").arg("KDE"));
   217 		dia.setCaption(QObject::tr("Warning: Overwriting %1 bookmarks").arg("KDE"));
   218 		dia.setShowAgainName("/exports/KDE/overwriteKDEBookmarks");
   219 		if (dia.exec()==QDialog::Accepted)
   220 		{
   221 			me->exportXML(tmpDir.path());
   222 
   223 			XSLTProc p;
   224 			p.setInputFile (tmpDir.path()+"/"+me->getMapName()+".xml");
   225 			p.setOutputFile (tmpDir.home().path()+"/.kde/share/apps/konqueror/bookmarks.xml");
   226 			p.setXSLFile (vymBaseDir.path()+"/styles/vym2kdebookmarks.xsl");
   227 			p.process();
   228 
   229 			QString ub=vymBaseDir.path()+"/scripts/update-bookmarks";
   230 			QProcess *proc= new QProcess ;
   231 			proc->start( ub);
   232 			if (!proc->waitForStarted())
   233 			{
   234 				QMessageBox::warning(0, 
   235 					QObject::tr("Warning"),
   236 					QObject::tr("Couldn't find script %1\nto notifiy Browsers of changed bookmarks.").arg(ub));
   237 			}	
   238 		}
   239 	}
   240 
   241 }
   242 
   243 ////////////////////////////////////////////////////////////////////////
   244 void ExportFirefoxBookmarks::doExport() 
   245 {
   246 	MapEditor *me=NULL;
   247 	if (mapCenter) me=mapCenter->getMapEditor();
   248 	if (me)
   249 	{
   250 		WarningDialog dia;
   251 		dia.showCancelButton (true);
   252 		dia.setText(QObject::tr("Exporting the %1 bookmarks will overwrite\nyour existing bookmarks file.").arg("Firefox"));
   253 		dia.setCaption(QObject::tr("Warning: Overwriting %1 bookmarks").arg("Firefox"));
   254 		dia.setShowAgainName("/vym/warnings/overwriteImportBookmarks");
   255 		if (dia.exec()==QDialog::Accepted)
   256 		{
   257 			me->exportXML(tmpDir.path());
   258 
   259 /*
   260 			XSLTProc p;
   261 			p.setInputFile (tmpDir.path()+"/"+me->getMapName()+".xml");
   262 			p.setOutputFile (tmpDir.home().path()+"/.kde/share/apps/konqueror/bookmarks.xml");
   263 			p.setXSLFile (vymBaseDir.path()+"/styles/vym2kdebookmarks.xsl");
   264 			p.process();
   265 
   266 			QString ub=vymBaseDir.path()+"/scripts/update-bookmarks";
   267 			QProcess *proc = new QProcess( );
   268 			proc->addArgument(ub);
   269 
   270 			if ( !proc->start() ) 
   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 	}
   281 }
   282 
   283 ////////////////////////////////////////////////////////////////////////
   284 void ExportTaskjuggler::doExport() 
   285 {
   286 	MapEditor *me=NULL;
   287 	if (mapCenter) me=mapCenter->getMapEditor();
   288 	if (me)
   289 	{
   290 		me->exportXML(tmpDir.path());
   291 
   292 		XSLTProc p;
   293 		p.setInputFile (tmpDir.path()+"/"+me->getMapName()+".xml");
   294 		p.setOutputFile (outputFile);
   295 		p.setXSLFile (vymBaseDir.path()+"/styles/vym2taskjuggler.xsl");
   296 		p.process();
   297 	}
   298 
   299 }
   300 
   301 ////////////////////////////////////////////////////////////////////////
   302 void ExportLaTeX::doExport() 
   303 {
   304 	// Exports a map to a LaTex file.  
   305 	// This file needs to be included 
   306 	// or inported into a LaTex document
   307 	// it will not add a preamble, or anything 
   308 	// that makes a full LaTex document.
   309   QFile file (outputFile);
   310   if ( !file.open( QIODevice::WriteOnly ) ) {
   311 	QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not write %1").arg(outputFile));
   312 	mainWindow->statusMessage(QString(QObject::tr("Export failed.")));
   313     return;
   314   }
   315   QTextStream ts( &file );	// use LANG decoding here...
   316   ts.setEncoding (QTextStream::UnicodeUTF8); // Force UTF8
   317   
   318   // Main loop over all branches
   319   QString s;
   320   // QString actIndent("");
   321   // int i;
   322   BranchObj *bo;
   323   bo=mapCenter->first();
   324   while (bo) {
   325 	if (bo->getDepth()==0);
   326 	else if (bo->getDepth()==1) {
   327 	  ts << ("\\chapter{" + bo->getHeading()+ "}\n");
   328 	}
   329 	else if (bo->getDepth()==2) {
   330 	  ts << ("\\section{" + bo->getHeading()+ "}\n");
   331 	}
   332 	else if (bo->getDepth()==3) {
   333 	  ts << ("\\subsection{" + bo->getHeading()+ "}\n");
   334 	}
   335 	else if (bo->getDepth()==4) {
   336 	  ts << ("\\subsubsection{" + bo->getHeading()+ "}\n");
   337 	}
   338 	else {
   339 	  ts << ("\\paragraph*{" + bo->getHeading()+ "}\n");
   340 	}
   341 	
   342 	// If necessary, write note
   343 	if (!bo->getNote().isEmpty()) {
   344 	  ts << (bo->getNoteASCII());
   345 	  ts << ("\n");
   346 	}
   347     bo=bo->next();
   348    }
   349   file.close();
   350 }
   351 
   352 ////////////////////////////////////////////////////////////////////////
   353 ExportOO::ExportOO()
   354 {
   355 	useSections=false;
   356 }
   357 
   358 ExportOO::~ExportOO()
   359 {
   360 }	
   361 
   362 QString ExportOO::buildList (BranchObj *current)
   363 {
   364     QString r;
   365     BranchObj *bo;
   366 
   367     uint i=0;
   368     bo=current->getFirstBranch();
   369     if (bo)
   370     {
   371         // Start list
   372         r+="<text:list text:style-name=\"vym-list\">\n";
   373         while (bo)
   374         {
   375 			r+="<text:list-item><text:p >";
   376 			r+=quotemeta(bo->getHeading());
   377 			// If necessary, write note
   378 			if (!bo->getNote().isEmpty())
   379 				r+=bo->getNoteOpenDoc();
   380 			r+="</text:p>";
   381 			r+=buildList (bo);	// recursivly add deeper branches
   382 			r+="</text:list-item>\n";
   383 			i++;
   384 			bo=current->getBranchNum(i);
   385         }
   386         r+="</text:list>\n";
   387     }
   388     return r;
   389 }
   390 
   391 
   392 void ExportOO::exportPresentation()
   393 {
   394 	QString allPages;
   395 
   396 	// Insert new content
   397 	content.replace ("<!-- INSERT TITLE -->",quotemeta(mapCenter->getHeading()));
   398 	content.replace ("<!-- INSERT AUTHOR -->",quotemeta(mapCenter->getAuthor()));
   399 
   400 	QString	onePage;
   401 	QString list;
   402 	
   403 	BranchObj *sectionBO=mapCenter->getFirstBranch();
   404     int i=0;
   405 	BranchObj *pagesBO;
   406     int j=0;
   407 
   408 	// Walk sections
   409 	while (sectionBO)
   410 	{
   411 		if (useSections)
   412 		{
   413 			// Add page with section title
   414 			onePage=sectionTemplate;
   415 			onePage.replace ("<!-- INSERT PAGE HEADING -->", quotemeta(sectionBO->getHeading() ) );
   416 			allPages+=onePage;
   417 		} else
   418 		{
   419 			i=-2;	// only use inner loop to 
   420 			        // turn mainbranches into pages
   421 			sectionBO=mapCenter;
   422 		}
   423 
   424 		// Walk mainpages
   425 		pagesBO=sectionBO->getFirstBranch();
   426 		j=0;
   427 		while (pagesBO)
   428 		{
   429 			// Add page with list of items
   430 			onePage=pageTemplate;
   431 			onePage.replace ("<!-- INSERT PAGE HEADING -->", quotemeta (pagesBO->getHeading() ) );
   432 			list=buildList (pagesBO);
   433 			onePage.replace ("<!-- INSERT LIST -->", list);
   434 			allPages+=onePage;
   435 			j++;
   436 			pagesBO=sectionBO->getBranchNum(j);
   437 		}
   438 		i++;
   439 		sectionBO=mapCenter->getBranchNum(i);
   440 	}
   441 	
   442 	content.replace ("<!-- INSERT PAGES -->",allPages);
   443 
   444 	// Write modified content
   445 	QFile f (contentFile);
   446     if ( !f.open( QIODevice::WriteOnly ) ) 
   447 	{
   448 		QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not write %1").arg(contentFile));
   449 		mainWindow->statusMessage(QString(QObject::tr("Export failed.")));
   450 		return;
   451     }
   452 
   453     QTextStream t( &f );
   454     t << content;
   455     f.close();
   456 
   457 	// zip tmpdir to destination
   458 	zipDir (tmpDir,outputFile);	
   459 }
   460 
   461 bool ExportOO::setConfigFile (const QString &cf)
   462 {
   463 	configFile=cf;
   464 	int i=cf.findRev ("/");
   465 	if (i>=0) configDir=cf.left(i);
   466 	SimpleSettings set;
   467 	set.readSettings(configFile);
   468 
   469 	// set paths
   470 	templateDir=configDir+"/"+set.readEntry ("Template");
   471 
   472 	QDir d (templateDir);
   473 	if (!d.exists())
   474 	{
   475 		QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Check \"%1\" in\n%2").arg("Template="+set.readEntry ("Template")).arg(configFile));
   476 		return false;
   477 
   478 	}
   479 
   480 	contentTemplateFile=templateDir+"content-template.xml";
   481 	contentFile=tmpDir.path()+"/content.xml";
   482 	pageTemplateFile=templateDir+"page-template.xml";
   483 	sectionTemplateFile=templateDir+"section-template.xml";
   484 
   485 	if (set.readEntry("useSections").contains("yes"))
   486 		useSections=true;
   487 
   488 	// Copy template to tmpdir
   489 	system ("cp -r "+templateDir+"* "+tmpDir.path());
   490 
   491 	// Read content-template
   492 	if (!loadStringFromDisk (contentTemplateFile,content))
   493 	{
   494 		QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not read %1").arg(contentTemplateFile));
   495 		return false;
   496 	}
   497 
   498 	// Read page-template
   499 	if (!loadStringFromDisk (pageTemplateFile,pageTemplate))
   500 	{
   501 		QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not read %1").arg(pageTemplateFile));
   502 		return false;
   503 	}
   504 	
   505 	// Read section-template
   506 	if (useSections && !loadStringFromDisk (sectionTemplateFile,sectionTemplate))
   507 	{
   508 		QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not read %1").arg(sectionTemplateFile));
   509 		return false;
   510 	}
   511 	return true;
   512 }
   513