file.cpp
author insilmaril
Wed, 29 Apr 2009 21:40:37 +0000
changeset 761 0301e6109702
parent 721 12958f987bcf
child 831 25a950c2eb98
permissions -rw-r--r--
speedup
     1 #include <QDir>
     2 #include <QMessageBox>
     3 #include <QPixmap>
     4 #include <QLabel>
     5 #include <QTextStream>
     6 #include <iostream>
     7 
     8 #include "file.h"
     9 #include "process.h"
    10 
    11 #if defined(Q_OS_WIN32)
    12 #include "mkdtemp.h"
    13 #include <windows.h>
    14 #endif
    15 
    16 QString maskPath(QString p)
    17 {
    18 	// Change " " to "\ " to enable blanks in filenames
    19 	p=p.replace(QChar('&'),"\\&");
    20 	return p.replace(QChar(' '),"\\ ");
    21 }
    22 
    23 QString convertToRel (const QString &src, const QString &dst)
    24 {
    25 	QString s=src;
    26 	QString d=dst;
    27 	int i;
    28 
    29 	if (s==d) 
    30 	{
    31 		// Special case, we just need the name of the file,
    32 		// not the complete path
    33 		i=d.findRev ("/");
    34 		d=d.right (d.length()-i-1);
    35 	} else
    36 	{
    37 		// Find relative path from src to dst
    38 
    39 		// Remove the first "/"
    40 		if (s.section ("/",0,0).isEmpty()) 
    41 		{
    42 			s=s.right (s.length()-1);
    43 			d=d.right (d.length()-1);
    44 		}
    45 		
    46 		// remove identical left parts
    47 		while (s.section("/",0,0) == d.section("/",0,0) ) 
    48 		{
    49 			i=s.find ("/");
    50 			s=s.right (s.length()-i-1);
    51 			d=d.right (d.length()-i-1);
    52 		}
    53 
    54 		// Now take care of paths where we have to go back first
    55 		int srcsep=s.count("/");
    56 		int dstsep=d.count("/");
    57 		if (srcsep <=  dstsep )
    58 		{
    59 			// find path to go up first and then back to dst
    60 			i=1;
    61 			while (i<=srcsep) 
    62 			{
    63 				d="../"+d;
    64 				i++;
    65 			}	
    66 		}
    67 	}	
    68 	return d;
    69 }
    70 
    71 #include <QFileDialog>
    72 extern QString vymName;
    73 extern QDir lastFileDir;
    74 
    75 QString browseDirectory (QWidget *parent,const QString &caption)
    76 {
    77 	QFileDialog fd(parent,caption);
    78 	fd.setMode (QFileDialog::DirectoryOnly);
    79 	fd.setCaption(vymName+ " - "+caption);
    80 	fd.setDir (lastFileDir);
    81 	fd.show();
    82 	
    83 	if ( fd.exec() == QDialog::Accepted )
    84 		return fd.selectedFile();
    85 	else
    86 		return "";
    87 }
    88 
    89 
    90 
    91 bool reallyWriteDirectory(const QString &dir)
    92 {
    93 	QStringList eList = QDir(dir).entryList();
    94 	if (eList.first() ==".")  eList.pop_front();	// remove "."
    95 	if (eList.first() =="..") eList.pop_front();	// remove "."
    96 	if (!eList.isEmpty())
    97 	{
    98 		QMessageBox mb( vymName,
    99 			QObject::tr("The directory %1 is not empty.\nDo you risk to overwrite its contents?","write directory").arg(dir),
   100 		QMessageBox::Warning,
   101 		QMessageBox::Yes ,
   102 		QMessageBox::Cancel | QMessageBox::Default,
   103 		QMessageBox::NoButton );
   104 
   105 		mb.setButtonText( QMessageBox::Yes, QObject::tr("Overwrite") );
   106 		mb.setButtonText( QMessageBox::No, QObject::tr("Cancel"));
   107 		switch( mb.exec() ) 
   108 		{
   109 			case QMessageBox::Yes:
   110 				// save 
   111 				return true;
   112 			case QMessageBox::Cancel:
   113 				// do nothing
   114 				return false;
   115 		}
   116 	}
   117 	return true;
   118 }
   119 
   120 QString makeTmpDir (bool &ok, QString prefix)
   121 {
   122 	bool b;
   123 	QString path=makeUniqueDir (b,QDir::tempPath()+"/"+prefix+"-XXXXXX");
   124 	ok=b;
   125 	return path;
   126 }
   127 
   128 bool isInTmpDir(QString fn)
   129 {
   130 	QString temp=QDir::tempPath();
   131 	int l=temp.length();
   132 	return fn.left(l)==temp;
   133 }
   134 
   135 QString makeUniqueDir (bool &ok,QString s)
   136 {
   137 	// Create unique directory e.g. for s="/tmp/vym-XXXXXX"
   138 
   139 	// Convert Separators
   140 	s=QDir::convertSeparators(s);
   141 
   142 	// Convert QString to string 
   143 	ok=true;
   144 	char *p;
   145 	int bytes=s.length();
   146 	p=(char*) malloc (bytes+1);
   147 	int i;
   148 	for (i=0;i<bytes;i++)
   149 		p[i]=s.at(i).latin1();
   150 	p[bytes]=0;	
   151 
   152 	QString r=mkdtemp (p);
   153 	if (r.isEmpty()) ok=false;
   154 	free (p);
   155 	return r;
   156 }
   157 
   158 void removeDir(QDir d)
   159 {
   160 	// This check should_ not be necessary, but proved to be useful ;-)
   161 	if (!isInTmpDir(d.path()))
   162 	{
   163 		qWarning ("file.cpp::removeDir should remove "+d.path()+" - aborted.");
   164 		return;
   165 	}
   166 
   167 	// Traverse directories
   168 	d.setFilter( QDir::Dirs| QDir::Hidden | QDir::NoSymLinks );
   169 	QFileInfoList list = d.entryInfoList();
   170 	QFileInfo fi;
   171 
   172 	for (int i = 0; i < list.size(); ++i) 
   173 	{
   174 		fi=list.at(i);
   175 		if (fi.fileName() != "." && fi.fileName() != ".." )
   176 		{
   177 			if ( !d.cd(fi.fileName()) ) 
   178 				qWarning ("removeDir() cannot find the directory "+fi.fileName());
   179 			else 
   180 			{
   181 				// Recursively remove subdirs
   182 				removeDir (d);
   183 				d.cdUp();
   184 			}
   185 		}	
   186 	}
   187 
   188 	// Traverse files
   189 	d.setFilter( QDir::Files| QDir::Hidden | QDir::NoSymLinks );
   190 	list = d.entryInfoList();
   191 
   192 	for (int i = 0; i < list.size(); ++i) 
   193 	{
   194 		fi=list.at(i);
   195 		QFile (fi.filePath()).remove(); 
   196 	}	
   197 
   198 	if (!d.rmdir(d.path()))
   199 		qWarning ("removeDir("+d.path()+") failed!");
   200 }		
   201 
   202 void copyDir (QDir src, QDir dst)
   203 {
   204 	system ("cp -r "+src.path()+"/* "+dst.path());
   205 
   206 	/*
   207 	ErrorCode err=success;
   208 
   209 	Process *cpProc=new Process ();
   210 	QStringList args;
   211 	cpProc->setWorkingDirectory (src.path());
   212 	args <<"-r";
   213 	args <<src.path();
   214 	args <<dst.path();
   215 
   216 	cpProc->start ("cp",args);
   217 	if (!cpProc->waitForStarted() )
   218 	{	
   219 		// zip could not be started
   220 		QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
   221 					   QObject::tr("Couldn't start zip to compress data."));
   222 		err=aborted;
   223 	} else
   224 	{
   225 		// zip could be started
   226 		cpProc->waitForFinished();
   227 		if (cpProc->exitStatus()!=QProcess::NormalExit )
   228 		{
   229 			QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
   230 						   QObject::tr("cp didn't exit normally")+
   231 						   "\n" + cpProc->getErrout());
   232 			err=aborted;
   233 		} else
   234 		{
   235 			if (cpProc->exitCode()>0)
   236 			{
   237 				QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
   238 						   QString("cp exit code:  %1").arg(cpProc->exitCode() )+
   239 						   "\n" + cpProc->getErrout() );
   240 				err=aborted;
   241 			}
   242 		}
   243 	}	// cp could be started
   244 	*/
   245 }
   246 
   247 void makeSubDirs (const QString &s)
   248 {
   249 	QDir d(s);
   250 	d.mkdir(s);
   251 	d.mkdir ("images");	
   252 	d.mkdir ("flags");	
   253 }
   254 
   255 ErrorCode zipDir (const QDir &zipDir, const QString &zipName)
   256 {
   257 	ErrorCode err=success;
   258 	
   259 	// zip the temporary directory
   260 	QStringList args;
   261 	Process *zipProc=new Process ();
   262 	zipProc->setWorkingDirectory (zipDir.path());
   263 	args <<"-r";
   264 	args <<zipName;
   265 	args <<".";
   266 
   267 	zipProc->start ("zip",args);
   268 	if (!zipProc->waitForStarted() )
   269 	{	
   270 		// zip could not be started
   271 		QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
   272 					   QObject::tr("Couldn't start zip to compress data."));
   273 		err=aborted;
   274 	} else
   275 	{
   276 		// zip could be started
   277 		zipProc->waitForFinished();
   278 		if (zipProc->exitStatus()!=QProcess::NormalExit )
   279 		{
   280 			QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
   281 						   QObject::tr("zip didn't exit normally")+
   282 						   "\n" + zipProc->getErrout());
   283 			err=aborted;
   284 		} else
   285 		{
   286 			if (zipProc->exitCode()>0)
   287 			{
   288 				QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
   289 						   QString("zip exit code:  %1").arg(zipProc->exitCode() )+
   290 						   "\n" + zipProc->getErrout() );
   291 				err=aborted;
   292 			}
   293 		}
   294 	}	// zip could be started
   295 	return err;	
   296 }
   297 
   298 ErrorCode unzipDir (const QDir &zipDir, const QString &zipName)
   299 {
   300 	ErrorCode err=success;
   301 
   302 	// Try to unzip file
   303 #if !defined(Q_OS_WIN32)
   304 	QStringList args;
   305 	Process *zipProc=new Process ();
   306 	zipProc->setWorkingDirectory (zipDir.path());
   307 	args << "-o";	// overwrite existing files!
   308 	args << zipName ;
   309 	args << "-d";
   310 	args << zipDir.path();
   311 
   312 	zipProc->start ("unzip",args);
   313 	if (!zipProc->waitForStarted() )
   314 	{
   315 		QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
   316 					   QObject::tr("Couldn't start unzip to decompress data."));
   317 		err=aborted;
   318 		
   319 	} else
   320 	{
   321 		zipProc->waitForFinished();
   322 		if (zipProc->exitStatus()!=QProcess::NormalExit )
   323 		{
   324 			QMessageBox::critical( 0,QObject::tr( "Critical Error" ),
   325 						   QObject::tr("unzip didn't exit normally") +
   326 						   zipProc->getErrout() );
   327 			err=aborted;
   328 		} else
   329 		{
   330 			if (zipProc->exitCode()>0)
   331 			{
   332 				if (zipProc->exitCode()==9)
   333 					// no zipped file, but maybe .xml or old version? Try again.
   334 					err=nozip;
   335 				else	
   336 				{
   337 					QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
   338 								   QString("unzip exit code:  %1").arg(zipProc->exitCode() ) +
   339 								   zipProc->getErrout() );
   340 					err=aborted;
   341 				}
   342 			} 
   343 		}
   344 	}
   345 #else
   346     // Do this process creation using Win32 API.
   347     //! Create process.
   348     PROCESS_INFORMATION piProcInfo;
   349     STARTUPINFO siStartInfo;
   350 
   351     // Initialize members of the PROCESS_INFORMATION structure.
   352     ::ZeroMemory( &piProcInfo, sizeof(PROCESS_INFORMATION) );
   353 
   354     // Set up members of the STARTUPINFO structure.
   355     ::ZeroMemory( &siStartInfo, sizeof(STARTUPINFO) );
   356     siStartInfo.cb = sizeof(STARTUPINFO);
   357 
   358     // Create command line.
   359     QString argv("unzip -o ");
   360     argv.append(QDir::convertSeparators(zipName));
   361     argv.append(" -d ");
   362     argv.append(QDir::convertSeparators(zipDir.path()));
   363 
   364     // Create the child process.
   365     if( !::CreateProcess(NULL, 
   366         (LPWSTR)argv.unicode(), // command line
   367         NULL, // process security attributes
   368         NULL, // primary thread security attributes
   369         TRUE, // handles are inherited
   370         0, // creation flags
   371         NULL, // use parent's environment
   372         NULL, // use parent's current directory
   373         &siStartInfo, // STARTUPINFO pointer
   374         &piProcInfo) ) // receives PROCESS_INFORMATION
   375     {
   376         err = aborted;
   377     }
   378     else
   379     {
   380         // Wait for it to finish.
   381         ::WaitForSingleObject( piProcInfo.hProcess, 10000 );
   382     }
   383 #endif
   384 	return err;	
   385 }
   386 
   387 bool loadStringFromDisk (const QString &fname, QString &s)
   388 {
   389 	s="";
   390 	QFile file ( fname);
   391 	if ( !file.open( QIODevice::ReadOnly ) ) return false;
   392 
   393 	QTextStream ts( &file );
   394 	ts.setEncoding (QTextStream::UnicodeUTF8);
   395 	while ( !ts.atEnd() ) 
   396 		s+=ts.readLine()+"\n"; 
   397 	file.close();
   398 	return true;
   399 }
   400 
   401 bool saveStringToDisk (const QString &fname, const QString &s)
   402 {
   403 	QFile file( fname);
   404 
   405 	file.setName ( fname);
   406 	if ( !file.open( QIODevice::WriteOnly ) ) 
   407 	{
   408 		file.close();
   409 		return false;
   410 	}	
   411 
   412 	// Write it finally, and write in UTF8, no matter what 
   413 	QTextStream ts( &file );
   414 	ts.setEncoding (QTextStream::UnicodeUTF8);
   415 	ts << s;
   416 	file.close();
   417 	return true;
   418 }
   419 
   420 
   421 ImagePreview::ImagePreview (QWidget *par=0): QLabel (par)
   422 {
   423 	fdia=(Q3FileDialog*)par;
   424 }
   425 
   426 void ImagePreview::previewUrl( const Q3Url &u )
   427 {
   428     QString path = u.path();
   429     QPixmap pix( path );
   430     if ( pix.isNull() )
   431 	{
   432 		// Strange: If we have fd->setMode (QFileDialog::ExistingFiles)
   433 		// in the filedialog, then there are 3 calls to previewURL 
   434 		// for each selection. And only the first is the actual selected file
   435 		// while the following 2 point to the directory above the current one.
   436 		// So here's my workaround:
   437 		
   438 		if (fdia && fdia->selectedFiles().count()==0)
   439 			setText( QObject::tr("This is not an image.") );
   440 		if (fdia &&fdia->selectedFiles().count()>1)
   441 			setText( QObject::tr("Sorry, no preview for\nmultiple selected files.") );
   442 	}	
   443     else
   444 	{
   445 		float max_w=300;
   446 		float max_h=300;
   447 		float r;
   448 		if (pix.width()>max_w)
   449 		{
   450 			r=max_w / pix.width();
   451 			pix.resize(qRound(pix.width()*r), qRound(pix.height()*r));
   452 			// TODO not a resize, but a shrink/enlarge is needed here...
   453 		}
   454 		if (pix.height()>max_h)
   455 		{
   456 			r=max_h / pix.height();
   457 			pix.resize(qRound(pix.width()*r), qRound(pix.height()*r));
   458 			// TODO not a resize, but a shrink/enlarge is needed here...
   459 		}
   460         setPixmap( pix );
   461 	}	
   462 }
   463 
   464 ImageIO::ImageIO ()
   465 {
   466 	// Create list with supported image types
   467 	// foreach (QByteArray format, QImageWriter::supportedImageFormats()) 
   468 	// imageTypes.append( tr("%1...").arg(QString(format).toUpper()));
   469 	imageFilters.append ("Images (*.png *.jpg *.jpeg *.bmp *.bmp *.ppm *.xpm *.xbm)");
   470 	imageTypes.append ("PNG");
   471 	imageFilters.append ("Portable Network Graphics (*.png)");
   472 	imageTypes.append ("PNG");
   473 	imageFilters.append ("Joint Photographic Experts Group (*.jpg)");
   474 	imageTypes.append ("JPG");
   475 	imageFilters.append ("Joint Photographic Experts Group (*.jpeg)");
   476 	imageTypes.append ("JPG");
   477 	imageFilters.append ("Windows Bitmap (*.bmp)");
   478 	imageTypes.append ("BMP");
   479 	imageFilters.append ("Portable Pixmap (*.ppm)");
   480 	imageTypes.append ("PPM");
   481 	imageFilters.append ("X11 Bitmap (*.xpm)");
   482 	imageTypes.append ("XPM");
   483 	imageFilters.append ("X11 Bitmap (*.xbm)");
   484 	imageTypes.append ("XBM");
   485 }
   486 
   487 QStringList ImageIO::getFilters()
   488 {
   489 	return imageFilters;
   490 }
   491 
   492 QString ImageIO::getType(QString filter)
   493 {
   494 	for (int i=0;i<imageFilters.count()+1;i++)
   495 		if (imageFilters.at(i)==filter) return imageTypes.at(i);
   496 	return QString();	
   497 }
   498 
   499