1
/* This file is part of the KDE project
2
Copyright (C) 2004 David Faure <faure@kde.org>
4
This library is free software; you can redistribute it and/or
5
modify it under the terms of the GNU Library General Public
6
License as published by the Free Software Foundation; either
7
version 2 of the License, or (at your option) any later version.
9
This library is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
Library General Public License for more details.
14
You should have received a copy of the GNU Library General Public License
15
along with this library; see the file COPYING.LIB. If not, write to
16
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17
Boston, MA 02110-1301, USA.
20
#include <kapplication.h>
21
#include <kio/netaccess.h>
23
#include <kcmdlineargs.h>
25
#include <kdirnotify_stub.h>
28
static KCmdLineOptions options[] =
30
{ "empty", I18N_NOOP( "Empty the contents of the trash" ), 0 },
31
//{ "migrate", I18N_NOOP( "Migrate contents of old trash" ), 0 },
32
{ "restore <file>", I18N_NOOP( "Restore a trashed file to its original location" ), 0 },
33
// This hack is for the servicemenu on trash.desktop which uses Exec=ktrash -empty. %f is implied...
34
{ "+[ignored]", I18N_NOOP( "Ignored" ), 0 },
38
int main(int argc, char *argv[])
40
KApplication::disableAutoDcopRegistration();
41
KCmdLineArgs::init( argc, argv, "ktrash",
42
I18N_NOOP( "ktrash" ),
43
I18N_NOOP( "Helper program to handle the KDE trash can\n"
44
"Note: to move files to the trash, do not use ktrash, but \"kfmclient move 'url' trash:/\"" ),
46
KCmdLineArgs::addCmdLineOptions( options );
49
KCmdLineArgs* args = KCmdLineArgs::parsedArgs();
50
if ( args->isSet( "empty" ) ) {
51
// We use a kio job instead of linking to TrashImpl, for a smaller binary
52
// (and the possibility of a central service at some point)
53
QByteArray packedArgs;
54
QDataStream stream( packedArgs, IO_WriteOnly );
56
KIO::Job* job = KIO::special( "trash:/", packedArgs );
57
(void)KIO::NetAccess::synchronousRun( job, 0 );
59
// Update konq windows opened on trash:/
60
KDirNotify_stub allDirNotify("*", "KDirNotify*");
61
allDirNotify.FilesAdded( "trash:/" ); // yeah, files were removed, but we don't know which ones...
66
// This is only for testing. KDesktop handles it automatically.
67
if ( args->isSet( "migrate" ) ) {
68
QByteArray packedArgs;
69
QDataStream stream( packedArgs, IO_WriteOnly );
71
KIO::Job* job = KIO::special( "trash:/", packedArgs );
72
(void)KIO::NetAccess::synchronousRun( job, 0 );
77
QCString restoreArg = args->getOption( "restore" );
78
if ( !restoreArg.isEmpty() ) {
80
if (restoreArg.find("system:/trash")==0) {
81
restoreArg.remove(0, 13);
82
restoreArg.prepend("trash:");
85
KURL trashURL( restoreArg );
86
if ( !trashURL.isValid() || trashURL.protocol() != "trash" ) {
87
kdError() << "Invalid URL for restoring a trashed file:" << trashURL << endl;
91
QByteArray packedArgs;
92
QDataStream stream( packedArgs, IO_WriteOnly );
93
stream << (int)3 << trashURL;
94
KIO::Job* job = KIO::special( trashURL, packedArgs );
95
bool ok = KIO::NetAccess::synchronousRun( job, 0 );
97
kdError() << KIO::NetAccess::lastErrorString() << endl;