file.cpp
author insilmaril
Wed, 27 Feb 2008 16:09:06 +0000
changeset 675 e524edc45aff
parent 633 dad2c8993342
child 721 12958f987bcf
permissions -rw-r--r--
autosave fix
     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     siStartInfo.dwFlags = STARTF_USESHOWWINDOW;
   358     siStartInfo.wShowWindow = SW_MINIMIZE;
   359 
   360     // Create command line.
   361     QString argv("unzip -o ");
   362     argv.append(QDir::convertSeparators(zipName));
   363     argv.append(" -d ");
   364     argv.append(QDir::convertSeparators(zipDir.path()));
   365 
   366     // Create the child process.
   367     if (!::CreateProcess(NULL, 
   368                          (LPWSTR) argv.unicode(), // command line
   369                          NULL, // process security attributes
   370                          NULL, // primary thread security attributes
   371                          TRUE, // handles are inherited
   372                          0, // creation flags
   373                          NULL, // use parent's environment
   374                          NULL, // use parent's current directory
   375                          &siStartInfo, // STARTUPINFO pointer
   376                          &piProcInfo) ) // receives PROCESS_INFORMATION
   377     {
   378 		QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
   379                                QObject::tr("Couldn't start unzip to decompress data."));
   380         err = aborted;
   381     }
   382     else
   383     {
   384         // Wait for it to finish.
   385         ::WaitForSingleObject( piProcInfo.hProcess, INFINITE );
   386 
   387         // Clean up handles.
   388         CloseHandle(piProcInfo.hThread);
   389         CloseHandle(piProcInfo.hProcess);
   390     }
   391 #endif
   392 	return err;
   393 }
   394 
   395 bool loadStringFromDisk (const QString &fname, QString &s)
   396 {
   397 	s="";
   398 	QFile file ( fname);
   399 	if ( !file.open( QIODevice::ReadOnly ) ) return false;
   400 
   401 	QTextStream ts( &file );
   402 	ts.setEncoding (QTextStream::UnicodeUTF8);
   403 	while ( !ts.atEnd() ) 
   404 		s+=ts.readLine()+"\n"; 
   405 	file.close();
   406 	return true;
   407 }
   408 
   409 bool saveStringToDisk (const QString &fname, const QString &s)
   410 {
   411 	QFile file( fname);
   412 
   413 	file.setName ( fname);
   414 	if ( !file.open( QIODevice::WriteOnly ) ) 
   415 	{
   416 		file.close();
   417 		return false;
   418 	}	
   419 
   420 	// Write it finally, and write in UTF8, no matter what 
   421 	QTextStream ts( &file );
   422 	ts.setEncoding (QTextStream::UnicodeUTF8);
   423 	ts << s;
   424 	file.close();
   425 	return true;
   426 }
   427 
   428 
   429 ImagePreview::ImagePreview (QWidget *par=0): QLabel (par)
   430 {
   431 	fdia=(Q3FileDialog*)par;
   432 }
   433 
   434 void ImagePreview::previewUrl( const Q3Url &u )
   435 {
   436     QString path = u.path();
   437     QPixmap pix( path );
   438     if ( pix.isNull() )
   439 	{
   440 		// Strange: If we have fd->setMode (QFileDialog::ExistingFiles)
   441 		// in the filedialog, then there are 3 calls to previewURL 
   442 		// for each selection. And only the first is the actual selected file
   443 		// while the following 2 point to the directory above the current one.
   444 		// So here's my workaround:
   445 		
   446 		if (fdia && fdia->selectedFiles().count()==0)
   447 			setText( QObject::tr("This is not an image.") );
   448 		if (fdia &&fdia->selectedFiles().count()>1)
   449 			setText( QObject::tr("Sorry, no preview for\nmultiple selected files.") );
   450 	}	
   451     else
   452 	{
   453 		float max_w=300;
   454 		float max_h=300;
   455 		float r;
   456 		if (pix.width()>max_w)
   457 		{
   458 			r=max_w / pix.width();
   459 			pix.resize(qRound(pix.width()*r), qRound(pix.height()*r));
   460 			// TODO not a resize, but a shrink/enlarge is needed here...
   461 		}
   462 		if (pix.height()>max_h)
   463 		{
   464 			r=max_h / pix.height();
   465 			pix.resize(qRound(pix.width()*r), qRound(pix.height()*r));
   466 			// TODO not a resize, but a shrink/enlarge is needed here...
   467 		}
   468         setPixmap( pix );
   469 	}	
   470 }
   471 
   472 ImageIO::ImageIO ()
   473 {
   474 	// Create list with supported image types
   475 	// foreach (QByteArray format, QImageWriter::supportedImageFormats()) 
   476 	// imageTypes.append( tr("%1...").arg(QString(format).toUpper()));
   477 	imageFilters.append ("Images (*.png *.jpg *.jpeg *.bmp *.bmp *.ppm *.xpm *.xbm)");
   478 	imageTypes.append ("PNG");
   479 	imageFilters.append ("Portable Network Graphics (*.png)");
   480 	imageTypes.append ("PNG");
   481 	imageFilters.append ("Joint Photographic Experts Group (*.jpg)");
   482 	imageTypes.append ("JPG");
   483 	imageFilters.append ("Joint Photographic Experts Group (*.jpeg)");
   484 	imageTypes.append ("JPG");
   485 	imageFilters.append ("Windows Bitmap (*.bmp)");
   486 	imageTypes.append ("BMP");
   487 	imageFilters.append ("Portable Pixmap (*.ppm)");
   488 	imageTypes.append ("PPM");
   489 	imageFilters.append ("X11 Bitmap (*.xpm)");
   490 	imageTypes.append ("XPM");
   491 	imageFilters.append ("X11 Bitmap (*.xbm)");
   492 	imageTypes.append ("XBM");
   493 }
   494 
   495 QStringList ImageIO::getFilters()
   496 {
   497 	return imageFilters;
   498 }
   499 
   500 QString ImageIO::getType(QString filter)
   501 {
   502 	for (int i=0;i<imageFilters.count()+1;i++)
   503 		if (imageFilters.at(i)==filter) return imageTypes.at(i);
   504 	return QString();	
   505 }