~ubuntu-branches/ubuntu/vivid/krusader/vivid-proposed

« back to all changes in this revision

Viewing changes to krusader/VFS/packjob.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Scott Kitterman
  • Date: 2010-05-05 22:26:37 UTC
  • mfrom: (3.1.4 squeeze)
  • Revision ID: james.westby@ubuntu.com-20100505222637-ydv3cwjwy365on2r
Tags: 1:2.1.0~beta1-1ubuntu1
* Merge from Debian Unstable.  Remaining changes:
  - Retain Kubuntu doc path

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
                         packjob.cpp  -  description
 
3
                             -------------------
 
4
    copyright            : (C) 2009 + by Csaba Karai
 
5
    e-mail               : krusader@users.sourceforge.net
 
6
    web site             : http://krusader.sourceforge.net
 
7
 ---------------------------------------------------------------------------
 
8
  Description
 
9
 ***************************************************************************
 
10
 
 
11
  A
 
12
 
 
13
     db   dD d8888b. db    db .d8888.  .d8b.  d8888b. d88888b d8888b.
 
14
     88 ,8P' 88  `8D 88    88 88'  YP d8' `8b 88  `8D 88'     88  `8D
 
15
     88,8P   88oobY' 88    88 `8bo.   88ooo88 88   88 88ooooo 88oobY'
 
16
     88`8b   88`8b   88    88   `Y8b. 88~~~88 88   88 88~~~~~ 88`8b
 
17
     88 `88. 88 `88. 88b  d88 db   8D 88   88 88  .8D 88.     88 `88.
 
18
     YP   YD 88   YD ~Y8888P' `8888Y' YP   YP Y8888D' Y88888P 88   YD
 
19
 
 
20
                                                     S o u r c e    F i l e
 
21
 
 
22
 ***************************************************************************
 
23
 *                                                                         *
 
24
 *   This program is free software; you can redistribute it and/or modify  *
 
25
 *   it under the terms of the GNU General Public License as published by  *
 
26
 *   the Free Software Foundation; either version 2 of the License, or     *
 
27
 *   (at your option) any later version.                                   *
 
28
 *                                                                         *
 
29
 ***************************************************************************/
 
30
 
 
31
#include "packjob.h"
 
32
#include "krarchandler.h"
 
33
#include <QtCore/QTimer>
 
34
#include <QtCore/QDir>
 
35
#include <klocale.h>
 
36
#include <kmimetype.h>
 
37
 
 
38
PackJob::PackJob(const KUrl &srcUrl, const KUrl &destUrl, const QStringList & fileNames, const QString &type, const QMap<QString, QString> &packProps) : AbstractThreadedJob()
 
39
{
 
40
    start(new PackThread(srcUrl, destUrl, fileNames, type, packProps));
 
41
}
 
42
 
 
43
PackJob * PackJob::createPacker(const KUrl &srcUrl, const KUrl &destUrl, const QStringList & fileNames, const QString &type, const QMap<QString, QString> &packProps)
 
44
{
 
45
    return new PackJob(srcUrl, destUrl, fileNames, type, packProps);
 
46
}
 
47
 
 
48
PackThread::PackThread(const KUrl &srcUrl, const KUrl &destUrl, const QStringList & fileNames,
 
49
                       const QString &type, const QMap<QString, QString> &packProps) :
 
50
        AbstractJobThread(), _sourceUrl(srcUrl), _destUrl(destUrl), _fileNames(fileNames),
 
51
        _type(type), _packProperties(packProps)
 
52
{
 
53
}
 
54
 
 
55
 
 
56
void PackThread::slotStart()
 
57
{
 
58
    KUrl newSource = downloadIfRemote(_sourceUrl, _fileNames);
 
59
    if (newSource.isEmpty())
 
60
        return;
 
61
 
 
62
    KIO::filesize_t totalSize = 0;
 
63
    unsigned long totalDirs = 0, totalFiles = 0;
 
64
 
 
65
    calcSpaceLocal(newSource, _fileNames, totalSize, totalDirs, totalFiles);
 
66
 
 
67
    QString arcFile = tempFileIfRemote(_destUrl, _type);
 
68
    QString arcDir = newSource.path(KUrl::RemoveTrailingSlash);
 
69
 
 
70
    setProgressTitle(i18n("Processed files"));
 
71
 
 
72
    QString save = QDir::currentPath();
 
73
    QDir::setCurrent(arcDir);
 
74
    bool result = KRarcHandler::pack(_fileNames, _type, arcFile, totalFiles + totalDirs, _packProperties, observer());
 
75
    QDir::setCurrent(save);
 
76
 
 
77
    if (isExited())
 
78
        return;
 
79
    if (!result) {
 
80
        sendError(KIO::ERR_INTERNAL, i18n("Error at packing"));
 
81
        return;
 
82
    }
 
83
 
 
84
    if (!uploadTempFiles())
 
85
        return;
 
86
 
 
87
    sendSuccess();
 
88
}
 
89
 
 
90
TestArchiveJob::TestArchiveJob(const KUrl &srcUrl, const QStringList & fileNames) : AbstractThreadedJob()
 
91
{
 
92
    start(new TestArchiveThread(srcUrl, fileNames));
 
93
}
 
