file.cpp
author insilmaril
Wed, 04 Jul 2007 11:05:52 +0000
changeset 541 2501bce00de9
parent 521 27cb122a88a6
child 606 84c73902f727
permissions -rw-r--r--
Version 1.9.0
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@521
    66
#include <QFileDialog>
insilmaril@521
    67
extern QString vymName;
insilmaril@521
    68
extern QDir lastFileDir;
insilmaril@521
    69
insilmaril@521
    70
QString browseDirectory (QWidget *parent,const QString &caption)
insilmaril@521
    71
{
insilmaril@521
    72
	QFileDialog fd(parent,caption);
insilmaril@521
    73
	fd.setMode (QFileDialog::DirectoryOnly);
insilmaril@521
    74
	fd.setCaption(vymName+ " - "+caption);
insilmaril@521
    75
	fd.setDir (lastFileDir);
insilmaril@521
    76
	fd.show();
insilmaril@521
    77
	
insilmaril@521
    78
	if ( fd.exec() == QDialog::Accepted )
insilmaril@521
    79
		return fd.selectedFile();
insilmaril@521
    80
	else
insilmaril@521
    81
		return "";
insilmaril@521
    82
}
insilmaril@521
    83
insilmaril@521
    84
insilmaril@521
    85
insilmaril@521
    86
bool reallyWriteDirectory(const QString &dir)
insilmaril@521
    87
{
insilmaril@521
    88
	QStringList eList = QDir(dir).entryList();
insilmaril@521
    89
	if (eList.first() ==".")  eList.pop_front();	// remove "."
insilmaril@521
    90
	if (eList.first() =="..") eList.pop_front();	// remove "."
insilmaril@521
    91
	if (!eList.isEmpty())
insilmaril@521
    92
	{
insilmaril@521
    93
		QMessageBox mb( vymName,
insilmaril@521
    94
			QObject::tr("The directory %1 is not empty.\nDo you risk to overwrite its contents?","write directory").arg(dir),
insilmaril@521
    95
		QMessageBox::Warning,
insilmaril@521
    96
		QMessageBox::Yes ,
insilmaril@521
    97
		QMessageBox::Cancel | QMessageBox::Default,
insilmaril@521
    98
		QMessageBox::QMessageBox::NoButton );
insilmaril@521
    99
insilmaril@521
   100
		mb.setButtonText( QMessageBox::Yes, QObject::tr("Overwrite") );
insilmaril@521
   101
		mb.setButtonText( QMessageBox::No, QObject::tr("Cancel"));
insilmaril@521
   102
		switch( mb.exec() ) 
insilmaril@521
   103
		{
insilmaril@521
   104
			case QMessageBox::Yes:
insilmaril@521
   105
				// save 
insilmaril@521
   106
				return true;
insilmaril@521
   107
			case QMessageBox::Cancel:
insilmaril@521
   108
				// do nothing
insilmaril@521
   109
				return false;
insilmaril@521
   110
		}
insilmaril@521
   111
	}
insilmaril@521
   112
	return true;
insilmaril@521
   113
}
insilmaril@521
   114
insilmaril@366
   115
QString makeUniqueDir (bool &ok,QString s)
insilmaril@195
   116
{
insilmaril@195
   117
	// Create unique directory e.g. s="/tmp/vym-XXXXXX"
insilmaril@195
   118
insilmaril@195
   119
	// Convert QString to string first
insilmaril@366
   120
	ok=true;
insilmaril@195
   121
	char *p;
insilmaril@195
   122
	int bytes=s.length();
insilmaril@195
   123
	p=(char*) malloc (bytes+1);
insilmaril@195
   124
	int i;
insilmaril@195
   125
	for (i=0;i<bytes;i++)
insilmaril@195
   126
		p[i]=s.at(i).latin1();
insilmaril@195
   127
	p[bytes]=0;	
insilmaril@195
   128
	QString r=mkdtemp (p);
insilmaril@366
   129
	if (r.isEmpty()) ok=false;
insilmaril@195
   130
	free (p);
insilmaril@195
   131
	return r;
insilmaril@195
   132
}
insilmaril@195
   133
insilmaril@195
   134
