2014-11-15 04:16:00 +02:00
|
|
|
/* This file is part of the KDE project
|
|
|
|
Copyright (C) 2004 David Faure <faure@kde.org>
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Library General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Library General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Library General Public License
|
|
|
|
along with this library; see the file COPYING.LIB. If not, write to
|
|
|
|
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
|
|
Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <kapplication.h>
|
|
|
|
#include <kio/netaccess.h>
|
|
|
|
#include <kio/job.h>
|
|
|
|
#include <kcmdlineargs.h>
|
|
|
|
#include <klocale.h>
|
|
|
|
#include <kdirnotify.h>
|
|
|
|
#include <kdebug.h>
|
2015-11-20 00:42:21 +02:00
|
|
|
#include <kdeversion.h>
|
2014-11-15 04:16:00 +02:00
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2023-06-27 08:57:10 +03:00
|
|
|
KCmdLineArgs::init(
|
|
|
|
argc, argv, "ktrash", "kio_trash",
|
|
|
|
ki18n("ktrash"),
|
|
|
|
KDE_VERSION_STRING ,
|
|
|
|
ki18n("Helper program to handle the KDE trash can\n"
|
|
|
|
"Note: to move files to the trash, do not use ktrash, but \"kioclient move 'url' trash:/\"")
|
|
|
|
);
|
2014-11-15 04:16:00 +02:00
|
|
|
|
|
|
|
KCmdLineOptions options;
|
2023-06-27 08:57:10 +03:00
|
|
|
options.add("empty", ki18n("Empty the contents of the trash"));
|
2014-11-15 04:16:00 +02:00
|
|
|
//{ "migrate", I18N_NOOP( "Migrate contents of old trash" ), 0 },
|
2023-06-27 08:57:10 +03:00
|
|
|
options.add("restore <file>", ki18n("Restore a trashed file to its original location"));
|
2014-11-15 04:16:00 +02:00
|
|
|
// This hack is for the servicemenu on trash.desktop which uses Exec=ktrash -empty. %f is implied...
|
2023-06-27 08:57:10 +03:00
|
|
|
options.add("+[ignored]", ki18n("Ignored"));
|
|
|
|
KCmdLineArgs::addCmdLineOptions(options);
|
2014-11-15 04:16:00 +02:00
|
|
|
KApplication app;
|
|
|
|
|
|
|
|
KCmdLineArgs* args = KCmdLineArgs::parsedArgs();
|
2023-06-27 08:57:10 +03:00
|
|
|
if (args->isSet("empty")) {
|
2014-11-15 04:16:00 +02:00
|
|
|
// We use a kio job instead of linking to TrashImpl, for a smaller binary
|
|
|
|
// (and the possibility of a central service at some point)
|
|
|
|
QByteArray packedArgs;
|
2023-06-27 08:57:10 +03:00
|
|
|
QDataStream stream(&packedArgs, QIODevice::WriteOnly);
|
2014-11-15 04:16:00 +02:00
|
|
|
stream << (int)1;
|
2023-06-27 08:57:10 +03:00
|
|
|
KIO::Job* job = KIO::special(KUrl("trash:/"), packedArgs);
|
|
|
|
(void)KIO::NetAccess::synchronousRun(job, 0);
|
2014-11-15 04:16:00 +02:00
|
|
|
|
2023-06-27 08:57:10 +03:00
|
|
|
// Update konq windows opened on trash:/. yeah, files were removed, but we don't know which ones..
|
|
|
|
org::kde::KDirNotify::emitFilesAdded(QString::fromLatin1("trash:/"));
|
2014-11-15 04:16:00 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-06-27 08:57:10 +03:00
|
|
|
QString restoreArg = args->getOption("restore");
|
|
|
|
if (!restoreArg.isEmpty()) {
|
|
|
|
KUrl trashURL(restoreArg);
|
|
|
|
if (!trashURL.isValid() || trashURL.protocol() != QLatin1String("trash")) {
|
2022-11-13 01:41:49 +02:00
|
|
|
kError() << "Invalid URL for restoring a trashed file:" << trashURL;
|
2014-11-15 04:16:00 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
QByteArray packedArgs;
|
2023-06-27 08:57:10 +03:00
|
|
|
QDataStream stream(&packedArgs, QIODevice::WriteOnly);
|
2023-06-27 09:10:47 +03:00
|
|
|
stream << (int)2 << trashURL;
|
|
|
|
KIO::Job* job = KIO::special(trashURL, packedArgs);
|
2023-06-27 08:57:10 +03:00
|
|
|
bool ok = KIO::NetAccess::synchronousRun(job, 0);
|
|
|
|
if (!ok) {
|
2022-11-13 01:41:49 +02:00
|
|
|
kError() << KIO::NetAccess::lastErrorString();
|
2023-06-27 08:57:10 +03:00
|
|
|
}
|
2014-11-15 04:16:00 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|