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 (QString s)
70 // Create unique directory e.g. s="/tmp/vym-XXXXXX"
72 // Convert QString to string first
75 p=(char*) malloc (bytes+1);
78 p[i]=s.at(i).latin1();
80 QString r=mkdtemp (p);
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
142 Process *zipProc=new Process ();
143 zipProc->clearArguments();
144 zipProc->setWorkingDirectory (QDir(zipDir));
145 zipProc->addArgument ("zip");
146 zipProc->addArgument ("-r");
147 zipProc->addArgument (zipName);
148 zipProc->addArgument (".");
150 if (!zipProc->start() )
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->waitFinished();
160 if (!zipProc->normalExit() )
162 QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
163 QObject::tr("zip didn't exit normally")+
164 "\n" + zipProc->getErrout());
168 if (zipProc->exitStatus()>0)
170 QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
171 QString("zip exit code: %1").arg(zipProc->exitStatus() )+
172 "\n" + zipProc->getErrout() );
176 } // zip could be started
180 ErrorCode unzipDir (const QDir &zipDir, const QString &zipName)
182 ErrorCode err=success;
185 Process *zipProc=new Process ();
186 zipProc->clearArguments();
187 zipProc->setWorkingDirectory (zipDir);
188 zipProc->addArgument ("unzip");
189 zipProc->addArgument ("-o"); // overwrite existing files!
190 zipProc->addArgument (zipName );
191 zipProc->addArgument ("-d");
192 zipProc->addArgument (zipDir.path());
194 if (!zipProc->start() )
196 QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
197 QObject::tr("Couldn't start unzip to decompress data."));
202 zipProc->waitFinished();
203 if (!zipProc->normalExit() )
205 QMessageBox::critical( 0,QObject::tr( "Critical Error" ),
206 QObject::tr("unzip didn't exit normally") +
207 zipProc->getErrout() );
211 if (zipProc->exitStatus()>0)
213 if (zipProc->exitStatus()==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->exitStatus() ) +
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 // FIXME 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 // FIXME not a resize, but a shrink/enlarge is needed here...