mirror of
https://bitbucket.org/smil3y/kde-playground.git
synced 2025-02-23 10:22:50 +00:00
230 lines
6.9 KiB
C++
230 lines
6.9 KiB
C++
/* This file is part of KArchiveManager
|
|
Copyright (C) 2018 Ivailo Monev <xakepa10@gmail.com>
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
modify it under the terms of the GNU Library General Public
|
|
License version 2, as published by the Free Software Foundation.
|
|
|
|
This library 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
|
|
Library General Public License for more details.
|
|
|
|
You should have received a copy of the GNU Library General Public License
|
|
along with this library; see the file COPYING.LIB. If not, write to
|
|
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
Boston, MA 02110-1301, USA.
|
|
*/
|
|
|
|
#include <QFileInfo>
|
|
#include <QDir>
|
|
#include <QMessageBox>
|
|
#include <QFileDialog>
|
|
#include <KFileDialog>
|
|
#include <KDebug>
|
|
#include <KLocale>
|
|
#include <ksettings.h>
|
|
|
|
#include "karchivemanager.hpp"
|
|
#include "karchiveapp.hpp"
|
|
#include "ui_karchiveapp.h"
|
|
|
|
class KArchiveAppPrivate
|
|
{
|
|
public:
|
|
Ui_KArchiveAppWindow ui;
|
|
|
|
KArchiveModel m_model;
|
|
KArchive *m_archive;
|
|
};
|
|
|
|
KArchiveApp::KArchiveApp()
|
|
: d(new KArchiveAppPrivate())
|
|
{
|
|
// setupUi() will set the object name required by KSettings save/restore
|
|
d->ui.setupUi(this);
|
|
|
|
d->ui.archiveView->setModel(&d->m_model);
|
|
|
|
KSettings settings("karchivemanagerrc", KSettings::SimpleConfig);
|
|
settings.restore(this);
|
|
|
|
connect(d->ui.actionOpen, SIGNAL(triggered()), this, SLOT(slotOpenAction()));
|
|
d->ui.actionOpen->setShortcut(QKeySequence::Open);
|
|
connect(d->ui.actionQuit, SIGNAL(triggered()), this, SLOT(slotQuitAction()));
|
|
d->ui.actionQuit->setShortcut(QKeySequence::Quit);
|
|
|
|
connect(d->ui.actionAdd, SIGNAL(triggered()), this, SLOT(slotAddAction()));
|
|
connect(d->ui.actionRemove, SIGNAL(triggered()), this, SLOT(slotRemoveAction()));
|
|
connect(d->ui.actionExtract, SIGNAL(triggered()), this, SLOT(slotExtractAction()));
|
|
|
|
connect(d->ui.archiveView->selectionModel(), SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
|
|
this, SLOT(slotSelectionChanged(const QItemSelection&, const QItemSelection&)));
|
|
connect(&d->m_model, SIGNAL(loadStarted()), this, SLOT(slotLoadStarted()));
|
|
connect(&d->m_model, SIGNAL(loadFinished()), this, SLOT(slotLoadFinished()));
|
|
}
|
|
|
|
KArchiveApp::~KArchiveApp()
|
|
{
|
|
KSettings settings("karchivemanagerrc", KSettings::SimpleConfig);
|
|
settings.save(this);
|
|
|
|
if (d->m_archive) {
|
|
delete d->m_archive;
|
|
}
|
|
delete d;
|
|
}
|
|
|
|
void KArchiveApp::changePath(const QString path)
|
|
{
|
|
if (d->m_archive) {
|
|
delete d->m_archive;
|
|
}
|
|
|
|
d->m_archive = new KArchive(path);
|
|
d->m_model.loadArchive(d->m_archive);
|
|
|
|
statusBar()->showMessage(path);
|
|
|
|
const bool iswritable = d->m_archive->isWritable();
|
|
const bool isreadable = d->m_archive->isReadable();
|
|
d->ui.actionAdd->setEnabled(iswritable);
|
|
d->ui.actionRemove->setEnabled(iswritable);
|
|
d->ui.actionExtract->setEnabled(isreadable);
|
|
}
|
|
|
|
void KArchiveApp::slotOpenAction()
|
|
{
|
|
QString mimespattern;
|
|
QString allmimespattern;
|
|
foreach(const QString &mimetype, KArchive::readableMimeTypes()) {
|
|
const KMimeType::Ptr mime = KMimeType::mimeType(mimetype);
|
|
if (mime) {
|
|
if (!mimespattern.isEmpty()) {
|
|
mimespattern.append("\n");
|
|
}
|
|
if (!allmimespattern.isEmpty()) {
|
|
allmimespattern.append(" ");
|
|
}
|
|
const QString patterns = mime->patterns().join(" ");
|
|
mimespattern.append(patterns + QLatin1Char('|') + mime->comment());
|
|
allmimespattern.append(patterns);
|
|
}
|
|
}
|
|
|
|
if (!allmimespattern.isEmpty()) {
|
|
mimespattern.prepend(allmimespattern + QLatin1Char('|') + i18n("All Archives") + "\n");
|
|
}
|
|
|
|
const QString path = KFileDialog::getOpenFileName(
|
|
KUrl("kfiledialog:///KArchiveManager"),
|
|
mimespattern,
|
|
this,
|
|
i18n("Archive path")
|
|
);
|
|
if (!path.isEmpty()) {
|
|
changePath(path);
|
|
}
|
|
}
|
|
|
|
void KArchiveApp::slotQuitAction()
|
|
{
|
|
qApp->quit();
|
|
}
|
|
|
|
void KArchiveApp::slotAddAction()
|
|
{
|
|
QFileDialog opendialog(this, windowTitle());
|
|
opendialog.setFileMode(QFileDialog::ExistingFiles);
|
|
opendialog.exec();
|
|
const QStringList selected = opendialog.selectedFiles();
|
|
if (!opendialog.result() || selected.isEmpty()) {
|
|
return;
|
|
}
|
|
|
|
kDebug() << "adding" << selected;
|
|
const QByteArray stripdir = opendialog.directory().path().toUtf8();
|
|
QString destdir;
|
|
foreach (const QModelIndex &item, d->ui.archiveView->selectionModel()->selectedIndexes()) {
|
|
destdir = d->m_model.dir(item);
|
|
}
|
|
d->m_archive->add(selected, stripdir + "/", destdir.toUtf8() + "/");
|
|
|
|
kDebug() << "reloading archive";
|
|
d->m_model.loadArchive(d->m_archive);
|
|
}
|
|
|
|
void KArchiveApp::slotRemoveAction()
|
|
{
|
|
QStringList selected;
|
|
foreach (const QModelIndex &item, d->ui.archiveView->selectionModel()->selectedIndexes()) {
|
|
selected += d->m_model.paths(item);
|
|
}
|
|
|
|
QMessageBox::StandardButton answer = QMessageBox::question(this, windowTitle(),
|
|
QApplication::tr("Are you sure you want to delete:\n\n%1?").arg(selected.join("\n")),
|
|
QMessageBox::No | QMessageBox::Yes);
|
|
if (answer != QMessageBox::Yes) {
|
|
return;
|
|
}
|
|
|
|
kDebug() << "removing" << selected;
|
|
d->m_archive->remove(selected);
|
|
|
|
kDebug() << "reloading archive";
|
|
d->m_model.loadArchive(d->m_archive);
|
|
}
|
|
|
|
void KArchiveApp::slotExtractAction()
|
|
{
|
|
QStringList selected;
|
|
foreach (const QModelIndex &item, d->ui.archiveView->selectionModel()->selectedIndexes()) {
|
|
selected += d->m_model.paths(item);
|
|
}
|
|
|
|
const QString destination = QFileDialog::getExistingDirectory(this, windowTitle());
|
|
if (destination.isEmpty()) {
|
|
return;
|
|
}
|
|
|
|
kDebug() << "extracting" << selected << "to" << destination;
|
|
d->m_archive->extract(selected, destination, true);
|
|
}
|
|
|
|
void KArchiveApp::slotSelectionChanged(const QItemSelection ¤t, const QItemSelection &previous)
|
|
{
|
|
Q_UNUSED(previous);
|
|
d->ui.menuAction->setEnabled(true);
|
|
|
|
if (current.indexes().isEmpty()) {
|
|
d->ui.menuAction->setEnabled(false);
|
|
}
|
|
}
|
|
|
|
void KArchiveApp::slotLoadStarted()
|
|
{
|
|
d->ui.menuAction->setEnabled(false);
|
|
d->ui.archiveView->setEnabled(false);
|
|
d->ui.progressBar->setRange(0, 0);
|
|
d->ui.progressBar->setVisible(true);
|
|
|
|
QHeaderView* header = d->ui.archiveView->header();
|
|
if (header) {
|
|
header->setVisible(false);
|
|
}
|
|
}
|
|
|
|
void KArchiveApp::slotLoadFinished()
|
|
{
|
|
d->ui.archiveView->setEnabled(true);
|
|
d->ui.progressBar->setRange(0, 1);
|
|
d->ui.progressBar->setVisible(false);
|
|
|
|
QHeaderView* header = d->ui.archiveView->header();
|
|
if (header && header->count() > 0) {
|
|
header->setVisible(true);
|
|
header->setResizeMode(0, QHeaderView::Stretch);
|
|
}
|
|
|
|
d->m_model.sort(1, Qt::AscendingOrder);
|
|
}
|