file.cpp
author insilmaril
Wed, 20 Jun 2007 10:51:44 +0000
changeset 515 f396157bbb06
parent 502 f3465a5f0dc4
child 521 27cb122a88a6
permissions -rw-r--r--
scripted exports (continued)
insilmaril@408
     1
#include <QMessageBox>
insilmaril@366
     2
#include <QPixmap>
insilmaril@366
     3
#include <QLabel>
insilmaril@366
     4
#include <QTextStream>
insilmaril@195
     5
#include <iostream>
insilmaril@195
     6
insilmaril@195
     7
#include "file.h"
insilmaril@195
     8
#include "process.h"
insilmaril@195
     9
insilmaril@195
    10
insilmaril@195
    11
QString maskPath(QString p)
insilmaril@195
    12
{
insilmaril@195
    13
	// Change " " to "\ " to enable blanks in filenames
insilmaril@195
    14
	p=p.replace(QChar('&'),"\\&");
insilmaril@195
    15
	return p.replace(QChar(' '),"\\ ");
insilmaril@195
    16
}
insilmaril@195
    17
insilmaril@195
    18
QString convertToRel (const QString &src, const QString &dst)
insilmaril@195
    19
{
insilmaril@195
    20
	QString s=src;
insilmaril@195
    21
	QString d=dst;
insilmaril@195
    22
	int i;
insilmaril@195
    23
insilmaril@195
    24
	if (s==d) 
insilmaril@195
    25
	{
insilmaril@195
    26
		// Special case, we just need the name of the file,
insilmaril@195
    27
		// not the complete path
insilmaril@195
    28
		i=d.findRev ("/");
insilmaril@195
    29
		d=d.right (d.length()-i-1);
insilmaril@195
    30
	} else
insilmaril@195
    31
	{
insilmaril@195
    32
		// Find relative path from src to dst
insilmaril@195
    33
insilmaril@195
    34
		// Remove the first "/"
insilmaril@195
    35
		if (s.section ("/",0,0).isEmpty()) 
insilmaril@195
    36
		{
insilmaril@195
    37
			s=s.right (s.length()-1);
insilmaril@195
    38
			d=d.right (d.length()-1);
insilmaril@195
    39
		}
insilmaril@195
    40
		
insilmaril@195
    41
		// remove identical left parts
insilmaril@195
    42
		while (s.section("/",0,0) == d.section("/",0,0) ) 
insilmaril@195
    43
		{
insilmaril@195
    44
			i=s.find ("/");
insilmaril@195
    45
			s=s.right (s.length()-i-1);
insilmaril@195
    46
			d=d.right (d.length()-i-1);
insilmaril@195
    47
		}
insilmaril@195
    48
insilmaril@255
    49
		// Now take care of paths where we have to go back first
insilmaril@366
    50
		int srcsep=s.count("/");
insilmaril@366
    51
		int dstsep=d.count("/");
insilmaril@255
    52
		if (srcsep <=  dstsep )
insilmaril@195
    53
		{
insilmaril@195
    54
			// find path to go up first and then back to dst
insilmaril@195
    55
			i=1;
insilmaril@195
    56
			while (i<=srcsep) 
insilmaril@195
    57
			{
insilmaril@195
    58
				d="../"+d;
insilmaril@195
    59
				i++;
insilmaril@195
    60
			}	
insilmaril@195
    61
		}
insilmaril@195
    62
	}	
insilmaril@195
    63
	return d;
insilmaril@195
    64
}
insilmaril@195
    65
insilmaril@366
    66
QString makeUniqueDir (bool &ok,QString s)
insilmaril@195
    67
{
insilmaril@195
    68
	// Create unique directory e.g. s="/tmp/vym-XXXXXX"
insilmaril@195
    69
insilmaril@195
    70
	// Convert QString to string first
insilmaril@366
    71
	ok=true;
insilmaril@195
    72
	char *p;
insilmaril@195
    73
	int bytes=s.length();
insilmaril@195
    74
	p=(char*) malloc (bytes+1);
insilmaril@195
    75
	int i;
insilmaril@195
    76
	for (i=0;i<bytes;i++)
insilmaril@195
    77
		p[i]=s.at(i).latin1();
insilmaril@195
    78
	p[bytes]=0;	
insilmaril@195
    79
	QString r=mkdtemp (p);
insilmaril@366
    80
	if (r.isEmpty()) ok=false;
insilmaril@195
    81
	free (p);
insilmaril@195
    82
	return r;
insilmaril@195
    83
}
insilmaril@195
    84
