file.cpp
author insilmaril
Wed, 01 Feb 2006 16:39:06 +0000
changeset 202 c6945404eb48
parent 195 98f260cfb1fe
child 205 30c4a6c7ff10
permissions -rw-r--r--
added new logo
     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 	if (err==aborted) qWarning("file.cpp: zip aborted"); 
   175 	return err;	
   176 }
   177 
   178 errorCode unzipDir (const QDir &zipDir, const QString &zipName)
   179 {
   180 	errorCode err=success;
   181 
   182 	// Try to unzip file
   183 	Process *zipProc=new Process ();
   184 	zipProc->clearArguments();
   185 	zipProc->setWorkingDirectory (zipDir);
   186 	zipProc->addArgument ("unzip");
   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 	if (err==aborted) qWarning("file.cpp: unzip aborted"); 
   224 	return err;	
   225 }
   226 
   227 bool loadStringFromDisk (const QString &fname, QString &s)
   228 {
   229 	s="";
   230 	QFile file ( fname);
   231 	if ( !file.open( IO_ReadOnly ) ) return false;
   232 
   233 	QTextStream ts( &file );
   234 	ts.setEncoding (QTextStream::UnicodeUTF8);
   235 	while ( !ts.atEnd() ) 
   236 		s+=ts.readLine()+"\n"; 
   237 	file.close();
   238 	return true;
   239 }
   240 
   241 bool saveStringToDisk (const QString &fname, const QString &s)
   242 {
   243 	QFile file( fname);
   244 
   245 	file.setName ( fname);
   246 	if ( !file.open( IO_WriteOnly ) ) 
   247 	{
   248 		file.close();
   249 		return false;
   250 	}	
   251 
   252 	// Write it finally, and write in UTF8, no matter what 
   253 	QTextStream ts( &file );
   254 	ts.setEncoding (QTextStream::UnicodeUTF8);
   255 	ts << s;
   256 	file.close();
   257 	return true;
   258 }
   259 
   260 
   261 ImagePreview::ImagePreview (QWidget *parent=0): QLabel (parent)
   262 {
   263 }
   264 
   265 void ImagePreview::previewUrl( const QUrl &u )
   266 {
   267     QString path = u.path();
   268     QPixmap pix( path );
   269     if ( pix.isNull() )
   270         setText( QObject::tr("This is not an image.") );
   271     else
   272 	{
   273 		float max_w=300;
   274 		float max_h=300;
   275 		float r;
   276 		if (pix.width()>max_w)
   277 		{
   278 			r=max_w / pix.width();
   279 			pix.resize(qRound(pix.width()*r), qRound(pix.height()*r));
   280 			// FIXME not a resize, but a shrink/enlarge is needed here...
   281 		}
   282 		if (pix.height()>max_h)
   283 		{
   284 			r=max_h / pix.height();
   285 			pix.resize(qRound(pix.width()*r), qRound(pix.height()*r));
   286 			// FIXME not a resize, but a shrink/enlarge is needed here...
   287 		}
   288         setPixmap( pix );
   289 	}	
   290 }
   291