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