11 // Avoid full inclusion of io.h by just defining mktemp's prototype here.
12 #if defined(Q_OS_WIN32)
13 extern "C" char *_mktemp(char *fmt);
17 QString maskPath(QString p)
19 // Change " " to "\ " to enable blanks in filenames
20 p=p.replace(QChar('&'),"\\&");
21 return p.replace(QChar(' '),"\\ ");
24 QString convertToRel (const QString &src, const QString &dst)
32 // Special case, we just need the name of the file,
33 // not the complete path
35 d=d.right (d.length()-i-1);
38 // Find relative path from src to dst
40 // Remove the first "/"
41 if (s.section ("/",0,0).isEmpty())
43 s=s.right (s.length()-1);
44 d=d.right (d.length()-1);
47 // remove identical left parts
48 while (s.section("/",0,0) == d.section("/",0,0) )
51 s=s.right (s.length()-i-1);
52 d=d.right (d.length()-i-1);
55 // Now take care of paths where we have to go back first
56 int srcsep=s.count("/");
57 int dstsep=d.count("/");
58 if (srcsep <= dstsep )
60 // find path to go up first and then back to dst
72 #include <QFileDialog>
73 extern QString vymName;
74 extern QDir lastFileDir;
76 QString browseDirectory (QWidget *parent,const QString &caption)
78 QFileDialog fd(parent,caption);
79 fd.setMode (QFileDialog::DirectoryOnly);
80 fd.setCaption(vymName+ " - "+caption);
81 fd.setDir (lastFileDir);
84 if ( fd.exec() == QDialog::Accepted )
85 return fd.selectedFile();
92 bool reallyWriteDirectory(const QString &dir)
94 QStringList eList = QDir(dir).entryList();
95 if (eList.first() ==".") eList.pop_front(); // remove "."
96 if (eList.first() =="..") eList.pop_front(); // remove "."
99 QMessageBox mb( vymName,
100 QObject::tr("The directory %1 is not empty.\nDo you risk to overwrite its contents?","write directory").arg(dir),
101 QMessageBox::Warning,
103 QMessageBox::Cancel | QMessageBox::Default,
104 QMessageBox::NoButton );
106 mb.setButtonText( QMessageBox::Yes, QObject::tr("Overwrite") );
107 mb.setButtonText( QMessageBox::No, QObject::tr("Cancel"));
110 case QMessageBox::Yes:
113 case QMessageBox::Cancel:
121 QString makeTmpDir (bool &ok, QString prefix)
124 QString path=makeUniqueDir (b,QDir::tempPath()+"/"+prefix+"-XXXXXX");
129 bool isInTmpDir(QString fn)
131 QString temp=QDir::tempPath();
133 return fn.left(l)==temp;
136 QString makeUniqueDir (bool &ok,QString s)
138 // Create unique directory e.g. for s="/tmp/vym-XXXXXX"
140 // Convert Separators
141 s=QDir::convertSeparators(s);
143 // Convert QString to string
146 int bytes=s.length();
147 p=(char*) malloc (bytes+1);
149 for (i=0;i<bytes;i++)
150 p[i]=s.at(i).latin1();
153 #if defined(Q_OS_WIN32)
154 // There's no mkdtemp on VCEE.
155 QString r=_mktemp(p);
157 QString r=mkdtemp (p);
159 if (r.isEmpty()) ok=false;
164 void removeDir(QDir d)
166 // This check should_ not be necessary, but proved to be useful ;-)
167 if (!isInTmpDir(d.path()))
169 qWarning ("file.cpp::removeDir should remove "+d.path()+" - aborted.");
173 // Traverse directories
174 d.setFilter( QDir::Dirs| QDir::Hidden | QDir::NoSymLinks );
175 QFileInfoList list = d.entryInfoList();
178 for (int i = 0; i < list.size(); ++i)
181 if (fi.fileName() != "." && fi.fileName() != ".." )
183 if ( !d.cd(fi.fileName()) )
184 qWarning ("removeDir() cannot find the directory "+fi.fileName());
187 // Recursively remove subdirs
195 d.setFilter( QDir::Files| QDir::Hidden | QDir::NoSymLinks );
196 list = d.entryInfoList();
198 for (int i = 0; i < list.size(); ++i)
201 QFile (fi.filePath()).remove();
204 if (!d.rmdir(d.path()))
205 qWarning ("removeDir("+d.path()+") failed!");
208 void copyDir (QDir src, QDir dst)
210 system ("cp -r "+src.path()+"/* "+dst.path());
213 ErrorCode err=success;
215 Process *cpProc=new Process ();
217 cpProc->setWorkingDirectory (src.path());
222 cpProc->start ("cp",args);
223 if (!cpProc->waitForStarted() )
225 // zip could not be started
226 QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
227 QObject::tr("Couldn't start zip to compress data."));
231 // zip could be started
232 cpProc->waitForFinished();
233 if (cpProc->exitStatus()!=QProcess::NormalExit )
235 QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
236 QObject::tr("cp didn't exit normally")+
237 "\n" + cpProc->getErrout());
241 if (cpProc->exitCode()>0)
243 QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
244 QString("cp exit code: %1").arg(cpProc->exitCode() )+
245 "\n" + cpProc->getErrout() );
249 } // cp could be started
253 void makeSubDirs (const QString &s)
261 ErrorCode zipDir (const QDir &zipDir, const QString &zipName)
263 ErrorCode err=success;
265 // zip the temporary directory
267 Process *zipProc=new Process ();
268 zipProc->setWorkingDirectory (zipDir.path());
273 zipProc->start ("zip",args);
274 if (!zipProc->waitForStarted() )
276 // zip could not be started
277 QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
278 QObject::tr("Couldn't start zip to compress data."));
282 // zip could be started
283 zipProc->waitForFinished();
284 if (zipProc->exitStatus()!=QProcess::NormalExit )
286 QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
287 QObject::tr("zip didn't exit normally")+
288 "\n" + zipProc->getErrout());
292 if (zipProc->exitCode()>0)
294 QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
295 QString("zip exit code: %1").arg(zipProc->exitCode() )+
296 "\n" + zipProc->getErrout() );
300 } // zip could be started
304 ErrorCode unzipDir (const QDir &zipDir, const QString &zipName)
306 ErrorCode err=success;
309 #if !defined(Q_OS_WIN32)
311 Process *zipProc=new Process ();
312 zipProc->setWorkingDirectory (zipDir.path());
313 args << "-o"; // overwrite existing files!
316 args << zipDir.path();
318 zipProc->start ("unzip",args);
319 if (!zipProc->waitForStarted() )
321 QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
322 QObject::tr("Couldn't start unzip to decompress data."));
327 zipProc->waitForFinished();
328 if (zipProc->exitStatus()!=QProcess::NormalExit )
330 QMessageBox::critical( 0,QObject::tr( "Critical Error" ),
331 QObject::tr("unzip didn't exit normally") +
332 zipProc->getErrout() );
336 if (zipProc->exitCode()>0)
338 if (zipProc->exitCode()==9)
339 // no zipped file, but maybe .xml or old version? Try again.
343 QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
344 QString("unzip exit code: %1").arg(zipProc->exitCode() ) +
345 zipProc->getErrout() );
352 // Do this process creation using Win32 API.
354 PROCESS_INFORMATION piProcInfo;
355 STARTUPINFO siStartInfo;
357 // Initialize members of the PROCESS_INFORMATION structure.
358 ::ZeroMemory( &piProcInfo, sizeof(PROCESS_INFORMATION) );
360 // Set up members of the STARTUPINFO structure.
361 ::ZeroMemory( &siStartInfo, sizeof(STARTUPINFO) );
362 siStartInfo.cb = sizeof(STARTUPINFO);
364 // Create command line.
365 QString argv("unzip -o ");
366 argv.append(QDir::convertSeparators(zipName));
368 argv.append(QDir::convertSeparators(zipDir.path()));
370 // Create the child process.
371 if( !::CreateProcess(NULL,
372 (LPWSTR)argv.unicode(), // command line
373 NULL, // process security attributes
374 NULL, // primary thread security attributes
375 TRUE, // handles are inherited
377 NULL, // use parent's environment
378 NULL, // use parent's current directory
379 &siStartInfo, // STARTUPINFO pointer
380 &piProcInfo) ) // receives PROCESS_INFORMATION
386 // Wait for it to finish.
387 ::WaitForSingleObject( piProcInfo.hProcess, 10000 );
393 bool loadStringFromDisk (const QString &fname, QString &s)
397 if ( !file.open( QIODevice::ReadOnly ) ) return false;
399 QTextStream ts( &file );
400 ts.setEncoding (QTextStream::UnicodeUTF8);
401 while ( !ts.atEnd() )
402 s+=ts.readLine()+"\n";
407 bool saveStringToDisk (const QString &fname, const QString &s)
411 file.setName ( fname);
412 if ( !file.open( QIODevice::WriteOnly ) )
418 // Write it finally, and write in UTF8, no matter what
419 QTextStream ts( &file );
420 ts.setEncoding (QTextStream::UnicodeUTF8);
427 ImagePreview::ImagePreview (QWidget *par=0): QLabel (par)
429 fdia=(Q3FileDialog*)par;
432 void ImagePreview::previewUrl( const Q3Url &u )
434 QString path = u.path();
438 // Strange: If we have fd->setMode (QFileDialog::ExistingFiles)
439 // in the filedialog, then there are 3 calls to previewURL
440 // for each selection. And only the first is the actual selected file
441 // while the following 2 point to the directory above the current one.
442 // So here's my workaround:
444 if (fdia && fdia->selectedFiles().count()==0)
445 setText( QObject::tr("This is not an image.") );
446 if (fdia &&fdia->selectedFiles().count()>1)
447 setText( QObject::tr("Sorry, no preview for\nmultiple selected files.") );
454 if (pix.width()>max_w)
456 r=max_w / pix.width();
457 pix.resize(qRound(pix.width()*r), qRound(pix.height()*r));
458 // TODO not a resize, but a shrink/enlarge is needed here...
460 if (pix.height()>max_h)
462 r=max_h / pix.height();
463 pix.resize(qRound(pix.width()*r), qRound(pix.height()*r));
464 // TODO not a resize, but a shrink/enlarge is needed here...
472 // Create list with supported image types
473 // foreach (QByteArray format, QImageWriter::supportedImageFormats())
474 // imageTypes.append( tr("%1...").arg(QString(format).toUpper()));
475 imageFilters.append ("Images (*.png *.jpg *.jpeg *.bmp *.bmp *.ppm *.xpm *.xbm)");
476 imageTypes.append ("PNG");
477 imageFilters.append ("Portable Network Graphics (*.png)");
478 imageTypes.append ("PNG");
479 imageFilters.append ("Joint Photographic Experts Group (*.jpg)");
480 imageTypes.append ("JPG");
481 imageFilters.append ("Joint Photographic Experts Group (*.jpeg)");
482 imageTypes.append ("JPG");
483 imageFilters.append ("Windows Bitmap (*.bmp)");
484 imageTypes.append ("BMP");
485 imageFilters.append ("Portable Pixmap (*.ppm)");
486 imageTypes.append ("PPM");
487 imageFilters.append ("X11 Bitmap (*.xpm)");
488 imageTypes.append ("XPM");
489 imageFilters.append ("X11 Bitmap (*.xbm)");
490 imageTypes.append ("XBM");
493 QStringList ImageIO::getFilters()
498 QString ImageIO::getType(QString filter)
500 for (int i=0;i<imageFilters.count()+1;i++)
501 if (imageFilters.at(i)==filter) return imageTypes.at(i);