mainwindow.cpp 8.46 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.
 *
 */
Henrique Pinto's avatar
Henrique Pinto committed
24
#include "mainwindow.h"
25
#include "kerfuffle/archive_kerfuffle.h"
26
#include "part/interface.h"
Henrique Pinto's avatar
Henrique Pinto committed
27

28
#include <KPluginFactory>
Henrique Pinto's avatar
Henrique Pinto committed
29 30
#include <KMessageBox>
#include <KApplication>
31
#include <KLocalizedString>
Henrique Pinto's avatar
Henrique Pinto committed
32 33
#include <KActionCollection>
#include <KStandardAction>
34
#include <KRecentFilesAction>
Laurent Montel's avatar
Laurent Montel committed
35
#include <QDebug>
36 37
#include <KEditToolBar>
#include <KShortcutsDialog>
Bhushan Shah's avatar
Bhushan Shah committed
38
#include <KService>
Bhushan Shah's avatar
Bhushan Shah committed
39
#include <KConfigGroup>
40
#include <KXMLGUIFactory>
Henrique Pinto's avatar
Henrique Pinto committed
41

42 43
#include <QDragEnterEvent>
#include <QDragMoveEvent>
44
#include <QWeakPointer>
Lukáš Tinkl's avatar
Lukáš Tinkl committed
45 46
#include <QFileDialog>
#include <QMimeData>
47

48 49
static bool isValidArchiveDrag(const QMimeData *data)
{
50
    return ((data->hasUrls()) && (data->urls().count() == 1));
51 52
}

53 54
MainWindow::MainWindow(QWidget *)
        : KParts::MainWindow()
