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