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