mainwindow.cpp 9.64 KB
Newer Older
1 2 3
/*
 * ark -- archiver for the KDE project
 *
4
 * Copyright (C) 2002-2003: Georg Robbers <Georg.Robbers@urz.uni-hd.de>
5 6 7
 * Copyright (C) 2003: Helio Chissini de Castro <helio@conectiva.com>
 * Copyright (C) 2007 Henrique Pinto <henrique.pinto@kdemail.net>
 * Copyright (C) 2008 Harald Hvaal <haraldhv@stud.ntnu.no>
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 *
 * 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.
 *
 */
24

Henrique Pinto's avatar
Henrique Pinto committed
25
#include "mainwindow.h"
26
#include "ark_debug.h"
27 28 29 30
#include "archive_kerfuffle.h"
#include "createdialog.h"
#include "settingspage.h"
#include "pluginmanager.h"
31
#include "part/interface.h"
Henrique Pinto's avatar
Henrique Pinto committed
32

33
#include <KParts/ReadWritePart>
34
#include <KPluginFactory>
Henrique Pinto's avatar
Henrique Pinto committed
35
#include <KMessageBox>
36
#include <KLocalizedString>
Henrique Pinto's avatar
Henrique Pinto committed
37 38
#include <KActionCollection>
#include <KStandardAction>
39
#include <KRecentFilesAction>
Bhushan Shah's avatar
Bhushan Shah committed
40
#include <KService>
41
#include <KSharedConfig>
42
#include <KConfigDialog>
Bhushan Shah's avatar
Bhushan Shah committed
43
#include <KConfigGroup>
44
#include <KConfigSkeleton>
45
#include <KXMLGUIFactory>
Henrique Pinto's avatar
Henrique Pinto committed
46

47
#include <QApplication>
48 49
#include <QDragEnterEvent>
#include <QDragMoveEvent>
Lukáš Tinkl's avatar
Lukáš Tinkl committed
50 51
#include <QFileDialog>
#include <QMimeData>
52
#include <QPointer>
53
#include <QRegularExpression>
Elvis Angelaccio's avatar
Elvis Angelaccio committed
54
#include <QStatusBar>
55

56 57
static bool isValidArchiveDrag(const QMimeData *data)
{
58
    return ((data->hasUrls()) && (data->urls().count() == 1));
59 60
}

61 62
MainWindow::MainWindow(QWidget *)
        : KParts::MainWindow()
Henrique Pinto's avatar
Henrique Pinto committed
63
{
64 65
    setupActions();
    setAcceptDrops(true);
Henrique Pinto's avatar
Henrique Pinto committed
66 67 68 69
}

MainWindow::~MainWindow()
{
70
    if (m_recentFilesAction) {
Laurent Montel's avatar
Laurent Montel committed
71
        m_recentFilesAction->saveEntries(KSharedConfig::openConfig()->group("Recent Files"));
72
    }
73 74

    guiFactory()->removeClient(m_part);
75 76
    delete m_part;
    m_part = 0;
Henrique Pinto's avatar
Henrique Pinto committed
77 78
}

79
void MainWindow::dragEnterEvent(QDragEnterEvent * event)
80
{
81
    qCDebug(ARK) << "dragEnterEvent" << event;
82

83
    Interface *iface = qobject_cast<Interface*>(m_part);
Raphael Kubo da Costa's avatar
Raphael Kubo da Costa committed
84 85 86
    if (iface->isBusy()) {
        return;
    }
87

Lukáš Tinkl's avatar
Lukáš Tinkl committed
88
    if (!event->source() && isValidArchiveDrag(event->mimeData())) {
89
        event->acceptProposedAction();
Raphael Kubo da Costa's avatar
Raphael Kubo da Costa committed
90
    }
91
    return;
92 93
}

94
void MainWindow::dropEvent(QDropEvent * event)
95
{
96
    qCDebug(ARK) << "dropEvent" << event;
97

98
    Interface *iface = qobject_cast<Interface*>(m_part);
Raphael Kubo da Costa's avatar
Raphael Kubo da Costa committed
99 100 101
    if (iface->isBusy()) {
        return;
    }
102

Raphael Kubo da Costa's avatar
Raphael Kubo da Costa committed
103 104
    if ((event->source() == NULL) &&
        (isValidArchiveDrag(event->mimeData()))) {
105
        event->acceptProposedAction();
Raphael Kubo da Costa's avatar
Raphael Kubo da Costa committed
106
    }
107 108 109 110

    //TODO: if this call provokes a message box the drag will still be going
    //while the box is onscreen. looks buggy, do something about it
    openUrl(event->mimeData()->urls().at(0));
111 112
}

