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