1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/file.cpp Tue Jun 06 14:58:11 2006 +0000
1.3 @@ -0,0 +1,305 @@
1.4 +#include <qobject.h>
1.5 +#include <qmessagebox.h>
1.6 +//Added by qt3to4:
1.7 +#include <QPixmap>
1.8 +#include <QLabel>
1.9 +#include <QTextStream>
1.10 +#include <iostream>
1.11 +
1.12 +#include "file.h"
1.13 +#include "process.h"
1.14 +
1.15 +
1.16 +QString maskPath(QString p)
1.17 +{
1.18 + // Change " " to "\ " to enable blanks in filenames
1.19 + p=p.replace(QChar('&'),"\\&");
1.20 + return p.replace(QChar(' '),"\\ ");
1.21 +}
1.22 +
1.23 +QString convertToRel (const QString &src, const QString &dst)
1.24 +{
1.25 + QString s=src;
1.26 + QString d=dst;
1.27 + int i;
1.28 +
1.29 + if (s==d)
1.30 + {
1.31 + // Special case, we just need the name of the file,
1.32 + // not the complete path
1.33 + i=d.findRev ("/");
1.34 + d=d.right (d.length()-i-1);
1.35 + } else
1.36 + {
1.37 + // Find relative path from src to dst
1.38 +
1.39 + // Remove the first "/"
1.40 + if (s.section ("/",0,0).isEmpty())
1.41 + {
1.42 + s=s.right (s.length()-1);
1.43 + d=d.right (d.length()-1);
1.44 + }
1.45 +
1.46 + // remove identical left parts
1.47 + while (s.section("/",0,0) == d.section("/",0,0) )
1.48 + {
1.49 + i=s.find ("/");
1.50 + s=s.right (s.length()-i-1);
1.51 + d=d.right (d.length()-i-1);
1.52 + }
1.53 +
1.54 + // Now take care of paths where we have to go back first
1.55 + int srcsep=s.count("/");
1.56 + int dstsep=d.count("/");
1.57 + if (srcsep <= dstsep )
1.58 + {
1.59 + // find path to go up first and then back to dst
1.60 + i=1;
1.61 + while (i<=srcsep)
1.62 + {
1.63 + d="../"+d;
1.64 + i++;
1.65 + }
1.66 + }
1.67 + }
1.68 + return d;
1.69 +}
1.70 +
1.71 +QString makeUniqueDir (QString s)
1.72 +{
1.73 + // Create unique directory e.g. s="/tmp/vym-XXXXXX"
1.74 +
1.75 + // Convert QString to string first
1.76 + char *p;
1.77 + int bytes=s.length();
1.78 + p=(char*) malloc (bytes+1);
1.79 + int i;
1.80 + for (i=0;i<bytes;i++)
1.81 + p[i]=s.at(i).latin1();
1.82 + p[bytes]=0;
1.83 + QString r=mkdtemp (p);
1.84 + free (p);
1.85 + return r;
1.86 +}
1.87 +
1.88 +void removeDir(QDir d)
1.89 +{
1.90 + if (d.path().left(4)!="/tmp")
1.91 + {
1.92 + // This _should_ not be necessary, but proved to be useful ;-)
1.93 + qWarning ("file.cpp::removeDir should remove "+d.path()+" - aborted.");
1.94 + return;
1.95 + }
1.96 +
1.97 + // Traverse directories
1.98 + d.setFilter( QDir::Dirs| QDir::Hidden | QDir::NoSymLinks );
1.99 + QFileInfoList list = d.entryInfoList();
1.100 + QFileInfo fi;
1.101 +
1.102 + for (int i = 0; i < list.size(); ++i)
1.103 + {
1.104 + fi=list.at(i);
1.105 + if (fi.fileName() != "." && fi.fileName() != ".." )
1.106 + {
1.107 + if ( !d.cd(fi.fileName()) )
1.108 + qWarning ("removeDir() cannot find the directory "+fi.fileName());
1.109 + else
1.110 + {
1.111 + // Recursively remove subdirs
1.112 + removeDir (d);
1.113 + d.cdUp();
1.114 + }
1.115 + }
1.116 + }
1.117 +
1.118 + // Traverse files
1.119 + d.setFilter( QDir::Files| QDir::Hidden | QDir::NoSymLinks );
1.120 + list = d.entryInfoList();
1.121 +
1.122 + for (int i = 0; i < list.size(); ++i)
1.123 + {
1.124 + fi=list.at(i);
1.125 + QFile (fi.filePath()).remove();
1.126 + }
1.127 +
1.128 + if (!d.rmdir(d.path()))
1.129 + qWarning ("removeDir("+d.path()+") failed!");
1.130 +}
1.131 +
1.132 +void makeSubDirs (const QString &s)
1.133 +{
1.134 + QDir d(s);
1.135 + d.mkdir(s);
1.136 + d.mkdir ("images");
1.137 + d.mkdir ("flags");
1.138 +}
1.139 +
1.140 +ErrorCode zipDir (const QDir &zipDir, const QString &zipName)
1.141 +{
1.142 + ErrorCode err=success;
1.143 +
1.144 + // zip the temporary directory
1.145 + Process *zipProc=new Process ();
1.146 + zipProc->clearArguments();
1.147 + zipProc->setWorkingDirectory (QDir(zipDir));
1.148 + zipProc->addArgument ("zip");
1.149 + zipProc->addArgument ("-r");
1.150 + zipProc->addArgument (zipName);
1.151 + zipProc->addArgument (".");
1.152 +
1.153 + if (!zipProc->start() )
1.154 + {
1.155 + // zip could not be started
1.156 + QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
1.157 + QObject::tr("Couldn't start zip to compress data."));
1.158 + err=aborted;
1.159 + } else
1.160 + {
1.161 + // zip could be started
1.162 + zipProc->waitFinished();
1.163 + if (!zipProc->normalExit() )
1.164 + {
1.165 + QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
1.166 + QObject::tr("zip didn't exit normally")+
1.167 + "\n" + zipProc->getErrout());
1.168 + err=aborted;
1.169 + } else
1.170 + {
1.171 + if (zipProc->exitStatus()>0)
1.172 + {
1.173 + QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
1.174 + QString("zip exit code: %1").arg(zipProc->exitStatus() )+
1.175 + "\n" + zipProc->getErrout() );
1.176 + err=aborted;
1.177 + }
1.178 + }
1.179 + } // zip could be started
1.180 + return err;
1.181 +}
1.182 +
1.183 +ErrorCode unzipDir (const QDir &zipDir, const QString &zipName)
1.184 +{
1.185 + ErrorCode err=success;
1.186 +
1.187 + // Try to unzip file
1.188 + Process *zipProc=new Process ();
1.189 + zipProc->clearArguments();
1.190 + zipProc->setWorkingDirectory (zipDir);
1.191 + zipProc->addArgument ("unzip");
1.192 + zipProc->addArgument ("-o"); // overwrite existing files!
1.193 + zipProc->addArgument (zipName );
1.194 + zipProc->addArgument ("-d");
1.195 + zipProc->addArgument (zipDir.path());
1.196 +
1.197 + if (!zipProc->start() )
1.198 + {
1.199 + QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
1.200 + QObject::tr("Couldn't start unzip to decompress data."));
1.201 + err=aborted;
1.202 +
1.203 + } else
1.204 + {
1.205 + zipProc->waitFinished();
1.206 + if (!zipProc->normalExit() )
1.207 + {
1.208 + QMessageBox::critical( 0,QObject::tr( "Critical Error" ),
1.209 + QObject::tr("unzip didn't exit normally") +
1.210 + zipProc->getErrout() );
1.211 + err=aborted;
1.212 + } else
1.213 + {
1.214 + if (zipProc->exitStatus()>0)
1.215 + {
1.216 + if (zipProc->exitStatus()==9)
1.217 + // no zipped file, but maybe .xml or old version? Try again.
1.218 + err=nozip;
1.219 + else
1.220 + {
1.221 + QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
1.222 + QString("unzip exit code: %1").arg(zipProc->exitStatus() ) +
1.223 + zipProc->getErrout() );
1.224 + err=aborted;
1.225 + }
1.226 + }
1.227 + }
1.228 + }
1.229 + return err;
1.230 +}
1.231 +
1.232 +bool loadStringFromDisk (const QString &fname, QString &s)
1.233 +{
1.234 + s="";
1.235 + QFile file ( fname);
1.236 + if ( !file.open( QIODevice::ReadOnly ) ) return false;
1.237 +
1.238 + QTextStream ts( &file );
1.239 + ts.setEncoding (QTextStream::UnicodeUTF8);
1.240 + while ( !ts.atEnd() )
1.241 + s+=ts.readLine()+"\n";
1.242 + file.close();
1.243 + return true;
1.244 +}
1.245 +
1.246 +bool saveStringToDisk (const QString &fname, const QString &s)
1.247 +{
1.248 + QFile file( fname);
1.249 +
1.250 + file.setName ( fname);
1.251 + if ( !file.open( QIODevice::WriteOnly ) )
1.252 + {
1.253 + file.close();
1.254 + return false;
1.255 + }
1.256 +
1.257 + // Write it finally, and write in UTF8, no matter what
1.258 + QTextStream ts( &file );
1.259 + ts.setEncoding (QTextStream::UnicodeUTF8);
1.260 + ts << s;
1.261 + file.close();
1.262 + return true;
1.263 +}
1.264 +
1.265 +
1.266 +ImagePreview::ImagePreview (QWidget *par=0): QLabel (par)
1.267 +{
1.268 + fdia=(Q3FileDialog*)par;
1.269 +}
1.270 +
1.271 +void ImagePreview::previewUrl( const Q3Url &u )
1.272 +{
1.273 + QString path = u.path();
1.274 + QPixmap pix( path );
1.275 + if ( pix.isNull() )
1.276 + {
1.277 + // Strange: If we have fd->setMode (QFileDialog::ExistingFiles)
1.278 + // in the filedialog, then there are 3 calls to previewURL
1.279 + // for each selection. And only the first is the actual selected file
1.280 + // while the following 2 point to the directory above the current one.
1.281 + // So here's my workaround:
1.282 +
1.283 + if (fdia && fdia->selectedFiles().count()==0)
1.284 + setText( QObject::tr("This is not an image.") );
1.285 + if (fdia &&fdia->selectedFiles().count()>1)
1.286 + setText( QObject::tr("Sorry, no preview for\nmultiple selected files.") );
1.287 + }
1.288 + else
1.289 + {
1.290 + float max_w=300;
1.291 + float max_h=300;
1.292 + float r;
1.293 + if (pix.width()>max_w)
1.294 + {
1.295 + r=max_w / pix.width();
1.296 + pix.resize(qRound(pix.width()*r), qRound(pix.height()*r));
1.297 + // FIXME not a resize, but a shrink/enlarge is needed here...
1.298 + }
1.299 + if (pix.height()>max_h)
1.300 + {
1.301 + r=max_h / pix.height();
1.302 + pix.resize(qRound(pix.width()*r), qRound(pix.height()*r));
1.303 + // FIXME not a resize, but a shrink/enlarge is needed here...
1.304 + }
1.305 + setPixmap( pix );
1.306 + }
1.307 +}
1.308 +