void removeDir(QDir d)
insilmaril@195
   135
{
insilmaril@195
   136
	if (d.path().left(4)!="/tmp")
insilmaril@195
   137
	{
insilmaril@248
   138
		// This _should_ not be necessary, but proved to be useful ;-)
insilmaril@195
   139
		qWarning ("file.cpp::removeDir should remove "+d.path()+" - aborted.");
insilmaril@195
   140
		return;
insilmaril@195
   141
	}
insilmaril@195
   142
insilmaril@195
   143
	// Traverse directories
insilmaril@195
   144
	d.setFilter( QDir::Dirs| QDir::Hidden | QDir::NoSymLinks );
insilmaril@366
   145
	QFileInfoList list = d.entryInfoList();
insilmaril@366
   146
	QFileInfo fi;
insilmaril@195
   147
insilmaril@366
   148
	for (int i = 0; i < list.size(); ++i) 
insilmaril@195
   149
	{
insilmaril@366
   150
		fi=list.at(i);
insilmaril@366
   151
		if (fi.fileName() != "." && fi.fileName() != ".." )
insilmaril@195
   152
		{
insilmaril@366
   153
			if ( !d.cd(fi.fileName()) ) 
insilmaril@366
   154
				qWarning ("removeDir() cannot find the directory "+fi.fileName());
insilmaril@195
   155
			else 
insilmaril@195
   156
			{
insilmaril@195
   157
				// Recursively remove subdirs
insilmaril@195
   158
				removeDir (d);
insilmaril@195
   159
				d.cdUp();
insilmaril@195
   160
			}
insilmaril@195
   161
		}	
insilmaril@366
   162
	}
insilmaril@366
   163
insilmaril@195
   164
	// Traverse files
insilmaril@195
   165
	d.setFilter( QDir::Files| QDir::Hidden | QDir::NoSymLinks );
insilmaril@366
   166
	list = d.entryInfoList();
insilmaril@195
   167
insilmaril@366
   168
	for (int i = 0; i < list.size(); ++i) 
insilmaril@195
   169
	{
insilmaril@366
   170
		fi=list.at(i);
insilmaril@366
   171
		QFile (fi.filePath()).remove(); 
insilmaril@195
   172
	}	
insilmaril@195
   173
insilmaril@195
   174
	if (!d.rmdir(d.path()))
insilmaril@195
   175
		qWarning ("removeDir("+d.path()+") failed!");
insilmaril@195
   176
}		
insilmaril@195
   177
insilmaril@502
   178
void copyDir (QDir src, QDir dst)
insilmaril@502
   179
{
insilmaril@502
   180
	system ("cp -r "+src.path()+"/* "+dst.path());
insilmaril@502
   181
insilmaril@502
   182
	/*
insilmaril@502
   183
	ErrorCode err=success;
insilmaril@502
   184
insilmaril@502
   185
	Process *cpProc=new Process ();
insilmaril@502
   186
	QStringList args;
insilmaril@502
   187
	cpProc->setWorkingDirectory (src.path());
insilmaril@502
   188
	args <<"-r";
insilmaril@502
   189
	args <<src.path();
insilmaril@502
   190
	args <<dst.path();
insilmaril@502
   191
insilmaril@502
   192
	cpProc->start ("cp",args);
insilmaril@502
   193
	if (!cpProc->waitForStarted() )
insilmaril@502
   194
	{	
insilmaril@502
   195
		// zip could not be started
insilmaril@502
   196
		QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
insilmaril@502
   197
					   QObject::tr("Couldn't start zip to compress data."));
insilmaril@502
   198
		err=aborted;
insilmaril@502
   199
	} else
insilmaril@502
   200
	{
insilmaril@502
   201
		// zip could be started
insilmaril@502
   202
		cpProc->waitForFinished();
insilmaril@502
   203
		if (cpProc->exitStatus()!=QProcess::NormalExit )
insilmaril@502
   204
		{
insilmaril@502
   205
			QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
insilmaril@502
   206
						   QObject::tr("cp didn't exit normally")+
insilmaril@502
   207
						   "\n" + cpProc->getErrout());
insilmaril@502
   208
			err=aborted;
insilmaril@502
   209
		} else
insilmaril@502
   210
		{
insilmaril@502
   211
			if (cpProc->exitCode()>0)
insilmaril@502
   212
			{
insilmaril@502
   213
				QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
insilmaril@502
   214
						   QString("cp exit code:  %1").arg(cpProc->exitCode() )+
insilmaril@502
   215
						   "\n" + cpProc->getErrout() );
insilmaril@502
   216
				err=aborted;
insilmaril@502
   217
			}
insilmaril@502
   218
		}
insilmaril@502
   219
	}	// cp could be started
insilmaril@502
   220
	*/
insilmaril@502
   221
}
insilmaril@502
   222
