addtoarchive.cpp 5.29 KB
Newer Older
1
2
3
4
/*
 * ark -- archiver for the KDE project
 *
 * Copyright (C) 2008 Harald Hvaal <haraldhv (at@at) stud.ntnu.no>
5
 * Copyright (C) 2009 Raphael Kubo da Costa <kubito@gmail.com>
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 *
 * 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.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 */
22

23
24
#include "addtoarchive.h"
#include "adddialog.h"
25
26
#include "archive.h"
#include "jobs.h"
27

28
#include <KConfig>
29
30
31
32
33
34
#include <kdebug.h>
#include <kjobtrackerinterface.h>
#include <kmessagebox.h>
#include <klocale.h>
#include <KStandardDirs>

35
36
37
38
#include <QFileInfo>
#include <QDir>
#include <QPointer>

39
40
namespace Kerfuffle
{
41
42
43
44
45
46
47
48
49
AddToArchive::AddToArchive(QObject *parent)
        : KJob(parent), m_changeToFirstPath(false)
{
}

AddToArchive::~AddToArchive()
{
}

50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
void AddToArchive::setAutoFilenameSuffix(const QString& suffix)
{
    m_autoFilenameSuffix = suffix;
}

void AddToArchive::setChangeToFirstPath(bool value)
{
    m_changeToFirstPath = value;
}

void AddToArchive::setFilename(const KUrl& path)
{
    m_filename = path.path();
}

void AddToArchive::setMimeType(const QString & mimeType)
{
    m_mimeType = mimeType;
}

70
71
72
73
74
75
76
77
78
79
80
81
bool AddToArchive::showAddDialog(void)
{
    QPointer<Kerfuffle::AddDialog> dialog = new Kerfuffle::AddDialog(
        m_inputs, // itemsToAdd
        KUrl(m_firstPath), // startDir
        "", // filter
        NULL, // parent
        NULL); // widget

    bool ret = dialog->exec();

    if (ret) {
82
83
        kDebug() << "Returned URL:" << dialog->selectedUrl();
        kDebug() << "Returned mime:" << dialog->currentMimeFilter();
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
        setFilename(dialog->selectedUrl());
        setMimeType(dialog->currentMimeFilter());
    }

    return ret;
}

bool AddToArchive::addInput(const KUrl& url)
{
    m_inputs << url.path(
        QFileInfo(url.path()).isDir() ?
        KUrl::AddTrailingSlash :
        KUrl::RemoveTrailingSlash
    );

    if (m_firstPath.isEmpty()) {
        QString firstEntry = url.path(KUrl::RemoveTrailingSlash);
        m_firstPath = QFileInfo(firstEntry).dir().absolutePath();
    }

    return true;
}

// TODO: If this class should ever be called outside main.cpp,
//       the returns should be preceded by emitResult().
void AddToArchive::start(void)
{
111
    kDebug();
112
113
114
115
116
117
118
119
120
121
122

    Kerfuffle::CompressionOptions options;

    if (!m_inputs.size()) {
        KMessageBox::error(NULL, i18n("No input files were given."));
        return;
    }

    Kerfuffle::Archive *archive;
    if (!m_filename.isEmpty()) {
        archive = Kerfuffle::factory(m_filename, m_mimeType);
123
        kDebug() << "Set filename to " + m_filename;
124
125
    } else {
        if (m_autoFilenameSuffix.isEmpty()) {
Frederik Schwarzer's avatar
Frederik Schwarzer committed
126
            KMessageBox::error(NULL, i18n("You need to either supply a filename for the archive or a suffix (such as rar, tar.gz) with the --autofilename argument."));
127
128
129
130
            return;
        }

        if (m_firstPath.isEmpty()) {
131
            kDebug() << "Weird, this should not happen. no firstpath defined. aborting";
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
            return;
        }

        QString base;
        QFileInfo fi(m_inputs.first());

        base = fi.absoluteFilePath();

        if (base.endsWith('/')) {
            base.chop(1);
        }

        QString finalName = base + '.' + m_autoFilenameSuffix;

        //if file already exists, append a number to the base until it doesn't
        //exist
        int appendNumber = 0;
        while (KStandardDirs::exists(finalName)) {
            ++appendNumber;
            finalName = base + '_' + QString::number(appendNumber) + '.' + m_autoFilenameSuffix;
        }

154
        kDebug() << "Autoset filename to " + finalName;
155
156
157
158
159
160
161
162
163
164
165
        archive = Kerfuffle::factory(finalName, m_mimeType);
    }

    // TODO Post-4.3 string freeze: the check for read-only must cause a separate error
    if (archive == NULL || archive->isReadOnly()) {
        KMessageBox::error(NULL, i18n("Failed to create the new archive. Permissions might not be sufficient."));
        return;
    }

    if (m_changeToFirstPath) {
        if (m_firstPath.isEmpty()) {
166
            kDebug() << "Weird, this should not happen. no firstpath defined. aborting";
167
168
169
170
171
172
173
174
175
176
            return;
        }

        QDir stripDir = QDir(m_firstPath);

        for (int i = 0; i < m_inputs.size(); ++i) {
            m_inputs[i] = stripDir.absoluteFilePath(m_inputs.at(i));
        }

        options["GlobalWorkDir"] = stripDir.path();
177
        kDebug() << "Setting GlobalWorkDir to " << stripDir.path();
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
    }

    Kerfuffle::AddJob *job =
        archive->addFiles(m_inputs, options);

    KIO::getJobTracker()->registerJob(job);

    connect(job, SIGNAL(result(KJob*)),
            this, SLOT(slotFinished(KJob*)));

    job->start();
}

void AddToArchive::slotFinished(KJob *job)
{
193
    kDebug();
194
195
196
197
198
199
200

    if (job->error()) {
        KMessageBox::error(NULL, job->errorText());
    }

    emitResult();
}
201
}