add more spacing
[personal-kdebase.git] / runtime / kioslave / trash / ktrash.cpp
blobc78f53f0af3a711455e766ac19b7e87dcfa34ef5
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>
22 #include <kio/job.h>
23 #include <kcmdlineargs.h>
24 #include <klocale.h>
25 #include <kdirnotify.h>
26 #include <kdebug.h>
28 int main(int argc, char *argv[])
30 //KApplication::disableAutoDcopRegistration();
31 KCmdLineArgs::init( argc, argv, "ktrash", "kio_trash",
32 ki18n( "ktrash" ),
33 KDE_VERSION_STRING ,
34 ki18n( "Helper program to handle the KDE trash can\n"
35 "Note: to move files to the trash, do not use ktrash, but \"kioclient move 'url' trash:/\"" ));
37 KCmdLineOptions options;
38 options.add("empty", ki18n( "Empty the contents of the trash" ));
39 //{ "migrate", I18N_NOOP( "Migrate contents of old trash" ), 0 },
40 options.add("restore <file>", ki18n( "Restore a trashed file to its original location" ));
41 // This hack is for the servicemenu on trash.desktop which uses Exec=ktrash -empty. %f is implied...
42 options.add("+[ignored]", ki18n( "Ignored" ));
43 KCmdLineArgs::addCmdLineOptions( options );
44 KApplication app;
46 KCmdLineArgs* args = KCmdLineArgs::parsedArgs();
47 if ( args->isSet( "empty" ) ) {
48 // We use a kio job instead of linking to TrashImpl, for a smaller binary
49 // (and the possibility of a central service at some point)
50 QByteArray packedArgs;
51 QDataStream stream( &packedArgs, QIODevice::WriteOnly );
52 stream << (int)1;
53 KIO::Job* job = KIO::special( KUrl("trash:/"), packedArgs );
54 (void)KIO::NetAccess::synchronousRun( job, 0 );
56 // Update konq windows opened on trash:/
57 org::kde::KDirNotify::emitFilesAdded( "trash:/" ); // yeah, files were removed, but we don't know which ones...
58 return 0;
61 #if 0
62 // This is only for testing. KDesktop handles it automatically.
63 if ( args->isSet( "migrate" ) ) {
64 QByteArray packedArgs;
65 QDataStream stream( packedArgs, QIODevice::WriteOnly );
66 stream << (int)2;
67 KIO::Job* job = KIO::special( "trash:/", packedArgs );
68 (void)KIO::NetAccess::synchronousRun( job, 0 );
69 return 0;
71 #endif
73 QString restoreArg = args->getOption( "restore" );
74 if ( !restoreArg.isEmpty() ) {
76 if (restoreArg.indexOf("system:/trash")==0) {
77 restoreArg.remove(0, 13);
78 restoreArg.prepend("trash:");
81 KUrl trashURL( restoreArg );
82 if ( !trashURL.isValid() || trashURL.protocol() != "trash" ) {
83 kError() << "Invalid URL for restoring a trashed file:" << trashURL << endl;
84 return 1;
87 QByteArray packedArgs;
88 QDataStream stream( &packedArgs, QIODevice::WriteOnly );
89 stream << (int)3 << trashURL;
90 KIO::Job* job = KIO::special( trashURL, packedArgs );
91 bool ok = KIO::NetAccess::synchronousRun( job, 0 );
92 if ( !ok )
93 kError() << KIO::NetAccess::lastErrorString() << endl;
94 return 0;
97 return 0;