insilmaril@195
   223
void makeSubDirs (const QString &s)
insilmaril@195
   224
{
insilmaril@195
   225
	QDir d(s);
insilmaril@195
   226
	d.mkdir(s);
insilmaril@195
   227
	d.mkdir ("images");	
insilmaril@195
   228
	d.mkdir ("flags");	
insilmaril@195
   229
}
insilmaril@195
   230
insilmaril@205
   231
ErrorCode zipDir (const QDir &zipDir, const QString &zipName)
insilmaril@195
   232
{
insilmaril@205
   233
	ErrorCode err=success;
insilmaril@195
   234
	
insilmaril@195
   235
	// zip the temporary directory
insilmaril@408
   236
	QStringList args;
insilmaril@195
   237
	Process *zipProc=new Process ();
insilmaril@408
   238
	zipProc->setWorkingDirectory (zipDir.path());
insilmaril@408
   239
	args <<"-r";
insilmaril@408
   240
	args <<zipName;
insilmaril@408
   241
	args <<".";
insilmaril@195
   242
insilmaril@408
   243
	zipProc->start ("zip",args);
insilmaril@408
   244
	if (!zipProc->waitForStarted() )
insilmaril@195
   245
	{	
insilmaril@195
   246
		// zip could not be started
insilmaril@195
   247
		QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
insilmaril@195
   248
					   QObject::tr("Couldn't start zip to compress data."));
insilmaril@195
   249
		err=aborted;
insilmaril@195
   250
	} else
insilmaril@195
   251
	{
insilmaril@195
   252
		// zip could be started
insilmaril@408
   253
		zipProc->waitForFinished();
insilmaril@408
   254
		if (zipProc->exitStatus()!=QProcess::NormalExit )
insilmaril@195
   255
		{
insilmaril@195
   256
			QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
insilmaril@195
   257
						   QObject::tr("zip didn't exit normally")+
insilmaril@195
   258
						   "\n" + zipProc->getErrout());
insilmaril@195
   259
			err=aborted;
insilmaril@195
   260
		} else
insilmaril@195
   261
		{
insilmaril@408
   262
			if (zipProc->exitCode()>0)
insilmaril@195
   263
			{
insilmaril@195
   264
				QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
insilmaril@408
   265
						   QString("zip exit code:  %1").arg(zipProc->exitCode() )+
insilmaril@195
   266
						   "\n" + zipProc->getErrout() );
insilmaril@195
   267
				err=aborted;
insilmaril@195
   268
			}
insilmaril@195
   269
		}
insilmaril@195
   270
	}	// zip could be started
insilmaril@195
   271
	return err;	
insilmaril@195
   272
}
insilmaril@195
   273
insilmaril@205
   274
ErrorCode unzipDir (const QDir &zipDir, const QString &zipName)
insilmaril@195
   275
{
insilmaril@205
   276
	ErrorCode err=success;
insilmaril@195
   277
insilmaril@195
   278
	// Try to unzip file
insilmaril@408
   279
	QStringList args;
insilmaril@195
   280
	Process *zipProc=new Process ();
insilmaril@408
   281
	zipProc->setWorkingDirectory (zipDir.path());
insilmaril@408
   282
	args << "-o";	// overwrite existing files!
insilmaril@408
   283
	args << zipName ;
insilmaril@408
   284
	args << "-d";
insilmaril@408
   285
	args << zipDir.path();
insilmaril@195
   286
insilmaril@408
   287
	zipProc->start ("unzip",args);
insilmaril@408
   288
	if (!zipProc->waitForStarted() )
insilmaril@195
   289
	{
insilmaril@195
   290
		QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
insilmaril@195
   291
					   QObject::tr("Couldn't start unzip to decompress data."));
insilmaril@195
   292
		err=aborted;
insilmaril@195
   293
		
insilmaril@195
   294
	} else
