11 QString maskPath(QString p)
13 // Change " " to "\ " to enable blanks in filenames
14 p=p.replace(QChar('&'),"\\&");
15 return p.replace(QChar(' '),"\\ ");
18 QString convertToRel (const QString &src, const QString &dst)
26 // Special case, we just need the name of the file,
27 // not the complete path
29 d=d.right (d.length()-i-1);
32 // Find relative path from src to dst
34 // Remove the first "/"
35 if (s.section ("/",0,0).isEmpty())
37 s=s.right (s.length()-1);
38 d=d.right (d.length()-1);
41 // remove identical left parts
42 while (s.section("/",0,0) == d.section("/",0,0) )
45 s=s.right (s.length()-i-1);
46 d=d.right (d.length()-i-1);
49 // Now take care of paths where we have to go back first
50 int srcsep=s.count("/");
51 int dstsep=d.count("/");
52 if (srcsep <= dstsep )
54 // find path to go up first and then back to dst
66 QString makeUniqueDir (bool &ok,QString s)
68 // Create unique directory e.g. s="/tmp/vym-XXXXXX"
70 // Convert QString to string first
74 p=(char*) malloc (bytes+1);
77 p[i]=s.at(i).latin1();
79 QString r=mkdtemp (p);
80 if (r.isEmpty()) ok=false;
85 void removeDir(QDir d)
87 if (d.path().left(4)!="/tmp")
89 // This _should_ not be necessary, but proved to be useful ;-)
90 qWarning ("file.cpp::removeDir should remove "+d.path()+" - aborted.");
94 // Traverse directories
95 d.setFilter( QDir::Dirs| QDir::Hidden | QDir::NoSymLinks );
96 QFileInfoList list = d.entryInfoList();
99 for (int i = 0; i < list.size(); ++i)
102 if (fi.fileName() != "." && fi.fileName() != ".." )
104 if ( !d.cd(fi.fileName()) )
105 qWarning ("removeDir() cannot find the directory "+fi.fileName());
108 // Recursively remove subdirs
116 d.setFilter( QDir::Files| QDir::Hidden | QDir::NoSymLinks );
117 list = d.entryInfoList();
119 for (int i = 0; i < list.size(); ++i)
122 QFile (fi.filePath()).remove();
125 if (!d.rmdir(d.path()))
126 qWarning ("removeDir("+d.path()+") failed!");
129 void copyDir (QDir src, QDir dst)
131 system ("cp -r "+src.path()+"/* "+dst.path());
134 ErrorCode err=success;
136 Process *cpProc=new Process ();
138 cpProc->setWorkingDirectory (src.path());
143 cpProc->start ("cp",args);
144 if (!cpProc->waitForStarted() )
146 // zip could not be started
147 QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
148 QObject::tr("Couldn't start zip to compress data."));
152 // zip could be started
153 cpProc->waitForFinished();
154 if (cpProc->exitStatus()!=QProcess::NormalExit )
156 QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
157 QObject::tr("cp didn't exit normally")+
158 "\n" + cpProc->getErrout());
162 if (cpProc->exitCode()>0)
164 QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
165 QString("cp exit code: %1").arg(cpProc->exitCode() )+
166 "\n" + cpProc->getErrout() );
170 } // cp could be started
174 void makeSubDirs (const QString &s)
182 ErrorCode zipDir (const QDir &zipDir, const QString &zipName)
184 ErrorCode err=success;
186 // zip the temporary directory
188 Process *zipProc=new Process ();
189 zipProc->setWorkingDirectory (zipDir.path());
194 zipProc->start ("zip",args);
195 if (!zipProc->waitForStarted() )
197 // zip could not be started
198 QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
199 QObject::tr("Couldn't start zip to compress data."));
203 // zip could be started
204 zipProc->waitForFinished();
205 if (zipProc->exitStatus()!=QProcess::NormalExit )
207 QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
208 QObject::tr("zip didn't exit normally")+
209 "\n" + zipProc->getErrout());
213 if (zipProc->exitCode()>0)
215 QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
216 QString("zip exit code: %1").arg(zipProc->exitCode() )+
217 "\n" + zipProc->getErrout() );
221 } // zip could be started
225 ErrorCode unzipDir (const QDir &zipDir, const QString &zipName)
227 ErrorCode err=success;
231 Process *zipProc=new Process ();
232 zipProc->setWorkingDirectory (zipDir.path());
233 args << "-o"; // overwrite existing files!
236 args << zipDir.path();
238 zipProc->start ("unzip",args);
239 if (!zipProc->waitForStarted() )
241 QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
242 QObject::tr("Couldn't start unzip to decompress data."));
247 zipProc->waitForFinished();
248 if (zipProc->exitStatus()!=QProcess::NormalExit )
250 QMessageBox::critical( 0,QObject::tr( "Critical Error" ),
251 QObject::tr("unzip didn't exit normally") +
252 zipProc->getErrout() );
256 if (zipProc->exitCode()>0)
258 if (zipProc->exitCode()==9)
259 // no zipped file, but maybe .xml or old version? Try again.
263 QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
264 QString("unzip exit code: %1").arg(zipProc->exitCode() ) +
265 zipProc->getErrout() );
274 bool loadStringFromDisk (const QString &fname, QString &s)
278 if ( !file.open( QIODevice::ReadOnly ) ) return false;
280 QTextStream ts( &file );
281 ts.setEncoding (QTextStream::UnicodeUTF8);
282 while ( !ts.atEnd() )
283 s+=ts.readLine()+"\n";
288 bool saveStringToDisk (const QString &fname, const QString &s)
292 file.setName ( fname);
293 if ( !file.open( QIODevice::WriteOnly ) )
299 // Write it finally, and write in UTF8, no matter what
300 QTextStream ts( &file );
301 ts.setEncoding (QTextStream::UnicodeUTF8);
308 ImagePreview::ImagePreview (QWidget *par=0): QLabel (par)
310 fdia=(Q3FileDialog*)par;
313 void ImagePreview::previewUrl( const Q3Url &u )
315 QString path = u.path();
319 // Strange: If we have fd->setMode (QFileDialog::ExistingFiles)
320 // in the filedialog, then there are 3 calls to previewURL
321 // for each selection. And only the first is the actual selected file
322 // while the following 2 point to the directory above the current one.
323 // So here's my workaround:
325 if (fdia && fdia->selectedFiles().count()==0)
326 setText( QObject::tr("This is not an image.") );
327 if (fdia &&fdia->selectedFiles().count()>1)
328 setText( QObject::tr("Sorry, no preview for\nmultiple selected files.") );
335 if (pix.width()>max_w)
337 r=max_w / pix.width();
338 pix.resize(qRound(pix.width()*r), qRound(pix.height()*r));
339 // TODO not a resize, but a shrink/enlarge is needed here...
341 if (pix.height()>max_h)
343 r=max_h / pix.height();
344 pix.resize(qRound(pix.width()*r), qRound(pix.height()*r));
345 // TODO not a resize, but a shrink/enlarge is needed here...
353 // Create list with supported image types
354 // foreach (QByteArray format, QImageWriter::supportedImageFormats())
355 // imageTypes.append( tr("%1...").arg(QString(format).toUpper()));
356 imageFilters.append ("Images (*.png *.jpg *.jpeg *.bmp *.bmp *.ppm *.xpm *.xbm)");
357 imageTypes.append ("PNG");
358 imageFilters.append ("Portable Network Graphics (*.png)");
359 imageTypes.append ("PNG");
360 imageFilters.append ("Joint Photographic Experts Group (*.jpg)");
361 imageTypes.append ("JPG");
362 imageFilters.append ("Joint Photographic Experts Group (*.jpeg)");
363 imageTypes.append ("JPG");
364 imageFilters.append ("Windows Bitmap (*.bmp)");
365 imageTypes.append ("BMP");
366 imageFilters.append ("Portable Pixmap (*.ppm)");
367 imageTypes.append ("PPM");
368 imageFilters.append ("X11 Bitmap (*.xpm)");
369 imageTypes.append ("XPM");
370 imageFilters.append ("X11 Bitmap (*.xbm)");
371 imageTypes.append ("XBM");
374 QStringList ImageIO::getFilters()
379 QString ImageIO::getType(QString filter)
381 for (int i=0;i<imageFilters.count()+1;i++)
382 if (imageFilters.at(i)==filter) return imageTypes.at(i);