insilmaril@195
    85
void removeDir(QDir d)
insilmaril@195
    86
{
insilmaril@195
    87
	if (d.path().left(4)!="/tmp")
insilmaril@195
    88
	{
insilmaril@248
    89
		// This _should_ not be necessary, but proved to be useful ;-)
insilmaril@195
    90
		qWarning ("file.cpp::removeDir should remove "+d.path()+" - aborted.");
insilmaril@195
    91
		return;
insilmaril@195
    92
	}
insilmaril@195
    93
insilmaril@195
    94
	// Traverse directories
insilmaril@195
    95
	d.setFilter( QDir::Dirs| QDir::Hidden | QDir::NoSymLinks );
insilmaril@366
    96
	QFileInfoList list = d.entryInfoList();
insilmaril@366
    97
	QFileInfo fi;
insilmaril@195
    98
insilmaril@366
    99
	for (int i = 0; i < list.size(); ++i) 
insilmaril@195
   100
	{
insilmaril@366
   101
		fi=list.at(i);
insilmaril@366
   102
		if (fi.fileName() != "." && fi.fileName() != ".." )
insilmaril@195
   103
		{
insilmaril@366
   104
			if ( !d.cd(fi.fileName()) ) 
insilmaril@366
   105
				qWarning ("removeDir() cannot find the directory "+fi.fileName());
insilmaril@195
   106
			else 
insilmaril@195
   107
			{
insilmaril@195
   108
				// Recursively remove subdirs
insilmaril@195
   109
				removeDir (d);
insilmaril@195
   110
				d.cdUp();
insilmaril@195
   111
			}
insilmaril@195
   112
		}	
insilmaril@366
   113
	}
insilmaril@366
   114
insilmaril@195
   115
	// Traverse files
insilmaril@195
   116
	d.setFilter( QDir::Files| QDir::Hidden | QDir::NoSymLinks );
insilmaril@366
   117
	list = d.entryInfoList();
insilmaril@195
   118
insilmaril@366
   119
	for (int i = 0; i < list.size(); ++i) 
insilmaril@195
   120
	{
insilmaril@366
   121
		fi=list.at(i);
insilmaril@366
   122
		QFile (fi.filePath()).remove(); 
insilmaril@195
   123
	}	
insilmaril@195
   124
insilmaril@195
   125
	if (!d.rmdir(d.path()))
insilmaril@195
   126
		qWarning ("removeDir("+d.path()+") failed!");
insilmaril@195
   127
}		
insilmaril@195
   128
insilmaril@502
   129
void copyDir (QDir src, QDir dst)
insilmaril@502
   130
{
insilmaril@502
   131
	system ("cp -r "+src.path()+"/* "+dst.path());
insilmaril@502
   132
insilmaril@502
   133
	/*
insilmaril@502
   134
	ErrorCode err=success;
insilmaril@502
   135
insilmaril@502
   136
	Process *cpProc=new Process ();
insilmaril@502
   137
	QStringList args;
insilmaril@502
   138
	cpProc->setWorkingDirectory (src.path());
insilmaril@502
   139
	args <<"-r";
insilmaril@502
   140
	args <<src.path();
insilmaril@502
   141
	args <<dst.path();
insilmaril@502
   142
insilmaril@502
   143
	cpProc->start ("cp",args);
insilmaril@502
   144
	if (!cpProc->waitForStarted() )
insilmaril@502
   145
	{	
insilmaril@502
   146
		// zip could not be started
insilmaril@502
   147
		QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
insilmaril@502
   148
					   QObject::tr("Couldn't start zip to compress data."));
insilmaril@502
   149
		err=aborted;
insilmaril@502
   150
	} else
insilmaril@502
   151
	{
insilmaril@502
   152
		// zip could be started
insilmaril@502
   153
		cpProc->waitForFinished();
insilmaril@502
   154
		if (cpProc->exitStatus()!=QProcess::NormalExit )
insilmaril@502
   155
		{
insilmaril@502
   156
			QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
insilmaril@502
   157
						   QObject::tr("cp didn't exit normally")+
insilmaril@502
   158
						   "\n" + cpProc->getErrout());
insilmaril@502
   159
			err=aborted;
insilmaril@502
   160
		} else
insilmaril@502
   161
		{
insilmaril@502
   162
			if (cpProc->exitCode()>0)
insilmaril@502
   163
			{
insilmaril@502
   164
				QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
insilmaril@502
   165
						   QString("cp exit code:  %1").arg(cpProc->exitCode() )+
insilmaril@502
   166
						   "\n" + cpProc->getErrout() );
insilmaril@502
   167
				err=aborted;
insilmaril@502
   168
			}
insilmaril@502
   169
		}
insilmaril@502
   170
	}	// cp could be started
insilmaril@502
   171
	*/
insilmaril@502
   172
}
insilmaril@502
   173
