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