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