Exemplo n.º 1
0
bool GalleryUtil::MoveDirectory(const QFileInfo src, QFileInfo &dst)
{
    QDir srcDir(src.absoluteFilePath());

    dst = MakeUniqueDirectory(dst);
    if (!dst.exists())
    {
        srcDir.mkdir(dst.absoluteFilePath());
        dst.refresh();
    }

    if (!dst.exists() || !dst.isDir())
        return false;

    bool ok = true;
    QDir dstDir(dst.absoluteFilePath());
    srcDir.setFilter(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot);
    QFileInfoList list = srcDir.entryInfoList();
    QFileInfoList::const_iterator it = list.begin();
    for (; it != list.end(); ++it)
    {
        const QString fn = it->fileName();
        QFileInfo dfi(dstDir, fn);
        ok &= Move(*it, dfi);
    }

    return ok && FileDelete(src);
}
Exemplo n.º 2
0
bool GalleryUtil::CopyDirectory(const QFileInfo src, QFileInfo &dst)
{
    QDir srcDir(src.absoluteFilePath());

    dst = MakeUniqueDirectory(dst);
    if (!dst.exists())
    {
        srcDir.mkdir(dst.absoluteFilePath());
        dst.refresh();
    }

    if (!dst.exists() || !dst.isDir())
        return false;

    bool ok = true;
    QDir dstDir(dst.absoluteFilePath());
    QFileInfoList list = srcDir.entryInfoList();
    QFileInfoList::const_iterator it = list.begin();
    for (; it != list.end(); ++it)
    {
        const QString fn = it->fileName();
        if (fn != "." && fn != "..")
        {
            QFileInfo dfi(dstDir, fn);
            ok &= Copy(*it, dfi);
        }
    }

    return ok;
}
Exemplo n.º 3
0
 FileInfo RunManager_Util::dirFile(QFileInfo fi) 
 {
   fi.refresh();
   return FileInfo(toString(fi.fileName()),
       (fi.exists()&&fi.lastModified().isValid())?toDateTime(fi.lastModified()):openstudio::DateTime(),
       toString(fi.fileName()), /* The default key is the filename */
       toPath(fi.absoluteFilePath()),
       fi.exists());
 }
