insilmaril@195: #include insilmaril@195: #include insilmaril@366: //Added by qt3to4: insilmaril@366: #include insilmaril@366: #include insilmaril@366: #include insilmaril@195: #include insilmaril@195: insilmaril@195: #include "file.h" insilmaril@195: #include "process.h" insilmaril@195: insilmaril@195: insilmaril@195: QString maskPath(QString p) insilmaril@195: { insilmaril@195: // Change " " to "\ " to enable blanks in filenames insilmaril@195: p=p.replace(QChar('&'),"\\&"); insilmaril@195: return p.replace(QChar(' '),"\\ "); insilmaril@195: } insilmaril@195: insilmaril@195: QString convertToRel (const QString &src, const QString &dst) insilmaril@195: { insilmaril@195: QString s=src; insilmaril@195: QString d=dst; insilmaril@195: int i; insilmaril@195: insilmaril@195: if (s==d) insilmaril@195: { insilmaril@195: // Special case, we just need the name of the file, insilmaril@195: // not the complete path insilmaril@195: i=d.findRev ("/"); insilmaril@195: d=d.right (d.length()-i-1); insilmaril@195: } else insilmaril@195: { insilmaril@195: // Find relative path from src to dst insilmaril@195: insilmaril@195: // Remove the first "/" insilmaril@195: if (s.section ("/",0,0).isEmpty()) insilmaril@195: { insilmaril@195: s=s.right (s.length()-1); insilmaril@195: d=d.right (d.length()-1); insilmaril@195: } insilmaril@195: insilmaril@195: // remove identical left parts insilmaril@195: while (s.section("/",0,0) == d.section("/",0,0) ) insilmaril@195: { insilmaril@195: i=s.find ("/"); insilmaril@195: s=s.right (s.length()-i-1); insilmaril@195: d=d.right (d.length()-i-1); insilmaril@195: } insilmaril@195: insilmaril@255: // Now take care of paths where we have to go back first insilmaril@366: int srcsep=s.count("/"); insilmaril@366: int dstsep=d.count("/"); insilmaril@255: if (srcsep <= dstsep ) insilmaril@195: { insilmaril@195: // find path to go up first and then back to dst insilmaril@195: i=1; insilmaril@195: while (i<=srcsep) insilmaril@195: { insilmaril@195: d="../"+d; insilmaril@195: i++; insilmaril@195: } insilmaril@195: } insilmaril@195: } insilmaril@195: return d; insilmaril@195: } insilmaril@195: insilmaril@366: QString makeUniqueDir (bool &ok,QString s) insilmaril@195: { insilmaril@195: // Create unique directory e.g. s="/tmp/vym-XXXXXX" insilmaril@195: insilmaril@195: // Convert QString to string first insilmaril@366: ok=true; insilmaril@195: char *p; insilmaril@195: int bytes=s.length(); insilmaril@195: p=(char*) malloc (bytes+1); insilmaril@195: int i; insilmaril@195: for (i=0;iclearArguments(); insilmaril@195: zipProc->setWorkingDirectory (QDir(zipDir)); insilmaril@195: zipProc->addArgument ("zip"); insilmaril@195: zipProc->addArgument ("-r"); insilmaril@195: zipProc->addArgument (zipName); insilmaril@195: zipProc->addArgument ("."); insilmaril@195: insilmaril@195: if (!zipProc->start() ) insilmaril@195: { insilmaril@195: // zip could not be started insilmaril@195: QMessageBox::critical( 0, QObject::tr( "Critical Error" ), insilmaril@195: QObject::tr("Couldn't start zip to compress data.")); insilmaril@195: err=aborted; insilmaril@195: } else insilmaril@195: { insilmaril@195: // zip could be started insilmaril@195: zipProc->waitFinished(); insilmaril@195: if (!zipProc->normalExit() ) insilmaril@195: { insilmaril@195: QMessageBox::critical( 0, QObject::tr( "Critical Error" ), insilmaril@195: QObject::tr("zip didn't exit normally")+ insilmaril@195: "\n" + zipProc->getErrout()); insilmaril@195: err=aborted; insilmaril@195: } else insilmaril@195: { insilmaril@195: if (zipProc->exitStatus()>0) insilmaril@195: { insilmaril@195: QMessageBox::critical( 0, QObject::tr( "Critical Error" ), insilmaril@195: QString("zip exit code: %1").arg(zipProc->exitStatus() )+ insilmaril@195: "\n" + zipProc->getErrout() ); insilmaril@195: err=aborted; insilmaril@195: } insilmaril@195: } insilmaril@195: } // zip could be started insilmaril@195: return err; insilmaril@195: } insilmaril@195: insilmaril@205: ErrorCode unzipDir (const QDir &zipDir, const QString &zipName) insilmaril@195: { insilmaril@205: ErrorCode err=success; insilmaril@195: insilmaril@195: // Try to unzip file insilmaril@195: Process *zipProc=new Process (); insilmaril@195: zipProc->clearArguments(); insilmaril@195: zipProc->setWorkingDirectory (zipDir); insilmaril@195: zipProc->addArgument ("unzip"); insilmaril@215: zipProc->addArgument ("-o"); // overwrite existing files! insilmaril@195: zipProc->addArgument (zipName ); insilmaril@195: zipProc->addArgument ("-d"); insilmaril@195: zipProc->addArgument (zipDir.path()); insilmaril@195: insilmaril@195: if (!zipProc->start() ) insilmaril@195: { insilmaril@195: QMessageBox::critical( 0, QObject::tr( "Critical Error" ), insilmaril@195: QObject::tr("Couldn't start unzip to decompress data.")); insilmaril@195: err=aborted; insilmaril@195: insilmaril@195: } else insilmaril@195: { insilmaril@195: zipProc->waitFinished(); insilmaril@195: if (!zipProc->normalExit() ) insilmaril@195: { insilmaril@195: QMessageBox::critical( 0,QObject::tr( "Critical Error" ), insilmaril@195: QObject::tr("unzip didn't exit normally") + insilmaril@195: zipProc->getErrout() ); insilmaril@195: err=aborted; insilmaril@195: } else insilmaril@195: { insilmaril@195: if (zipProc->exitStatus()>0) insilmaril@195: { insilmaril@195: if (zipProc->exitStatus()==9) insilmaril@195: // no zipped file, but maybe .xml or old version? Try again. insilmaril@195: err=nozip; insilmaril@195: else insilmaril@195: { insilmaril@195: QMessageBox::critical( 0, QObject::tr( "Critical Error" ), insilmaril@195: QString("unzip exit code: %1").arg(zipProc->exitStatus() ) + insilmaril@195: zipProc->getErrout() ); insilmaril@195: err=aborted; insilmaril@195: } insilmaril@195: } insilmaril@195: } insilmaril@195: } insilmaril@195: return err; insilmaril@195: } insilmaril@195: insilmaril@195: bool loadStringFromDisk (const QString &fname, QString &s) insilmaril@195: { insilmaril@195: s=""; insilmaril@195: QFile file ( fname); insilmaril@366: if ( !file.open( QIODevice::ReadOnly ) ) return false; insilmaril@195: insilmaril@195: QTextStream ts( &file ); insilmaril@195: ts.setEncoding (QTextStream::UnicodeUTF8); insilmaril@195: while ( !ts.atEnd() ) insilmaril@195: s+=ts.readLine()+"\n"; insilmaril@195: file.close(); insilmaril@195: return true; insilmaril@195: } insilmaril@195: insilmaril@195: bool saveStringToDisk (const QString &fname, const QString &s) insilmaril@195: { insilmaril@195: QFile file( fname); insilmaril@195: insilmaril@195: file.setName ( fname); insilmaril@366: if ( !file.open( QIODevice::WriteOnly ) ) insilmaril@195: { insilmaril@195: file.close(); insilmaril@195: return false; insilmaril@195: } insilmaril@195: insilmaril@195: // Write it finally, and write in UTF8, no matter what insilmaril@195: QTextStream ts( &file ); insilmaril@195: ts.setEncoding (QTextStream::UnicodeUTF8); insilmaril@195: ts << s; insilmaril@195: file.close(); insilmaril@195: return true; insilmaril@195: } insilmaril@195: insilmaril@195: insilmaril@234: ImagePreview::ImagePreview (QWidget *par=0): QLabel (par) insilmaril@195: { insilmaril@366: fdia=(Q3FileDialog*)par; insilmaril@195: } insilmaril@195: insilmaril@366: void ImagePreview::previewUrl( const Q3Url &u ) insilmaril@195: { insilmaril@195: QString path = u.path(); insilmaril@195: QPixmap pix( path ); insilmaril@195: if ( pix.isNull() ) insilmaril@234: { insilmaril@234: // Strange: If we have fd->setMode (QFileDialog::ExistingFiles) insilmaril@234: // in the filedialog, then there are 3 calls to previewURL insilmaril@234: // for each selection. And only the first is the actual selected file insilmaril@234: // while the following 2 point to the directory above the current one. insilmaril@234: // So here's my workaround: insilmaril@234: insilmaril@234: if (fdia && fdia->selectedFiles().count()==0) insilmaril@234: setText( QObject::tr("This is not an image.") ); insilmaril@234: if (fdia &&fdia->selectedFiles().count()>1) insilmaril@234: setText( QObject::tr("Sorry, no preview for\nmultiple selected files.") ); insilmaril@234: } insilmaril@195: else insilmaril@195: { insilmaril@195: float max_w=300; insilmaril@195: float max_h=300; insilmaril@195: float r; insilmaril@195: if (pix.width()>max_w) insilmaril@195: { insilmaril@195: r=max_w / pix.width(); insilmaril@195: pix.resize(qRound(pix.width()*r), qRound(pix.height()*r)); insilmaril@195: // FIXME not a resize, but a shrink/enlarge is needed here... insilmaril@195: } insilmaril@195: if (pix.height()>max_h) insilmaril@195: { insilmaril@195: r=max_h / pix.height(); insilmaril@195: pix.resize(qRound(pix.width()*r), qRound(pix.height()*r)); insilmaril@195: // FIXME not a resize, but a shrink/enlarge is needed here... insilmaril@195: } insilmaril@195: setPixmap( pix ); insilmaril@195: } insilmaril@195: } insilmaril@195: insilmaril@366: ImageIO::ImageIO () insilmaril@366: { insilmaril@366: // Create list with supported image types insilmaril@366: // foreach (QByteArray format, QImageWriter::supportedImageFormats()) insilmaril@366: // imageTypes.append( tr("%1...").arg(QString(format).toUpper())); insilmaril@366: imageFilters.append ("Images (*.png *.jpg *.jpeg *.bmp *.bmp *.ppm *.xpm *.xbm)"); insilmaril@366: imageTypes.append ("PNG"); insilmaril@366: imageFilters.append ("Portable Network Graphics (*.png)"); insilmaril@366: imageTypes.append ("PNG"); insilmaril@366: imageFilters.append ("Joint Photographic Experts Group (*.jpg)"); insilmaril@366: imageTypes.append ("JPG"); insilmaril@366: imageFilters.append ("Joint Photographic Experts Group (*.jpeg)"); insilmaril@366: imageTypes.append ("JPG"); insilmaril@366: imageFilters.append ("Windows Bitmap (*.bmp)"); insilmaril@366: imageTypes.append ("BMP"); insilmaril@366: imageFilters.append ("Portable Pixmap (*.ppm)"); insilmaril@366: imageTypes.append ("PPM"); insilmaril@366: imageFilters.append ("X11 Bitmap (*.xpm)"); insilmaril@366: imageTypes.append ("XPM"); insilmaril@366: imageFilters.append ("X11 Bitmap (*.xbm)"); insilmaril@366: imageTypes.append ("XBM"); insilmaril@366: } insilmaril@366: insilmaril@366: QStringList ImageIO::getFilters() insilmaril@366: { insilmaril@366: return imageFilters; insilmaril@366: } insilmaril@366: insilmaril@366: QString ImageIO::getType(QString filter) insilmaril@366: { insilmaril@366: for (int i=0;i