1/*
2 This file is part of the KDE project
3 SPDX-FileCopyrightText: 2004 David Faure <faure@kde.org>
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6*/
7
8#include <KIO/EmptyTrashJob>
9#include <KLocalizedString>
10#include <QCommandLineOption>
11#include <QCommandLineParser>
12#include <QCoreApplication>
13#include <QDataStream>
14#include <QDebug>
15#include <QUrl>
16
17int main(int argc, char *argv[])
18{
19 QCoreApplication app(argc, argv);
20 app.setApplicationName(QStringLiteral("ktrash"));
21 app.setApplicationVersion(QStringLiteral(PROJECT_VERSION));
22 app.setOrganizationDomain(QStringLiteral("kde.org"));
23
24 QCommandLineParser parser;
25 parser.addVersionOption();
26 parser.addHelpOption();
27 parser.setApplicationDescription(
28 i18n("Helper program to handle the KDE trash can\n"
29 "Note: to move files to the trash, do not use ktrash, but \"kioclient move 'url' trash:/\""));
30
31 parser.addOption(commandLineOption: QCommandLineOption(QStringList{QStringLiteral("empty")}, i18n("Empty the contents of the trash")));
32 parser.addOption(
33 commandLineOption: QCommandLineOption(QStringList{QStringLiteral("restore")}, i18n("Restore a trashed file to its original location"), QStringLiteral("file")));
34
35 parser.process(app);
36
37 if (parser.isSet(QStringLiteral("empty"))) {
38 // We use a kio job instead of linking to TrashImpl, for a smaller binary
39 // (and the possibility of a central service at some point)
40 KIO::Job *job = KIO::emptyTrash();
41 job->exec();
42 return 0;
43 }
44
45 QString restoreArg = parser.value(QStringLiteral("restore"));
46 if (!restoreArg.isEmpty()) {
47 if (restoreArg.indexOf(s: QLatin1String("system:/trash")) == 0) {
48 restoreArg.replace(i: 0, len: 13, QStringLiteral("trash:"));
49 }
50
51 QUrl trashURL(restoreArg);
52 if (!trashURL.isValid() || trashURL.scheme() != QLatin1String("trash")) {
53 qCritical() << "Invalid URL for restoring a trashed file, trash:// URL expected:" << trashURL;
54 return 1;
55 }
56
57 QByteArray packedArgs;
58 QDataStream stream(&packedArgs, QIODevice::WriteOnly);
59 stream << (int)3 << trashURL;
60 KIO::Job *job = KIO::special(url: trashURL, data: packedArgs);
61 bool ok = job->exec() ? true : false;
62 if (!ok) {
63 qCritical() << job->errorString();
64 }
65 return 0;
66 }
67
68 return 0;
69}
70

source code of kio/src/kioworkers/trash/ktrash.cpp