13

I'm trying to move (delete) a file to a Trash Can (in Linux) using C++ (also using QT4 for GUI). Unfortunately it seems to be quite difficult to do so and as far as I can tell there isn't a unified API for it.

I would like for my application to run not only on KDE but on GNOME, Xfce and other Linux desktop environments. That's why I'm searching for a more universal approach.

The best I could find so far is:

  • send2trash - but that's using Python/QT4 and not C++/QT4
  • trash-cli - this has the drawback of being a stand alone command line program and not a library

I would be happy with any approach that requires as little desktop environment specific code as possible. Or in other words that's as much independent from KDE/GNOME/Xfce components as possible.

Any help in finding a solution (if there even is one) would be greatly appreciated.

Engos
  • 296
  • 1
  • 7
  • 1
    Can't you look at how trash-cli does it? – R. Martinho Fernandes Jul 21 '11 at 13:08
  • 3
    This link might help you: http://www.ramendik.ru/docs/trashspec.html – SirDarius Jul 21 '11 at 13:11
  • You are both right, but that would be a last resort solution. It's reinventing the wheel. In other words instead of using a lib it would mean writing one. Also trash-cli just like send2trash is written in Python in which I have no experience. – Engos Jul 21 '11 at 13:22
  • 4
    The original FreeDesktop spec is [here](http://freedesktop.org/wiki/Specifications/trash-spec); AFAIK at least KDE, Gnome and XFCE follow it, and it's the one supported by `trash-cli`. – Matteo Italia Jul 21 '11 at 13:31

3 Answers3

1

Why not find a terminal command to move the files and then call system() to run it for you inside your C++ program?

This might (I haven't tested it) be a possible one-liner in Linux to move files to the trash via the terminal. You would just pass the command as a quoted string to system() and call it in your C++ implementation.

kevlar1818
  • 3,055
  • 6
  • 29
  • 43
  • It's similar to the trash-cli I've mentioned in my original post. The problem with your solution is that it doesn't account for Trash Cans and files on other partitions/drives. It just moves them (and if it's on another drive - copies them between drives) to your home Trash Can. Thank you for your answer anyway. – Engos Jul 23 '11 at 11:14
  • `std::system("gio trash ...")` worked great for me – Matt Eding Apr 27 '21 at 18:45
1

The answer is in

http://www.freedesktop.org/wiki/Specifications/trash-spec

For every user a “home trash” directory MUST be available. Its name and location are $XDG_DATA_HOME/Trash

you only need to write C++ code move your file into such directory.

You can move files using boost file system and you can retrieve the XDG_DATA_HOME value using cstlib getenv.

rodrigob
  • 2,891
  • 3
  • 30
  • 34
0

As far as I know there's no standard trash can in Linux in the first place.

dascandy
  • 7,184
  • 1
  • 29
  • 50