~ubuntu-branches/ubuntu/lucid/kdebase/lucid

« back to all changes in this revision

Viewing changes to kioslave/trash/ktrash.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Ana Beatriz Guerrero Lopez
  • Date: 2009-04-05 05:22:13 UTC
  • mfrom: (0.4.2 experimental) (0.2.2 upstream)
  • mto: This revision was merged to the branch mainline in revision 235.
  • Revision ID: james.westby@ubuntu.com-20090405052213-39thr4l6p2ss07uj
Tags: 4:4.2.2-1
* New upstream release:
  - khtml fixes. (Closes: #290285, #359680)
  - Default konsole sessions can be deleted. (Closes: #286342)
  - Tag widget uses standard application palette. (Closes: #444800)
  - ... and surely many more but we have lost track...

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* This file is part of the KDE project
2
 
   Copyright (C) 2004 David Faure <faure@kde.org>
3
 
 
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.
8
 
 
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.
13
 
 
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.
18
 
*/
19
 
 
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_stub.h>
26
 
#include <kdebug.h>
27
 
 
28
 
static KCmdLineOptions options[] =
29
 
{
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 },
35
 
    KCmdLineLastOption
36
 
};
37
 
 
38
 
int main(int argc, char *argv[])
39
 
{
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:/\"" ),
45
 
                        KDE_VERSION_STRING );
46
 
    KCmdLineArgs::addCmdLineOptions( options );
47
 
    KApplication app;
48
 
 
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 );
55
 
        stream << (int)1;
56
 
        KIO::Job* job = KIO::special( "trash:/", packedArgs );
57
 
        (void)KIO::NetAccess::synchronousRun( job, 0 );
58
 
 
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...
62
 
        return 0;
63
 
    }
64
 
 
65
 
#if 0
66
 
    // This is only for testing. KDesktop handles it automatically.
67
 
    if ( args->isSet( "migrate" ) ) {
68
 
        QByteArray packedArgs;
69
 
        QDataStream stream( packedArgs, IO_WriteOnly );
70
 
        stream << (int)2;
71
 
        KIO::Job* job = KIO::special( "trash:/", packedArgs );
72
 
        (void)KIO::NetAccess::synchronousRun( job, 0 );
73
 
        return 0;
74
 
    }
75
 
#endif
76
 
 
77
 
    QCString restoreArg = args->getOption( "restore" );
78
 
    if ( !restoreArg.isEmpty() ) {
79
 
 
80
 
        if (restoreArg.find("system:/trash")==0) {
81
 
            restoreArg.remove(0, 13);
82
 
            restoreArg.prepend("trash:");
83
 
        }
84
 
 
85
 
        KURL trashURL( restoreArg );
86
 
        if ( !trashURL.isValid() || trashURL.protocol() != "trash" ) {
87
 
            kdError() << "Invalid URL for restoring a trashed file:" << trashURL << endl;
88
 
            return 1;
89
 
        }
90
 
 
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 );
96
 
        if ( !ok )
97
 
            kdError() << KIO::NetAccess::lastErrorString() << endl;
98
 
        return 0;
99
 
    }
100
 
 
101
 
    return 0;
102
 
}