2 #include <qmessagebox.h>
13 QString maskPath(QString p)
15 // Change " " to "\ " to enable blanks in filenames
16 p=p.replace(QChar('&'),"\\&");
17 return p.replace(QChar(' '),"\\ ");
20 QString convertToRel (const QString &src, const QString &dst)
28 // Special case, we just need the name of the file,
29 // not the complete path
31 d=d.right (d.length()-i-1);
34 // Find relative path from src to dst
36 // Remove the first "/"
37 if (s.section ("/",0,0).isEmpty())
39 s=s.right (s.length()-1);
40 d=d.right (d.length()-1);
43 // remove identical left parts
44 while (s.section("/",0,0) == d.section("/",0,0) )
47 s=s.right (s.length()-i-1);
48 d=d.right (d.length()-i-1);
51 // Now take care of paths where we have to go back first
52 int srcsep=s.count("/");
53 int dstsep=d.count("/");
54 if (srcsep <= dstsep )
56 // find path to go up first and then back to dst
68 QString makeUniqueDir (bool &ok,QString s)
70 // Create unique directory e.g. s="/tmp/vym-XXXXXX"
72 // Convert QString to string first
76 p=(char*) malloc (bytes+1);
79 p[i]=s.at(i).latin1();
81 QString r=mkdtemp (p);
82 if (r.isEmpty()) ok=false;
87 void removeDir(QDir d)
89 if (d.path().left(4)!="/tmp")
91 // This _should_ not be necessary, but proved to be useful ;-)
92 qWarning ("file.cpp::removeDir should remove "+d.path()+" - aborted.");
96 // Traverse directories
97 d.setFilter( QDir::Dirs| QDir::Hidden | QDir::NoSymLinks );
98 QFileInfoList list = d.entryInfoList();
101 for (int i = 0; i < list.size(); ++i)
104 if (fi.fileName() != "." && fi.fileName() != ".." )
106 if ( !d.cd(fi.fileName()) )
107 qWarning ("removeDir() cannot find the directory "+fi.fileName());
110 // Recursively remove subdirs
118 d.setFilter( QDir::Files| QDir::Hidden | QDir::NoSymLinks );
119 list = d.entryInfoList();
121 for (int i = 0; i < list.size(); ++i)
124 QFile (fi.filePath()).remove();
127 if (!d.rmdir(d.path()))
128 qWarning ("removeDir("+d.path()+") failed!");
131 void makeSubDirs (const QString &s)
139 ErrorCode zipDir (const QDir &zipDir, const QString &zipName)
141 ErrorCode err=success;
143 // zip the temporary directory
144 Process *zipProc=new Process ();
145 zipProc->clearArguments();
146 zipProc->setWorkingDirectory (QDir(zipDir));
147 zipProc->addArgument ("zip");
148 zipProc->addArgument ("-r");
149 zipProc->addArgument (zipName);
150 zipProc->addArgument (".");
152 if (!zipProc->start() )
154 // zip could not be started
155 QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
156 QObject::tr("Couldn't start zip to compress data."));
160 // zip could be started
161 zipProc->waitFinished();
162 if (!zipProc->normalExit() )
164 QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
165 QObject::tr("zip didn't exit normally")+
166 "\n" + zipProc->getErrout());
170 if (zipProc->exitStatus()>0)
172 QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
173 QString("zip exit code: %1").arg(zipProc->exitStatus() )+
174 "\n" + zipProc->getErrout() );
178 } // zip could be started
182 ErrorCode unzipDir (const QDir &zipDir, const QString &zipName)
184 ErrorCode err=success;
187 Process *zipProc=new Process ();
188 zipProc->clearArguments();
189 zipProc->setWorkingDirectory (zipDir);
190 zipProc->addArgument ("unzip");
191 zipProc->addArgument ("-o"); // overwrite existing files!
192 zipProc->addArgument (zipName );
193 zipProc->addArgument ("-d");
194 zipProc->addArgument (zipDir.path());
196 if (!zipProc->start() )
198 QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
199 QObject::tr("Couldn't start unzip to decompress data."));
204 zipProc->waitFinished();
205 if (!zipProc->normalExit() )
207 QMessageBox::critical( 0,QObject::tr( "Critical Error" ),
208 QObject::tr("unzip didn't exit normally") +
209 zipProc->getErrout() );
213 if (zipProc->exitStatus()>0)
215 if (zipProc->exitStatus()==9)
216 // no zipped file, but maybe .xml or old version? Try again.
220 QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
221 QString("unzip exit code: %1").arg(zipProc->exitStatus() ) +
222 zipProc->getErrout() );
231 bool loadStringFromDisk (const QString &fname, QString &s)
235 if ( !file.open( QIODevice::ReadOnly ) ) return false;
237 QTextStream ts( &file );
238 ts.setEncoding (QTextStream::UnicodeUTF8);
239 while ( !ts.atEnd() )
240 s+=ts.readLine()+"\n";
245 bool saveStringToDisk (const QString &fname, const QString &s)
249 file.setName ( fname);
250 if ( !file.open( QIODevice::WriteOnly ) )
256 // Write it finally, and write in UTF8, no matter what
257 QTextStream ts( &file );
258 ts.setEncoding (QTextStream::UnicodeUTF8);
265 ImagePreview::ImagePreview (QWidget *par=0): QLabel (par)
267 fdia=(Q3FileDialog*)par;
270 void ImagePreview::previewUrl( const Q3Url &u )
272 QString path = u.path();
276 // Strange: If we have fd->setMode (QFileDialog::ExistingFiles)
277 // in the filedialog, then there are 3 calls to previewURL
278 // for each selection. And only the first is the actual selected file
279 // while the following 2 point to the directory above the current one.
280 // So here's my workaround:
282 if (fdia && fdia->selectedFiles().count()==0)
283 setText( QObject::tr("This is not an image.") );
284 if (fdia &&fdia->selectedFiles().count()>1)
285 setText( QObject::tr("Sorry, no preview for\nmultiple selected files.") );
292 if (pix.width()>max_w)
294 r=max_w / pix.width();
295 pix.resize(qRound(pix.width()*r), qRound(pix.height()*r));
296 // FIXME not a resize, but a shrink/enlarge is needed here...
298 if (pix.height()>max_h)
300 r=max_h / pix.height();
301 pix.resize(qRound(pix.width()*r), qRound(pix.height()*r));
302 // FIXME not a resize, but a shrink/enlarge is needed here...
310 // Create list with supported image types
311 // foreach (QByteArray format, QImageWriter::supportedImageFormats())
312 // imageTypes.append( tr("%1...").arg(QString(format).toUpper()));
313 imageFilters.append ("Images (*.png *.jpg *.jpeg *.bmp *.bmp *.ppm *.xpm *.xbm)");
314 imageTypes.append ("PNG");
315 imageFilters.append ("Portable Network Graphics (*.png)");
316 imageTypes.append ("PNG");
317 imageFilters.append ("Joint Photographic Experts Group (*.jpg)");
318 imageTypes.append ("JPG");
319 imageFilters.append ("Joint Photographic Experts Group (*.jpeg)");
320 imageTypes.append ("JPG");
321 imageFilters.append ("Windows Bitmap (*.bmp)");
322 imageTypes.append ("BMP");
323 imageFilters.append ("Portable Pixmap (*.ppm)");
324 imageTypes.append ("PPM");
325 imageFilters.append ("X11 Bitmap (*.xpm)");
326 imageTypes.append ("XPM");
327 imageFilters.append ("X11 Bitmap (*.xbm)");
328 imageTypes.append ("XBM");
331 QStringList ImageIO::getFilters()
336 QString ImageIO::getType(QString filter)
338 for (int i=0;i<imageFilters.count()+1;i++)
339 if (imageFilters.at(i)==filter) return imageTypes.at(i);