summaryrefslogtreecommitdiffstats
path: root/krename/fileoperation.cpp
blob: 813ee46fc2fe780deb32629aa779124276f188e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/***************************************************************************
                          fileoperation.cpp  -  description
                             -------------------
    begin                : Sun Nov 11 2001
    copyright            : (C) 2001 by Dominik Seichter
    email                : domseichter@web.de
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

// KDE includes
#include <tdeapplication.h>
#include <tdelocale.h>
#include <tdeio/netaccess.h>
#include <tdeio/job.h>

// QT includes
#include <tqeventloop.h>

#include <tqfileinfo.h>

// OS includes
#include <stdio.h>
#include <unistd.h>

// Own includes
#include "fileoperation.h"
#include "ProgressDialog.h"
#include "batchrenamer.h"

using namespace TDEIO;

FileOperation::FileOperation()
{
    canceled = false;
}

FileOperation::~FileOperation() { }

bool FileOperation::start( const KURL & src, const KURL & dest, int mode, bool overwrite )
{
    locked = true;
    result = 0;

    if( src == dest && !overwrite ) {
        m_error = i18n( "File %1 exists already!").arg( dest.prettyURL() );
        return false;
    }
    
    Job* job = 0;
    switch( mode ) {
        case RENAME:
        case MOVE:
        default:
            job = file_move( src, dest, -1, overwrite, false, false ); 
            break;
        case COPY:
            job = file_copy( src, dest, -1, overwrite, false, false ); 
            break;
        case LINK:
            // Does only work if both files are on the same host
            if( src.protocol() == dest.protocol() && src.host() == dest.host() )
            {
                job = symlink( src.path(), dest, overwrite, false );
            }
            else
            {
                m_error = i18n("Can't create symlinks on different hosts for file %1.").arg( src.prettyURL() );
                result = true;
                return !result;
            }
            break;
    };

    if( !job )
        return false;
        
    job->setAutoErrorHandlingEnabled( false, 0 );
    connect( job, TQT_SIGNAL( result (TDEIO::Job *) ),
           this, TQT_SLOT( slotResult (TDEIO::Job *) ) );

    kapp->eventLoop()->enterLoop();
    return !result;
}

bool FileOperation::fcopy( const TQString & src, const TQString & dest )
{
    return start( KURL( src ), KURL( dest ), TDEIO::CopyJob::Copy, false );
}

TQString FileOperation::getName( const TQString & file )
{
    TQFileInfo info( file );
    return info.fileName();
}

void FileOperation::slotResult( TDEIO::Job * job )
{
    result = job->error();
    if( result )
        m_error = job->errorString();

    kapp->eventLoop()->exitLoop();
}

#include "fileoperation.moc"