insilmaril@195
   174
void makeSubDirs (const QString &s)
insilmaril@195
   175
{
insilmaril@195
   176
	QDir d(s);
insilmaril@195
   177
	d.mkdir(s);
insilmaril@195
   178
	d.mkdir ("images");	
insilmaril@195
   179
	d.mkdir ("flags");	
insilmaril@195
   180
}
insilmaril@195
   181
insilmaril@205
   182
ErrorCode zipDir (const QDir &zipDir, const QString &zipName)
insilmaril@195
   183
{
insilmaril@205
   184
	ErrorCode err=success;
insilmaril@195
   185
	
insilmaril@195
   186
	// zip the temporary directory
insilmaril@408
   187
	QStringList args;
insilmaril@195
   188
	Process *zipProc=new Process ();
insilmaril@408
   189
	zipProc->setWorkingDirectory (zipDir.path());
insilmaril@408
   190
	args <<"-r";
insilmaril@408
   191
	args <<zipName;
insilmaril@408
   192
	args <<".";
insilmaril@195
   193
insilmaril@408
   194
	zipProc->start ("zip",args);
insilmaril@408
   195
	if (!zipProc->waitForStarted() )
insilmaril@195
   196
	{	
insilmaril@195
   197
		// zip could not be started
insilmaril@195
   198
		QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
insilmaril@195
   199
					   QObject::tr("Couldn't start zip to compress data."));
insilmaril@195
   200
		err=aborted;
insilmaril@195
   201
	} else
insilmaril@195
   202
	{
insilmaril@195
   203
		// zip could be started
insilmaril@408
   204
		zipProc->waitForFinished();
insilmaril@408
   205
		if (zipProc->exitStatus()!=QProcess::NormalExit )
insilmaril@195
   206
		{
insilmaril@195
   207
			QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
insilmaril@195
   208
						   QObject::tr("zip didn't exit normally")+
insilmaril@195
   209
						   "\n" + zipProc->getErrout());
insilmaril@195
   210
			err=aborted;
insilmaril@195
   211
		} else
insilmaril@195
   212
		{
insilmaril@408
   213
			if (zipProc->exitCode()>0)
insilmaril@195
   214
			{
insilmaril@195
   215
				QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
insilmaril@408
   216
						   QString("zip exit code:  %1").arg(zipProc->exitCode() )+
insilmaril@195
   217
						   "\n" + zipProc->getErrout() );
insilmaril@195
   218
				err=aborted;
insilmaril@195
   219
			}
insilmaril@195
   220
		}
insilmaril@195
   221
	}	// zip could be started
insilmaril@195
   222
	return err;	
insilmaril@195
   223
}
insilmaril@195
   224
insilmaril@205
   225
ErrorCode unzipDir (const QDir &zipDir, const QString &zipName)
insilmaril@195
   226
{
insilmaril@205
   227
	ErrorCode err=success;
insilmaril@195
   228
insilmaril@195
   229
	// Try to unzip file
insilmaril@408
   230
	QStringList args;
insilmaril@195
   231
	Process *zipProc=new Process ();
insilmaril@408
   232
	zipProc->setWorkingDirectory (zipDir.path());
insilmaril@408
   233
	args << "-o";	// overwrite existing files!
insilmaril@408
   234
	args << zipName ;
insilmaril@408
   235
	args << "-d";
insilmaril@408
   236
	args << zipDir.path();
insilmaril@195
   237
insilmaril@408
   238
	zipProc->start ("unzip",args);
insilmaril@408
   239
	if (!zipProc->waitForStarted() )
insilmaril@195
   240
	{
insilmaril@195
   241
		QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
insilmaril@195
   242
					   QObject::tr("Couldn't start unzip to decompress data."));
insilmaril@195
   243
		err=aborted;
insilmaril@195
   244
		
insilmaril@195
   245
	} else
