在Qt中将文件移动到废纸篓/回收站

时间:2013-07-31 07:42:06

标签: c++ qt cross-platform recycle-bin

是否有Qt函数将文件移动到回收站而不是真正删除它们,对于支持它的操作系统,还是需要使用特定于操作系统的代码?

7 个答案:

答案 0 :(得分:6)

Qt没有提供MoveToTrash。这是我的代码的一部分

for Windows

#ifdef Q_OS_WIN32

#include "windows.h"

void MoveToTrashImpl( QString file ){
    QFileInfo fileinfo( file );
    if( !fileinfo.exists() )
        throw OdtCore::Exception( "File doesnt exists, cant move to trash" );
    WCHAR from[ MAX_PATH ];
    memset( from, 0, sizeof( from ));
    int l = fileinfo.absoluteFilePath().toWCharArray( from );
    Q_ASSERT( 0 <= l && l < MAX_PATH );
    from[ l ] = '\0';
    SHFILEOPSTRUCT fileop;
    memset( &fileop, 0, sizeof( fileop ) );
    fileop.wFunc = FO_DELETE;
    fileop.pFrom = from;
    fileop.fFlags = FOF_ALLOWUNDO | FOF_NOCONFIRMATION | FOF_NOERRORUI | FOF_SILENT;
    int rv = SHFileOperation( &fileop );
    if( 0 != rv ){
        qDebug() << rv << QString::number( rv ).toInt( 0, 8 );
        throw OdtCore::Exception( "move to trash failed" );
    }
}
#endif

和Linux

#ifdef Q_OS_LINUX

bool TrashInitialized = false;
QString TrashPath;
QString TrashPathInfo;
QString TrashPathFiles;

void MoveToTrashImpl( QString file ){
    #ifdef QT_GUI_LIB
        if( !TrashInitialized ){
            QStringList paths;
            const char* xdg_data_home = getenv( "XDG_DATA_HOME" );
            if( xdg_data_home ){
                qDebug() << "XDG_DATA_HOME not yet tested";
                QString xdgTrash( xdg_data_home );
                paths.append( xdgTrash + "/Trash" );
            }
            QString home = QStandardPaths::writableLocation( QStandardPaths::HomeLocation );
            paths.append( home + "/.local/share/Trash" );
            paths.append( home + "/.trash" );
            foreach( QString path, paths ){
                if( TrashPath.isEmpty() ){
                    QDir dir( path );
                    if( dir.exists() ){
                        TrashPath = path;
                    }
                }
            }
            if( TrashPath.isEmpty() )
                throw Exception( "Cant detect trash folder" );
            TrashPathInfo = TrashPath + "/info";
            TrashPathFiles = TrashPath + "/files";
            if( !QDir( TrashPathInfo ).exists() || !QDir( TrashPathFiles ).exists() )
                throw Exception( "Trash doesnt looks like FreeDesktop.org Trash specification" );
            TrashInitialized = true;
        }
        QFileInfo original( file );
        if( !original.exists() )
            throw Exception( "File doesnt exists, cant move to trash" );
        QString info;
        info += "[Trash Info]\nPath=";
        info += original.absoluteFilePath();
        info += "\nDeletionDate=";
        info += QDateTime::currentDateTime().toString("yyyy-MM-ddThh:mm:ss.zzzZ");
        info += "\n";
        QString trashname = original.fileName();
        QString infopath = TrashPathInfo + "/" + trashname + ".trashinfo";
        QString filepath = TrashPathFiles + "/" + trashname;
        int nr = 1;
        while( QFileInfo( infopath ).exists() || QFileInfo( filepath ).exists() ){
            nr++;
            trashname = original.baseName() + "." + QString::number( nr );
            if( !original.completeSuffix().isEmpty() ){
                trashname += QString( "." ) + original.completeSuffix();
            }
            infopath = TrashPathInfo + "/" + trashname + ".trashinfo";
            filepath = TrashPathFiles + "/" + trashname;
        }
        QDir dir;
        if( !dir.rename( original.absoluteFilePath(), filepath ) ){
            throw Exception( "move to trash failed" );
        }
        File infofile;
        infofile.createUtf8( infopath, info );
    #else
        Q_UNUSED( file );
        throw Exception( "Trash in server-mode not supported" );
    #endif
}
#endif

答案 1 :(得分:4)

还没有API。

https://bugreports.qt.io/browse/QTBUG-181

问题已解决,修复版本为:未来版本

修改https://bugreports.qt.io/browse/QTBUG-47703已打开一个新问题。

答案 2 :(得分:3)

我认为没有跨平台的方式。简单移动文件到“垃圾”位置不会生效,因为用户可能会关闭这种可能性。

也许,这个网址会有所帮助:http://www.hardcoded.net/articles/send-files-to-trash-on-all-platforms.htm

答案 3 :(得分:3)

