exports.cpp
author insilmaril
Tue, 29 Aug 2006 08:31:42 +0000
branchqt4-port
changeset 16 41c3d7f9f532
parent 9 f94317a94db1
child 18 70c41284cb48
permissions -rw-r--r--
Runs basically on Mac now. Undo debug output still enabled
     1 #include <q3filedialog.h>
     2 #include <qmessagebox.h>
     3 #include <QProcess>
     4 //Added by qt3to4:
     5 #include <QTextStream>
     6 
     7 #include "exports.h"
     8 #include "file.h"
     9 #include "linkablemapobj.h"
    10 #include "misc.h"
    11 #include "mainwindow.h"
    12 #include "warningdialog.h"
    13 #include "xsltproc.h"
    14 
    15 extern Main *mainWindow;
    16 extern QDir vymBaseDir;
    17 
    18 
    19 ExportBase::ExportBase()
    20 {
    21 	indentPerDepth="  ";
    22 	// Create tmpdir
    23 	tmpDir.setPath (makeUniqueDir("/tmp/vym-XXXXXX"));
    24 }
    25 
    26 ExportBase::~ExportBase()
    27 {
    28 	// Remove tmpdir
    29 	removeDir (tmpDir);
    30 }
    31 
    32 void ExportBase::setDir(const QString &p)
    33 {
    34 	outputDir=p;
    35 }
    36 
    37 void ExportBase::setFile (const QString &p)
    38 {
    39 	outputFile=p;
    40 }
    41 
    42 void ExportBase::setMapCenter(MapCenterObj *mc)
    43 {
    44 	mapCenter=mc;
    45 }
    46 
    47 void ExportBase::setCaption (const QString &s)
    48 {
    49 	caption=s;
    50 }
    51 
    52 void ExportBase::addFilter(const QString &s)
    53 {
    54 	filter=s;
    55 }
    56 
    57 bool ExportBase::execDialog()
    58 {
    59 	if (mapCenter && mapCenter->getMapEditor())
    60 	{
    61 		QFileDialog *fd=new QFileDialog( mapCenter->getMapEditor(), caption);
    62 		fd->setFilter (filter);
    63 		fd->setCaption(caption);
    64 		fd->setMode( QFileDialog::AnyFile );
    65 		fd->show();
    66 
    67 		if ( fd->exec() == QDialog::Accepted )
    68 		{
    69 			if (QFile (fd->selectedFile()).exists() )
    70 			{
    71 				QMessageBox mb( __VYM,
    72 					QObject::tr("The file %1 exists already.\nDo you want to overwrite it?").arg(fd->selectedFile()), 
    73 				QMessageBox::Warning,
    74 				QMessageBox::Yes | QMessageBox::Default,
    75 				QMessageBox::Cancel | QMessageBox::Escape,
    76 				Qt::NoButton );
    77 				mb.setButtonText( QMessageBox::Yes, QObject::tr("Overwrite") );
    78 				mb.setButtonText( QMessageBox::No, QObject::tr("Cancel"));
    79 				ExportBase ex;
    80 				switch( mb.exec() ) 
    81 				{
    82 					case QMessageBox::Yes:
    83 						// save 
    84 						break;;
    85 					case QMessageBox::Cancel:
    86 						// return, do nothing
    87 						return false;
    88 						break;
    89 				}
    90 			}
    91 			outputFile=fd->selectedFile();
    92 			return true;
    93 		}
    94 	}
    95 	return false;
    96 }
    97 
    98 QString ExportBase::getSectionString(BranchObj *bostart)
    99 {
   100 	// Make prefix like "2.5.3" for "bo:2,bo:5,bo:3"
   101 	QString r;
   102 	BranchObj *bo=bostart;
   103 	int depth=bo->getDepth();
   104 	while (depth>0)
   105 	{
   106 		r=QString("%1").arg(1+bo->getNum(),0,10)+"." + r;
   107 		bo=(BranchObj*)(bo->getParObj());
   108 		depth=bo->getDepth();
   109 	}	
   110 	if (r.isEmpty())
   111 		return r;
   112 	else	
   113 		return r + " ";
   114 }
   115 
   116 
   117 ////////////////////////////////////////////////////////////////////////
   118 void ExportASCII::doExport()
   119 {
   120 	QFile file (outputFile);
   121 	if ( !file.open( QIODevice::WriteOnly ) )
   122 	{
   123 		// FIXME experimental, testing
   124 		qWarning ("ExportBase::exportXML  couldn't open "+outputFile);
   125 		return;
   126 	}
   127 	QTextStream ts( &file );	// use LANG decoding here...
   128 
   129 	// Main loop over all branches
   130 	QString s;
   131 	QString actIndent("");
   132 	int i,j;
   133 	BranchObj *bo;
   134 	bo=mapCenter->first();
   135 	while (bo) 
   136 	{
   137 		// Make indentstring
   138 		for (i=0;i<bo->getDepth();i++) actIndent+= indentPerDepth;
   139 
   140 		if (bo->getDepth()==0)
   141 		{
   142 			ts << (bo->getHeading()+ "\n");
   143 			for (j=0;j<bo->getHeading().length();j++) ts<<"=";
   144 			ts << "\n";
   145 		} else 	if (bo->getDepth()==1)
   146 			ts << ("\n"+getSectionString(bo) + bo->getHeading()+ "\n");
   147 		else	if (bo->getDepth()==2)
   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.setCancelButton (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("/vym/warnings/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.setCancelButton (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