insilmaril@831: #include insilmaril@606: #include insilmaril@408: #include insilmaril@366: #include insilmaril@366: #include insilmaril@366: #include insilmaril@195: insilmaril@195: #include "file.h" insilmaril@195: #include "process.h" insilmaril@195: insilmaril@606: #if defined(Q_OS_WIN32) insilmaril@624: #include "mkdtemp.h" insilmaril@606: #include insilmaril@606: #endif 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@521: #include insilmaril@521: extern QString vymName; insilmaril@521: extern QDir lastFileDir; insilmaril@521: insilmaril@521: QString browseDirectory (QWidget *parent,const QString &caption) insilmaril@521: { insilmaril@521: QFileDialog fd(parent,caption); insilmaril@521: fd.setMode (QFileDialog::DirectoryOnly); insilmaril@521: fd.setCaption(vymName+ " - "+caption); insilmaril@521: fd.setDir (lastFileDir); insilmaril@521: fd.show(); insilmaril@521: insilmaril@521: if ( fd.exec() == QDialog::Accepted ) insilmaril@521: return fd.selectedFile(); insilmaril@521: else insilmaril@521: return ""; insilmaril@521: } insilmaril@521: insilmaril@521: insilmaril@521: insilmaril@521: bool reallyWriteDirectory(const QString &dir) insilmaril@521: { insilmaril@521: QStringList eList = QDir(dir).entryList(); insilmaril@521: if (eList.first() ==".") eList.pop_front(); // remove "." insilmaril@521: if (eList.first() =="..") eList.pop_front(); // remove "." insilmaril@521: if (!eList.isEmpty()) insilmaril@521: { insilmaril@521: QMessageBox mb( vymName, insilmaril@521: QObject::tr("The directory %1 is not empty.\nDo you risk to overwrite its contents?","write directory").arg(dir), insilmaril@521: QMessageBox::Warning, insilmaril@521: QMessageBox::Yes , insilmaril@521: QMessageBox::Cancel | QMessageBox::Default, insilmaril@606: QMessageBox::NoButton ); insilmaril@521: insilmaril@521: mb.setButtonText( QMessageBox::Yes, QObject::tr("Overwrite") ); insilmaril@521: mb.setButtonText( QMessageBox::No, QObject::tr("Cancel")); insilmaril@521: switch( mb.exec() ) insilmaril@521: { insilmaril@521: case QMessageBox::Yes: insilmaril@521: // save insilmaril@521: return true; insilmaril@521: case QMessageBox::Cancel: insilmaril@521: // do nothing insilmaril@521: return false; insilmaril@521: } insilmaril@521: } insilmaril@521: return true; insilmaril@521: } insilmaril@521: insilmaril@606: QString makeTmpDir (bool &ok, QString prefix) insilmaril@606: { insilmaril@606: bool b; insilmaril@606: QString path=makeUniqueDir (b,QDir::tempPath()+"/"+prefix+"-XXXXXX"); insilmaril@606: ok=b; insilmaril@606: return path; insilmaril@606: } insilmaril@606: insilmaril@606: bool isInTmpDir(QString fn) insilmaril@606: { insilmaril@606: QString temp=QDir::tempPath(); insilmaril@606: int l=temp.length(); insilmaril@606: return fn.left(l)==temp; insilmaril@606: } insilmaril@606: insilmaril@366: QString makeUniqueDir (bool &ok,QString s) insilmaril@195: { insilmaril@606: // Create unique directory e.g. for s="/tmp/vym-XXXXXX" insilmaril@195: insilmaril@606: // Convert Separators insilmaril@606: s=QDir::convertSeparators(s); insilmaril@606: insilmaril@606: // Convert QString to string 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;isetWorkingDirectory (src.path()); insilmaril@502: args <<"-r"; insilmaril@502: args <start ("cp",args); insilmaril@502: if (!cpProc->waitForStarted() ) insilmaril@502: { insilmaril@502: // zip could not be started insilmaril@502: QMessageBox::critical( 0, QObject::tr( "Critical Error" ), insilmaril@502: QObject::tr("Couldn't start zip to compress data.")); insilmaril@502: err=aborted; insilmaril@502: } else insilmaril@502: { insilmaril@502: // zip could be started insilmaril@502: cpProc->waitForFinished(); insilmaril@502: if (cpProc->exitStatus()!=QProcess::NormalExit ) insilmaril@502: { insilmaril@502: QMessageBox::critical( 0, QObject::tr( "Critical Error" ), insilmaril@502: QObject::tr("cp didn't exit normally")+ insilmaril@502: "\n" + cpProc->getErrout()); insilmaril@502: err=aborted; insilmaril@502: } else insilmaril@502: { insilmaril@502: if (cpProc->exitCode()>0) insilmaril@502: { insilmaril@502: QMessageBox::critical( 0, QObject::tr( "Critical Error" ), insilmaril@502: QString("cp exit code: %1").arg(cpProc->exitCode() )+ insilmaril@502: "\n" + cpProc->getErrout() ); insilmaril@502: err=aborted; insilmaril@502: } insilmaril@502: } insilmaril@502: } // cp could be started insilmaril@502: */ insilmaril@502: } insilmaril@502: insilmaril@195: void makeSubDirs (const QString &s) insilmaril@195: { insilmaril@195: QDir d(s); insilmaril@195: d.mkdir(s); insilmaril@195: d.mkdir ("images"); insilmaril@195: d.mkdir ("flags"); insilmaril@195: } insilmaril@195: insilmaril@205: ErrorCode zipDir (const QDir &zipDir, const QString &zipName) insilmaril@195: { insilmaril@205: ErrorCode err=success; insilmaril@195: insilmaril@195: // zip the temporary directory insilmaril@408: QStringList args; insilmaril@195: Process *zipProc=new Process (); insilmaril@408: zipProc->setWorkingDirectory (zipDir.path()); insilmaril@408: args <<"-r"; insilmaril@408: args <start ("zip",args); insilmaril@408: if (!zipProc->waitForStarted() ) 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@408: zipProc->waitForFinished(); insilmaril@408: if (zipProc->exitStatus()!=QProcess::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@408: if (zipProc->exitCode()>0) insilmaril@195: { insilmaril@195: QMessageBox::critical( 0, QObject::tr( "Critical Error" ), insilmaril@408: QString("zip exit code: %1").arg(zipProc->exitCode() )+ 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@606: #if !defined(Q_OS_WIN32) insilmaril@408: QStringList args; insilmaril@195: Process *zipProc=new Process (); insilmaril@408: zipProc->setWorkingDirectory (zipDir.path()); insilmaril@408: args << "-o"; // overwrite existing files! insilmaril@408: args << zipName ; insilmaril@408: args << "-d"; insilmaril@408: args << zipDir.path(); insilmaril@195: insilmaril@408: zipProc->start ("unzip",args); insilmaril@408: if (!zipProc->waitForStarted() ) 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@408: zipProc->waitForFinished(); insilmaril@408: if (zipProc->exitStatus()!=QProcess::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@408: if (zipProc->exitCode()>0) insilmaril@195: { insilmaril@428: if (zipProc->exitCode()==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@408: QString("unzip exit code: %1").arg(zipProc->exitCode() ) + insilmaril@195: zipProc->getErrout() ); insilmaril@195: err=aborted; insilmaril@195: } insilmaril@195: } insilmaril@195: } insilmaril@195: } insilmaril@606: #else insilmaril@606: // Do this process creation using Win32 API. insilmaril@606: //! Create process. insilmaril@606: PROCESS_INFORMATION piProcInfo; insilmaril@606: STARTUPINFO siStartInfo; insilmaril@606: insilmaril@606: // Initialize members of the PROCESS_INFORMATION structure. insilmaril@606: ::ZeroMemory( &piProcInfo, sizeof(PROCESS_INFORMATION) ); insilmaril@606: insilmaril@606: // Set up members of the STARTUPINFO structure. insilmaril@606: ::ZeroMemory( &siStartInfo, sizeof(STARTUPINFO) ); insilmaril@606: siStartInfo.cb = sizeof(STARTUPINFO); insilmaril@606: insilmaril@606: // Create command line. insilmaril@606: QString argv("unzip -o "); insilmaril@606: argv.append(QDir::convertSeparators(zipName)); insilmaril@606: argv.append(" -d "); insilmaril@606: argv.append(QDir::convertSeparators(zipDir.path())); insilmaril@606: insilmaril@606: // Create the child process. insilmaril@721: if( !::CreateProcess(NULL, insilmaril@721: (LPWSTR)argv.unicode(), // command line insilmaril@721: NULL, // process security attributes insilmaril@721: NULL, // primary thread security attributes insilmaril@721: TRUE, // handles are inherited insilmaril@721: 0, // creation flags insilmaril@721: NULL, // use parent's environment insilmaril@721: NULL, // use parent's current directory insilmaril@721: &siStartInfo, // STARTUPINFO pointer insilmaril@721: &piProcInfo) ) // receives PROCESS_INFORMATION insilmaril@606: { insilmaril@606: err = aborted; insilmaril@606: } insilmaril@606: else insilmaril@606: { insilmaril@606: // Wait for it to finish. insilmaril@721: ::WaitForSingleObject( piProcInfo.hProcess, 10000 ); insilmaril@606: } insilmaril@606: #endif insilmaril@721: 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@412: // TODO 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@412: // TODO 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