Added missing file.h file.cpp
authorinsilmaril
Wed, 25 Jan 2006 09:05:57 +0000
changeset 19598f260cfb1fe
parent 194 de97201180ea
child 196 a9493dcaa306
Added missing file.h file.cpp
file.cpp
file.h
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/file.cpp	Wed Jan 25 09:05:57 2006 +0000
     1.3 @@ -0,0 +1,291 @@
     1.4 +#include <qobject.h>
     1.5 +#include <qmessagebox.h>
     1.6 +#include <iostream>
     1.7 +
     1.8 +#include "file.h"
     1.9 +#include "process.h"
    1.10 +
    1.11 +
    1.12 +QString maskPath(QString p)
    1.13 +{
    1.14 +	// Change " " to "\ " to enable blanks in filenames
    1.15 +	p=p.replace(QChar('&'),"\\&");
    1.16 +	return p.replace(QChar(' '),"\\ ");
    1.17 +}
    1.18 +
    1.19 +QString convertToRel (const QString &src, const QString &dst)
    1.20 +{
    1.21 +	QString s=src;
    1.22 +	QString d=dst;
    1.23 +	int i;
    1.24 +
    1.25 +	if (s==d) 
    1.26 +	{
    1.27 +		// Special case, we just need the name of the file,
    1.28 +		// not the complete path
    1.29 +		i=d.findRev ("/");
    1.30 +		d=d.right (d.length()-i-1);
    1.31 +	} else
    1.32 +	{
    1.33 +		// Find relative path from src to dst
    1.34 +
    1.35 +		// Remove the first "/"
    1.36 +		if (s.section ("/",0,0).isEmpty()) 
    1.37 +		{
    1.38 +			s=s.right (s.length()-1);
    1.39 +			d=d.right (d.length()-1);
    1.40 +		}
    1.41 +		
    1.42 +		// remove identical left parts
    1.43 +		while (s.section("/",0,0) == d.section("/",0,0) ) 
    1.44 +		{
    1.45 +			i=s.find ("/");
    1.46 +			s=s.right (s.length()-i-1);
    1.47 +			d=d.right (d.length()-i-1);
    1.48 +		}
    1.49 +
    1.50 +		int srcsep=s.contains("/");
    1.51 +		int dstsep=d.contains("/");
    1.52 +		if (srcsep >=  dstsep )
    1.53 +		{
    1.54 +			// find path to go up first and then back to dst
    1.55 +			i=1;
    1.56 +			while (i<=srcsep) 
    1.57 +			{
    1.58 +				d="../"+d;
    1.59 +				i++;
    1.60 +			}	
    1.61 +		}
    1.62 +	}	
    1.63 +	return d;
    1.64 +}
    1.65 +
    1.66 +QString makeUniqueDir (QString s)
    1.67 +{
    1.68 +	// Create unique directory e.g. s="/tmp/vym-XXXXXX"
    1.69 +
    1.70 +	// Convert QString to string first
    1.71 +	char *p;
    1.72 +	int bytes=s.length();
    1.73 +	p=(char*) malloc (bytes+1);
    1.74 +	int i;
    1.75 +	for (i=0;i<bytes;i++)
    1.76 +		p[i]=s.at(i).latin1();
    1.77 +	p[bytes]=0;	
    1.78 +	QString r=mkdtemp (p);
    1.79 +	free (p);
    1.80 +	return r;
    1.81 +}
    1.82 +
    1.83 +void removeDir(QDir d)
    1.84 +{
    1.85 +	if (d.path().left(4)!="/tmp")
    1.86 +	{
    1.87 +		// FIXME testing
    1.88 +		qWarning ("file.cpp::removeDir should remove "+d.path()+" - aborted.");
    1.89 +		return;
    1.90 +	}
    1.91 +
    1.92 +	// Traverse directories
    1.93 +	d.setFilter( QDir::Dirs| QDir::Hidden | QDir::NoSymLinks );
    1.94 +	const QFileInfoList *dirlist = d.entryInfoList();
    1.95 +	QFileInfoListIterator itdir( *dirlist );
    1.96 +	QFileInfo *fi;
    1.97 +
    1.98 +	while ( (fi = itdir.current()) != 0 ) 
    1.99 +	{
   1.100 +		if (fi->fileName() != "." && fi->fileName() != ".." )
   1.101 +		{
   1.102 +			if ( !d.cd(fi->fileName()) ) 
   1.103 +				qWarning ("removeDir() cannot find the directory "+fi->fileName());
   1.104 +			else 
   1.105 +			{
   1.106 +				// Recursively remove subdirs
   1.107 +				removeDir (d);
   1.108 +				d.cdUp();
   1.109 +			}
   1.110 +		}	
   1.111 +		++itdir;
   1.112 +	}		
   1.113 +	// Traverse files
   1.114 +	d.setFilter( QDir::Files| QDir::Hidden | QDir::NoSymLinks );
   1.115 +	const QFileInfoList *filelist = d.entryInfoList();
   1.116 +	QFileInfoListIterator itfile( *filelist );
   1.117 +
   1.118 +	while ( (fi = itfile.current()) != 0 ) 
   1.119 +	{
   1.120 +		QFile (fi->filePath()).remove();
   1.121 +		
   1.122 +		++itfile;
   1.123 +	}	
   1.124 +
   1.125 +	if (!d.rmdir(d.path()))
   1.126 +		qWarning ("removeDir("+d.path()+") failed!");
   1.127 +}		
   1.128 +
   1.129 +void makeSubDirs (const QString &s)
   1.130 +{
   1.131 +	QDir d(s);
   1.132 +	d.mkdir(s);
   1.133 +	d.mkdir ("images");	
   1.134 +	d.mkdir ("flags");	
   1.135 +}
   1.136 +
   1.137 +errorCode zipDir (const QDir &zipDir, const QString &zipName)
   1.138 +{
   1.139 +	errorCode err=success;
   1.140 +	
   1.141 +	// zip the temporary directory
   1.142 +	Process *zipProc=new Process ();
   1.143 +	zipProc->clearArguments();
   1.144 +	zipProc->setWorkingDirectory (QDir(zipDir));
   1.145 +	zipProc->addArgument ("zip");
   1.146 +	zipProc->addArgument ("-r");
   1.147 +	zipProc->addArgument (zipName);
   1.148 +	zipProc->addArgument (".");
   1.149 +
   1.150 +	if (!zipProc->start() )
   1.151 +	{	
   1.152 +		// zip could not be started
   1.153 +		QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
   1.154 +					   QObject::tr("Couldn't start zip to compress data."));
   1.155 +		err=aborted;
   1.156 +	} else
   1.157 +	{
   1.158 +		// zip could be started
   1.159 +		zipProc->waitFinished();
   1.160 +		if (!zipProc->normalExit() )
   1.161 +		{
   1.162 +			QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
   1.163 +						   QObject::tr("zip didn't exit normally")+
   1.164 +						   "\n" + zipProc->getErrout());
   1.165 +			err=aborted;
   1.166 +		} else
   1.167 +		{
   1.168 +			if (zipProc->exitStatus()>0)
   1.169 +			{
   1.170 +				QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
   1.171 +						   QString("zip exit code:  %1").arg(zipProc->exitStatus() )+
   1.172 +						   "\n" + zipProc->getErrout() );
   1.173 +				err=aborted;
   1.174 +			}
   1.175 +		}
   1.176 +	}	// zip could be started
   1.177 +	if (err==aborted) qWarning("file.cpp: zip aborted"); 
   1.178 +	return err;	
   1.179 +}
   1.180 +
   1.181 +errorCode unzipDir (const QDir &zipDir, const QString &zipName)
   1.182 +{
   1.183 +	errorCode err=success;
   1.184 +
   1.185 +	// Try to unzip file
   1.186 +	Process *zipProc=new Process ();
   1.187 +	zipProc->clearArguments();
   1.188 +	zipProc->setWorkingDirectory (zipDir);
   1.189 +	zipProc->addArgument ("unzip");
   1.190 +	zipProc->addArgument (zipName );
   1.191 +	zipProc->addArgument ("-d");
   1.192 +	zipProc->addArgument (zipDir.path());
   1.193 +
   1.194 +	if (!zipProc->start() )
   1.195 +	{
   1.196 +		QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
   1.197 +					   QObject::tr("Couldn't start unzip to decompress data."));
   1.198 +		err=aborted;
   1.199 +		
   1.200 +	} else
   1.201 +	{
   1.202 +		zipProc->waitFinished();
   1.203 +		if (!zipProc->normalExit() )
   1.204 +		{
   1.205 +			QMessageBox::critical( 0,QObject::tr( "Critical Error" ),
   1.206 +						   QObject::tr("unzip didn't exit normally") +
   1.207 +						   zipProc->getErrout() );
   1.208 +			err=aborted;
   1.209 +		} else
   1.210 +		{
   1.211 +			if (zipProc->exitStatus()>0)
   1.212 +			{
   1.213 +				if (zipProc->exitStatus()==9)
   1.214 +					// no zipped file, but maybe .xml or old version? Try again.
   1.215 +					err=nozip;
   1.216 +				else	
   1.217 +				{
   1.218 +					QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
   1.219 +								   QString("unzip exit code:  %1").arg(zipProc->exitStatus() ) +
   1.220 +								   zipProc->getErrout() );
   1.221 +					err=aborted;
   1.222 +				}
   1.223 +			} 
   1.224 +		}
   1.225 +	}
   1.226 +	if (err==aborted) qWarning("file.cpp: unzip aborted"); 
   1.227 +	return err;	
   1.228 +}
   1.229 +
   1.230 +bool loadStringFromDisk (const QString &fname, QString &s)
   1.231 +{
   1.232 +	s="";
   1.233 +	QFile file ( fname);
   1.234 +	if ( !file.open( IO_ReadOnly ) ) return false;
   1.235 +
   1.236 +	QTextStream ts( &file );
   1.237 +	ts.setEncoding (QTextStream::UnicodeUTF8);
   1.238 +	while ( !ts.atEnd() ) 
   1.239 +		s+=ts.readLine()+"\n"; 
   1.240 +	file.close();
   1.241 +	return true;
   1.242 +}
   1.243 +
   1.244 +bool saveStringToDisk (const QString &fname, const QString &s)
   1.245 +{
   1.246 +	QFile file( fname);
   1.247 +
   1.248 +	file.setName ( fname);
   1.249 +	if ( !file.open( IO_WriteOnly ) ) 
   1.250 +	{
   1.251 +		file.close();
   1.252 +		return false;
   1.253 +	}	
   1.254 +
   1.255 +	// Write it finally, and write in UTF8, no matter what 
   1.256 +	QTextStream ts( &file );
   1.257 +	ts.setEncoding (QTextStream::UnicodeUTF8);
   1.258 +	ts << s;
   1.259 +	file.close();
   1.260 +	return true;
   1.261 +}
   1.262 +
   1.263 +
   1.264 +ImagePreview::ImagePreview (QWidget *parent=0): QLabel (parent)
   1.265 +{
   1.266 +}
   1.267 +
   1.268 +void ImagePreview::previewUrl( const QUrl &u )
   1.269 +{
   1.270 +    QString path = u.path();
   1.271 +    QPixmap pix( path );
   1.272 +    if ( pix.isNull() )
   1.273 +        setText( QObject::tr("This is not an image.") );
   1.274 +    else
   1.275 +	{
   1.276 +		float max_w=300;
   1.277 +		float max_h=300;
   1.278 +		float r;
   1.279 +		if (pix.width()>max_w)
   1.280 +		{
   1.281 +			r=max_w / pix.width();
   1.282 +			pix.resize(qRound(pix.width()*r), qRound(pix.height()*r));
   1.283 +			// FIXME not a resize, but a shrink/enlarge is needed here...
   1.284 +		}
   1.285 +		if (pix.height()>max_h)
   1.286 +		{
   1.287 +			r=max_h / pix.height();
   1.288 +			pix.resize(qRound(pix.width()*r), qRound(pix.height()*r));
   1.289 +			// FIXME not a resize, but a shrink/enlarge is needed here...
   1.290 +		}
   1.291 +        setPixmap( pix );
   1.292 +	}	
   1.293 +}
   1.294 +
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/file.h	Wed Jan 25 09:05:57 2006 +0000
     2.3 @@ -0,0 +1,34 @@
     2.4 +#ifndef FILE_H
     2.5 +#define FILE_H
     2.6 +
     2.7 +#include <qdir.h>
     2.8 +
     2.9 +enum LoadMode {NewMap,ImportAdd,ImportReplace};
    2.10 +enum SaveMode {PartOfMap,CompleteMap,UndoCommand};
    2.11 +enum errorCode {success,aborted,nozip};
    2.12 +
    2.13 +
    2.14 +/////////////////////////////////////////////////////////////////////////////
    2.15 +QString maskPath (QString );
    2.16 +QString convertToRel (const QString &,const QString &);
    2.17 +QString makeUniqueDir (QString);
    2.18 +void removeDir(QDir);
    2.19 +void makeSubDirs (const QString &);
    2.20 +errorCode zipDir (const QDir &,const QString&);
    2.21 +errorCode unzipDir (const QDir &,const QString&);
    2.22 +bool loadStringFromDisk (const QString &, QString &);
    2.23 +bool saveStringToDisk (const QString &, const QString &s);
    2.24 +
    2.25 +/////////////////////////////////////////////////////////////////////////////
    2.26 +#include <qlabel.h>
    2.27 +#include <qfiledialog.h>
    2.28 +#include <qpixmap.h>
    2.29 +
    2.30 +class ImagePreview : public QLabel, public QFilePreview
    2.31 +{
    2.32 +public:
    2.33 +    ImagePreview( QWidget * );
    2.34 +    void previewUrl( const QUrl & );
    2.35 +};
    2.36 +
    2.37 +#endif