mainwindow.cpp 7.54 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>
Henrique Pinto's avatar
Henrique Pinto committed
41

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

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

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

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

59
60
61
    if (!initialGeometrySet()) {
        resize(640, 480);
    }
62
    setAutoSaveSettings(QLatin1String( "MainWindow" ));
63

64
    setAcceptDrops(true);
Henrique Pinto's avatar
Henrique Pinto committed
65
66
67
68
}

MainWindow::~MainWindow()
{
69
70
71
72
73
    if (m_recentFilesAction) {
        m_recentFilesAction->saveEntries(KGlobal::config()->group("Recent Files"));
    }
    delete m_part;
    m_part = 0;
Henrique Pinto's avatar
Henrique Pinto committed
74
75
}

76
void MainWindow::dragEnterEvent(QDragEnterEvent * event)
77
{
78
    kDebug() << event;
79

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

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

92
void MainWindow::dropEvent(QDropEvent * event)
93
{
94
    kDebug() << event;
95

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

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

    //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));
109
110
}

111
void MainWindow::dragMoveEvent(QDragMoveEvent * event)
112
{
113
    kDebug() << event;
114

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

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

126
127
bool MainWindow::loadPart()
{
128
    KPluginFactory *factory = KPluginLoader(QLatin1String( "arkpart" )).factory();
129
130
131
132
133
134
135
    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."));
        return false;
    }
136

Laurent Montel's avatar
Laurent Montel committed
137
    m_part->setObjectName( QLatin1String("ArkPart" ));
138
139
    setCentralWidget(m_part->widget());
    createGUI(m_part);
140

141
142
    connect(m_part, SIGNAL(busy()), this, SLOT(updateActions()));
    connect(m_part, SIGNAL(ready()), this, SLOT(updateActions()));
143
    connect(m_part, SIGNAL(quit()), this, SLOT(quit()));
144

145
    return true;
146
147
}

Henrique Pinto's avatar
Henrique Pinto committed
148
149
void MainWindow::setupActions()
{
150
151
152
    m_newAction = KStandardAction::openNew(this, SLOT(newArchive()), actionCollection());
    m_openAction = KStandardAction::open(this, SLOT(openArchive()), actionCollection());
    KStandardAction::quit(this, SLOT(quit()), actionCollection());
153

Laurent Montel's avatar
Laurent Montel committed
154
    m_recentFilesAction = KStandardAction::openRecent(this, SLOT(openUrl(KUrl)), actionCollection());
155
156
157
158
159
160
161
162
    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()));
163

164
    createStandardStatusBarAction();
165

166
167
    KStandardAction::configureToolbars(this, SLOT(editToolbars()), actionCollection());
    KStandardAction::keyBindings(this, SLOT(editKeyBindings()), actionCollection());
168
169
}

170
171
void MainWindow::updateActions()
{
172
173
174
175
    Interface *iface = qobject_cast<Interface*>(m_part);
    m_newAction->setEnabled(!iface->isBusy());
    m_openAction->setEnabled(!iface->isBusy());
    m_recentFilesAction->setEnabled(!iface->isBusy());
176
177
}

178
179
void MainWindow::editKeyBindings()
{
180
181
182
    KShortcutsDialog dlg(KShortcutsEditor::AllActions, KShortcutsEditor::LetterShortcutsAllowed, this);
    dlg.addCollection(actionCollection());
    dlg.addCollection(m_part->actionCollection());
183

184
    dlg.configure();
185
186
187
188
}

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

191
192
    QWeakPointer<KEditToolBar> dlg = new KEditToolBar(factory(), this);
    dlg.data()->exec();
193

194
    createGUI(m_part);
195

196
    applyMainWindowSettings(KGlobal::config()->group(QLatin1String("MainWindow")));
197

198
    delete dlg.data();
Henrique Pinto's avatar
Henrique Pinto committed
199
200
201
202
}

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

211
void MainWindow::openUrl(const KUrl& url)
212
{
213
214
    if (!url.isEmpty()) {
        m_part->setArguments(m_openArgs);
215

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

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

Henrique Pinto's avatar
Henrique Pinto committed
233
234
void MainWindow::quit()
{
235
    close();
236
237
238
239
}

void MainWindow::newArchive()
{
240
241
    Interface *iface = qobject_cast<Interface*>(m_part);
    Q_ASSERT(iface);
Raphael Kubo da Costa's avatar
const++    
Raphael Kubo da Costa committed
242
243

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

245
    kDebug() << "Supported mimetypes are" << mimeTypes.join( QLatin1String( " " ));
246

247
248
249
    const KUrl saveFileUrl =
        KFileDialog::getSaveUrl(KUrl("kfiledialog:///ArkNewDir"),
                                mimeTypes.join(QLatin1String(" ")));
250

251
    m_openArgs.metaData()[QLatin1String( "createNewArchive" )] = QLatin1String( "true" );
252

253
    openUrl(saveFileUrl);
254

255
256
    m_openArgs.metaData().remove(QLatin1String( "showExtractDialog" ));
    m_openArgs.metaData().remove(QLatin1String( "createNewArchive" ));
Henrique Pinto's avatar
Henrique Pinto committed
257
}