113
void MainWindow::dragMoveEvent(QDragMoveEvent * event)
114
{
115
    qCDebug(ARK) << "dragMoveEvent" << event;
116

117
    Interface *iface = qobject_cast<Interface*>(m_part);
Raphael Kubo da Costa's avatar
Raphael Kubo da Costa committed
118 119 120
    if (iface->isBusy()) {
        return;
    }
121

Raphael Kubo da Costa's avatar
Raphael Kubo da Costa committed
122 123
    if ((event->source() == NULL) &&
        (isValidArchiveDrag(event->mimeData()))) {
124
        event->acceptProposedAction();
Raphael Kubo da Costa's avatar
Raphael Kubo da Costa committed
125
    }
126 127
}

128 129
bool MainWindow::loadPart()
{
Bhushan Shah's avatar
Bhushan Shah committed
130
    KPluginFactory *factory = 0;
131
    KService::Ptr service = KService::serviceByDesktopName(QStringLiteral("ark_part"));
Bhushan Shah's avatar
Bhushan Shah committed
132 133 134

    if (service) {
        factory = KPluginLoader(service->library()).factory();
135
    }
Bhushan Shah's avatar
Bhushan Shah committed
136 137 138 139

    m_part = factory ? static_cast<KParts::ReadWritePart*>(factory->create<KParts::ReadWritePart>(this)) : 0;

    if (!m_part) {
140
        KMessageBox::error(this, i18n("Unable to find Ark's KPart component, please check your installation."));
141
        qCWarning(ARK) << "Error loading Ark KPart.";
142 143
        return false;
    }
144

145
    m_part->setObjectName(QStringLiteral("ArkPart"));
146
    setCentralWidget(m_part->widget());
Elvis Angelaccio's avatar
Elvis Angelaccio committed
147

148
    setupGUI(ToolBar | Keys | Save, QStringLiteral("arkui.rc"));
149
    createGUI(m_part);
Elvis Angelaccio's avatar
Elvis Angelaccio committed
150

Elvis Angelaccio's avatar
Elvis Angelaccio committed
151
    statusBar()->hide();
152

153 154
    connect(m_part, SIGNAL(busy()), this, SLOT(updateActions()));
    connect(m_part, SIGNAL(ready()), this, SLOT(updateActions()));
155
    connect(m_part, SIGNAL(quit()), this, SLOT(quit()));
156

157
    return true;
158 159
}

Henrique Pinto's avatar
Henrique Pinto committed
160 161
void MainWindow::setupActions()
{
162 163 164 165 166 167
    m_newAction = actionCollection()->addAction(KStandardAction::New, QStringLiteral("ark_file_new"), this, SLOT(newArchive()));
    m_openAction = actionCollection()->addAction(KStandardAction::Open, QStringLiteral("ark_file_open"), this, SLOT(openArchive()));
    actionCollection()->addAction(KStandardAction::Quit, QStringLiteral("ark_quit"), this, SLOT(quit()));

    m_recentFilesAction = KStandardAction::openRecent(this, SLOT(openUrl(QUrl)), Q_NULLPTR);
    actionCollection()->addAction(QStringLiteral("ark_file_open_recent"), m_recentFilesAction);
168

169 170 171 172
    m_recentFilesAction->setToolBarMode(KRecentFilesAction::MenuMode);
    m_recentFilesAction->setToolButtonPopupMode(QToolButton::DelayedPopup);
    m_recentFilesAction->setIconText(i18nc("action, to open an archive", "Open"));
    m_recentFilesAction->setToolTip(i18n("Open an archive"));
Laurent Montel's avatar
Laurent Montel committed
173
    m_recentFilesAction->loadEntries(KSharedConfig::openConfig()->group("Recent Files"));
174

175
    KStandardAction::preferences(this, SLOT(showSettings()), actionCollection());
176 177
}

178 179
void MainWindow::updateActions()
{
180 181 182 183
    Interface *iface = qobject_cast<Interface*>(m_part);
    m_newAction->setEnabled(!iface->isBusy());
    m_openAction->setEnabled(!iface->isBusy());
    m_recentFilesAction->setEnabled(!iface->isBusy());
184 185
}

Henrique Pinto's avatar
Henrique Pinto committed
186 187
void MainWindow::openArchive()
{
188 189
    Interface *iface = qobject_cast<Interface*>(m_part);
    Q_ASSERT(iface);
190
    Q_UNUSED(iface);
Lukáš Tinkl's avatar
Lukáš Tinkl committed
191

192
    Kerfuffle::PluginManager pluginManager;
193
    auto dlg = new QFileDialog(this, i18nc("to open an archive", "Open Archive"));
194

195
    dlg->setMimeTypeFilters(pluginManager.supportedMimeTypes());
196 197
    dlg->setFileMode(QFileDialog::ExistingFile);
    dlg->setAcceptMode(QFileDialog::AcceptOpen);
198 199 200 201 202 203 204 205 206

    connect(dlg, &QDialog::finished, this, [this, dlg](int result) {
        if (result == QDialog::Accepted) {
            openUrl(dlg->selectedUrls().first());
        }
        dlg->deleteLater();
    });

    dlg->open();
207
}
Henrique Pinto's avatar
Henrique Pinto committed
208

