karchivemanager: port to karchive library

fun story - I turned most of this application into library used in Katana

Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
This commit is contained in:
Ivailo Monev 2024-05-17 14:08:16 +03:00
parent 0401c7ec83
commit 09bc8ddd10
5 changed files with 114 additions and 1300 deletions

View file

@ -9,30 +9,25 @@ if(${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR})
add_definitions(${QT_DEFINITIONS} ${KDE4_DEFINITIONS})
endif()
find_package(LibArchive 3.0.3)
set_package_properties(LibArchive PROPERTIES
DESCRIPTION "A library for dealing with a wide variety of archive file formats"
URL "https://www.libarchive.org/"
PURPOSE "Required for among others tar, tar.gz, tar.bz2 formats in KArchiveManager"
TYPE REQUIRED
)
include_directories(${LibArchive_INCLUDE_DIRS})
set(karchivemanager_sources
karchiveapp.cpp
karchivemanager.cpp
main.cpp
# TODO: only if system does not have strmode()
strmode.c
)
add_executable(karchivemanager ${karchivemanager_sources})
target_link_libraries(karchivemanager
KDE4::kdeui
KDE4::kio
${LibArchive_LIBRARIES}
KDE4::karchive
)
install(TARGETS karchivemanager DESTINATION ${KDE4_BIN_INSTALL_DIR})
install(PROGRAMS karchivemanager.desktop DESTINATION ${KDE4_XDG_APPS_INSTALL_DIR})
install(
TARGETS karchivemanager
DESTINATION ${KDE4_BIN_INSTALL_DIR}
)
install(
PROGRAMS karchivemanager.desktop
DESTINATION ${KDE4_XDG_APPS_INSTALL_DIR}
)

View file

