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
174 if (err==aborted) qWarning("file.cpp: zip aborted");
178 errorCode unzipDir (const QDir &zipDir, const QString &zipName)
180 errorCode err=success;
183 Process *zipProc=new Process ();
184 zipProc->clearArguments();
185 zipProc->setWorkingDirectory (zipDir);
186 zipProc->addArgument ("unzip");
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() );
223 if (err==aborted) qWarning("file.cpp: unzip aborted");
227 bool loadStringFromDisk (const QString &fname, QString &s)
231 if ( !file.open( IO_ReadOnly ) ) return false;
233 QTextStream ts( &file );
234 ts.setEncoding (QTextStream::UnicodeUTF8);
235 while ( !ts.atEnd() )
236 s+=ts.readLine()+"\n";
241 bool saveStringToDisk (const QString &fname, const QString &s)
245 file.setName ( fname);
246 if ( !file.open( IO_WriteOnly ) )
252 // Write it finally, and write in UTF8, no matter what
253 QTextStream ts( &file );
254 ts.setEncoding (QTextStream::UnicodeUTF8);
261 ImagePreview::ImagePreview (QWidget *parent=0): QLabel (parent)
265 void ImagePreview::previewUrl( const QUrl &u )
267 QString path = u.path();
270 setText( QObject::tr("This is not an image.") );
276 if (pix.width()>max_w)
278 r=max_w / pix.width();
279 pix.resize(qRound(pix.width()*r), qRound(pix.height()*r));
280 // FIXME not a resize, but a shrink/enlarge is needed here...
282 if (pix.height()>max_h)
284 r=max_h / pix.height();
285 pix.resize(qRound(pix.width()*r), qRound(pix.height()*r));
286 // FIXME not a resize, but a shrink/enlarge is needed here...