Lukáš Tinkl's avatar
Lukáš Tinkl committed
209
void MainWindow::openUrl(const QUrl& url)
210
{
211 212
    if (!url.isEmpty()) {
        m_part->setArguments(m_openArgs);
213

214 215 216 217 218 219
        if (m_part->openUrl(url)) {
            m_recentFilesAction->addUrl(url);
        } else {
            m_recentFilesAction->removeUrl(url);
        }
    }
Henrique Pinto's avatar
Henrique Pinto committed
220 221
}

222 223
void MainWindow::setShowExtractDialog(bool option)
{
224
    if (option) {
225
        m_openArgs.metaData()[QStringLiteral("showExtractDialog")] = QStringLiteral("true");
226
    } else {
227
        m_openArgs.metaData().remove(QStringLiteral("showExtractDialog"));
228
    }
229 230
}

231 232 233 234 235 236 237 238
void MainWindow::closeEvent(QCloseEvent *event)
{
    // Preview windows don't have a parent, so we need to manually close them.
    foreach (QWidget *widget, qApp->topLevelWidgets()) {
        if (widget->isVisible()) {
            widget->close();
        }
    }
239 240

    KParts::MainWindow::closeEvent(event);
241 242
}

Henrique Pinto's avatar
Henrique Pinto committed
243 244
void MainWindow::quit()
{
245
    close();
246 247
}

248 249 250 251 252
void MainWindow::showSettings()
{
    Interface *iface = qobject_cast<Interface*>(m_part);
    Q_ASSERT(iface);

253
    KConfigDialog *dialog = new KConfigDialog(this, QStringLiteral("settings"), iface->config());
254 255 256 257 258 259 260

    foreach (Kerfuffle::SettingsPage *page, iface->settingsPages(this)) {
        dialog->addPage(page, page->name(), page->iconName());
    }
    // Hide the icons list if only one page has been added.
    dialog->setFaceType(KPageDialog::Auto);

Laurent Montel's avatar
Laurent Montel committed
261
    connect(dialog, &KConfigDialog::settingsChanged, this, &MainWindow::writeSettings);
262 263 264 265 266 267 268 269 270 271
    dialog->show();
}

void MainWindow::writeSettings()
{
    Interface *iface = qobject_cast<Interface*>(m_part);
    Q_ASSERT(iface);
    iface->config()->save();
}

272 273
void MainWindow::newArchive()
{
274 275
    qCDebug(ARK) << "Creating new archive";

276 277
    Interface *iface = qobject_cast<Interface*>(m_part);
    Q_ASSERT(iface);
278
    Q_UNUSED(iface);
Raphael Kubo da Costa's avatar
const++  
Raphael Kubo da Costa committed
279

280 281
    QPointer<Kerfuffle::CreateDialog> dialog = new Kerfuffle::CreateDialog(
        Q_NULLPTR, // parent
Ragnar Thomsen's avatar
Ragnar Thomsen committed
282
        i18n("Create New Archive"), // caption
283 284 285
        QUrl()); // startDir

    if (dialog.data()->exec()) {
Ragnar Thomsen's avatar
Ragnar Thomsen committed
286
        const QUrl saveFileUrl = dialog.data()->selectedUrl();
287
        const QString password = dialog.data()->password();
288
        const QString fixedMimeType = dialog.data()->currentMimeType().name();
289 290

        qCDebug(ARK) << "CreateDialog returned URL:" << saveFileUrl.toString();
291
        qCDebug(ARK) << "CreateDialog returned mime:" << fixedMimeType;
292

293
        m_openArgs.metaData()[QStringLiteral("createNewArchive")] = QStringLiteral("true");
294
        m_openArgs.metaData()[QStringLiteral("fixedMimeType")] = fixedMimeType;
295 296 297
        if (dialog.data()->compressionLevel() > -1) {
            m_openArgs.metaData()[QStringLiteral("compressionLevel")] = QString::number(dialog.data()->compressionLevel());
        }
298
        m_openArgs.metaData()[QStringLiteral("encryptionPassword")] = password;
299

300
        if (dialog.data()->isHeaderEncryptionEnabled()) {
301
            m_openArgs.metaData()[QStringLiteral("encryptHeader")] = QStringLiteral("true");
302 303 304 305
        }

        openUrl(saveFileUrl);

306 307
        m_openArgs.metaData().remove(QStringLiteral("showExtractDialog"));
        m_openArgs.metaData().remove(QStringLiteral("createNewArchive"));
308
        m_openArgs.metaData().remove(QStringLiteral("fixedMimeType"));
309
        m_openArgs.metaData().remove(QStringLiteral("compressionLevel"));
310 311
        m_openArgs.metaData().remove(QStringLiteral("encryptionPassword"));
        m_openArgs.metaData().remove(QStringLiteral("encryptHeader"));
Lukáš Tinkl's avatar
Lukáš Tinkl committed
312
    }
313 314

    delete dialog.data();
Henrique Pinto's avatar
Henrique Pinto committed
315
}