Henrique Pinto's avatar
Henrique Pinto committed
55
{
56
    setXMLFile(QLatin1String( "arkui.rc" ));
Henrique Pinto's avatar
Henrique Pinto committed
57

58 59
    setupActions();
    statusBar();
Henrique Pinto's avatar
Henrique Pinto committed
60

61 62
    resize(640, 480);

63
    setAutoSaveSettings(QLatin1String( "MainWindow" ));
64

65
    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
{
Laurent Montel's avatar
Laurent Montel committed
81
    //qDebug() << 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
{
Laurent Montel's avatar
Laurent Montel committed
96
    //qDebug() << 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
{
Laurent Montel's avatar
Laurent Montel committed
115
    //qDebug() << 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 131 132 133 134 135 136
//    KPluginLoader loader(QLatin1String( "arkpart" ));
//    KPluginFactory *factory = loader.factory();
//    if (factory) {
//        m_part = static_cast<KParts::ReadWritePart*>(factory->create<KParts::ReadWritePart>(this));
//    }
//    if (!factory || !m_part) {
//        KMessageBox::error(this, i18n("Unable to find Ark's KPart component, please check your installation."));
Laurent Montel's avatar
Laurent Montel committed
137
//        qWarning() << "Error loading Ark KPart: " << loader.errorString();
Bhushan Shah's avatar
Bhushan Shah committed
138 139 140 141 142 143 144 145
//        return false;
//    }

    KPluginFactory *factory = 0;
    KService::Ptr service = KService::serviceByDesktopName(QLatin1String("ark_part"));

    if (service) {
        factory = KPluginLoader(service->library()).factory();
146
    }
Bhushan Shah's avatar
Bhushan Shah committed
147 148 149 150

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

    if (!m_part) {
151
        KMessageBox::error(this, i18n("Unable to find Ark's KPart component, please check your installation."));
Laurent Montel's avatar
Laurent Montel committed
152
        qWarning() << "Error loading Ark KPart: ";
153 154
        return false;
    }
155

Bhushan Shah's avatar
Bhushan Shah committed
156

Laurent Montel's avatar
Laurent Montel committed
157
    m_part->setObjectName( QLatin1String("ArkPart" ));
158 159
    setCentralWidget(m_part->widget());
    createGUI(m_part);
160

161 162
    connect(m_part, SIGNAL(busy()), this, SLOT(updateActions()));
    connect(m_part, SIGNAL(ready()), this, SLOT(updateActions()));
163
    connect(m_part, SIGNAL(quit()), this, SLOT(quit()));
164

165
    return true;
166 167
}

Henrique Pinto's avatar
Henrique Pinto committed
168 169
void MainWindow::setupActions()
{
170 171 172
    m_newAction = KStandardAction::openNew(this, SLOT(newArchive()), actionCollection());
    m_openAction = KStandardAction::open(this, SLOT(openArchive()), actionCollection());
    KStandardAction::quit(this, SLOT(quit()), actionCollection());
173

Lukáš Tinkl's avatar
Lukáš Tinkl committed
174
    m_recentFilesAction = KStandardAction::openRecent(this, SLOT(openUrl(QUrl)), actionCollection());
175 176 177 178 179
    m_recentFilesAction->setToolBarMode(KRecentFilesAction::MenuMode);
    m_recentFilesAction->setToolButtonPopupMode(QToolButton::DelayedPopup);
    m_recentFilesAction->setIconText(i18nc("action, to open an archive", "Open"));
    m_recentFilesAction->setStatusTip(i18n("Click to open an archive, click and hold to open a recently-opened archive"));
    m_recentFilesAction->setToolTip(i18n("Open an archive"));
Laurent Montel's avatar
Laurent Montel committed
180
    m_recentFilesAction->loadEntries(KSharedConfig::openConfig()->group("Recent Files"));
181 182
    connect(m_recentFilesAction, SIGNAL(triggered()),
            this, SLOT(openArchive()));
183

184
    createStandardStatusBarAction();
185

186 187
    KStandardAction::configureToolbars(this, SLOT(editToolbars()), actionCollection());
    KStandardAction::keyBindings(this, SLOT(editKeyBindings()), actionCollection());
188 189
}

190 191
void MainWindow::updateActions()
{
192 193 194 195
    Interface *iface = qobject_cast<Interface*>(m_part);
    m_newAction->setEnabled(!iface->isBusy());
    m_openAction->setEnabled(!iface->isBusy());
    m_recentFilesAction->setEnabled(!iface->isBusy());
196 197
}

198 199
void MainWindow::editKeyBindings()
{
200 201 202
    KShortcutsDialog dlg(KShortcutsEditor::AllActions, KShortcutsEditor::LetterShortcutsAllowed, this);
    dlg.addCollection(actionCollection());
    dlg.addCollection(m_part->actionCollection());
203

204
    dlg.configure();
205 206 207 208
}

void MainWindow::editToolbars()
{
Bhushan Shah's avatar
Bhushan Shah committed
209 210
    KConfigGroup cfg(KSharedConfig::openConfig(), "MainWindow");
    saveMainWindowSettings(cfg);
211

212 213
    QWeakPointer<KEditToolBar> dlg = new KEditToolBar(factory(), this);
    dlg.data()->exec();
214

215
    createGUI(m_part);
216

Bhushan Shah's avatar
Bhushan Shah committed
217
    applyMainWindowSettings(KSharedConfig::openConfig()->group(QLatin1String("MainWindow")));
218

219
    delete dlg.data();
Henrique Pinto's avatar
Henrique Pinto committed
220 221 222 223
}

void MainWindow::openArchive()
{
224 225
    Interface *iface = qobject_cast<Interface*>(m_part);
    Q_ASSERT(iface);
226
    Q_UNUSED(iface);
Lukáš Tinkl's avatar
Lukáš Tinkl committed
227 228 229 230 231 232 233 234

    QFileDialog dlg(this, i18nc("to open an archive", "Open Archive"));
    dlg.setMimeTypeFilters(Kerfuffle::supportedMimeTypes());
    dlg.setFileMode(QFileDialog::ExistingFile);
    dlg.setAcceptMode(QFileDialog::AcceptOpen);
    if (dlg.exec() == QDialog::Accepted) {
        openUrl(dlg.selectedUrls().first());
    }
235
}
Henrique Pinto's avatar
Henrique Pinto committed
236

Lukáš Tinkl's avatar
Lukáš Tinkl committed
237
void MainWindow::openUrl(const QUrl& url)
238
{
239 240
    if (!url.isEmpty()) {
        m_part->setArguments(m_openArgs);
241

242 243 244 245 246 247
        if (m_part->openUrl(url)) {
            m_recentFilesAction->addUrl(url);
        } else {
            m_recentFilesAction->removeUrl(url);
        }
    }
Henrique Pinto's avatar
Henrique Pinto committed
248 249
}

250 251
void MainWindow::setShowExtractDialog(bool option)
{
252
    if (option) {
253
        m_openArgs.metaData()[QLatin1String( "showExtractDialog" )] = QLatin1String( "true" );
254
    } else {
255
        m_openArgs.metaData().remove(QLatin1String( "showExtractDialog" ));
256
    }
257 258
}

Henrique Pinto's avatar
Henrique Pinto committed
259 260
void MainWindow::quit()
{
261
    close();
262 263 264 265
}

void MainWindow::newArchive()
{
266 267
    Interface *iface = qobject_cast<Interface*>(m_part);
    Q_ASSERT(iface);
268
    Q_UNUSED(iface);
Raphael Kubo da Costa's avatar
const++  
Raphael Kubo da Costa committed
269 270

    const QStringList mimeTypes = Kerfuffle::supportedWriteMimeTypes();
271

Laurent Montel's avatar
Laurent Montel committed
272
    //qDebug() << "Supported mimetypes are" << mimeTypes.join( QLatin1String( " " ));
273

Lukáš Tinkl's avatar
Lukáš Tinkl committed
274 275 276 277 278 279 280 281 282 283
    QFileDialog dlg(this);
    dlg.setMimeTypeFilters(mimeTypes);
    dlg.setFileMode(QFileDialog::ExistingFile);
    dlg.setAcceptMode(QFileDialog::AcceptSave);
    if (dlg.exec() == QDialog::Accepted) {
        m_openArgs.metaData()[QLatin1String( "createNewArchive" )] = QLatin1String( "true" );
        openUrl(dlg.selectedUrls().first());
        m_openArgs.metaData().remove(QLatin1String( "showExtractDialog" ));
        m_openArgs.metaData().remove(QLatin1String( "createNewArchive" ));
    }
Henrique Pinto's avatar
Henrique Pinto committed
284
}