2 #include <qmessagebox.h>
9 QString maskPath(QString p)
11 // Change " " to "\ " to enable blanks in filenames
12 p=p.replace(QChar('&'),"\\&");
13 return p.replace(QChar(' '),"\\ ");
16 QString convertToRel (const QString &src, const QString &dst)
24 // Special case, we just need the name of the file,
25 // not the complete path
27 d=d.right (d.length()-i-1);
30 // Find relative path from src to dst
32 // Remove the first "/"
33 if (s.section ("/",0,0).isEmpty())
35 s=s.right (s.length()-1);
36 d=d.right (d.length()-1);
39 // remove identical left parts
40 while (s.section("/",0,0) == d.section("/",0,0) )
43 s=s.right (s.length()-i-1);
44 d=d.right (d.length()-i-1);
47 int srcsep=s.contains("/");
48 int dstsep=d.contains("/");
49 if (srcsep >= dstsep )
51 // find path to go up first and then back to dst
63 QString makeUniqueDir (QString s)
65 // Create unique directory e.g. s="/tmp/vym-XXXXXX"
67 // Convert QString to string first
70 p=(char*) malloc (bytes+1);
73 p[i]=s.at(i).latin1();
75 QString r=mkdtemp (p);
80 void removeDir(QDir d)
82 if (d.path().left(4)!="/tmp")
85 qWarning ("file.cpp::removeDir should remove "+d.path()+" - aborted.");
89 // Traverse directories
90 d.setFilter( QDir::Dirs| QDir::Hidden | QDir::NoSymLinks );
91 const QFileInfoList *dirlist = d.entryInfoList();
92 QFileInfoListIterator itdir( *dirlist );
95 while ( (fi = itdir.current()) != 0 )
97 if (fi->fileName() != "." && fi->fileName() != ".." )
99 if ( !d.cd(fi->fileName()) )
100 qWarning ("removeDir() cannot find the directory "+fi->fileName());
103 // Recursively remove subdirs
111 d.setFilter( QDir::Files| QDir::Hidden | QDir::NoSymLinks );
112 const QFileInfoList *filelist = d.entryInfoList();
113 QFileInfoListIterator itfile( *filelist );
115 while ( (fi = itfile.current()) != 0 )
117 QFile (fi->filePath()).remove();
122 if (!d.rmdir(d.path()))
123 qWarning ("removeDir("+d.path()+") failed!");
126 void makeSubDirs (const QString &s)
134 ErrorCode zipDir (const QDir &zipDir, const QString &zipName)
136 ErrorCode err=success;
138 // zip the temporary directory
139 Process *zipProc=new Process ();
140 zipProc->clearArguments();
141 zipProc->setWorkingDirectory (QDir(zipDir));
142 zipProc->addArgument ("zip");
143 zipProc->addArgument ("-r");
144 zipProc->addArgument (zipName);
145 zipProc->addArgument (".");
147 if (!zipProc->start() )
149 // zip could not be started
150 QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
151 QObject::tr("Couldn't start zip to compress data."));
155 // zip could be started
156 zipProc->waitFinished();
157 if (!zipProc->normalExit() )
159 QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
160 QObject::tr("zip didn't exit normally")+
161 "\n" + zipProc->getErrout());
165 if (zipProc->exitStatus()>0)
167 QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
168 QString("zip exit code: %1").arg(zipProc->exitStatus() )+
169 "\n" + zipProc->getErrout() );
173 } // zip could be started
177 ErrorCode unzipDir (const QDir &zipDir, const QString &zipName)
179 ErrorCode err=success;
182 Process *zipProc=new Process ();
183 zipProc->clearArguments();
184 zipProc->setWorkingDirectory (zipDir);
185 zipProc->addArgument ("unzip");
186 zipProc->addArgument ("-o"); // overwrite existing files!
187 zipProc->addArgument (zipName );
188 zipProc->addArgument ("-d");
189 zipProc->addArgument (zipDir.path());
191 if (!zipProc->start() )
193 QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
194 QObject::tr("Couldn't start unzip to decompress data."));
199 zipProc->waitFinished();
200 if (!zipProc->normalExit() )
202 QMessageBox::critical( 0,QObject::tr( "Critical Error" ),
203 QObject::tr("unzip didn't exit normally") +
204 zipProc->getErrout() );
208 if (zipProc->exitStatus()>0)
210 if (zipProc->exitStatus()==9)
211 // no zipped file, but maybe .xml or old version? Try again.
215 QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
216 QString("unzip exit code: %1").arg(zipProc->exitStatus() ) +
217 zipProc->getErrout() );
226 bool loadStringFromDisk (const QString &fname, QString &s)
230 if ( !file.open( IO_ReadOnly ) ) return false;
232 QTextStream ts( &file );
233 ts.setEncoding (QTextStream::UnicodeUTF8);
234 while ( !ts.atEnd() )
235 s+=ts.readLine()+"\n";
240 bool saveStringToDisk (const QString &fname, const QString &s)
244 file.setName ( fname);
245 if ( !file.open( IO_WriteOnly ) )
251 // Write it finally, and write in UTF8, no matter what
252 QTextStream ts( &file );
253 ts.setEncoding (QTextStream::UnicodeUTF8);
260 ImagePreview::ImagePreview (QWidget *par=0): QLabel (par)
262 fdia=(QFileDialog*)par;
265 void ImagePreview::previewUrl( const QUrl &u )
267 QString path = u.path();
271 // Strange: If we have fd->setMode (QFileDialog::ExistingFiles)
272 // in the filedialog, then there are 3 calls to previewURL
273 // for each selection. And only the first is the actual selected file
274 // while the following 2 point to the directory above the current one.
275 // So here's my workaround:
277 if (fdia && fdia->selectedFiles().count()==0)
278 setText( QObject::tr("This is not an image.") );
279 if (fdia &&fdia->selectedFiles().count()>1)
280 setText( QObject::tr("Sorry, no preview for\nmultiple selected files.") );
287 if (pix.width()>max_w)
289 r=max_w / pix.width();
290 pix.resize(qRound(pix.width()*r), qRound(pix.height()*r));
291 // FIXME not a resize, but a shrink/enlarge is needed here...
293 if (pix.height()>max_h)
295 r=max_h / pix.height();
296 pix.resize(qRound(pix.width()*r), qRound(pix.height()*r));
297 // FIXME not a resize, but a shrink/enlarge is needed here...