exports.cpp
author insilmaril
Mon, 12 Feb 2007 09:28:47 +0000
changeset 427 fb74fa7bfb4c
parent 424 d886c77ac0fe
child 428 9ae68208e2ff
permissions -rw-r--r--
1.8.66 Various fixes
     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 ////////////////////////////////////////////////////////////////////////
   116 void ExportASCII::doExport()
   117 {
   118 	QFile file (outputFile);
   119 	if ( !file.open( QIODevice::WriteOnly ) )
   120 	{
   121 		qWarning ("ExportBase::exportXML  couldn't open "+outputFile);
   122 		return;
   123 	}
   124 	QTextStream ts( &file );	// use LANG decoding here...
   125 
   126 	// Main loop over all branches
   127 	QString s;
   128 	QString actIndent("");
   129 	int i,j;
   130 	BranchObj *bo;
   131 	bo=mapCenter->first();
   132 	while (bo) 
   133 	{
   134 		// Make indentstring
   135 		for (i=0;i<bo->getDepth();i++) actIndent+= indentPerDepth;
   136 
   137 		if (bo->getDepth()==0)
   138 		{
   139 			ts << (bo->getHeading()+ "\n");
   140 			for (j=0;j<bo->getHeading().length();j++) ts<<"=";
   141 			ts << "\n";
   142 		} else 	if (bo->getDepth()==1)
   143 			ts << ("\n"+getSectionString(bo) + bo->getHeading()+ "\n");
   144 		else	if (bo->getDepth()==2)
   145 			ts << (actIndent + " o " + bo->getHeading()+ "\n");
   146 		else	
   147 			ts << (actIndent + " - " + bo->getHeading()+ "\n");
   148 		
   149 		// If necessary, write note
   150 		if (!bo->getNote().isEmpty())
   151 		{
   152 			s =bo->getNoteASCII();
   153 			s=s.replace ("\n","\n"+actIndent);
   154 			ts << (s+"\n\n");
   155 		}
   156 		bo=bo->next();
   157 		actIndent="";
   158 	}
   159 	file.close();
   160 }
   161 
   162 ////////////////////////////////////////////////////////////////////////
   163 void ExportKDEBookmarks::doExport() 
   164 {
   165 	MapEditor *me=NULL;
   166 	if (mapCenter) me=mapCenter->getMapEditor();
   167 	if (me)
   168 	{
   169 		WarningDialog dia;
   170 		dia.showCancelButton (true);
   171 		dia.setText(QObject::tr("Exporting the %1 bookmarks will overwrite\nyour existing bookmarks file.").arg("KDE"));
   172 		dia.setCaption(QObject::tr("Warning: Overwriting %1 bookmarks").arg("KDE"));
   173 		dia.setShowAgainName("/vym/warnings/overwriteKDEBookmarks");
   174 		if (dia.exec()==QDialog::Accepted)
   175 		{
   176 			me->exportXML(tmpDir.path());
   177 
   178 			XSLTProc p;
   179 			p.setInputFile (tmpDir.path()+"/"+me->getMapName()+".xml");
   180 			p.setOutputFile (tmpDir.home().path()+"/.kde/share/apps/konqueror/bookmarks.xml");
   181 			p.setXSLFile (vymBaseDir.path()+"/styles/vym2kdebookmarks.xsl");
   182 			p.process();
   183 
   184 			QString ub=vymBaseDir.path()+"/scripts/update-bookmarks";
   185 			QProcess *proc= new QProcess ;
   186 			proc->start( ub);
   187 			if (!proc->waitForStarted())
   188 			{
   189 				QMessageBox::warning(0, 
   190 					QObject::tr("Warning"),
   191 					QObject::tr("Couldn't find script %1\nto notifiy Browsers of changed bookmarks.").arg(ub));
   192 			}	
   193 		}
   194 	}
   195 
   196 }
   197 
   198 ////////////////////////////////////////////////////////////////////////
   199 void ExportFirefoxBookmarks::doExport() 
   200 {
   201 	MapEditor *me=NULL;
   202 	if (mapCenter) me=mapCenter->getMapEditor();
   203 	if (me)
   204 	{
   205 		WarningDialog dia;
   206 		dia.showCancelButton (true);
   207 		dia.setText(QObject::tr("Exporting the %1 bookmarks will overwrite\nyour existing bookmarks file.").arg("Firefox"));
   208 		dia.setCaption(QObject::tr("Warning: Overwriting %1 bookmarks").arg("Firefox"));
   209 		dia.setShowAgainName("/vym/warnings/overwriteImportBookmarks");
   210 		if (dia.exec()==QDialog::Accepted)
   211 		{
   212 			me->exportXML(tmpDir.path());
   213 
   214 /*
   215 			XSLTProc p;
   216 			p.setInputFile (tmpDir.path()+"/"+me->getMapName()+".xml");
   217 			p.setOutputFile (tmpDir.home().path()+"/.kde/share/apps/konqueror/bookmarks.xml");
   218 			p.setXSLFile (vymBaseDir.path()+"/styles/vym2kdebookmarks.xsl");
   219 			p.process();
   220 
   221 			QString ub=vymBaseDir.path()+"/scripts/update-bookmarks";
   222 			QProcess *proc = new QProcess( );
   223 			proc->addArgument(ub);
   224 
   225 			if ( !proc->start() ) 
   226 			{
   227 				QMessageBox::warning(0, 
   228 					QObject::tr("Warning"),
   229 					QObject::tr("Couldn't find script %1\nto notifiy Browsers of changed bookmarks.").arg(ub));
   230 			}	
   231 
   232 */
   233 
   234 		}
   235 	}
   236 }
   237 
   238 ////////////////////////////////////////////////////////////////////////
   239 void ExportTaskjuggler::doExport() 
   240 {
   241 	MapEditor *me=NULL;
   242 	if (mapCenter) me=mapCenter->getMapEditor();
   243 	if (me)
   244 	{
   245 		me->exportXML(tmpDir.path());
   246 
   247 		XSLTProc p;
   248 		p.setInputFile (tmpDir.path()+"/"+me->getMapName()+".xml");
   249 		p.setOutputFile (outputFile);
   250 		p.setXSLFile (vymBaseDir.path()+"/styles/vym2taskjuggler.xsl");
   251 		p.process();
   252 	}
   253 
   254 }
   255 
   256 ////////////////////////////////////////////////////////////////////////
   257 void ExportLaTeX::doExport() 
   258 {
   259 	// Exports a map to a LaTex file.  
   260 	// This file needs to be included 
   261 	// or inported into a LaTex document
   262 	// it will not add a preamble, or anything 
   263 	// that makes a full LaTex document.
   264   QFile file (outputFile);
   265   if ( !file.open( QIODevice::WriteOnly ) ) {
   266 	QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not write %1").arg(outputFile));
   267 	mainWindow->statusMessage(QString(QObject::tr("Export failed.")));
   268     return;
   269   }
   270   QTextStream ts( &file );	// use LANG decoding here...
   271   ts.setEncoding (QTextStream::UnicodeUTF8); // Force UTF8
   272   
   273   // Main loop over all branches
   274   QString s;
   275   // QString actIndent("");
   276   // int i;
   277   BranchObj *bo;
   278   bo=mapCenter->first();
   279   while (bo) {
   280 	if (bo->getDepth()==0);
   281 	else if (bo->getDepth()==1) {
   282 	  ts << ("\\chapter{" + bo->getHeading()+ "}\n");
   283 	}
   284 	else if (bo->getDepth()==2) {
   285 	  ts << ("\\section{" + bo->getHeading()+ "}\n");
   286 	}
   287 	else if (bo->getDepth()==3) {
   288 	  ts << ("\\subsection{" + bo->getHeading()+ "}\n");
   289 	}
   290 	else if (bo->getDepth()==4) {
   291 	  ts << ("\\subsubsection{" + bo->getHeading()+ "}\n");
   292 	}
   293 	else {
   294 	  ts << ("\\paragraph*{" + bo->getHeading()+ "}\n");
   295 	}
   296 	
   297 	// If necessary, write note
   298 	if (!bo->getNote().isEmpty()) {
   299 	  ts << (bo->getNoteASCII());
   300 	  ts << ("\n");
   301 	}
   302     bo=bo->next();
   303    }
   304   file.close();
   305 }
   306 
   307 ////////////////////////////////////////////////////////////////////////
   308 ExportOO::ExportOO()
   309 {
   310 	useSections=false;
   311 }
   312 
   313 ExportOO::~ExportOO()
   314 {
   315 }	
   316 
   317 QString ExportOO::buildList (BranchObj *current)
   318 {
   319     QString r;
   320     BranchObj *bo;
   321 
   322     uint i=0;
   323     bo=current->getFirstBranch();
   324     if (bo)
   325     {
   326         // Start list
   327         r+="<text:list text:style-name=\"vym-list\">\n";
   328         while (bo)
   329         {
   330 			r+="<text:list-item><text:p >";
   331 			r+=quotemeta(bo->getHeading());
   332 			// If necessary, write note
   333 			if (!bo->getNote().isEmpty())
   334 				r+=bo->getNoteOpenDoc();
   335 			r+="</text:p>";
   336 			r+=buildList (bo);	// recursivly add deeper branches
   337 			r+="</text:list-item>\n";
   338 			i++;
   339 			bo=current->getBranchNum(i);
   340         }
   341         r+="</text:list>\n";
   342     }
   343     return r;
   344 }
   345 
   346 
   347 void ExportOO::exportPresentation()
   348 {
   349 	QString allPages;
   350 
   351 	// Insert new content
   352 	content.replace ("<!-- INSERT TITLE -->",quotemeta(mapCenter->getHeading()));
   353 	content.replace ("<!-- INSERT AUTHOR -->",quotemeta(mapCenter->getAuthor()));
   354 
   355 	QString	onePage;
   356 	QString list;
   357 	
   358 	BranchObj *sectionBO=mapCenter->getFirstBranch();
   359     int i=0;
   360 	BranchObj *pagesBO;
   361     int j=0;
   362 
   363 	// Walk sections
   364 	while (sectionBO)
   365 	{
   366 		if (useSections)
   367 		{
   368 			// Add page with section title
   369 			onePage=sectionTemplate;
   370 			onePage.replace ("<!-- INSERT PAGE HEADING -->", quotemeta(sectionBO->getHeading() ) );
   371 			allPages+=onePage;
   372 		} else
   373 		{
   374 			i=-2;	// only use inner loop to 
   375 			        // turn mainbranches into pages
   376 			sectionBO=mapCenter;
   377 		}
   378 
   379 		// Walk mainpages
   380 		pagesBO=sectionBO->getFirstBranch();
   381 		j=0;
   382 		while (pagesBO)
   383 		{
   384 			// Add page with list of items
   385 			onePage=pageTemplate;
   386 			onePage.replace ("<!-- INSERT PAGE HEADING -->", quotemeta (pagesBO->getHeading() ) );
   387 			list=buildList (pagesBO);
   388 			onePage.replace ("<!-- INSERT LIST -->", list);
   389 			allPages+=onePage;
   390 			j++;
   391 			pagesBO=sectionBO->getBranchNum(j);
   392 		}
   393 		i++;
   394 		sectionBO=mapCenter->getBranchNum(i);
   395 	}
   396 	
   397 	content.replace ("<!-- INSERT PAGES -->",allPages);
   398 
   399 	// Write modified content
   400 	QFile f (contentFile);
   401     if ( !f.open( QIODevice::WriteOnly ) ) 
   402 	{
   403 		QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not write %1").arg(contentFile));
   404 		mainWindow->statusMessage(QString(QObject::tr("Export failed.")));
   405 		return;
   406     }
   407 
   408     QTextStream t( &f );
   409     t << content;
   410     f.close();
   411 
   412 	// zip tmpdir to destination
   413 	zipDir (tmpDir,outputFile);	
   414 }
   415 
   416 bool ExportOO::setConfigFile (const QString &cf)
   417 {
   418 	configFile=cf;
   419 	int i=cf.findRev ("/");
   420 	if (i>=0) configDir=cf.left(i);
   421 	SimpleSettings set;
   422 	set.readSettings(configFile);
   423 
   424 	// set paths
   425 	templateDir=configDir+"/"+set.readEntry ("Template");
   426 
   427 	QDir d (templateDir);
   428 	if (!d.exists())
   429 	{
   430 		QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Check \"%1\" in\n%2").arg("Template="+set.readEntry ("Template")).arg(configFile));
   431 		return false;
   432 
   433 	}
   434 
   435 	contentTemplateFile=templateDir+"content-template.xml";
   436 	contentFile=tmpDir.path()+"/content.xml";
   437 	pageTemplateFile=templateDir+"page-template.xml";
   438 	sectionTemplateFile=templateDir+"section-template.xml";
   439 
   440 	if (set.readEntry("useSections").contains("yes"))
   441 		useSections=true;
   442 
   443 	// Copy template to tmpdir
   444 	system ("cp -r "+templateDir+"* "+tmpDir.path());
   445 
   446 	// Read content-template
   447 	if (!loadStringFromDisk (contentTemplateFile,content))
   448 	{
   449 		QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not read %1").arg(contentTemplateFile));
   450 		return false;
   451 	}
   452 
   453 	// Read page-template
   454 	if (!loadStringFromDisk (pageTemplateFile,pageTemplate))
   455 	{
   456 		QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not read %1").arg(pageTemplateFile));
   457 		return false;
   458 	}
   459 	
   460 	// Read section-template
   461 	if (useSections && !loadStringFromDisk (sectionTemplateFile,sectionTemplate))
   462 	{
   463 		QMessageBox::critical (0,QObject::tr("Critical Export Error"),QObject::tr("Could not read %1").arg(sectionTemplateFile));
   464 		return false;
   465 	}
   466 	return true;
   467 }
   468