我相对确定没有Qt API为所有支持的平台包装它。不幸的是,这意味着您必须编写特定于平台的代码。

我不知道Linux发行版在何处/如何存储已删除的文件,我想它可能会因您使用的文件管理器而异。我相信将文件移动到~/.Trash文件夹是标准的方法,但我不确定这是否可靠。例如,对于存储在外部卷上的文件。

在Mac OS X上,事情变得容易一些,其中有一个支持的API来执行此操作:FSMoveObjectToTrashSync,由Core Services提供。至少,这就是我记得你应该这样做的方式。 The documentation声称此方法现已在OS X 10.8中弃用。我不知道推荐的替代方案是什么。

作为Windows程序员,我认为该平台更容易。 :-)基本解决方案是调用SHFileOperation函数:

#include <Windows.h>   // general Windows header file
#include <ShellAPI.h>  // for shell functions, like SHFileOperation
#include <string>      // (or use QString)

void RecycleFileOnWindows()
{
   std::wstring path = L"C:\\Users\\Administrator\\Documents\\deleteme.txt";
   path.append(1, L'\0');        // path string must be double nul-terminated

   SHFILEOPSTRUCT shfos = {};
   shfos.hwnd   = nullptr;       // handle to window that will own generated windows, if applicable
   shfos.wFunc  = FO_DELETE;
   shfos.pFrom  = path.c_str();
   shfos.pTo    = nullptr;       // not used for deletion operations
   shfos.fFlags = FOF_ALLOWUNDO; // use the recycle bin

   const int retVal = SHFileOperation(&shfos);
   if (retVal != 0)
   {
      // The operation failed...
      if (shfos.fAnyOperationsAborted)
      {
         // ...but that's because the user canceled.
         MessageBox(nullptr, L"Operation was canceled", nullptr, MB_OK | MB_ICONINFORMATION);
      }
      else
      {
         // ...for one of the other reasons given in the documentation.
         MessageBox(nullptr, L"Operation failed", nullptr, MB_OK | MB_ICONERROR);
      }
   }
}

还可以设置标志以自定义确认,错误报告和其他行为。链接的文档包含了构建此基本示例所需的所有详细信息。

在Windows Vista及更高版本中,SHFileOperation功能已被IFileOperation界面提供的方法取代。如果您只定位这些更高版本的Windows,则应该更喜欢使用此界面。否则,SHFileOperation将继续正常工作。

答案 4 :(得分:2)

Qt 5.15.0 Alpha 起,已经添加了此方法,这应该是您所要的。

bool QFile::moveToTrash()

可以找到相应的代码更改here

(此问题较旧,并且已经发布了https://bugreports.qt.io/browse/QTBUG-47703上的相应Bugreport,但是我目前尚无评论的声誉,并且发现这是有用的信息。)

答案 5 :(得分:1)

if(QSysInfo::kernelType()=="linux")
{
    QDateTime currentTime(QDateTime::currentDateTime());    // save System time

    QString trashFilePath=QDir::homePath()+"/.local/share/Trash/files/";    // trash file path contain delete files
    QString trashInfoPath=QDir::homePath()+"/.local/share/Trash/info/";     // trash info path contain delete files information

    // create file format for trash info file----- START
    QFile infoFile(trashInfoPath+FileName.completeBaseName()+"."+FileName.completeSuffix()+".trashinfo");     //filename+extension+.trashinfo //  create file information file in /.local/share/Trash/info/ folder

    infoFile.open(QIODevice::ReadWrite);

    QTextStream stream(&infoFile);         // for write data on open file

    stream<<"[Trash Info]"<<endl;
    stream<<"Path="+QString(QUrl::toPercentEncoding(FileName.absoluteFilePath(),"~_-./"))<<endl;     // convert path string in percentage decoding scheme string
    stream<<"DeletionDate="+currentTime.toString("yyyy-MM-dd")+"T"+currentTime.toString("hh:mm:ss")<<endl;      // get date and time format YYYY-MM-DDThh:mm:ss

    infoFile.close();

    // create info file format of trash file----- END

    QDir file;
    file.rename(FileName.absoluteFilePath(),trashFilePath+FileName.completeBaseName()+"."+FileName.completeSuffix());  // rename(file old path, file trash path)



}

答案 6 :(得分:1)

linux中的垃圾文件存在/home/user_name/.local/share/Trash/files/目录,但它还需要/home/user_name/.local/share/Trash/info/目录中存在的每个垃圾文件的信息文件。当我们想要将文件移入垃圾箱时,实际上将文件移动到 /home/user_name/.local/share/Trash/files/ 目录并在 /home/user_name/.local/share/Trash/info/ 目录中创建信息文件。在 .trashinfo 格式内,对存在文件的设置文件路径使用百分比解码方案,信息文件还包含删除的时间和日期。

相关问题