mainwindow.cpp 7.7 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.h"
26
#include "part/interface.h"
Henrique Pinto's avatar
Henrique Pinto committed
27

28
29
#include <KPluginLoader>
#include <KPluginFactory>
Henrique Pinto's avatar
Henrique Pinto committed
30
31
32
33
34
35
#include <KMessageBox>
#include <KApplication>
#include <KLocale>
#include <KActionCollection>
#include <KStandardAction>
#include <KFileDialog>
36
37
#include <KRecentFilesAction>
#include <KGlobal>
Andreas Pakulat's avatar
Andreas Pakulat committed
38
#include <KDebug>
39
40
#include <KEditToolBar>
#include <KShortcutsDialog>
41
#include <KXMLGUIFactory>
Henrique Pinto's avatar
Henrique Pinto committed
42

43
44
#include <QDragEnterEvent>
#include <QDragMoveEvent>
45
#include <QWeakPointer>
46

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

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

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

60
61
62
    if (!initialGeometrySet()) {
        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
71
72
    if (m_recentFilesAction) {
        m_recentFilesAction->saveEntries(KGlobal::config()->group("Recent Files"));
    }
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
    kDebug() << 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

Raphael Kubo da Costa's avatar
Raphael Kubo da Costa committed
88
89
    if ((event->source() == NULL) &&
        (isValidArchiveDrag(event->mimeData()))) {
90
        event->acceptProposedAction();
Raphael Kubo da Costa's avatar
Raphael Kubo da Costa committed
91
    }
92
    return;
93
94
}

95
void MainWindow::dropEvent(QDropEvent * event)
96
{
97
    kDebug() << event;
98

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

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

    //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));
112
113
}

114
void MainWindow::dragMoveEvent(QDragMoveEvent * event)
115
{
116
    kDebug() << event;
117

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

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

129
130
bool MainWindow::loadPart()
{
131
132
    KPluginLoader loader(QLatin1String( "arkpart" ));
    KPluginFactory *factory = loader.factory();
133
134
135
136
137
    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."));
138
        kWarning() << "Error loading Ark KPart: " << loader.errorString();
139
140
        return false;
    }
141

Laurent Montel's avatar
Laurent Montel committed
142
    m_part->setObjectName( QLatin1String("ArkPart" ));
143
144
    setCentralWidget(m_part->widget());
    createGUI(m_part);
145

146
147
    connect(m_part, SIGNAL(busy()), this, SLOT(updateActions()));
    connect(m_part, SIGNAL(ready()), this, SLOT(updateActions()));
148
    connect(m_part, SIGNAL(quit()), this, SLOT(quit()));
149

150
    return true;
151
152
}

Henrique Pinto's avatar
Henrique Pinto committed
153
154
void MainWindow::setupActions()
{
155
156
157
    m_newAction = KStandardAction::openNew(this, SLOT(newArchive()), actionCollection());
    m_openAction = KStandardAction::open(this, SLOT(openArchive()), actionCollection());
    KStandardAction::quit(this, SLOT(quit()), actionCollection());
158

Laurent Montel's avatar
Laurent Montel committed
159
    m_recentFilesAction = KStandardAction::openRecent(this, SLOT(openUrl(KUrl)), actionCollection());
160
161
162
163
164
165
166
167
    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"));
    m_recentFilesAction->loadEntries(KGlobal::config()->group("Recent Files"));
    connect(m_recentFilesAction, SIGNAL(triggered()),
            this, SLOT(openArchive()));
168

169
    createStandardStatusBarAction();
170

171
172
    KStandardAction::configureToolbars(this, SLOT(editToolbars()), actionCollection());
    KStandardAction::keyBindings(this, SLOT(editKeyBindings()), actionCollection());
173
174
}

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

183
184
void MainWindow::editKeyBindings()
{
185
186
187
    KShortcutsDialog dlg(KShortcutsEditor::AllActions, KShortcutsEditor::LetterShortcutsAllowed, this);
    dlg.addCollection(actionCollection());
    dlg.addCollection(m_part->actionCollection());
188

189
    dlg.configure();
190
191
192
193
}

void MainWindow::editToolbars()
{
194
    saveMainWindowSettings(KGlobal::config()->group(QLatin1String("MainWindow")));
195

196
197
    QWeakPointer<KEditToolBar> dlg = new KEditToolBar(factory(), this);
    dlg.data()->exec();
198

199
    createGUI(m_part);
200

201
    applyMainWindowSettings(KGlobal::config()->group(QLatin1String("MainWindow")));
202

203
    delete dlg.data();
Henrique Pinto's avatar
Henrique Pinto committed
204
205
206
207
}

void MainWindow::openArchive()
{
208
209
    Interface *iface = qobject_cast<Interface*>(m_part);
    Q_ASSERT(iface);
Laurent Montel's avatar
Laurent Montel committed
210
    const KUrl url = KFileDialog::getOpenUrl(KUrl("kfiledialog:///ArkOpenDir"),
211
                                       Kerfuffle::supportedMimeTypes().join( QLatin1String( " " )),
212
213
                                       this);
    openUrl(url);
214
}
Henrique Pinto's avatar
Henrique Pinto committed
215

216
void MainWindow::openUrl(const KUrl& url)
217
{
218
219
    if (!url.isEmpty()) {
        m_part->setArguments(m_openArgs);
220

221
222
223
224
225
226
        if (m_part->openUrl(url)) {
            m_recentFilesAction->addUrl(url);
        } else {
            m_recentFilesAction->removeUrl(url);
        }
    }
Henrique Pinto's avatar
Henrique Pinto committed
227
228
}

229
230
void MainWindow::setShowExtractDialog(bool option)
{
231
    if (option) {
232
        m_openArgs.metaData()[QLatin1String( "showExtractDialog" )] = QLatin1String( "true" );
233
    } else {
234
        m_openArgs.metaData().remove(QLatin1String( "showExtractDialog" ));
235
    }
236
237
}

Henrique Pinto's avatar
Henrique Pinto committed
238
239
void MainWindow::quit()
{
240
    close();
241
242
243
244
}

void MainWindow::newArchive()
{
245
246
    Interface *iface = qobject_cast<Interface*>(m_part);
    Q_ASSERT(iface);
Raphael Kubo da Costa's avatar
const++    
Raphael Kubo da Costa committed
247
248

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

250
    kDebug() << "Supported mimetypes are" << mimeTypes.join( QLatin1String( " " ));
251

252
253
254
    const KUrl saveFileUrl =
        KFileDialog::getSaveUrl(KUrl("kfiledialog:///ArkNewDir"),
                                mimeTypes.join(QLatin1String(" ")));
255

256
    m_openArgs.metaData()[QLatin1String( "createNewArchive" )] = QLatin1String( "true" );
257

258
    openUrl(saveFileUrl);
259

260
261
    m_openArgs.metaData().remove(QLatin1String( "showExtractDialog" ));
    m_openArgs.metaData().remove(QLatin1String( "createNewArchive" ));
Henrique Pinto's avatar
Henrique Pinto committed
262
}