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