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