insilmaril@195
   246
	{
insilmaril@408
   247
		zipProc->waitForFinished();
insilmaril@408
   248
		if (zipProc->exitStatus()!=QProcess::NormalExit )
insilmaril@195
   249
		{
insilmaril@195
   250
			QMessageBox::critical( 0,QObject::tr( "Critical Error" ),
insilmaril@195
   251
						   QObject::tr("unzip didn't exit normally") +
insilmaril@195
   252
						   zipProc->getErrout() );
insilmaril@195
   253
			err=aborted;
insilmaril@195
   254
		} else
insilmaril@195
   255
		{
insilmaril@408
   256
			if (zipProc->exitCode()>0)
insilmaril@195
   257
			{
insilmaril@428
   258
				if (zipProc->exitCode()==9)
insilmaril@195
   259
					// no zipped file, but maybe .xml or old version? Try again.
insilmaril@195
   260
					err=nozip;
insilmaril@195
   261
				else	
insilmaril@195
   262
				{
insilmaril@195
   263
					QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
insilmaril@408
   264
								   QString("unzip exit code:  %1").arg(zipProc->exitCode() ) +
insilmaril@195
   265
								   zipProc->getErrout() );
insilmaril@195
   266
					err=aborted;
insilmaril@195
   267
				}
insilmaril@195
   268
			} 
insilmaril@195
   269
		}
insilmaril@195
   270
	}
insilmaril@195
   271
	return err;	
insilmaril@195
   272
}
insilmaril@195
   273
insilmaril@195
   274
bool loadStringFromDisk (const QString &fname, QString &s)
insilmaril@195
   275
{
insilmaril@195
   276
	s="";
insilmaril@195
   277
	QFile file ( fname);
insilmaril@366
   278
	if ( !file.open( QIODevice::ReadOnly ) ) return false;
insilmaril@195
   279
insilmaril@195
   280
	QTextStream ts( &file );
insilmaril@195
   281
	ts.setEncoding (QTextStream::UnicodeUTF8);
insilmaril@195
   282
	while ( !ts.atEnd() ) 
insilmaril@195
   283
		s+=ts.readLine()+"\n"; 
insilmaril@195
   284
	file.close();
insilmaril@195
   285
	return true;
insilmaril@195
   286
}
insilmaril@195
   287
insilmaril@195
   288
bool saveStringToDisk (const QString &fname, const QString &s)
insilmaril@195
   289
{
insilmaril@195
   290
	QFile file( fname);
insilmaril@195
   291
insilmaril@195
   292
	file.setName ( fname);
insilmaril@366
   293
	if ( !file.open( QIODevice::WriteOnly ) ) 
insilmaril@195
   294
	{
insilmaril@195
   295
		file.close();
insilmaril@195
   296
		return false;
insilmaril@195
   297
	}	
insilmaril@195
   298
insilmaril@195
   299
	// Write it finally, and write in UTF8, no matter what 
insilmaril@195
   300
	QTextStream ts( &file );
insilmaril@195
   301
	ts.setEncoding (QTextStream::UnicodeUTF8);
insilmaril@195
   302
	ts << s;
insilmaril@195
   303
	file.close();
insilmaril@195
   304
	return true;
insilmaril@195
   305
}
insilmaril@195
   306
insilmaril@195
   307
insilmaril@234
   308
ImagePreview::ImagePreview (QWidget *par=0): QLabel (par)
insilmaril@195
   309
{
insilmaril@366
   310
	fdia=(Q3FileDialog*)par;
insilmaril@195
   311
}
insilmaril@195
   312
insilmaril@366
   313