insilmaril@195
   295
	{
insilmaril@408
   296
		zipProc->waitForFinished();
insilmaril@408
   297
		if (zipProc->exitStatus()!=QProcess::NormalExit )
insilmaril@195
   298
		{
insilmaril@195
   299
			QMessageBox::critical( 0,QObject::tr( "Critical Error" ),
insilmaril@195
   300
						   QObject::tr("unzip didn't exit normally") +
insilmaril@195
   301
						   zipProc->getErrout() );
insilmaril@195
   302
			err=aborted;
insilmaril@195
   303
		} else
insilmaril@195
   304
		{
insilmaril@408
   305
			if (zipProc->exitCode()>0)
insilmaril@195
   306
			{
insilmaril@428
   307
				if (zipProc->exitCode()==9)
insilmaril@195
   308
					// no zipped file, but maybe .xml or old version? Try again.
insilmaril@195
   309
					err=nozip;
insilmaril@195
   310
				else	
insilmaril@195
   311
				{
insilmaril@195
   312
					QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
insilmaril@408
   313
								   QString("unzip exit code:  %1").arg(zipProc->exitCode() ) +
insilmaril@195
   314
								   zipProc->getErrout() );
insilmaril@195
   315
					err=aborted;
insilmaril@195
   316
				}
insilmaril@195
   317
			} 
insilmaril@195
   318
		}
insilmaril@195
   319
	}
insilmaril@195
   320
	return err;	
insilmaril@195
   321
}
insilmaril@195
   322
insilmaril@195
   323
bool loadStringFromDisk (const QString &fname, QString &s)
insilmaril@195
   324
{
insilmaril@195
   325
	s="";
insilmaril@195
   326
	QFile file ( fname);
insilmaril@366
   327
	if ( !file.open( QIODevice::ReadOnly ) ) return false;
insilmaril@195
   328
insilmaril@195
   329
	QTextStream ts( &file );
insilmaril@195
   330
	ts.setEncoding (QTextStream::UnicodeUTF8);
insilmaril@195
   331
	while ( !ts.atEnd() ) 
insilmaril@195
   332
		s+=ts.readLine()+"\n"; 
insilmaril@195
   333
	file.close();
insilmaril@195
   334
	return true;
insilmaril@195
   335
}
insilmaril@195
   336
insilmaril@195
   337
bool saveStringToDisk (const QString &fname, const QString &s)
insilmaril@195
   338
{
insilmaril@195
   339
	QFile file( fname);
insilmaril@195
   340
insilmaril@195
   341
	file.setName ( fname);
insilmaril@366
   342
	if ( !file.open( QIODevice::WriteOnly ) ) 
insilmaril@195
   343
	{
insilmaril@195
   344
		file.close();
insilmaril@195
   345
		return false;
insilmaril@195
   346
	}	
insilmaril@195
   347
insilmaril@195
   348
	// Write it finally, and write in UTF8, no matter what 
insilmaril@195
   349
	QTextStream ts( &file );
insilmaril@195
   350
	ts.setEncoding (QTextStream::UnicodeUTF8);
insilmaril@195
   351
	ts << s;
insilmaril@195
   352
	file.close();
insilmaril@195
   353
	return true;
insilmaril@195
   354
}
insilmaril@195
   355
insilmaril@195
   356
insilmaril@234
   357
ImagePreview::ImagePreview (QWidget *par=0): QLabel (par)
insilmaril@195
   358
{
insilmaril@366
   359
	fdia=(Q3FileDialog*)par;
insilmaril@195
   360
}
insilmaril@195
   361
insilmaril@366
   362
