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