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 makeSubDirs (const QString &s)
137 ErrorCode zipDir (const QDir &zipDir, const QString &zipName)
139 ErrorCode err=success;
141 // zip the temporary directory
143 Process *zipProc=new Process ();
144 zipProc->setWorkingDirectory (zipDir.path());
149 zipProc->start ("zip",args);
150 if (!zipProc->waitForStarted() )
152 // zip could not be started
153 QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
154 QObject::tr("Couldn't start zip to compress data."));
158 // zip could be started
159 zipProc->waitForFinished();
160 if (zipProc->exitStatus()!=QProcess::NormalExit )
162 QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
163 QObject::tr("zip didn't exit normally")+
164 "\n" + zipProc->getErrout());
168 if (zipProc->exitCode()>0)
170 QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
171 QString("zip exit code: %1").arg(zipProc->exitCode() )+
172 "\n" + zipProc->getErrout() );
176 } // zip could be started
180 ErrorCode unzipDir (const QDir &zipDir, const QString &zipName)
182 ErrorCode err=success;
186 Process *zipProc=new Process ();
187 zipProc->setWorkingDirectory (zipDir.path());
188 args << "-o"; // overwrite existing files!
191 args << zipDir.path();
193 zipProc->start ("unzip",args);
194 if (!zipProc->waitForStarted() )
196 QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
197 QObject::tr("Couldn't start unzip to decompress data."));
202 zipProc->waitForFinished();
203 if (zipProc->exitStatus()!=QProcess::NormalExit )
205 QMessageBox::critical( 0,QObject::tr( "Critical Error" ),
206 QObject::tr("unzip didn't exit normally") +
207 zipProc->getErrout() );
211 if (zipProc->exitCode()>0)
213 if (zipProc->exitCode()==9)
214 // no zipped file, but maybe .xml or old version? Try again.
218 QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
219 QString("unzip exit code: %1").arg(zipProc->exitCode() ) +
220 zipProc->getErrout() );
229 bool loadStringFromDisk (const QString &fname, QString &s)
233 if ( !file.open( QIODevice::ReadOnly ) ) return false;
235 QTextStream ts( &file );
236 ts.setEncoding (QTextStream::UnicodeUTF8);
237 while ( !ts.atEnd() )
238 s+=ts.readLine()+"\n";
243 bool saveStringToDisk (const QString &fname, const QString &s)
247 file.setName ( fname);
248 if ( !file.open( QIODevice::WriteOnly ) )
254 // Write it finally, and write in UTF8, no matter what
255 QTextStream ts( &file );
256 ts.setEncoding (QTextStream::UnicodeUTF8);
263 ImagePreview::ImagePreview (QWidget *par=0): QLabel (par)
265 fdia=(Q3FileDialog*)par;
268 void ImagePreview::previewUrl( const Q3Url &u )
270 QString path = u.path();
274 // Strange: If we have fd->setMode (QFileDialog::ExistingFiles)
275 // in the filedialog, then there are 3 calls to previewURL
276 // for each selection. And only the first is the actual selected file
277 // while the following 2 point to the directory above the current one.
278 // So here's my workaround:
280 if (fdia && fdia->selectedFiles().count()==0)
281 setText( QObject::tr("This is not an image.") );
282 if (fdia &&fdia->selectedFiles().count()>1)
283 setText( QObject::tr("Sorry, no preview for\nmultiple selected files.") );
290 if (pix.width()>max_w)
292 r=max_w / pix.width();
293 pix.resize(qRound(pix.width()*r), qRound(pix.height()*r));
294 // TODO not a resize, but a shrink/enlarge is needed here...
296 if (pix.height()>max_h)
298 r=max_h / pix.height();
299 pix.resize(qRound(pix.width()*r), qRound(pix.height()*r));
300 // TODO not a resize, but a shrink/enlarge is needed here...
308 // Create list with supported image types
309 // foreach (QByteArray format, QImageWriter::supportedImageFormats())
310 // imageTypes.append( tr("%1...").arg(QString(format).toUpper()));
311 imageFilters.append ("Images (*.png *.jpg *.jpeg *.bmp *.bmp *.ppm *.xpm *.xbm)");
312 imageTypes.append ("PNG");
313 imageFilters.append ("Portable Network Graphics (*.png)");
314 imageTypes.append ("PNG");
315 imageFilters.append ("Joint Photographic Experts Group (*.jpg)");
316 imageTypes.append ("JPG");
317 imageFilters.append ("Joint Photographic Experts Group (*.jpeg)");
318 imageTypes.append ("JPG");
319 imageFilters.append ("Windows Bitmap (*.bmp)");
320 imageTypes.append ("BMP");
321 imageFilters.append ("Portable Pixmap (*.ppm)");
322 imageTypes.append ("PPM");
323 imageFilters.append ("X11 Bitmap (*.xpm)");
324 imageTypes.append ("XPM");
325 imageFilters.append ("X11 Bitmap (*.xbm)");
326 imageTypes.append ("XBM");
329 QStringList ImageIO::getFilters()
334 QString ImageIO::getType(QString filter)
336 for (int i=0;i<imageFilters.count()+1;i++)
337 if (imageFilters.at(i)==filter) return imageTypes.at(i);