exports.cpp
author insilmaril
Tue, 24 Jan 2006 15:09:48 +0000
changeset 190 68c49789ec0b
parent 183 97e0bd468439
child 194 de97201180ea
permissions -rw-r--r--
Introduced basic export to Open Document format
     1 #include "exports.h"
     2 #include "file.h"
     3 #include "linkablemapobj.h"
     4 #include "misc.h"
     5 #include "texteditor.h"
     6 #include "mainwindow.h"
     7 
     8 extern Main *mainWindow;
     9 
    10 ExportBase::ExportBase()
    11 {
    12 	indentPerDepth="  ";
    13 }
    14 
    15 void ExportBase::setPath (const QString &p)
    16 {
    17 	filepath=p;
    18 }
    19 
    20 void ExportBase::setMapCenter(MapCenterObj *mc)
    21 {
    22 	mapCenter=mc;
    23 }
    24 
    25 QString ExportBase::getSectionString(BranchObj *bostart)
    26 {
    27 	QString r;
    28 	BranchObj *bo=bostart;
    29 	int depth=bo->getDepth();
    30 	while (depth>0)
    31 	{
    32 		r=QString("%1").arg(1+bo->getNum(),0,10)+"." + r;
    33 		bo=(BranchObj*)(bo->getParObj());
    34 		depth=bo->getDepth();
    35 	}	
    36 	if (r.isEmpty())
    37 		return r;
    38 	else	
    39 		return r + " ";
    40 }
    41 
    42 void ExportBase::exportXML()
    43 {
    44 	QFile file (filepath);
    45 	if ( !file.open( IO_WriteOnly ) )
    46 	{
    47 		// FIXME experimental, testing
    48 		cout << "ExportBase::exportXML  couldn't open "<<filepath<<endl;
    49 		return;
    50 	}
    51 	QTextStream ts( &file );	// use LANG decoding here...
    52 
    53 	// Main loop over all branches
    54 	QString s;
    55 	QString actIndent("");
    56 	int i;
    57 	uint j;
    58 	BranchObj *bo;
    59 	bo=mapCenter->first();
    60 	while (bo) 
    61 	{
    62 		// Make indentstring
    63 		for (i=0;i<bo->getDepth();i++) actIndent+= indentPerDepth;
    64 
    65 		// Write heading
    66 		//	write (actIndent + getSectionString(bo) + bo->getHeading()+ "\n");
    67 		if (bo->getDepth()==0)
    68 		{
    69 			ts << (bo->getHeading()+ "\n");
    70 			for (j=0;j<bo->getHeading().length();j++) ts<<"=";
    71 			ts << "\n";
    72 		} else 	if (bo->getDepth()==1)
    73 			ts << ("\n"+getSectionString(bo) + bo->getHeading()+ "\n");
    74 		else	if (bo->getDepth()==2)
    75 			ts << (actIndent + " o " + bo->getHeading()+ "\n");
    76 		else	
    77 			ts << (actIndent + " - " + bo->getHeading()+ "\n");
    78 		
    79 		// If necessary, write note
    80 		if (!bo->getNote().isEmpty())
    81 		{
    82 			s =bo->getNoteASCII();
    83 			s=s.replace ("\n","\n"+actIndent);
    84 			ts << (s+"\n\n");
    85 		}
    86 		
    87 		bo=bo->next();
    88 		actIndent="";
    89 	}
    90 	file.close();
    91 }
    92 
    93 void ExportLaTeX::exportLaTeX() 
    94 {
    95 	// Exports a map to a LaTex file.  
    96 	// This file needs to be included 
    97 	// or inported into a LaTex document
    98 	// it will not add a preamble, or anything 
    99 	// that makes a full LaTex document.
   100   QFile file (filepath);
   101   if ( !file.open( IO_WriteOnly ) ) {
   102     // FIXME
   103     cout << "Export::exportMap  couldn't open "<<filepath<<endl;
   104     return;
   105   }
   106   QTextStream ts( &file );	// use LANG decoding here...
   107   ts.setEncoding (QTextStream::UnicodeUTF8); // Force UTF8
   108   
   109   // Main loop over all branches
   110   QString s;
   111   // QString actIndent("");
   112   // int i;
   113   BranchObj *bo;
   114   bo=mapCenter->first();
   115   while (bo) {
   116     if (bo->getDepth()==0);
   117     else if (bo->getDepth()==1) {
   118       ts << ("\\chapter{" + bo->getHeading()+ "}\n");
   119     }
   120     else if (bo->getDepth()==2) {
   121       ts << ("\\section{" + bo->getHeading()+ "}\n");
   122     }
   123     else if (bo->getDepth()==3) {
   124       ts << ("\\subsection{" + bo->getHeading()+ "}\n");
   125     }
   126     else if (bo->getDepth()==4) {
   127       ts << ("\\subsubsection{" + bo->getHeading()+ "}\n");
   128     }
   129     else {
   130       ts << ("\\paragraph*{" + bo->getHeading()+ "}\n");
   131     }
   132     
   133     // If necessary, write note
   134     if (!bo->getNote().isEmpty()) {
   135       ts << (bo->getNoteASCII());
   136       ts << ("\n");
   137     }
   138     
   139     bo=bo->next();
   140    }
   141   file.close();
   142 }
   143 
   144 ExportOO::ExportOO()
   145 {
   146 	// Create tmpdir and set paths
   147 	tmpDir.setPath (makeUniqueDir("/tmp/vym-XXXXXX"));
   148 	templateDir="exports/novell/template/";
   149 	contentTemplateFile=templateDir+"content-template.xml";
   150 	contentFile=tmpDir.path()+"/content.xml";
   151 	pageTemplateFile=templateDir+"page-template.xml";
   152 	chapterTemplateFile=templateDir+"chapter-template.xml";
   153 
   154 	outputFile=tmpDir.currentDirPath()+"/test.odp";
   155 }
   156 
   157 ExportOO::~ExportOO()
   158 {
   159 	// Remove tmpdir
   160 	removeDir (tmpDir);
   161 }	
   162 
   163 QString ExportOO::buildList (BranchObj *current)
   164 {
   165     QString r;
   166     BranchObj *bo;
   167 
   168     uint i=0;
   169     bo=current->getFirstBranch();
   170     if (bo)
   171     {
   172         // Start list
   173         r+="<text:list text:style-name=\"L4\">\n";
   174         while (bo)
   175         {
   176             r+="<text:list-item><text:p >";
   177 			r+=quotemeta(bo->getHeading());
   178 			// If necessary, write note
   179 			if (!bo->getNote().isEmpty())
   180 				r+=bo->getNoteOpenDoc();
   181 			r+="</text:p>";
   182 			r+=buildList (bo);	// recursivly add deeper branches
   183             r+="</text:list-item>\n";
   184 			i++;
   185 			bo=current->getBranchNum(i);
   186         }
   187         r+="</text:list>\n";
   188     }
   189     return r;
   190 }
   191 
   192 
   193 void ExportOO::exportPresentation()
   194 {
   195 	QString allPages;
   196 
   197 	bool useChapters=true;
   198 
   199 	// Insert new content
   200 	content.replace ("<!-- INSERT TITLE -->",quotemeta(mapCenter->getHeading()));
   201 	content.replace ("<!-- INSERT AUTHOR -->",quotemeta(mapCenter->getAuthor()));
   202 
   203 	QString	onePage;
   204 	QString list;
   205 	
   206 	BranchObj *chapterBO=mapCenter->getFirstBranch();
   207     int i=0;
   208 	BranchObj *pagesBO;
   209     int j=0;
   210 
   211 	// Walk chapters
   212 	while (chapterBO)
   213 	{
   214 		if (useChapters)
   215 		{
   216 			// Add page with chapter title
   217 			onePage=chapterTemplate;
   218 			onePage.replace ("<!-- INSERT PAGE HEADING -->", quotemeta(chapterBO->getHeading() ) );
   219 			allPages+=onePage;
   220 		} else
   221 		{
   222 			i=-2;	// only use inner loop to 
   223 			        // turn mainbranches into pages
   224 			chapterBO=mapCenter;
   225 		}
   226 
   227 		// Walk mainpages
   228 		pagesBO=chapterBO->getFirstBranch();
   229 		j=0;
   230 		while (pagesBO)
   231 		{
   232 			// Add page with list of items
   233 			onePage=pageTemplate;
   234 			onePage.replace ("<!-- INSERT PAGE HEADING -->", quotemeta (pagesBO->getHeading() ) );
   235 			list=buildList (pagesBO);
   236 			onePage.replace ("<!-- INSERT LIST -->", list);
   237 			allPages+=onePage;
   238 			j++;
   239 			pagesBO=chapterBO->getBranchNum(j);
   240 		}
   241 		i++;
   242 		chapterBO=mapCenter->getBranchNum(i);
   243 	}
   244 	
   245 	content.replace ("<!-- INSERT PAGES -->",allPages);
   246 
   247 	// Write modified content
   248 	QFile f (contentFile);
   249     if ( !f.open( IO_WriteOnly ) ) 
   250 	{
   251 		mainWindow->statusMessage(QString(QObject::tr("Could not write to %1")).arg(outputFile));
   252 		return;
   253     }
   254 
   255     QTextStream t( &f );
   256     t << content;
   257     f.close();
   258 
   259 	// zip tmpdir to destination
   260 	zipDir (tmpDir,outputFile);	
   261 }
   262 
   263 void ExportOO::setConfigFile (const QString &cf)
   264 {
   265 	configFile=cf;
   266 	int i=cf.findRev ("/");
   267 	if (i>=0) configDir=cf.left(i);
   268 	SimpleSettings set;
   269 	set.readSettings(configFile);
   270 
   271 	// set paths
   272 	templateDir=configDir+"/"+set.readEntry ("Template");
   273 
   274 	//FIXME check if templateDir is really set, otherwise abort
   275 
   276 	contentTemplateFile=templateDir+"content-template.xml";
   277 	contentFile=tmpDir.path()+"/content.xml";
   278 	pageTemplateFile=templateDir+"page-template.xml";
   279 	chapterTemplateFile=templateDir+"chapter-template.xml";
   280 
   281 	outputFile=tmpDir.currentDirPath()+"/test.odp";
   282 	
   283 	// Copy template to tmpdir
   284 	system ("cp -r "+templateDir+"* "+tmpDir.path());
   285 	
   286 
   287 	// Read content-template
   288 	if (!loadStringFromDisk (contentTemplateFile,content))
   289 	{
   290 		qWarning ("Export::exportOO() Couldn't load from "+contentTemplateFile);
   291 		return;
   292 	}
   293 
   294 	// Read page-template
   295 	if (!loadStringFromDisk (pageTemplateFile,pageTemplate))
   296 	{
   297 		qWarning ("Export::exportOO() Couldn't load from "+pageTemplateFile);
   298 		return;
   299 	}
   300 	
   301 	// Read chapter-template
   302 	if (!loadStringFromDisk (chapterTemplateFile,chapterTemplate))
   303 	{
   304 		qWarning ("Export::exportOO() Couldn't load from "+chapterTemplateFile);
   305 		return;
   306 	}
   307 
   308 }
   309