file.cpp
author insilmaril
Tue, 23 Jan 2007 11:50:53 +0000
changeset 422 07a2f3f31101
parent 412 8059b6aa74d7
child 428 9ae68208e2ff
permissions -rw-r--r--
1.8.65 Various fixes
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@195
   129
void makeSubDirs (const QString &s)
insilmaril@195
   130
{
insilmaril@195
   131
	QDir d(s);
insilmaril@195
   132
	d.mkdir(s);
insilmaril@195
   133
	d.mkdir ("images");	
insilmaril@195
   134
	d.mkdir ("flags");	
insilmaril@195
   135
}
insilmaril@195
   136
insilmaril@205
   137
ErrorCode zipDir (const QDir &zipDir, const QString &zipName)
insilmaril@195
   138
{
insilmaril@205
   139
	ErrorCode err=success;
insilmaril@195
   140
	
insilmaril@195
   141
	// zip the temporary directory
insilmaril@408
   142
	QStringList args;
insilmaril@195
   143
	Process *zipProc=new Process ();
insilmaril@408
   144
	zipProc->setWorkingDirectory (zipDir.path());
insilmaril@408
   145
	args <<"-r";
insilmaril@408
   146
	args <<zipName;
insilmaril@408
   147
	args <<".";
insilmaril@195
   148
insilmaril@408
   149
	zipProc->start ("zip",args);
insilmaril@408
   150
	if (!zipProc->waitForStarted() )
insilmaril@195
   151
	{	
insilmaril@195
   152
		// zip could not be started
insilmaril@195
   153
		QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
insilmaril@195
   154
					   QObject::tr("Couldn't start zip to compress data."));
insilmaril@195
   155
		err=aborted;
insilmaril@195
   156
	} else
insilmaril@195
   157
	{
insilmaril@195
   158
		// zip could be started
insilmaril@408
   159
		zipProc->waitForFinished();
insilmaril@408
   160
		if (zipProc->exitStatus()!=QProcess::NormalExit )
insilmaril@195
   161
		{
insilmaril@195
   162
			QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
insilmaril@195
   163
						   QObject::tr("zip didn't exit normally")+
insilmaril@195
   164
						   "\n" + zipProc->getErrout());
insilmaril@195
   165
			err=aborted;
insilmaril@195
   166
		} else
insilmaril@195
   167
		{
insilmaril@408
   168
			if (zipProc->exitCode()>0)
insilmaril@195
   169
			{
insilmaril@195
   170
				QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
insilmaril@408
   171
						   QString("zip exit code:  %1").arg(zipProc->exitCode() )+
insilmaril@195
   172
						   "\n" + zipProc->getErrout() );
insilmaril@195
   173
				err=aborted;
insilmaril@195
   174
			}
insilmaril@195
   175
		}
insilmaril@195
   176
	}	// zip could be started
insilmaril@195
   177
	return err;	
insilmaril@195
   178
}
insilmaril@195
   179
insilmaril@205
   180
ErrorCode unzipDir (const QDir &zipDir, const QString &zipName)
insilmaril@195
   181
{
insilmaril@205
   182
	ErrorCode err=success;
insilmaril@195
   183
insilmaril@195
   184
	// Try to unzip file
insilmaril@408
   185
	QStringList args;
insilmaril@195
   186
	Process *zipProc=new Process ();
insilmaril@408
   187
	zipProc->setWorkingDirectory (zipDir.path());
insilmaril@408
   188
	args << "-o";	// overwrite existing files!
insilmaril@408
   189
	args << zipName ;
insilmaril@408
   190
	args << "-d";
insilmaril@408
   191
	args << zipDir.path();
insilmaril@195
   192
insilmaril@408
   193
	zipProc->start ("unzip",args);
insilmaril@408
   194
	if (!zipProc->waitForStarted() )
insilmaril@195
   195
	{
insilmaril@195
   196
		QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
insilmaril@195
   197
					   QObject::tr("Couldn't start unzip to decompress data."));
insilmaril@195
   198
		err=aborted;
insilmaril@195
   199
		
insilmaril@195
   200
	} else
