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