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