mainwindow.cpp 7.21 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 <QPointer>
45

46
47
static bool isValidArchiveDrag(const QMimeData *data)
{
48
49
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("arkui.rc");
Henrique Pinto's avatar
Henrique Pinto committed
57

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

61
62
63
64
    if (!initialGeometrySet()) {
        resize(640, 480);
    }
    setAutoSaveSettings("MainWindow");
65

66
    setAcceptDrops(true);
Henrique Pinto's avatar
Henrique Pinto committed
67
68
69
70
}

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

78
void MainWindow::dragEnterEvent(QDragEnterEvent * event)
79
{
80
    kDebug() << event;
81

82
83
    Interface *iface = qobject_cast<Interface*>(m_part);
    if (iface->isBusy()) return;
84

85
86
87
88
    if (event->source() == NULL &&
            isValidArchiveDrag(event->mimeData()))
        event->acceptProposedAction();
    return;
89
90
}

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

95
96
    Interface *iface = qobject_cast<Interface*>(m_part);
    if (iface->isBusy()) return;
97

98
99
100
101
102
103
104
    if (event->source() == NULL &&
            isValidArchiveDrag(event->mimeData()))
        event->acceptProposedAction();

    //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));
105
106
}

107
void MainWindow::dragMoveEvent(QDragMoveEvent * event)
108
{
109
    kDebug() << event;
110

111
112
    Interface *iface = qobject_cast<Interface*>(m_part);
    if (iface->isBusy()) return;
113

114
115
116
    if (event->source() == NULL &&
            isValidArchiveDrag(event->mimeData()))
        event->acceptProposedAction();
117
118
}

119
120
bool MainWindow::loadPart()
{
121
    KPluginFactory *factory = KPluginLoader("arkpart").factory();
122
123
124
125
126
127
128
    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;
    }
129

130
131
132
    m_part->setObjectName("ArkPart");
    setCentralWidget(m_part->widget());
    createGUI(m_part);
133

134
135
    connect(m_part, SIGNAL(busy()), this, SLOT(updateActions()));
    connect(m_part, SIGNAL(ready()), this, SLOT(updateActions()));
136
    connect(m_part, SIGNAL(quit()), this, SLOT(quit()));
137

138
    return true;
139
140
}

Henrique Pinto's avatar
Henrique Pinto committed
141
142
void MainWindow::setupActions()
{
143
144
145
    m_newAction = KStandardAction::openNew(this, SLOT(newArchive()), actionCollection());
    m_openAction = KStandardAction::open(this, SLOT(openArchive()), actionCollection());
    KStandardAction::quit(this, SLOT(quit()), actionCollection());
146

147
148
149
150
151
152
153
154
155
    m_recentFilesAction = KStandardAction::openRecent(this, SLOT(openUrl(const KUrl&)), actionCollection());
    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()));
156

157
    createStandardStatusBarAction();
158

159
160
    KStandardAction::configureToolbars(this, SLOT(editToolbars()), actionCollection());
    KStandardAction::keyBindings(this, SLOT(editKeyBindings()), actionCollection());
161
162
}

163
164
void MainWindow::updateActions()
{
165
166
167
168
    Interface *iface = qobject_cast<Interface*>(m_part);
    m_newAction->setEnabled(!iface->isBusy());
    m_openAction->setEnabled(!iface->isBusy());
    m_recentFilesAction->setEnabled(!iface->isBusy());
169
170
}

171
172
void MainWindow::editKeyBindings()
{
173
174
175
    KShortcutsDialog dlg(KShortcutsEditor::AllActions, KShortcutsEditor::LetterShortcutsAllowed, this);
    dlg.addCollection(actionCollection());
    dlg.addCollection(m_part->actionCollection());
176

177
    dlg.configure();
178
179
180
181
}

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

184
185
    QPointer<KEditToolBar> dlg = new KEditToolBar(factory(), this);
    dlg->exec();
186

187
    createGUI(m_part);
188

189
    applyMainWindowSettings(KGlobal::config()->group(QLatin1String("MainWindow")));
190
191

    delete dlg;
Henrique Pinto's avatar
Henrique Pinto committed
192
193
194
195
}

void MainWindow::openArchive()
{
196
197
198
    Interface *iface = qobject_cast<Interface*>(m_part);
    Q_ASSERT(iface);
    KUrl url = KFileDialog::getOpenUrl(KUrl("kfiledialog:///ArkOpenDir"),
199
                                       Kerfuffle::supportedMimeTypes().join(" "),
200
201
                                       this);
    openUrl(url);
202
}
Henrique Pinto's avatar
Henrique Pinto committed
203

204
void MainWindow::openUrl(const KUrl& url)
205
{
206
207
    if (!url.isEmpty()) {
        m_part->setArguments(m_openArgs);
208

209
210
211
212
213
214
        if (m_part->openUrl(url)) {
            m_recentFilesAction->addUrl(url);
        } else {
            m_recentFilesAction->removeUrl(url);
        }
    }
Henrique Pinto's avatar
Henrique Pinto committed
215
216
}

217
218
void MainWindow::setShowExtractDialog(bool option)
{
219
220
221
222
223
    if (option) {
        m_openArgs.metaData()["showExtractDialog"] = "true";
    } else {
        m_openArgs.metaData().remove("showExtractDialog");
    }
224
225
}

Henrique Pinto's avatar
Henrique Pinto committed
226
227
void MainWindow::quit()
{
228
    close();
229
230
231
232
}

void MainWindow::newArchive()
{
233
234
    Interface *iface = qobject_cast<Interface*>(m_part);
    Q_ASSERT(iface);
235
    QStringList mimeTypes = Kerfuffle::supportedWriteMimeTypes();
236

237
    kDebug() << "Supported mimetypes are" << mimeTypes.join(" ");
238

239
240
    QString saveFile = KFileDialog::getSaveFileName(KUrl("kfiledialog:///ArkNewDir"),
                       mimeTypes.join(" "));
241

242
    m_openArgs.metaData()["createNewArchive"] = "true";
243

244
    openUrl(KUrl(saveFile));
245

246
247
    m_openArgs.metaData().remove("showExtractDialog");
    m_openArgs.metaData().remove("createNewArchive");
Henrique Pinto's avatar
Henrique Pinto committed
248
}