94
 
 
95
TestArchiveJob * TestArchiveJob::testArchives(const KUrl &srcUrl, const QStringList & fileNames)
 
96
{
 
97
    return new TestArchiveJob(srcUrl, fileNames);
 
98
}
 
99
 
 
100
TestArchiveThread::TestArchiveThread(const KUrl &srcUrl, const QStringList & fileNames) : AbstractJobThread(),
 
101
        _sourceUrl(srcUrl), _fileNames(fileNames)
 
102
{
 
103
}
 
104
 
 
105
void TestArchiveThread::slotStart()
 
106
{
 
107
    KUrl newSource = downloadIfRemote(_sourceUrl, _fileNames);
 
108
    if (newSource.isEmpty())
 
109
        return;
 
110
 
 
111
    for (int i = 0; i < _fileNames.count(); ++i) {
 
112
        QString arcName = _fileNames[ i ];
 
113
        if (arcName.isEmpty())
 
114
            continue;
 
115
        if (arcName == "..")
 
116
            continue; // safety
 
117
 
 
118
        KUrl url = newSource;
 
119
        url.addPath(arcName);
 
120
 
 
121
        QString path = url.path(KUrl::RemoveTrailingSlash);
 
122
 
 
123
        KMimeType::Ptr mt = KMimeType::findByUrl(url);
 
124
        QString mime = mt ? mt->name() : QString();
 
125
        bool encrypted = false;
 
126
        QString type = KRarcHandler::getType(encrypted, path, mime);
 
127
 
 
128
        // check we that archive is supported
 
129
        if (!KRarcHandler::arcSupported(type)) {
 
130
            sendError(KIO::ERR_NO_CONTENT, i18n("%1, unsupported archive type.", arcName));
 
131
            return ;
 
132
        }
 
133
 
 
134
        QString password = encrypted ? getPassword(path) : QString();
 
135
 
 
136
        // test the archive
 
137
        if (!KRarcHandler::test(path, type, password, 0, observer())) {
 
138
            sendError(KIO::ERR_NO_CONTENT, i18n("%1, test failed!", arcName));
 
139
            return ;
 
140
        }
 
141
    }
 
142
 
 
143
    sendMessage(i18n("Archive tests passed."));
 
144
    sendSuccess();
 
145
}
 
146
 
 
147
 
 
148
UnpackJob::UnpackJob(const KUrl &srcUrl, const KUrl &destUrl, const QStringList & fileNames) : AbstractThreadedJob()
 
149
{
 
150
    start(new UnpackThread(srcUrl, destUrl, fileNames));
 
151
}
 
152
 
 
153
UnpackJob * UnpackJob::createUnpacker(const KUrl &srcUrl, const KUrl &destUrl, const QStringList & fileNames)
 
154
{
 
155
    return new UnpackJob(srcUrl, destUrl, fileNames);
 
156
}
 
157
 
 
158
UnpackThread::UnpackThread(const KUrl &srcUrl, const KUrl &destUrl, const QStringList & fileNames) :
 
159
        AbstractJobThread(), _sourceUrl(srcUrl), _destUrl(destUrl), _fileNames(fileNames)
 
160
{
 
161
}
 
162
 
 
163
void UnpackThread::slotStart()
 
164
{
 
165
    KUrl newSource = downloadIfRemote(_sourceUrl, _fileNames);
 
166
    if (newSource.isEmpty())
 
167
        return;
 
168
 
 
169
    QString localDest = tempDirIfRemote(_destUrl);
 
170
 
 
171
    for (int i = 0; i < _fileNames.count(); ++i) {
 
172
        QString arcName = _fileNames[ i ];
 
173
        if (arcName.isEmpty())
 
174
            continue;
 
175
        if (arcName == "..")
 
176
            continue; // safety
 
177
 
 
178
        KUrl url = newSource;
 
179
        url.addPath(arcName);
 
180
 
 
181
        QString path = url.path(KUrl::RemoveTrailingSlash);
 
182
 
 
183
        KMimeType::Ptr mt = KMimeType::findByUrl(url);
 
184
        QString mime = mt ? mt->name() : QString();
 
185
        bool encrypted = false;
 
186
        QString type = KRarcHandler::getType(encrypted, path, mime);
 
187
 
 
188
        // check we that archive is supported
 
189
        if (!KRarcHandler::arcSupported(type)) {
 
190
            sendError(KIO::ERR_NO_CONTENT, i18n("%1, unsupported archive type.", arcName));
 
191
            return ;
 
192
        }
 
193
 
 
194
        QString password = encrypted ? getPassword(path) : QString();
 
195
 
 
196
        setProgressTitle(i18n("Processed files"));
 
197
        // unpack the files
 
198
        bool result = KRarcHandler::unpack(path, type, password, localDest, observer());
 
199
 
 
200
        if (isExited())
 
201
            return;
 
202
        if (!result) {
 
203
            sendError(KIO::ERR_INTERNAL, i18n("Error at unpacking"));
 
204
            return;
 
205
        }
 
206
    }
 
207
 
 
208
    if (!uploadTempFiles())
 
209
        return;
 
210
 
 
211
    sendSuccess();
 
212
}