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