12 #if defined(Q_OS_WIN32)
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 QString r=mkdtemp (p);
154 if (r.isEmpty()) ok=false;
159 void removeDir(QDir d)
161 // This check should_ not be necessary, but proved to be useful ;-)
162 if (!isInTmpDir(d.path()))
164 qWarning ("file.cpp::removeDir should remove "+d.path()+" - aborted.");
168 // Traverse directories
169 d.setFilter( QDir::Dirs| QDir::Hidden | QDir::NoSymLinks );
170 QFileInfoList list = d.entryInfoList();
173 for (int i = 0; i < list.size(); ++i)
176 if (fi.fileName() != "." && fi.fileName() != ".." )
178 if ( !d.cd(fi.fileName()) )
179 qWarning ("removeDir() cannot find the directory "+fi.fileName());
182 // Recursively remove subdirs
190 d.setFilter( QDir::Files| QDir::Hidden | QDir::NoSymLinks );
191 list = d.entryInfoList();
193 for (int i = 0; i < list.size(); ++i)
196 QFile (fi.filePath()).remove();
199 if (!d.rmdir(d.path()))
200 qWarning ("removeDir("+d.path()+") failed!");
203 void copyDir (QDir src, QDir dst)
205 system ("cp -r "+src.path()+"/* "+dst.path());
208 ErrorCode err=success;
210 Process *cpProc=new Process ();
212 cpProc->setWorkingDirectory (src.path());
217 cpProc->start ("cp",args);
218 if (!cpProc->waitForStarted() )
220 // zip could not be started
221 QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
222 QObject::tr("Couldn't start zip to compress data."));
226 // zip could be started
227 cpProc->waitForFinished();
228 if (cpProc->exitStatus()!=QProcess::NormalExit )
230 QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
231 QObject::tr("cp didn't exit normally")+
232 "\n" + cpProc->getErrout());
236 if (cpProc->exitCode()>0)
238 QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
239 QString("cp exit code: %1").arg(cpProc->exitCode() )+
240 "\n" + cpProc->getErrout() );
244 } // cp could be started
248 void makeSubDirs (const QString &s)
256 ErrorCode zipDir (const QDir &zipDir, const QString &zipName)
258 ErrorCode err=success;
260 // zip the temporary directory
262 Process *zipProc=new Process ();
263 zipProc->setWorkingDirectory (zipDir.path());
268 zipProc->start ("zip",args);
269 if (!zipProc->waitForStarted() )
271 // zip could not be started
272 QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
273 QObject::tr("Couldn't start zip to compress data."));
277 // zip could be started
278 zipProc->waitForFinished();
279 if (zipProc->exitStatus()!=QProcess::NormalExit )
281 QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
282 QObject::tr("zip didn't exit normally")+
283 "\n" + zipProc->getErrout());
287 if (zipProc->exitCode()>0)
289 QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
290 QString("zip exit code: %1").arg(zipProc->exitCode() )+
291 "\n" + zipProc->getErrout() );
295 } // zip could be started
299 ErrorCode unzipDir (const QDir &zipDir, const QString &zipName)
301 ErrorCode err=success;
304 #if !defined(Q_OS_WIN32)
306 Process *zipProc=new Process ();
307 zipProc->setWorkingDirectory (zipDir.path());
308 args << "-o"; // overwrite existing files!
311 args << zipDir.path();
313 zipProc->start ("unzip",args);
314 if (!zipProc->waitForStarted() )
316 QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
317 QObject::tr("Couldn't start unzip to decompress data."));
322 zipProc->waitForFinished();
323 if (zipProc->exitStatus()!=QProcess::NormalExit )
325 QMessageBox::critical( 0,QObject::tr( "Critical Error" ),
326 QObject::tr("unzip didn't exit normally") +
327 zipProc->getErrout() );
331 if (zipProc->exitCode()>0)
333 if (zipProc->exitCode()==9)
334 // no zipped file, but maybe .xml or old version? Try again.
338 QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
339 QString("unzip exit code: %1").arg(zipProc->exitCode() ) +
340 zipProc->getErrout() );
347 // Do this process creation using Win32 API.
349 PROCESS_INFORMATION piProcInfo;
350 STARTUPINFO siStartInfo;
352 // Initialize members of the PROCESS_INFORMATION structure.
353 ::ZeroMemory( &piProcInfo, sizeof(PROCESS_INFORMATION) );
355 // Set up members of the STARTUPINFO structure.
356 ::ZeroMemory( &siStartInfo, sizeof(STARTUPINFO) );
357 siStartInfo.cb = sizeof(STARTUPINFO);
359 // Create command line.
360 QString argv("unzip -o ");
361 argv.append(QDir::convertSeparators(zipName));
363 argv.append(QDir::convertSeparators(zipDir.path()));
365 // Create the child process.
366 if( !::CreateProcess(NULL,
367 (LPWSTR)argv.unicode(), // command line
368 NULL, // process security attributes
369 NULL, // primary thread security attributes
370 TRUE, // handles are inherited
372 NULL, // use parent's environment
373 NULL, // use parent's current directory
374 &siStartInfo, // STARTUPINFO pointer
375 &piProcInfo) ) // receives PROCESS_INFORMATION
381 // Wait for it to finish.
382 ::WaitForSingleObject( piProcInfo.hProcess, 10000 );
388 bool loadStringFromDisk (const QString &fname, QString &s)
392 if ( !file.open( QIODevice::ReadOnly ) ) return false;
394 QTextStream ts( &file );
395 ts.setEncoding (QTextStream::UnicodeUTF8);
396 while ( !ts.atEnd() )
397 s+=ts.readLine()+"\n";
402 bool saveStringToDisk (const QString &fname, const QString &s)
406 file.setName ( fname);
407 if ( !file.open( QIODevice::WriteOnly ) )
413 // Write it finally, and write in UTF8, no matter what
414 QTextStream ts( &file );
415 ts.setEncoding (QTextStream::UnicodeUTF8);
422 ImagePreview::ImagePreview (QWidget *par=0): QLabel (par)
424 fdia=(QFileDialog*)par;
427 void ImagePreview::previewUrl( const QUrl &u )
429 QString path = u.path();
433 // Strange: If we have fd->setMode (QFileDialog::ExistingFiles)
434 // in the filedialog, then there are 3 calls to previewURL
435 // for each selection. And only the first is the actual selected file
436 // while the following 2 point to the directory above the current one.
437 // So here's my workaround:
439 if (fdia && fdia->selectedFiles().count()==0)
440 setText( QObject::tr("This is not an image.") );
441 if (fdia &&fdia->selectedFiles().count()>1)
442 setText( QObject::tr("Sorry, no preview for\nmultiple selected files.") );
449 if (pix.width()>max_w)
451 r=max_w / pix.width();
452 pix.resize(qRound(pix.width()*r), qRound(pix.height()*r));
453 // TODO not a resize, but a shrink/enlarge is needed here...
455 if (pix.height()>max_h)
457 r=max_h / pix.height();
458 pix.resize(qRound(pix.width()*r), qRound(pix.height()*r));
459 // TODO not a resize, but a shrink/enlarge is needed here...
467 // Create list with supported image types
468 // foreach (QByteArray format, QImageWriter::supportedImageFormats())
469 // imageTypes.append( tr("%1...").arg(QString(format).toUpper()));
470 imageFilters.append ("Images (*.png *.jpg *.jpeg *.bmp *.bmp *.ppm *.xpm *.xbm)");
471 imageTypes.append ("PNG");
472 imageFilters.append ("Portable Network Graphics (*.png)");
473 imageTypes.append ("PNG");
474 imageFilters.append ("Joint Photographic Experts Group (*.jpg)");
475 imageTypes.append ("JPG");
476 imageFilters.append ("Joint Photographic Experts Group (*.jpeg)");
477 imageTypes.append ("JPG");
478 imageFilters.append ("Windows Bitmap (*.bmp)");
479 imageTypes.append ("BMP");
480 imageFilters.append ("Portable Pixmap (*.ppm)");
481 imageTypes.append ("PPM");
482 imageFilters.append ("X11 Bitmap (*.xpm)");
483 imageTypes.append ("XPM");
484 imageFilters.append ("X11 Bitmap (*.xbm)");
485 imageTypes.append ("XBM");
488 QStringList ImageIO::getFilters()
493 QString ImageIO::getType(QString filter)
495 for (int i=0;i<imageFilters.count()+1;i++)
496 if (imageFilters.at(i)==filter) return imageTypes.at(i);