insilmaril@195
   201
	{
insilmaril@408
   202
		zipProc->waitForFinished();
insilmaril@408
   203
		if (zipProc->exitStatus()!=QProcess::NormalExit )
insilmaril@195
   204
		{
insilmaril@195
   205
			QMessageBox::critical( 0,QObject::tr( "Critical Error" ),
insilmaril@195
   206
						   QObject::tr("unzip didn't exit normally") +
insilmaril@195
   207
						   zipProc->getErrout() );
insilmaril@195
   208
			err=aborted;
insilmaril@195
   209
		} else
insilmaril@195
   210
		{
insilmaril@408
   211
			if (zipProc->exitCode()>0)
insilmaril@195
   212
			{
insilmaril@195
   213
				if (zipProc->exitStatus()==9)
insilmaril@195
   214
					// no zipped file, but maybe .xml or old version? Try again.
insilmaril@195
   215
					err=nozip;
insilmaril@195
   216
				else	
insilmaril@195
   217
				{
insilmaril@195
   218
					QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
insilmaril@408
   219
								   QString("unzip exit code:  %1").arg(zipProc->exitCode() ) +
insilmaril@195
   220
								   zipProc->getErrout() );
insilmaril@195
   221
					err=aborted;
insilmaril@195
   222
				}
insilmaril@195
   223
			} 
insilmaril@195
   224
		}
insilmaril@195
   225
	}
insilmaril@195
   226
	return err;	
insilmaril@195
   227
}
insilmaril@195
   228
insilmaril@195
   229
bool loadStringFromDisk (const QString &fname, QString &s)
insilmaril@195
   230
{
insilmaril@195
   231
	s="";
insilmaril@195
   232
	QFile file ( fname);
insilmaril@366
   233
	if ( !file.open( QIODevice::ReadOnly ) ) return false;
insilmaril@195
   234
insilmaril@195
   235
	QTextStream ts( &file );
insilmaril@195
   236
	ts.setEncoding (QTextStream::UnicodeUTF8);
insilmaril@195
   237
	while ( !ts.atEnd() ) 
insilmaril@195
   238
		s+=ts.readLine()+"\n"; 
insilmaril@195
   239
	file.close();
insilmaril@195
   240
	return true;
insilmaril@195
   241
}
insilmaril@195
   242
insilmaril@195
   243
bool saveStringToDisk (const QString &fname, const QString &s)
insilmaril@195
   244
{
insilmaril@195
   245
	QFile file( fname);
insilmaril@195
   246
insilmaril@195
   247
	file.setName ( fname);
insilmaril@366
   248
	if ( !file.open( QIODevice::WriteOnly ) ) 
insilmaril@195
   249
	{
insilmaril@195
   250
		file.close();
insilmaril@195
   251
		return false;
insilmaril@195
   252
	}	
insilmaril@195
   253
insilmaril@195
   254
	// Write it finally, and write in UTF8, no matter what 
insilmaril@195
   255
	QTextStream ts( &file );
insilmaril@195
   256
	ts.setEncoding (QTextStream::UnicodeUTF8);
insilmaril@195
   257
	ts << s;
insilmaril@195
   258
	file.close();
insilmaril@195
   259
	return true;
insilmaril@195
   260
}
insilmaril@195
   261
insilmaril@195
   262
insilmaril@234
   263
ImagePreview::ImagePreview (QWidget *par=0): QLabel (par)
insilmaril@195
   264
{
insilmaril@366
   265
	fdia=(Q3FileDialog*)par;
insilmaril@195
   266
}
insilmaril@195
   267
insilmaril@366
   268
