11 #if defined(Q_OS_WIN32)
16 QString maskPath(QString p)
18 // Change " " to "\ " to enable blanks in filenames
19 p=p.replace(QChar('&'),"\\&");
20 return p.replace(QChar(' '),"\\ ");
23 QString convertToRel (const QString &src, const QString &dst)
31 // Special case, we just need the name of the file,
32 // not the complete path
34 d=d.right (d.length()-i-1);
37 // Find relative path from src to dst
39 // Remove the first "/"
40 if (s.section ("/",0,0).isEmpty())
42 s=s.right (s.length()-1);
43 d=d.right (d.length()-1);
46 // remove identical left parts
47 while (s.section("/",0,0) == d.section("/",0,0) )
50 s=s.right (s.length()-i-1);
51 d=d.right (d.length()-i-1);
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 )
59 // find path to go up first and then back to dst
71 #include <QFileDialog>
72 extern QString vymName;
73 extern QDir lastFileDir;
75 QString browseDirectory (QWidget *parent,const QString &caption)
77 QFileDialog fd(parent,caption);
78 fd.setMode (QFileDialog::DirectoryOnly);
79 fd.setCaption(vymName+ " - "+caption);
80 fd.setDir (lastFileDir);
83 if ( fd.exec() == QDialog::Accepted )
84 return fd.selectedFile();
91 bool reallyWriteDirectory(const QString &dir)
93 QStringList eList = QDir(dir).entryList();
94 if (eList.first() ==".") eList.pop_front(); // remove "."
95 if (eList.first() =="..") eList.pop_front(); // remove "."
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,
102 QMessageBox::Cancel | QMessageBox::Default,
103 QMessageBox::NoButton );
105 mb.setButtonText( QMessageBox::Yes, QObject::tr("Overwrite") );
106 mb.setButtonText( QMessageBox::No, QObject::tr("Cancel"));
109 case QMessageBox::Yes:
112 case QMessageBox::Cancel:
120 QString makeTmpDir (bool &ok, QString prefix)
123 QString path=makeUniqueDir (b,QDir::tempPath()+"/"+prefix+"-XXXXXX");
128 bool isInTmpDir(QString fn)
130 QString temp=QDir::tempPath();
132 return fn.left(l)==temp;
135 QString makeUniqueDir (bool &ok,QString s)
137 // Create unique directory e.g. for s="/tmp/vym-XXXXXX"
139 // Convert Separators
140 s=QDir::convertSeparators(s);
142 // Convert QString to string
145 int bytes=s.length();
146 p=(char*) malloc (bytes+1);
148 for (i=0;i<bytes;i++)
149 p[i]=s.at(i).latin1();
152 QString r=mkdtemp (p);
153 if (r.isEmpty()) ok=false;
158 void removeDir(QDir d)
160 // This check should_ not be necessary, but proved to be useful ;-)
161 if (!isInTmpDir(d.path()))
163 qWarning ("file.cpp::removeDir should remove "+d.path()+" - aborted.");
167 // Traverse directories
168 d.setFilter( QDir::Dirs| QDir::Hidden | QDir::NoSymLinks );
169 QFileInfoList list = d.entryInfoList();
172 for (int i = 0; i < list.size(); ++i)
175 if (fi.fileName() != "." && fi.fileName() != ".." )
177 if ( !d.cd(fi.fileName()) )
178 qWarning ("removeDir() cannot find the directory "+fi.fileName());
181 // Recursively remove subdirs
189 d.setFilter( QDir::Files| QDir::Hidden | QDir::NoSymLinks );
190 list = d.entryInfoList();
192 for (int i = 0; i < list.size(); ++i)
195 QFile (fi.filePath()).remove();
198 if (!d.rmdir(d.path()))
199 qWarning ("removeDir("+d.path()+") failed!");
202 void copyDir (QDir src, QDir dst)
204 system ("cp -r "+src.path()+"/* "+dst.path());
207 ErrorCode err=success;
209 Process *cpProc=new Process ();
211 cpProc->setWorkingDirectory (src.path());
216 cpProc->start ("cp",args);
217 if (!cpProc->waitForStarted() )
219 // zip could not be started
220 QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
221 QObject::tr("Couldn't start zip to compress data."));
225 // zip could be started
226 cpProc->waitForFinished();
227 if (cpProc->exitStatus()!=QProcess::NormalExit )
229 QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
230 QObject::tr("cp didn't exit normally")+
231 "\n" + cpProc->getErrout());
235 if (cpProc->exitCode()>0)
237 QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
238 QString("cp exit code: %1").arg(cpProc->exitCode() )+
239 "\n" + cpProc->getErrout() );
243 } // cp could be started
247 void makeSubDirs (const QString &s)
255 ErrorCode zipDir (const QDir &zipDir, const QString &zipName)
257 ErrorCode err=success;
259 // zip the temporary directory
261 Process *zipProc=new Process ();
262 zipProc->setWorkingDirectory (zipDir.path());
267 zipProc->start ("zip",args);
268 if (!zipProc->waitForStarted() )
270 // zip could not be started
271 QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
272 QObject::tr("Couldn't start zip to compress data."));
276 // zip could be started
277 zipProc->waitForFinished();
278 if (zipProc->exitStatus()!=QProcess::NormalExit )
280 QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
281 QObject::tr("zip didn't exit normally")+
282 "\n" + zipProc->getErrout());
286 if (zipProc->exitCode()>0)
288 QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
289 QString("zip exit code: %1").arg(zipProc->exitCode() )+
290 "\n" + zipProc->getErrout() );
294 } // zip could be started
298 ErrorCode unzipDir (const QDir &zipDir, const QString &zipName)
300 ErrorCode err=success;
303 #if !defined(Q_OS_WIN32)
305 Process *zipProc=new Process ();
306 zipProc->setWorkingDirectory (zipDir.path());
307 args << "-o"; // overwrite existing files!
310 args << zipDir.path();
312 zipProc->start ("unzip",args);
313 if (!zipProc->waitForStarted() )
315 QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
316 QObject::tr("Couldn't start unzip to decompress data."));
321 zipProc->waitForFinished();
322 if (zipProc->exitStatus()!=QProcess::NormalExit )
324 QMessageBox::critical( 0,QObject::tr( "Critical Error" ),
325 QObject::tr("unzip didn't exit normally") +
326 zipProc->getErrout() );
330 if (zipProc->exitCode()>0)
332 if (zipProc->exitCode()==9)
333 // no zipped file, but maybe .xml or old version? Try again.
337 QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
338 QString("unzip exit code: %1").arg(zipProc->exitCode() ) +
339 zipProc->getErrout() );
346 // Do this process creation using Win32 API.
348 PROCESS_INFORMATION piProcInfo;
349 STARTUPINFO siStartInfo;
351 // Initialize members of the PROCESS_INFORMATION structure.
352 ::ZeroMemory( &piProcInfo, sizeof(PROCESS_INFORMATION) );
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;
360 // Create command line.
361 QString argv("unzip -o ");
362 argv.append(QDir::convertSeparators(zipName));
364 argv.append(QDir::convertSeparators(zipDir.path()));
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
373 NULL, // use parent's environment
374 NULL, // use parent's current directory
375 &siStartInfo, // STARTUPINFO pointer
376 &piProcInfo) ) // receives PROCESS_INFORMATION
378 QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
379 QObject::tr("Couldn't start unzip to decompress data."));
384 // Wait for it to finish.
385 ::WaitForSingleObject( piProcInfo.hProcess, INFINITE );
388 CloseHandle(piProcInfo.hThread);
389 CloseHandle(piProcInfo.hProcess);
395 bool loadStringFromDisk (const QString &fname, QString &s)
399 if ( !file.open( QIODevice::ReadOnly ) ) return false;
401 QTextStream ts( &file );
402 ts.setEncoding (QTextStream::UnicodeUTF8);
403 while ( !ts.atEnd() )
404 s+=ts.readLine()+"\n";
409 bool saveStringToDisk (const QString &fname, const QString &s)
413 file.setName ( fname);
414 if ( !file.open( QIODevice::WriteOnly ) )
420 // Write it finally, and write in UTF8, no matter what
421 QTextStream ts( &file );
422 ts.setEncoding (QTextStream::UnicodeUTF8);
429 ImagePreview::ImagePreview (QWidget *par=0): QLabel (par)
431 fdia=(Q3FileDialog*)par;
434 void ImagePreview::previewUrl( const Q3Url &u )
436 QString path = u.path();
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:
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.") );
456 if (pix.width()>max_w)
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...
462 if (pix.height()>max_h)
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...
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");
495 QStringList ImageIO::getFilters()
500 QString ImageIO::getType(QString filter)
502 for (int i=0;i<imageFilters.count()+1;i++)
503 if (imageFilters.at(i)==filter) return imageTypes.at(i);