2013-07-31 62 views

回答

2

目前还没有API。

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

问题被关闭,该修补程序的版本是:未来的版本

编辑:一个新的问题已经在https://bugreports.qt.io/browse/QTBUG-47703打开。

+3

奇怪的问题处理。关闭为...?不会修复?太难?通常,当答案是“也许以后”时,这个问题只是保持开放。 –

+1

原因:超出范围 – fjardon

+0

有一个新的问题,在这里重新打开这个老之一:https://bugreports.qt.io/browse/QTBUG-47703 – Felix

3

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

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

+0

的制品是用于Python然而,不为C++。 – sashoalm

+2

@sashoalm这篇文章是关于使用API​​的。用什么语言来称呼他们并不重要。 –

3

我是比较肯定的是,没有Qt的API一个包装此对所有支持的平台。这意味着,不幸的是,你将不得不编写特定于平台的代码。

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

在Mac OS X上,有一个受支持的API可以执行此操作:在核心服务中提供的FSMoveObjectToTrashSync更容易一些。至少,我记得你应该这样做。 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将继续正常工作。

+2

在Linux上存在[FreeDesktop.org垃圾桶规格】(http://www.ramendik.ru/docs/trashspec.html),这似乎谈论存储在外部卷上的文件的情况。它受许多流行的Linux文件管理器支持,包括Nautilus和Dolphin。 – silviubogan

+0

或者Qt的人可能会很务实,只是将它用于Ubuntu和未来1-2个最受欢迎的发行版。 – sashoalm

6

Qt不提供MoveToTrash。下面是我的代码

一部分用于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
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) 



} 
+1

欢迎计算器。请解释一下你的答案。 –

+0

只要注意'QSysInfo :: kernelType()==“linux”',Android的内核也是Linux。 – sashoalm

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/目录中创建信息文件。里面其中file存在了一套文件路径.trashinfo格式使用百分比解码方案,信息文件还包含时间和删除的日期。