Exemplo n.º 4
0
bool DkImageContainer::saveImage(const QString& filePath, const QImage saveImg, int compression /* = -1 */) {

	QFileInfo saveFile = saveImageIntern(filePath, getLoader(), saveImg, compression);

	saveFile.refresh();
	qDebug() << "save file: " << saveFile.absoluteFilePath();

	return saveFile.exists() && saveFile.isFile();
}
Exemplo n.º 5
0
void GlobalTrackCache::relocateTracks(
        GlobalTrackCacheRelocator* pRelocator) {
    if (debugLogEnabled()) {
        kLogger.debug()
                << "Relocating tracks";
    }
    TracksByCanonicalLocation relocatedTracksByCanonicalLocation;
    for (auto&&
            i = m_tracksByCanonicalLocation.begin();
            i != m_tracksByCanonicalLocation.end();
            ++i) {
        const QString oldCanonicalLocation = i->first;
        Track* plainPtr = i->second->getPlainPtr();
        QFileInfo fileInfo = plainPtr->getFileInfo();
        // The file info has to be refreshed, otherwise it might return
        // a cached and outdated absolute and canonical location!
        fileInfo.refresh();
        TrackRef trackRef = TrackRef::fromFileInfo(
                fileInfo,
                plainPtr->getId());
        if (!trackRef.hasCanonicalLocation() && trackRef.hasId() && pRelocator) {
            fileInfo = pRelocator->relocateCachedTrack(
                        trackRef.getId(),
                        fileInfo);
            if (fileInfo != plainPtr->getFileInfo()) {
                plainPtr->relocate(fileInfo);
            }
            trackRef = TrackRef::fromFileInfo(
                    fileInfo,
                    trackRef.getId());
        }
        if (!trackRef.hasCanonicalLocation()) {
            kLogger.warning()
                    << "Failed to relocate track"
                    << oldCanonicalLocation
                    << trackRef;
            continue;
        }
        QString newCanonicalLocation = trackRef.getCanonicalLocation();
        if (oldCanonicalLocation == newCanonicalLocation) {
            // Copy the entry unmodified into the new map
            relocatedTracksByCanonicalLocation.insert(*i);
            continue;
        }
        if (debugLogEnabled()) {
            kLogger.debug()
                    << "Relocating track"
                    << "from" << oldCanonicalLocation
                    << "to" << newCanonicalLocation;
        }
        relocatedTracksByCanonicalLocation.insert(std::make_pair(
                std::move(newCanonicalLocation),
                i->second));
    }
    m_tracksByCanonicalLocation = std::move(relocatedTracksByCanonicalLocation);
}
Exemplo n.º 6
0
bool DkImageContainerT::loadImageThreaded(bool force) {

#ifdef WITH_QUAZIP
	//zip archives: get zip file fileInfo for checks
	if(isFromZip()) 
		setFilePath(getZipData()->getZipFilePath());
#endif
	
	// check file for updates
	QFileInfo fileInfo = filePath();
	QDateTime modifiedBefore = fileInfo.lastModified();
	fileInfo.refresh();

	if (force || fileInfo.lastModified() != modifiedBefore || getLoader()->isDirty()) {
		qDebug() << "updating image...";
		getThumb()->setImage(QImage());
		clear();
	}

	// null file?
	if (fileInfo.fileName().isEmpty() || !fileInfo.exists()) {

		QString msg = tr("Sorry, the file: %1 does not exist... ").arg(fileName());
		emit showInfoSignal(msg);
		mLoadState = exists_not;
		return false;
	}
	else if (!fileInfo.permission(QFile::ReadUser)) {

		QString msg = tr("Sorry, you are not allowed to read: %1").arg(fileName());
		emit showInfoSignal(msg);
		mLoadState = exists_not;
		return false;
	}

#ifdef WITH_QUAZIP
	//zip archives: use the image file info from now on
	if(isFromZip()) 
		setFilePath(getZipData()->getImageFileName());
#endif
	
	mLoadState = loading;
	fetchFile();
	return true;
}
Exemplo n.º 7
0
void KSaveFileTest::test_ksavefile()
{
    QString targetFile;

    {
        //This will be the file we eventually write to. Yes, I know you
        //should never remove the temporaryfile and then expect the filename
        //to continue to be unique, but this is a test for crying out loud. :)
        KTemporaryFile file;
        file.setPrefix("ksavefiletest");
        QVERIFY( file.open() );
        targetFile = file.fileName();
    }

    {
        //Test basic functionality
        KSaveFile saveFile;
        saveFile.setFileName(targetFile);
        QVERIFY( saveFile.open() );
        QVERIFY( !QFile::exists(targetFile) );

        QTextStream ts ( &saveFile );
        ts << "This is test data one.\n";
        ts.flush();
        QCOMPARE( saveFile.error(), QFile::NoError );
        QVERIFY( !QFile::exists(targetFile) );

        QVERIFY( saveFile.finalize() );
        QVERIFY( QFile::exists(targetFile) );

        QFile::remove(targetFile);
        QVERIFY( !QFile::exists(targetFile) );
    }

    {
        //Make sure destructor does what it is supposed to do.
        {
            KSaveFile saveFile;
            saveFile.setFileName(targetFile);
            QVERIFY( saveFile.open() );
            QVERIFY( !QFile::exists(targetFile) );
        }

        QVERIFY( QFile::exists(targetFile) );
        QFile::remove(targetFile);
        QVERIFY( !QFile::exists(targetFile) );
    }

    {
        //Test some error conditions
        KSaveFile saveFile;
        QVERIFY( !saveFile.open() ); //no filename
        saveFile.setFileName(targetFile);
        QVERIFY( saveFile.open() );
        QVERIFY( !QFile::exists(targetFile) );
        QVERIFY( !saveFile.open() ); //already open

        QVERIFY( saveFile.finalize() );
        QVERIFY( QFile::exists(targetFile) );
        QVERIFY( !saveFile.finalize() ); //already finalized

        QFile::remove(targetFile);
        QVERIFY( !QFile::exists(targetFile) );
    }

    {
        //Do it again, aborting this time
        KSaveFile saveFile ( targetFile );
        QVERIFY( saveFile.open() );
        QVERIFY( !QFile::exists(targetFile) );

        QTextStream ts ( &saveFile );
        ts << "This is test data two.\n";
        ts.flush();
        QCOMPARE( saveFile.error(), QFile::NoError );
        QVERIFY( !QFile::exists(targetFile) );

        saveFile.abort();
        QVERIFY( !QFile::exists(targetFile) );
    }

    QFile file ( targetFile );
    QVERIFY( file.open(QIODevice::WriteOnly | QIODevice::Text) );
    QVERIFY( file.setPermissions( file.permissions() | QFile::ExeUser ) );
    file.close();

    {
        //Test how it works when the file already exists
        //Also check for special permissions
        KSaveFile saveFile ( targetFile );
        QVERIFY( saveFile.open() );

        QVERIFY( QFile::exists(targetFile) );
        QFileInfo fi ( targetFile );

#ifndef Q_WS_WIN
        // Windows: qt_ntfs_permission_lookup is not set by default in
        // qfsfileengine_win.cpp, could change in future Qt versions.
        QVERIFY( fi.permission( QFile::ExeUser ) );
#endif
        QVERIFY( fi.size() == 0 );

        QTextStream ts ( &saveFile );
        ts << "This is test data three.\n";
        ts.flush();

        fi.refresh();
        QVERIFY( fi.size() == 0 );
        QVERIFY( saveFile.finalize() );

        fi.refresh();
        QVERIFY( fi.size() != 0 );
#ifndef Q_WS_WIN
        QVERIFY( fi.permission( QFile::ExeUser ) );
#endif

        QFile::remove(targetFile);
    }

    {
        QFileInfo fi ( targetFile );
        targetFile = fi.fileName();
        QDir::setCurrent(fi.path());

        //one more time, this time with relative filenames
        KSaveFile saveFile ( targetFile );
        QVERIFY( saveFile.open() );
        QVERIFY( !QFile::exists(targetFile) );

        QTextStream ts ( &saveFile );
        ts << "This is test data four.\n";
        ts.flush();
        QCOMPARE( saveFile.error(), QFile::NoError );
        QVERIFY( !QFile::exists(targetFile) );

        QVERIFY( saveFile.finalize() );
        QVERIFY( QFile::exists(targetFile) );
        QFile::remove(targetFile);
    }

}
Exemplo n.º 8
0
/*!
  \param path The QFileInfo of the path to open (it can be a file, a folder, a symlink,...).
  \param source The place where the path was found.
  \param sourceIsAContainerFile Indicates if "source" is a container file.
  \return Returns a ResultOfOpening value indicating the result of the operation.
*/
TNxSpooler::ResultOfOpening TNxSpooler::openPath(QFileInfo &path, const QString &source, bool /* sourceIsAContainerFile (temporary unused parameter) */) const
{
   QDEBUG_METHOD_NAME;

   // Object to store the arguments to some callings
   QStringList arguments;

   // Name of the application that can be used to open the file
   QString app;

   // Stores the result of opening the path. This will be the value to return
   TNxSpooler::ResultOfOpening opResult = TNxSpooler::OpeningError; // It has this default value

   // Folders and {paths that do not seem to exist (there are many cases)} are not managed in the next block
   if (!path.isDir() && path.exists())
   {
      // Get the index of the file extension
      int i = m_settings.value("exts").toStringList().indexOf(path.suffix());

      if (i == -1)
      {
         // This case can happen if the path to open was inside a container file
         syst.showError(tr("2208097 - NxSpooler is not configured to launch an application to open files "
                              "like \"%1\", which was found inside \"%2\".\n\n"
                              "The administrator of this computer should see if this is due to a mistake "
                              "of the program that created the file, an incorrect configuration of "
                              "NxSpooler, etc.")
                              .arg(QDir::toNativeSeparators(path.fileName()))
                              .arg(QDir::toNativeSeparators(source)));

         return TNxSpooler::OpeningError;
      }

      app = m_settings.value("apps").toStringList().value(i);
      // There's no problem if app.isEmpty()

#ifdef Q_WS_WIN
      arguments << "/C" << "start" << "/wait" << app;
#endif
   }

   // Note: this way it worked with paths like "smb://server/resource" in Linux
   arguments << QDir::toNativeSeparators(path.filePath());

   // Folders and {paths that do not seem to exist (there are many cases)} are not managed in the next block
   if (path.exists() && !path.isDir())
   {
      // For avoiding the problem of having a file still being formed
      // and trying to open it, we'll wait until it has a stable size

      path.setCaching(false); // To try to read the current information about the file
      qint64 file_size_in_instant_1, file_size_in_instant_2;

      do
      {
         // Get the size
         file_size_in_instant_1 = path.size();

         syst.wait(750);

         // Refresh the information that we have about the file
         path.refresh();

         file_size_in_instant_2 = path.size();
      } while (file_size_in_instant_1 != file_size_in_instant_2);

      // Try to open the file
#ifdef Q_WS_WIN
      if (syst.execute("cmd", arguments) == 0)
          opResult = TNxSpooler::OpeningOk;
      else
          opResult = TNxSpooler::OpeningError;
#else
      // If the user is using Linux and he has not specified the name of the program to use,
      // execute the default program
      if (app.isEmpty())
      {
         if (syst.execute(getDefaultProgramInLinux(), arguments) == 0)
            opResult = TNxSpooler::OpeningOk;
         else
            opResult = TNxSpooler::OpeningError;
      }
      else
      {
         if (syst.execute(app, arguments) == 0)
            opResult = TNxSpooler::OpeningOk;
         else
            opResult = TNxSpooler::OpeningError;
      }
#endif
   }
   else
   {
      // it's a folder, or something that doesn't seem to exist (there are many cases)

#ifdef Q_WS_WIN
      // Windows explorer has anti-standard behaviours: for example returning 1 if it could
      // open a file and also returning the same value in some cases that it couldn't
      if (syst.execute("explorer", arguments) == 1)
         opResult = TNxSpooler::OpeningOk;
      else
         opResult = TNxSpooler::OpeningError;
#else
      if (syst.execute("xdg-open", arguments) == 0)
         opResult = TNxSpooler::OpeningOk;
      else
         opResult = TNxSpooler::OpeningError;
#endif
   }

   return opResult;
}