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