Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moving files to Trash Can in Linux using C++

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.

like image 311
Engos Avatar asked Jul 21 '11 13:07

Engos


People also ask

How do I trash Linux?

Deleting a file or folder permanentlyBy directing the rm command to an asterisk ( * ), you delete all files and folders inside the Trash folder without deleting the Trash folder itself. If you accidentally delete the Trash folder, however, you can just recreate it because directories are easy and free to create.

Does Linux have a trash can?

Even if your Linux desktop environment of choice doesn't place an icon for it on the desktop, your Linux distro already has a trash directory. The advantage of relegating files to a trash directory is that it gives you the option to view all the deleted files and even restore files, empty the trash, etc.


2 Answers

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.

like image 43
rodrigob Avatar answered Sep 30 '22 10:09

rodrigob


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.

like image 109
kevlar1818 Avatar answered Sep 30 '22 12:09

kevlar1818