GPL V2. Added settings to documentation
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 #include <QFileDialog>
67 extern QString vymName;
68 extern QDir lastFileDir;
70 QString browseDirectory (QWidget *parent,const QString &caption)
72 QFileDialog fd(parent,caption);
73 fd.setMode (QFileDialog::DirectoryOnly);
74 fd.setCaption(vymName+ " - "+caption);
75 fd.setDir (lastFileDir);
78 if ( fd.exec() == QDialog::Accepted )
79 return fd.selectedFile();
86 bool reallyWriteDirectory(const QString &dir)
88 QStringList eList = QDir(dir).entryList();
89 if (eList.first() ==".") eList.pop_front(); // remove "."
90 if (eList.first() =="..") eList.pop_front(); // remove "."
93 QMessageBox mb( vymName,
94 QObject::tr("The directory %1 is not empty.\nDo you risk to overwrite its contents?","write directory").arg(dir),
97 QMessageBox::Cancel | QMessageBox::Default,
98 QMessageBox::QMessageBox::NoButton );
100 mb.setButtonText( QMessageBox::Yes, QObject::tr("Overwrite") );
101 mb.setButtonText( QMessageBox::No, QObject::tr("Cancel"));
104 case QMessageBox::Yes:
107 case QMessageBox::Cancel:
115 QString makeUniqueDir (bool &ok,QString s)
117 // Create unique directory e.g. s="/tmp/vym-XXXXXX"
119 // Convert QString to string first
122 int bytes=s.length();
123 p=(char*) malloc (bytes+1);
125 for (i=0;i<bytes;i++)
126 p[i]=s.at(i).latin1();
128 QString r=mkdtemp (p);
129 if (r.isEmpty()) ok=false;
134 void removeDir(QDir d)
136 if (d.path().left(4)!="/tmp")
138 // This _should_ not be necessary, but proved to be useful ;-)
139 qWarning ("file.cpp::removeDir should remove "+d.path()+" - aborted.");
143 // Traverse directories
144 d.setFilter( QDir::Dirs| QDir::Hidden | QDir::NoSymLinks );
145 QFileInfoList list = d.entryInfoList();
148 for (int i = 0; i < list.size(); ++i)
151 if (fi.fileName() != "." && fi.fileName() != ".." )
153 if ( !d.cd(fi.fileName()) )
154 qWarning ("removeDir() cannot find the directory "+fi.fileName());
157 // Recursively remove subdirs
165 d.setFilter( QDir::Files| QDir::Hidden | QDir::NoSymLinks );
166 list = d.entryInfoList();
168 for (int i = 0; i < list.size(); ++i)
171 QFile (fi.filePath()).remove();
174 if (!d.rmdir(d.path()))
175 qWarning ("removeDir("+d.path()+") failed!");
178 void copyDir (QDir src, QDir dst)
180 system ("cp -r "+src.path()+"/* "+dst.path());
183 ErrorCode err=success;
185 Process *cpProc=new Process ();
187 cpProc->setWorkingDirectory (src.path());
192 cpProc->start ("cp",args);
193 if (!cpProc->waitForStarted() )
195 // zip could not be started
196 QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
197 QObject::tr("Couldn't start zip to compress data."));
201 // zip could be started
202 cpProc->waitForFinished();
203 if (cpProc->exitStatus()!=QProcess::NormalExit )
205 QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
206 QObject::tr("cp didn't exit normally")+
207 "\n" + cpProc->getErrout());
211 if (cpProc->exitCode()>0)
213 QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
214 QString("cp exit code: %1").arg(cpProc->exitCode() )+
215 "\n" + cpProc->getErrout() );
219 } // cp could be started
223 void makeSubDirs (const QString &s)
231 ErrorCode zipDir (const QDir &zipDir, const QString &zipName)
233 ErrorCode err=success;
235 // zip the temporary directory
237 Process *zipProc=new Process ();
238 zipProc->setWorkingDirectory (zipDir.path());
243 zipProc->start ("zip",args);
244 if (!zipProc->waitForStarted() )
246 // zip could not be started
247 QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
248 QObject::tr("Couldn't start zip to compress data."));
252 // zip could be started
253 zipProc->waitForFinished();
254 if (zipProc->exitStatus()!=QProcess::NormalExit )
256 QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
257 QObject::tr("zip didn't exit normally")+
258 "\n" + zipProc->getErrout());
262 if (zipProc->exitCode()>0)
264 QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
265 QString("zip exit code: %1").arg(zipProc->exitCode() )+
266 "\n" + zipProc->getErrout() );
270 } // zip could be started
274 ErrorCode unzipDir (const QDir &zipDir, const QString &zipName)
276 ErrorCode err=success;
280 Process *zipProc=new Process ();
281 zipProc->setWorkingDirectory (zipDir.path());
282 args << "-o"; // overwrite existing files!
285 args << zipDir.path();
287 zipProc->start ("unzip",args);
288 if (!zipProc->waitForStarted() )
290 QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
291 QObject::tr("Couldn't start unzip to decompress data."));
296 zipProc->waitForFinished();
297 if (zipProc->exitStatus()!=QProcess::NormalExit )
299 QMessageBox::critical( 0,QObject::tr( "Critical Error" ),
300 QObject::tr("unzip didn't exit normally") +
301 zipProc->getErrout() );
305 if (zipProc->exitCode()>0)
307 if (zipProc->exitCode()==9)
308 // no zipped file, but maybe .xml or old version? Try again.
312 QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
313 QString("unzip exit code: %1").arg(zipProc->exitCode() ) +
314 zipProc->getErrout() );
323 bool loadStringFromDisk (const QString &fname, QString &s)
327 if ( !file.open( QIODevice::ReadOnly ) ) return false;
329 QTextStream ts( &file );
330 ts.setEncoding (QTextStream::UnicodeUTF8);
331 while ( !ts.atEnd() )
332 s+=ts.readLine()+"\n";
337 bool saveStringToDisk (const QString &fname, const QString &s)
341 file.setName ( fname);
342 if ( !file.open( QIODevice::WriteOnly ) )
348 // Write it finally, and write in UTF8, no matter what
349 QTextStream ts( &file );
350 ts.setEncoding (QTextStream::UnicodeUTF8);
357 ImagePreview::ImagePreview (QWidget *par=0): QLabel (par)
359 fdia=(Q3FileDialog*)par;
362 void ImagePreview::previewUrl( const Q3Url &u )
364 QString path = u.path();
368 // Strange: If we have fd->setMode (QFileDialog::ExistingFiles)
369 // in the filedialog, then there are 3 calls to previewURL
370 // for each selection. And only the first is the actual selected file
371 // while the following 2 point to the directory above the current one.
372 // So here's my workaround:
374 if (fdia && fdia->selectedFiles().count()==0)
375 setText( QObject::tr("This is not an image.") );
376 if (fdia &&fdia->selectedFiles().count()>1)
377 setText( QObject::tr("Sorry, no preview for\nmultiple selected files.") );
384 if (pix.width()>max_w)
386 r=max_w / pix.width();
387 pix.resize(qRound(pix.width()*r), qRound(pix.height()*r));
388 // TODO not a resize, but a shrink/enlarge is needed here...
390 if (pix.height()>max_h)
392 r=max_h / pix.height();
393 pix.resize(qRound(pix.width()*r), qRound(pix.height()*r));
394 // TODO not a resize, but a shrink/enlarge is needed here...
402 // Create list with supported image types
403 // foreach (QByteArray format, QImageWriter::supportedImageFormats())
404 // imageTypes.append( tr("%1...").arg(QString(format).toUpper()));
405 imageFilters.append ("Images (*.png *.jpg *.jpeg *.bmp *.bmp *.ppm *.xpm *.xbm)");
406 imageTypes.append ("PNG");
407 imageFilters.append ("Portable Network Graphics (*.png)");
408 imageTypes.append ("PNG");
409 imageFilters.append ("Joint Photographic Experts Group (*.jpg)");
410 imageTypes.append ("JPG");
411 imageFilters.append ("Joint Photographic Experts Group (*.jpeg)");
412 imageTypes.append ("JPG");
413 imageFilters.append ("Windows Bitmap (*.bmp)");
414 imageTypes.append ("BMP");
415 imageFilters.append ("Portable Pixmap (*.ppm)");
416 imageTypes.append ("PPM");
417 imageFilters.append ("X11 Bitmap (*.xpm)");
418 imageTypes.append ("XPM");
419 imageFilters.append ("X11 Bitmap (*.xbm)");
420 imageTypes.append ("XBM");
423 QStringList ImageIO::getFilters()
428 QString ImageIO::getType(QString filter)
430 for (int i=0;i<imageFilters.count()+1;i++)
431 if (imageFilters.at(i)==filter) return imageTypes.at(i);