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