mirror of
https://bitbucket.org/smil3y/kde-playground.git
synced 2025-02-24 02:42:51 +00:00
227 lines
7.2 KiB
C++
227 lines
7.2 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.
|
||
|
*/
|
||
|
|
||
|
#ifndef KARCHIVEMANAGER_H
|
||
|
#define KARCHIVEMANAGER_H
|
||
|
|
||
|
#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;
|
||
|
time_t atime;
|
||
|
time_t ctime;
|
||
|
time_t mtime;
|
||
|
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, provides plain GZip and BZip2 support aswell
|
||
|
|
||
|
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 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 info for archive entry
|
||
|
KArchiveInfo info(const QString &path) const;
|
||
|
|
||
|
//! @brief Report if path is writable archive
|
||
|
bool writable() const;
|
||
|
//! @brief Report if path is supported archive
|
||
|
static bool supported(const QString &path);
|
||
|
|
||
|
//! @brief Read gzip compressed path into buffer
|
||
|
static bool gzipRead(const QString &path, QByteArray &buffer);
|
||
|
//! @brief Compress data as gzip and write it to path
|
||
|
static bool gzipWrite(const QString &path, const QByteArray &data);
|
||
|
//! @brief Read bzip2 compressed path into buffer
|
||
|
static bool bzipRead(const QString &path, QByteArray &buffer);
|
||
|
//! @brief Compress data as bzip2 and write it to path
|
||
|
static bool bzipWrite(const QString &path, QByteArray data);
|
||
|
|
||
|
private:
|
||
|
KArchiveManagerPrivate *d;
|
||
|
};
|
||
|
|
||
|
class KArchiveModelPrivate;
|
||
|
|
||
|
/*!
|
||
|
Custom item model for displaying archives in tree views (@p QTreeView)
|
||
|
|
||
|
Example:
|
||
|
\code
|
||
|
QTreeView view;
|
||
|
KArchiveModel model;
|
||
|
KArchiveManager archive("/home/joe/archive.tar.gz");
|
||
|
|
||
|
view.setModel(model);
|
||
|
model.loadArchive(&archive);
|
||
|
\endcode
|
||
|
|
||
|
@see KArchiveManager, KArchiveInfo
|
||
|
*/
|
||
|
class KArchiveModel : public QStandardItemModel {
|
||
|
Q_OBJECT
|
||
|
|
||
|
public:
|
||
|
KArchiveModel(QObject *parent = Q_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;
|
||
|
|
||
|
Q_SIGNALS:
|
||
|
//! @brief Signals load was started
|
||
|
void loadStarted();
|
||
|
//! @brief Signals load was finished
|
||
|
void loadFinished();
|
||
|
|
||
|
private Q_SLOTS:
|
||
|
void slotLoadFinished();
|
||
|
|
||
|
private:
|
||
|
KArchiveModelPrivate *d;
|
||
|
};
|
||
|
|
||
|
Q_DECLARE_METATYPE(KArchiveInfo);
|
||
|
|
||
|
#endif // KARCHIVEMANAGER_H
|