1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
|
/*
* ark -- archiver for the KDE project
*
* Copyright (C) 2002-2003: Georg Robbers <Georg.Robbers@urz.uni-hd.de>
* 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>
*
* 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.
*
*/
#include "mainwindow.h"
#include "kerfuffle/archive.h"
#include "part/interface.h"
#include <KPluginLoader>
#include <KPluginFactory>
#include <KMessageBox>
#include <KApplication>
#include <KLocale>
#include <KActionCollection>
#include <KStandardAction>
#include <KFileDialog>
#include <KRecentFilesAction>
#include <KGlobal>
#include <KDebug>
#include <KEditToolBar>
#include <KShortcutsDialog>
#include <QDragEnterEvent>
#include <QDragMoveEvent>
#include <QWeakPointer>
static bool isValidArchiveDrag(const QMimeData *data)
{
return ((data->hasUrls()) && (data->urls().count() == 1));
}
MainWindow::MainWindow(QWidget *)
: KParts::MainWindow()
{
setXMLFile(QLatin1String( "arkui.rc" ));
setupActions();
statusBar();
if (!initialGeometrySet()) {
resize(640, 480);
}
setAutoSaveSettings(QLatin1String( "MainWindow" ));
setAcceptDrops(true);
}
MainWindow::~MainWindow()
{
if (m_recentFilesAction) {
m_recentFilesAction->saveEntries(KGlobal::config()->group("Recent Files"));
}
delete m_part;
m_part = 0;
}
void MainWindow::dragEnterEvent(QDragEnterEvent * event)
{
kDebug() << event;
Interface *iface = qobject_cast<Interface*>(m_part);
if (iface->isBusy()) {
return;
}
if ((event->source() == NULL) &&
(isValidArchiveDrag(event->mimeData()))) {
event->acceptProposedAction();
}
return;
}
void MainWindow::dropEvent(QDropEvent * event)
{
kDebug() << event;
Interface *iface = qobject_cast<Interface*>(m_part);
if (iface->isBusy()) {
return;
}
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));
}
void MainWindow::dragMoveEvent(QDragMoveEvent * event)
{
kDebug() << event;
Interface *iface = qobject_cast<Interface*>(m_part);
if (iface->isBusy()) {
return;
}
if ((event->source() == NULL) &&
(isValidArchiveDrag(event->mimeData()))) {
event->acceptProposedAction();
}
}
bool MainWindow::loadPart()
{
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."));
kWarning() << "Error loading Ark KPart: " << loader.errorString();
return false;
}
m_part->setObjectName( QLatin1String("ArkPart" ));
setCentralWidget(m_part->widget());
createGUI(m_part);
connect(m_part, SIGNAL(busy()), this, SLOT(updateActions()));
connect(m_part, SIGNAL(ready()), this, SLOT(updateActions()));
connect(m_part, SIGNAL(quit()), this, SLOT(quit()));
return true;
}
void MainWindow::setupActions()
{
m_newAction = KStandardAction::openNew(this, SLOT(newArchive()), actionCollection());
m_openAction = KStandardAction::open(this, SLOT(openArchive()), actionCollection());
KStandardAction::quit(this, SLOT(quit()), actionCollection());
m_recentFilesAction = KStandardAction::openRecent(this, SLOT(openUrl(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()));
createStandardStatusBarAction();
KStandardAction::configureToolbars(this, SLOT(editToolbars()), actionCollection());
KStandardAction::keyBindings(this, SLOT(editKeyBindings()), actionCollection());
}
void MainWindow::updateActions()
{
Interface *iface = qobject_cast<Interface*>(m_part);
m_newAction->setEnabled(!iface->isBusy());
m_openAction->setEnabled(!iface->isBusy());
m_recentFilesAction->setEnabled(!iface->isBusy());
}
void MainWindow::editKeyBindings()
{
KShortcutsDialog dlg(KShortcutsEditor::AllActions, KShortcutsEditor::LetterShortcutsAllowed, this);
dlg.addCollection(actionCollection());
dlg.addCollection(m_part->actionCollection());
dlg.configure();
}
void MainWindow::editToolbars()
{
saveMainWindowSettings(KGlobal::config()->group(QLatin1String("MainWindow")));
QWeakPointer<KEditToolBar> dlg = new KEditToolBar(factory(), this);
dlg.data()->exec();
createGUI(m_part);
applyMainWindowSettings(KGlobal::config()->group(QLatin1String("MainWindow")));
delete dlg.data();
}
void MainWindow::openArchive()
{
Interface *iface = qobject_cast<Interface*>(m_part);
Q_ASSERT(iface);
const KUrl url = KFileDialog::getOpenUrl(KUrl("kfiledialog:///ArkOpenDir"),
Kerfuffle::supportedMimeTypes().join( QLatin1String( " " )),
this);
openUrl(url);
}
void MainWindow::openUrl(const KUrl& url)
{
if (!url.isEmpty()) {
m_part->setArguments(m_openArgs);
if (m_part->openUrl(url)) {
m_recentFilesAction->addUrl(url);
} else {
m_recentFilesAction->removeUrl(url);
}
}
}
void MainWindow::setShowExtractDialog(bool option)
{
if (option) {
m_openArgs.metaData()[QLatin1String( "showExtractDialog" )] = QLatin1String( "true" );
} else {
m_openArgs.metaData().remove(QLatin1String( "showExtractDialog" ));
}
}
void MainWindow::quit()
{
close();
}
void MainWindow::newArchive()
{
Interface *iface = qobject_cast<Interface*>(m_part);
Q_ASSERT(iface);
const QStringList mimeTypes = Kerfuffle::supportedWriteMimeTypes();
kDebug() << "Supported mimetypes are" << mimeTypes.join( QLatin1String( " " ));
const KUrl saveFileUrl =
KFileDialog::getSaveUrl(KUrl("kfiledialog:///ArkNewDir"),
mimeTypes.join(QLatin1String(" ")));
m_openArgs.metaData()[QLatin1String( "createNewArchive" )] = QLatin1String( "true" );
openUrl(saveFileUrl);
m_openArgs.metaData().remove(QLatin1String( "showExtractDialog" ));
m_openArgs.metaData().remove(QLatin1String( "createNewArchive" ));
}
|