void ImagePreview::previewUrl( const Q3Url &u )
insilmaril@195
   314
{
insilmaril@195
   315
    QString path = u.path();
insilmaril@195
   316
    QPixmap pix( path );
insilmaril@195
   317
    if ( pix.isNull() )
insilmaril@234
   318
	{
insilmaril@234
   319
		// Strange: If we have fd->setMode (QFileDialog::ExistingFiles)
insilmaril@234
   320
		// in the filedialog, then there are 3 calls to previewURL 
insilmaril@234
   321
		// for each selection. And only the first is the actual selected file
insilmaril@234
   322
		// while the following 2 point to the directory above the current one.
insilmaril@234
   323
		// So here's my workaround:
insilmaril@234
   324
		
insilmaril@234
   325
		if (fdia && fdia->selectedFiles().count()==0)
insilmaril@234
   326
			setText( QObject::tr("This is not an image.") );
insilmaril@234
   327
		if (fdia &&fdia->selectedFiles().count()>1)
insilmaril@234
   328
			setText( QObject::tr("Sorry, no preview for\nmultiple selected files.") );
insilmaril@234
   329
	}	
insilmaril@195
   330
    else
insilmaril@195
   331
	{
insilmaril@195
   332
		float max_w=300;
insilmaril@195
   333
		float max_h=300;
insilmaril@195
   334
		float r;
insilmaril@195
   335
		if (pix.width()>max_w)
insilmaril@195
   336
		{
insilmaril@195
   337
			r=max_w / pix.width();
insilmaril@195
   338
			pix.resize(qRound(pix.width()*r), qRound(pix.height()*r));
insilmaril@412
   339
			// TODO not a resize, but a shrink/enlarge is needed here...
insilmaril@195
   340
		}
insilmaril@195
   341
		if (pix.height()>max_h)
insilmaril@195
   342
		{
insilmaril@195
   343
			r=max_h / pix.height();
insilmaril@195
   344
			pix.resize(qRound(pix.width()*r), qRound(pix.height()*r));
insilmaril@412
   345
			// TODO not a resize, but a shrink/enlarge is needed here...
insilmaril@195
   346
		}
insilmaril@195
   347
        setPixmap( pix );
insilmaril@195
   348
	}	
insilmaril@195
   349
}
insilmaril@195
   350
insilmaril@366
   351
ImageIO::ImageIO ()
insilmaril@366
   352
{
insilmaril@366
   353
	// Create list with supported image types
insilmaril@366
   354
	// foreach (QByteArray format, QImageWriter::supportedImageFormats()) 
insilmaril@366
   355
	// imageTypes.append( tr("%1...").arg(QString(format).toUpper()));
insilmaril@366
   356
	imageFilters.append ("Images (*.png *.jpg *.jpeg *.bmp *.bmp *.ppm *.xpm *.xbm)");
insilmaril@366
   357
	imageTypes.append ("PNG");
insilmaril@366
   358
	imageFilters.append ("Portable Network Graphics (*.png)");
insilmaril@366
   359
	imageTypes.append ("PNG");
insilmaril@366
   360
	imageFilters.append ("Joint Photographic Experts Group (*.jpg)");
insilmaril@366
   361
	imageTypes.append ("JPG");
insilmaril@366
   362
	imageFilters.append ("Joint Photographic Experts Group (*.jpeg)");
insilmaril@366
   363
	imageTypes.append ("JPG");
insilmaril@366
   364
	imageFilters.append ("Windows Bitmap (*.bmp)");
insilmaril@366
   365
	imageTypes.append ("BMP");
insilmaril@366
   366
	imageFilters.append ("Portable Pixmap (*.ppm)");
insilmaril@366
   367
	imageTypes.append ("PPM");
insilmaril@366
   368
	imageFilters.append ("X11 Bitmap (*.xpm)");
insilmaril@366
   369
	imageTypes.append ("XPM");
insilmaril@366
   370
	imageFilters.append ("X11 Bitmap (*.xbm)");
insilmaril@366
   371
	imageTypes.append ("XBM");
insilmaril@366
   372
}
insilmaril@366
   373
insilmaril@366
   374
QStringList ImageIO::getFilters()
insilmaril@366
   375
{
insilmaril@366
   376
	return imageFilters;
insilmaril@366
   377
}
insilmaril@366
   378
insilmaril@366
   379
QString ImageIO::getType(QString filter)
insilmaril@366
   380
{
insilmaril@366
   381
	for (int i=0;i<imageFilters.count()+1;i++)
insilmaril@366
   382
		if (imageFilters.at(i)==filter) return imageTypes.at(i);
insilmaril@366
   383
	return QString();	
insilmaril@366
   384
}
insilmaril@366
   385
insilmaril@366
   386