Moving files to Trash Can in Linux using C++
Asked Answered
P

3

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.

Parkway answered 21/7, 2011 at 13:3 Comment(4)
Can't you look at how trash-cli does it?Cheops
This link might help you: ramendik.ru/docs/trashspec.htmlTalbert
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.Parkway
The original FreeDesktop spec is here; AFAIK at least KDE, Gnome and XFCE follow it, and it's the one supported by trash-cli.Jaundiced
E
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.

Expert answered 22/7, 2011 at 16:2 Comment(2)
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.Parkway
std::system("gio trash ...") worked great for meKameko
L
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.

Lund answered 24/7, 2011 at 15:6 Comment(7)
Is it likely that the "MUST" means "IF you are to conform"? As it is, my Redhat Enterprise Linux box at work does not have a XDG_DATA_HOME environment var set by default. My Trash is in $HOME/Desktop/Trash/.directory ...Frank
I run Ubuntu 11.04 and there is no such folder in my home directory. But cstlib getenv will be useful for me in other ways, thank you.Parkway
How widely is this implemented?Vaso
freedesktop.org/wiki/Specifications lists this under Draft specifications that are new and not yet widely used.Vaso
Thx, Paŭlo. That explains a lot.Parkway
what's about restoring files from bin later? can they be restored via restore button from bin by user?Ty
@Parkway on Ubuntu the trash is here: /home/YOURUSERFOLDER/.local/share/Trash/files/Dilorenzo
S
0

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

Schlenger answered 21/7, 2011 at 13:8 Comment(1)
OP seems to be well aware that this feature lies in each window manager.Dorinda

© 2022 - 2024 — McMap. All rights reserved.