void ImagePreview::previewUrl( const Q3Url &u )
insilmaril@195
   269
{
insilmaril@195
   270
    QString path = u.path();
insilmaril@195
   271
    QPixmap pix( path );
insilmaril@195
   272
    if ( pix.isNull() )
insilmaril@234
   273
	{
insilmaril@234
   274
		// Strange: If we have fd->setMode (QFileDialog::ExistingFiles)
insilmaril@234
   275
		// in the filedialog, then there are 3 calls to previewURL 
insilmaril@234
   276
		// for each selection. And only the first is the actual selected file
insilmaril@234
   277
		// while the following 2 point to the directory above the current one.
insilmaril@234
   278
		// So here's my workaround:
insilmaril@234
   279
		
insilmaril@234
   280
		if (fdia && fdia->selectedFiles().count()==0)
insilmaril@234
   281
			setText( QObject::tr("This is not an image.") );
insilmaril@234
   282
		if (fdia &&fdia->selectedFiles().count()>1)
insilmaril@234
   283
			setText( QObject::tr("Sorry, no preview for\nmultiple selected files.") );
insilmaril@234
   284
	}	
insilmaril@195
   285
    else
insilmaril@195
   286
	{
insilmaril@195
   287
		float max_w=300;
insilmaril@195
   288
		float max_h=300;
insilmaril@195
   289
		float r;
insilmaril@195
   290
		if (pix.width()>max_w)
insilmaril@195
   291
		{
insilmaril@195
   292
			r=max_w / pix.width();
insilmaril@195
   293
			pix.resize(qRound(pix.width()*r), qRound(pix.height()*r));
insilmaril@412
   294
			// TODO not a resize, but a shrink/enlarge is needed here...
insilmaril@195
   295
		}
insilmaril@195
   296
		if (pix.height()>max_h)
insilmaril@195
   297
		{
insilmaril@195
   298
			r=max_h / pix.height();
insilmaril@195
   299
			pix.resize(qRound(pix.width()*r), qRound(pix.height()*r));
insilmaril@412
   300
			// TODO not a resize, but a shrink/enlarge is needed here...
insilmaril@195
   301
		}
insilmaril@195
   302
        setPixmap( pix );
insilmaril@195
   303
	}	
insilmaril@195
   304
}
insilmaril@195
   305
insilmaril@366
   306
ImageIO::ImageIO ()
insilmaril@366
   307
{
insilmaril@366
   308
	// Create list with supported image types
insilmaril@366
   309
	// foreach (QByteArray format, QImageWriter::supportedImageFormats()) 
insilmaril@366
   310
	// imageTypes.append( tr("%1...").arg(QString(format).toUpper()));
insilmaril@366
   311
	imageFilters.append ("Images (*.png *.jpg *.jpeg *.bmp *.bmp *.ppm *.xpm *.xbm)");
insilmaril@366
   312
	imageTypes.append ("PNG");
insilmaril@366
   313
	imageFilters.append ("Portable Network Graphics (*.png)");
insilmaril@366
   314
	imageTypes.append ("PNG");
insilmaril@366
   315
	imageFilters.append ("Joint Photographic Experts Group (*.jpg)");
insilmaril@366
   316
	imageTypes.append ("JPG");
insilmaril@366
   317
	imageFilters.append ("Joint Photographic Experts Group (*.jpeg)");
insilmaril@366
   318
	imageTypes.append ("JPG");
insilmaril@366
   319
	imageFilters.append ("Windows Bitmap (*.bmp)");
insilmaril@366
   320
	imageTypes.append ("BMP");
insilmaril@366
   321
	imageFilters.append ("Portable Pixmap (*.ppm)");
insilmaril@366
   322
	imageTypes.append ("PPM");
insilmaril@366
   323
	imageFilters.append ("X11 Bitmap (*.xpm)");
insilmaril@366
   324
	imageTypes.append ("XPM");
insilmaril@366
   325
	imageFilters.append ("X11 Bitmap (*.xbm)");
insilmaril@366
   326
	imageTypes.append ("XBM");
insilmaril@366
   327
}
insilmaril@366
   328
insilmaril@366
   329
QStringList ImageIO::getFilters()
insilmaril@366
   330
{
insilmaril@366
   331
	return imageFilters;
insilmaril@366
   332
}
insilmaril@366
   333
insilmaril@366
   334
QString ImageIO::getType(QString filter)
insilmaril@366
   335
{
insilmaril@366
   336
	for (int i=0;i<imageFilters.count()+1;i++)
insilmaril@366
   337
		if (imageFilters.at(i)==filter) return imageTypes.at(i);
insilmaril@366
   338
	return QString();	
insilmaril@366
   339
}
insilmaril@366
   340
insilmaril@366
   341