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 (zipName );
187 zipProc->addArgument ("-d");
188 zipProc->addArgument (zipDir.path());
190 if (!zipProc->start() )
192 QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
193 QObject::tr("Couldn't start unzip to decompress data."));
198 zipProc->waitFinished();
199 if (!zipProc->normalExit() )
201 QMessageBox::critical( 0,QObject::tr( "Critical Error" ),
202 QObject::tr("unzip didn't exit normally") +
203 zipProc->getErrout() );
207 if (zipProc->exitStatus()>0)
209 if (zipProc->exitStatus()==9)
210 // no zipped file, but maybe .xml or old version? Try again.
214 QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
215 QString("unzip exit code: %1").arg(zipProc->exitStatus() ) +
216 zipProc->getErrout() );
222 if (err==aborted) qWarning("file.cpp: unzip aborted");
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 *parent=0): QLabel (parent)
264 void ImagePreview::previewUrl( const QUrl &u )
266 QString path = u.path();
269 setText( QObject::tr("This is not an image.") );
275 if (pix.width()>max_w)
277 r=max_w / pix.width();
278 pix.resize(qRound(pix.width()*r), qRound(pix.height()*r));
279 // FIXME not a resize, but a shrink/enlarge is needed here...
281 if (pix.height()>max_h)
283 r=max_h / pix.height();
284 pix.resize(qRound(pix.width()*r), qRound(pix.height()*r));
285 // FIXME not a resize, but a shrink/enlarge is needed here...