@ -66,7 +66,7 @@ class KArchiveAppPrivate {
Ui_KArchiveAppWindow ui;
KArchiveModel m_model;
KArchiveManager *m_archive;
KArchive *m_archive;
};
KArchiveApp::KArchiveApp()
@ -109,15 +109,16 @@ void KArchiveApp::changePath(const QString path) {
delete d->m_archive;
}
d->m_archive = new KArchiveManager(path);
d->m_archive = new KArchive(path);
d->m_model.loadArchive(d->m_archive);
statusBar()->showMessage(path);
const bool iswritable = d->m_archive->writable();
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(iswritable);
d->ui.actionExtract->setEnabled(isreadable);
}
void KArchiveApp::slotOpenAction() {

File diff suppressed because it is too large Load diff

View file

@ -22,139 +22,7 @@
#include <QString>
#include <QStringList>
#include <QStandardItemModel>
#include <QDBusArgument>
#include <sys/types.h>
/*!
Archive entry information holder, valid object is obtained via @p KArchiveManager::info
@note It is up to the programmer to keep the integrity of the structure
@note D-Bus signature for the type is <b>(bxxxussssi)</b>
@warning The structure is not very portable (size, gid, uid)
@ingroup Types
@see KArchiveManager
@see https://dbus.freedesktop.org/doc/dbus-specification.html#container-types
*/
class KArchiveInfo {
public:
enum KArchiveType {
None = 0,
File = 1,
Directory = 2,
Link = 3,
Character = 4,
Block = 5,
Fifo = 6,
Socket = 7,
};
KArchiveInfo();
KArchiveInfo(const KArchiveInfo &info);
bool encrypted;
int64_t size;
int64_t gid;
int64_t uid;
mode_t mode;
QByteArray hardlink;
QByteArray symlink;
QByteArray pathname;
QByteArray mimetype;
KArchiveType type;
//! @brief Fancy encrypted for the purpose of widgets
QString fancyEncrypted() const;
//! @brief Fancy size for the purpose of widgets
QString fancySize() const;
//! @brief Fancy mode for the purpose of widgets
QString fancyMode() const;
//! @brief Fancy type for the purpose of widgets
QString fancyType() const;
//! @brief Fancy icon for the purpose of widgets
QIcon fancyIcon() const;
//! @brief Returns if the info is valid or not
bool isNull() const;
bool operator==(const KArchiveInfo &i) const;
KArchiveInfo &operator=(const KArchiveInfo &i);
};
#ifndef QT_NO_DEBUG_STREAM
QDebug operator<<(QDebug, const KArchiveInfo &i);
#endif
const QDBusArgument &operator<<(QDBusArgument &, const KArchiveInfo &i);
const QDBusArgument &operator>>(const QDBusArgument &, KArchiveInfo &i);
class KArchiveManagerPrivate;
/*!
Archive manager with support for many formats
Example:
\code
KArchiveManager archive("/home/joe/archive.tar.gz");
kDebug() << archive.list();
QDir::mkpath("/tmp/destination");
archive.extract("dir/in/archive/", "/tmp/destination");
archive.delete("file/in/archive.txt");
\endcode
@note Paths ending with "/" will be considered as directories
@warning The operations are done on temporary file, copy of the orignal, which after
successfull operation (add or remove) replaces the orignal thus if it is interrupted the
source may get corrupted
@see KArchiveInfo
@todo encrypted paths handling
@todo make listing consistent by faking dir info?
@todo set permissions on file after copy, same as the original
@todo error reporting
*/
class KArchiveManager {
public:
KArchiveManager(const QString &path);
~KArchiveManager();
/*!
@brief Add paths to the archive
@param strip string to remove from the start of every path
@param destination relative path where paths should be added to
*/
bool add(const QStringList &paths, const QByteArray &strip = "/", const QByteArray &destination = "") const;
//! @brief Remove paths from the archive
bool remove(const QStringList &paths) const;
/*!
@brief Extract paths to destination
@param destination existing directory, you can use @p QDir::mkpath(QString)
@param preserve preserve advanced attributes (ACL/ATTR)
*/
bool extract(const QStringList &paths, const QString &destination, bool preserve = true) const;
/*!
@brief List the content of the archive
@note may return empty list on both failure and success
@note some formats list directories, some do not
@todo report failure somehow
*/
QList<KArchiveInfo> list() const;
//! @brief Get information for path in archive
KArchiveInfo info(const QString &path) const;
//! @brief Report if archive is writable
bool writable() const;
//! @brief Report if path is supported archive
static bool supported(const QString &path);
private:
KArchiveManagerPrivate *d;
};
#include <KArchive>
class KArchiveModelPrivate;
@ -165,52 +33,49 @@ class KArchiveModelPrivate;
\code
QTreeView view;
KArchiveModel model;
KArchiveManager archive("/home/joe/archive.tar.gz");
KArchive archive("/home/joe/archive.tar.gz");
view.setModel(model);
model.loadArchive(&archive);
\endcode
@see KArchiveManager, KArchiveInfo
@todo sorting by column values is borked
*/
class KArchiveModel : public QStandardItemModel {
class KArchiveModel : public QStandardItemModel
{
Q_OBJECT
public:
KArchiveModel(QObject *parent = nullptr);
~KArchiveModel();
public:
KArchiveModel(QObject *parent = nullptr);
~KArchiveModel();
//! @brief Load archive into the model
bool loadArchive(const KArchiveManager *archive);
//! @brief Get path for index, propagates to parents to retrieve the full path
QString path(const QModelIndex &index) const;
/*!
@brief Get paths for index, propagates to childs to retrieve all sub-paths.
Usefull for obtaining the selected item paths for add, remove and extract
operations
*/
QStringList paths(const QModelIndex &index) const;
/*!
@brief Get parent directory for index. Usefull for obtaining the current dir
for add operations
*/
QString dir(const QModelIndex &index) const;
//! @brief Load archive into the model
bool loadArchive(const KArchive *archive);
//! @brief Get path for index, propagates to parents to retrieve the full path
QString path(const QModelIndex &index) const;
/*!
@brief Get paths for index, propagates to childs to retrieve all sub-paths.
Usefull for obtaining the selected item paths for add, remove and extract
operations
*/
QStringList paths(const QModelIndex &index) const;
/*!
@brief Get parent directory for index. Usefull for obtaining the current dir
for add operations
*/
QString dir(const QModelIndex &index) const;
Q_SIGNALS:
//! @brief Signals load was started
void loadStarted();
//! @brief Signals load was finished
void loadFinished();
Q_SIGNALS:
//! @brief Signals load was started
void loadStarted();
//! @brief Signals load was finished
void loadFinished();
private Q_SLOTS:
void slotLoadFinished();
private Q_SLOTS:
void slotLoadFinished();
private:
KArchiveModelPrivate *d;
private:
KArchiveModelPrivate *d;
};
Q_DECLARE_METATYPE(KArchiveInfo);
#endif // KARCHIVEMANAGER_H

View file

@ -1,143 +0,0 @@
/*-
* Copyright (c) 1990, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
// #include <sys/cdefs.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
void
strmode(mode_t mode, char *p)
{
/* print type */
switch (mode & S_IFMT) {
case S_IFDIR: /* directory */
*p++ = 'd';
break;
case S_IFCHR: /* character special */
*p++ = 'c';
break;
case S_IFBLK: /* block special */
*p++ = 'b';
break;
case S_IFREG: /* regular */
*p++ = '-';
break;
case S_IFLNK: /* symbolic link */
*p++ = 'l';
break;
case S_IFSOCK: /* socket */
*p++ = 's';
break;
#ifdef S_IFIFO
case S_IFIFO: /* fifo */
*p++ = 'p';
break;
#endif
#ifdef S_IFWHT
case S_IFWHT: /* whiteout */
*p++ = 'w';
break;
#endif
default: /* unknown */
*p++ = '?';
break;
}
/* usr */
if (mode & S_IRUSR)
*p++ = 'r';
else
*p++ = '-';
if (mode & S_IWUSR)
*p++ = 'w';
else
*p++ = '-';
switch (mode & (S_IXUSR | S_ISUID)) {
case 0:
*p++ = '-';
break;
case S_IXUSR:
*p++ = 'x';
break;
case S_ISUID:
*p++ = 'S';
break;
case S_IXUSR | S_ISUID:
*p++ = 's';
break;
}
/* group */
if (mode & S_IRGRP)
*p++ = 'r';
else
*p++ = '-';
if (mode & S_IWGRP)
*p++ = 'w';
else
*p++ = '-';
switch (mode & (S_IXGRP | S_ISGID)) {
case 0:
*p++ = '-';
break;
case S_IXGRP:
*p++ = 'x';
break;
case S_ISGID:
*p++ = 'S';
break;
case S_IXGRP | S_ISGID:
*p++ = 's';
break;
}
/* other */
if (mode & S_IROTH)
*p++ = 'r';
else
*p++ = '-';
if (mode & S_IWOTH)
*p++ = 'w';
else
*p++ = '-';
switch (mode & (S_IXOTH | S_ISVTX)) {
case 0:
*p++ = '-';
break;
case S_IXOTH:
*p++ = 'x';
break;
case S_ISVTX:
*p++ = 'T';
break;
case S_IXOTH | S_ISVTX:
*p++ = 't';
break;
}
*p++ = ' '; /* will be a '+' if ACL's implemented */
*p = '\0';
}