void ImagePreview::previewUrl( const Q3Url &u )
insilmaril@195
   363
{
insilmaril@195
   364
    QString path = u.path();
insilmaril@195
   365
    QPixmap pix( path );
insilmaril@195
   366
    if ( pix.isNull() )
insilmaril@234
   367
	{
insilmaril@234
   368
		// Strange: If we have fd->setMode (QFileDialog::ExistingFiles)
insilmaril@234
   369
		// in the filedialog, then there are 3 calls to previewURL 
insilmaril@234
   370
		// for each selection. And only the first is the actual selected file
insilmaril@234
   371
		// while the following 2 point to the directory above the current one.
insilmaril@234
   372
		// So here's my workaround:
insilmaril@234
   373
		
insilmaril@234
   374
		if (fdia && fdia->selectedFiles().count()==0)
insilmaril@234
   375
			setText( QObject::tr("This is not an image.") );
insilmaril@234
   376
		if (fdia &&fdia->selectedFiles().count()>1)
insilmaril@234
   377
			setText( QObject::tr("Sorry, no preview for\nmultiple selected files.") );
insilmaril@234
   378
	}	
insilmaril@195
   379
    else
insilmaril@195
   380
	{
insilmaril@195
   381
		float max_w=300;
insilmaril@195
   382
		float max_h=300;
insilmaril@195
   383
		float r;
insilmaril@195
   384
		if (pix.width()>max_w)
insilmaril@195
   385
		{
insilmaril@195
   386
			r=max_w / pix.width();
insilmaril@195
   387
			pix.resize(qRound(pix.width()*r), qRound(pix.height()*r));
insilmaril@412
   388
			// TODO not a resize, but a shrink/enlarge is needed here...
insilmaril@195
   389
		}
insilmaril@195
   390
		if (pix.height()>max_h)
insilmaril@195
   391
		{
insilmaril@195
   392
			r=max_h / pix.height();
insilmaril@195
   393
			pix.resize(qRound(pix.width()*r), qRound(pix.height()*r));
insilmaril@412
   394
			// TODO not a resize, but a shrink/enlarge is needed here...
insilmaril@195
   395
		}
insilmaril@195
   396
        setPixmap( pix );
insilmaril@195
   397
	}	
insilmaril@195
   398
}
insilmaril@195
   399
insilmaril@366
   400
ImageIO::ImageIO ()
insilmaril@366
   401
{
insilmaril@366
   402
	// Create list with supported image types
insilmaril@366
   403
	// foreach (QByteArray format, QImageWriter::supportedImageFormats()) 
insilmaril@366
   404
	// imageTypes.append( tr("%1...").arg(QString(format).toUpper()));
insilmaril@366
   405
	imageFilters.append ("Images (*.png *.jpg *.jpeg *.bmp *.bmp *.ppm *.xpm *.xbm)");
insilmaril@366
   406
	imageTypes.append ("PNG");
insilmaril@366
   407
	imageFilters.append ("Portable Network Graphics (*.png)");
insilmaril@366
   408
	imageTypes.append ("PNG");
insilmaril@366
   409
	imageFilters.append ("Joint Photographic Experts Group (*.jpg)");
insilmaril@366
   410
	imageTypes.append ("JPG");
insilmaril@366
   411
	imageFilters.append ("Joint Photographic Experts Group (*.jpeg)");
insilmaril@366
   412
	imageTypes.append ("JPG");
insilmaril@366
   413
	imageFilters.append ("Windows Bitmap (*.bmp)");
insilmaril@366
   414
	imageTypes.append ("BMP");
insilmaril@366
   415
	imageFilters.append ("Portable Pixmap (*.ppm)");
insilmaril@366
   416
	imageTypes.append ("PPM");
insilmaril@366
   417
	imageFilters.append ("X11 Bitmap (*.xpm)");
insilmaril@366
   418
	imageTypes.append ("XPM");
insilmaril@366
   419
	imageFilters.append ("X11 Bitmap (*.xbm)");
insilmaril@366
   420
	imageTypes.append ("XBM");
insilmaril@366
   421
}
insilmaril@366
   422
insilmaril@366
   423
QStringList ImageIO::getFilters()
insilmaril@366
   424
{
insilmaril@366
   425
	return imageFilters;
insilmaril@366
   426
}
insilmaril@366
   427
insilmaril@366
   428
QString ImageIO::getType(QString filter)
insilmaril@366
   429
{
insilmaril@366
   430
	for (int i=0;i<imageFilters.count()+1;i++)
insilmaril@366
   431
		if (imageFilters.at(i)==filter) return imageTypes.at(i);
insilmaril@366
   432
	return QString();	
insilmaril@366
   433
}
insilmaril@366
   434
insilmaril@366
   435