2019-10-03 12:17:16 -07:00
|
|
|
#include "quam/archive.h"
|
|
|
|
|
2019-10-04 10:37:34 -07:00
|
|
|
#include "quam/pack.h"
|
|
|
|
#include "quam/wad2.h"
|
|
|
|
|
|
|
|
#include <QIcon>
|
2019-10-03 12:17:16 -07:00
|
|
|
|
2019-10-05 16:58:19 -07:00
|
|
|
namespace Arc {
|
2019-10-08 20:43:35 -07:00
|
|
|
Option<ArcType> getArchiveType(std::istream &st) noexcept {
|
|
|
|
try {
|
|
|
|
auto pos = st.tellg();
|
|
|
|
auto magic = readBytes<4>(st);
|
|
|
|
st.seekg(pos);
|
|
|
|
if(magic == std::array{'P', 'A', 'C', 'K'}) {return ArcType::Pack;}
|
|
|
|
if(magic == std::array{'W', 'A', 'D', '2'}) {return ArcType::Wad2;}
|
|
|
|
} catch(...) {
|
|
|
|
}
|
|
|
|
return None;
|
|
|
|
}
|
2019-10-04 10:37:34 -07:00
|
|
|
|
2019-11-06 22:42:59 -08:00
|
|
|
Node::Node(std::string _name) :
|
2019-10-10 14:29:08 -07:00
|
|
|
name(_name),
|
2019-11-06 22:42:59 -08:00
|
|
|
parent(nullptr)
|
2019-10-10 14:29:08 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Node::~Node() {
|
|
|
|
}
|
|
|
|
|
2019-11-06 22:42:59 -08:00
|
|
|
Dir *Node::getRoot() {
|
|
|
|
if(!parent) {
|
|
|
|
return getDir();
|
|
|
|
} else {
|
|
|
|
Dir *rover = parent;
|
|
|
|
while(rover->parent) {
|
|
|
|
rover = rover->parent;
|
|
|
|
}
|
|
|
|
return rover;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Node *Node::findPath(std::string path) {
|
|
|
|
if(path.empty()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
Dir *rover;
|
|
|
|
|
|
|
|
// get the initial dir, which will either be:
|
|
|
|
// - the root if the path starts with /
|
|
|
|
// - the node itself
|
|
|
|
// - the node's current Dir (when it's not a Dir)
|
|
|
|
if(path.front() == '/') {
|
|
|
|
path.erase(path.begin());
|
|
|
|
rover = getRoot();
|
|
|
|
} else {
|
|
|
|
rover = getDir();
|
|
|
|
if(!rover) {
|
|
|
|
rover = parent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(path.empty()) {
|
|
|
|
// no file, so just the directory itself
|
|
|
|
return rover;
|
|
|
|
} else if(auto slash = path.find('/'); slash != std::string::npos) {
|
|
|
|
std::string prev;
|
|
|
|
prev = path.substr(0, slash);
|
|
|
|
path = path.substr(slash + 1);
|
|
|
|
|
|
|
|
if(prev == "..") {
|
|
|
|
// parent dir on ".."
|
|
|
|
rover = rover->parent ? rover->parent : rover;
|
|
|
|
} else if(prev != ".") {
|
|
|
|
if(auto node = rover->findNode(prev)) {
|
|
|
|
// we want a subdirectory since this path continues
|
|
|
|
rover = node->getDir();
|
|
|
|
} else {
|
|
|
|
// took a wrong turn
|
|
|
|
rover = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// recurse
|
|
|
|
return rover ? rover->findPath(path) : nullptr;
|
|
|
|
} else {
|
|
|
|
// must be a node inside a directory
|
|
|
|
return rover->findNode(path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Node::getPath(bool fromChild) const {
|
|
|
|
if(parent) {
|
|
|
|
return parent->getPath(true) + '/' + name;
|
|
|
|
} else if(fromChild) {
|
|
|
|
return std::string{};
|
|
|
|
} else {
|
|
|
|
return "/";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Dir::Dir(std::string _name) :
|
|
|
|
Node(_name),
|
|
|
|
m_data()
|
2019-10-10 14:29:08 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Dir::~Dir() {
|
|
|
|
}
|
|
|
|
|
2019-11-06 22:42:59 -08:00
|
|
|
UniquePtr<Node> const &Dir::emplaceBack(Node *node) {
|
|
|
|
node->parent = this;
|
|
|
|
return m_data.emplace_back(node);
|
|
|
|
}
|
|
|
|
|
2019-10-09 16:06:14 -07:00
|
|
|
Dir Dir::readArchive(std::istream &st, ArcType type) {
|
|
|
|
switch(type) {
|
2019-10-08 20:43:35 -07:00
|
|
|
case ArcType::Pack: return readPack(st);
|
|
|
|
case ArcType::Wad2: return readWad2(st);
|
|
|
|
}
|
2019-11-06 22:42:59 -08:00
|
|
|
Q_UNREACHABLE();
|
2019-10-04 10:37:34 -07:00
|
|
|
}
|
|
|
|
|
2019-10-10 14:29:08 -07:00
|
|
|
Node *Dir::findNode(std::string const &name) {
|
2019-11-06 22:42:59 -08:00
|
|
|
auto it = std::find_if(m_data.begin(), m_data.end(),
|
2019-10-10 14:29:08 -07:00
|
|
|
[&name](auto const &node) {
|
|
|
|
return node->name == name;
|
|
|
|
});
|
2019-11-06 22:42:59 -08:00
|
|
|
return it != m_data.end() ? &**it : nullptr;
|
2019-10-10 14:29:08 -07:00
|
|
|
}
|
|
|
|
|
2019-11-06 22:42:59 -08:00
|
|
|
File::File(std::string _name, QByteArray &&_data) :
|
|
|
|
Node(_name),
|
|
|
|
m_data(std::move(_data))
|
2019-10-10 14:29:08 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
File::~File() {
|
|
|
|
}
|
|
|
|
|
2019-10-09 16:06:14 -07:00
|
|
|
ArcInfo::ArcInfo(ArcType arcType) noexcept :
|
|
|
|
type{arcType}
|
|
|
|
{
|
|
|
|
switch(type) {
|
|
|
|
case ArcType::Pack:
|
|
|
|
pathMaxChars = 56;
|
|
|
|
validatorType = PackValidator::staticMetaObject;
|
|
|
|
break;
|
|
|
|
case ArcType::Wad2:
|
|
|
|
pathMaxChars = 16;
|
|
|
|
validatorType = Wad2Validator::staticMetaObject;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Arc::Arc(std::istream &st, QObject *parent) :
|
|
|
|
QObject{parent},
|
|
|
|
info{ArcInfo(orThrow(getArchiveType(st),
|
|
|
|
FileFormatError("not an archive")))},
|
|
|
|
root{Dir::readArchive(st, info.type)},
|
|
|
|
validator{qobject_cast<QValidator *>(info.validatorType
|
|
|
|
.newInstance(Q_ARG(QObject *,
|
|
|
|
this)))}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Arc::~Arc() {
|
|
|
|
}
|
|
|
|
|
2019-10-08 20:43:35 -07:00
|
|
|
Model::Model(QObject *parent) :
|
|
|
|
QAbstractItemModel{parent},
|
|
|
|
m_dir{nullptr}
|
|
|
|
{
|
|
|
|
}
|
2019-10-03 14:06:05 -07:00
|
|
|
|
2019-10-08 20:43:35 -07:00
|
|
|
Model::~Model() {
|
|
|
|
}
|
2019-10-03 12:17:16 -07:00
|
|
|
|
2019-10-08 20:43:35 -07:00
|
|
|
QVariant Model::data(QModelIndex const &index, int role) const {
|
|
|
|
if(!index.isValid()) {
|
|
|
|
return QVariant{};
|
|
|
|
}
|
2019-10-03 14:06:05 -07:00
|
|
|
|
2019-10-08 20:43:35 -07:00
|
|
|
auto node = static_cast<Node *>(index.internalPointer());
|
|
|
|
auto col = Column(index.column());
|
|
|
|
|
|
|
|
switch(role) {
|
|
|
|
case Qt::DecorationRole:
|
|
|
|
if(col == Column::Name) {
|
2019-10-10 14:29:08 -07:00
|
|
|
auto icon = node->getDir() ? "folder" : "text-x-generic";
|
2019-11-06 22:42:59 -08:00
|
|
|
return QVariant{QIcon::fromTheme(QString::fromUtf8(icon))};
|
2019-10-08 20:43:35 -07:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Qt::DisplayRole:
|
|
|
|
switch(col) {
|
|
|
|
case Column::Size:
|
2019-10-10 14:29:08 -07:00
|
|
|
if(auto file = node->getFile()) {
|
2019-10-08 20:43:35 -07:00
|
|
|
return QVariant{QString::number(file->size())};
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Column::Type:
|
2019-10-10 14:29:08 -07:00
|
|
|
if(auto file = node->getFile()) {
|
|
|
|
return QVariant{tr(enumToString(file->type))};
|
|
|
|
} else {
|
|
|
|
return QVariant{tr("Directory")};
|
|
|
|
}
|
2019-10-08 20:43:35 -07:00
|
|
|
case Column::Name:
|
|
|
|
return QVariant{tr(node->name.data())};
|
|
|
|
}
|
|
|
|
case Qt::UserRole:
|
|
|
|
return QVariant::fromValue(static_cast<void *>(node));
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2019-10-03 14:06:05 -07:00
|
|
|
return QVariant{};
|
|
|
|
}
|
|
|
|
|
2019-10-08 20:43:35 -07:00
|
|
|
Qt::ItemFlags Model::flags(QModelIndex const &index) const {
|
|
|
|
if(!index.isValid()) {
|
|
|
|
return Qt::NoItemFlags;
|
|
|
|
} else {
|
|
|
|
return Qt::ItemIsSelectable |
|
2019-10-08 20:58:52 -07:00
|
|
|
Qt::ItemIsDragEnabled |
|
|
|
|
Qt::ItemIsEnabled |
|
|
|
|
Qt::ItemNeverHasChildren;
|
2019-10-08 20:43:35 -07:00
|
|
|
}
|
2019-10-03 14:06:05 -07:00
|
|
|
}
|
|
|
|
|
2019-10-08 20:43:35 -07:00
|
|
|
QVariant Model::headerData(int section,
|
|
|
|
Qt::Orientation ori,
|
|
|
|
int role) const {
|
|
|
|
if(ori == Qt::Horizontal && role == Qt::DisplayRole) {
|
|
|
|
switch(Column(section)) {
|
|
|
|
case Column::Size: return QVariant{tr("Size")};
|
|
|
|
case Column::Type: return QVariant{tr("Type")};
|
|
|
|
case Column::Name: return QVariant{tr("Name")};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return QVariant{};
|
2019-10-03 14:06:05 -07:00
|
|
|
}
|
|
|
|
|
2019-10-08 20:43:35 -07:00
|
|
|
QModelIndex Model::index(int row,
|
|
|
|
int col,
|
|
|
|
QModelIndex const &parent) const {
|
|
|
|
if(!m_dir || !hasIndex(row, col, parent) || row > m_dir->size()) {
|
|
|
|
return QModelIndex{};
|
|
|
|
} else {
|
2019-11-06 22:42:59 -08:00
|
|
|
return createIndex(row, col, m_dir->at(row));
|
2019-10-03 14:06:05 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-08 20:43:35 -07:00
|
|
|
QModelIndex Model::parent(QModelIndex const &) const {
|
2019-10-03 14:06:05 -07:00
|
|
|
return QModelIndex{};
|
|
|
|
}
|
|
|
|
|
2019-10-08 20:43:35 -07:00
|
|
|
int Model::rowCount(QModelIndex const &) const {
|
|
|
|
return m_dir ? m_dir->size() : 0;
|
|
|
|
}
|
2019-10-05 16:58:19 -07:00
|
|
|
|
2019-10-08 20:43:35 -07:00
|
|
|
int Model::columnCount(QModelIndex const &) const {
|
|
|
|
return enumMax<Column>();
|
|
|
|
}
|
2019-10-05 16:58:19 -07:00
|
|
|
|
2019-10-08 20:43:35 -07:00
|
|
|
Dir *Model::dir() const {
|
|
|
|
return m_dir;
|
2019-10-05 16:58:19 -07:00
|
|
|
}
|
2019-10-03 14:06:05 -07:00
|
|
|
|
2019-11-06 22:42:59 -08:00
|
|
|
Node *Model::findPath(std::string path) {
|
|
|
|
return m_dir->findPath(path);
|
|
|
|
}
|
|
|
|
|
2019-10-09 16:06:14 -07:00
|
|
|
void Model::setDir(Dir *to) {
|
|
|
|
auto from = m_dir;
|
|
|
|
if(to != from) {
|
2019-10-08 20:58:52 -07:00
|
|
|
emit layoutAboutToBeChanged();
|
|
|
|
for(int row = 0, rows = rowCount(); row < rows; row++) {
|
|
|
|
for(int col = 0, cols = columnCount(); col < cols; col++) {
|
|
|
|
changePersistentIndex(index(row, col), QModelIndex{});
|
|
|
|
}
|
|
|
|
}
|
2019-10-09 16:06:14 -07:00
|
|
|
m_dir = to;
|
2019-10-08 20:43:35 -07:00
|
|
|
emit layoutChanged();
|
2019-10-09 16:06:14 -07:00
|
|
|
emit dirChanged(from, to);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Model::setDirToIndex(QModelIndex const &ind) {
|
|
|
|
auto node = static_cast<Node *>(ind.data(Qt::UserRole).value<void *>());
|
2019-10-10 14:29:08 -07:00
|
|
|
if(auto dir = node->getDir()) {
|
2019-10-09 16:06:14 -07:00
|
|
|
setDir(dir);
|
2019-10-08 20:43:35 -07:00
|
|
|
}
|
|
|
|
}
|
2019-10-10 14:29:08 -07:00
|
|
|
|
2019-11-06 22:42:59 -08:00
|
|
|
bool Model::goUp() {
|
2019-10-10 14:29:08 -07:00
|
|
|
if(m_dir && m_dir->parent) {
|
|
|
|
setDir(m_dir->parent);
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2019-11-06 22:42:59 -08:00
|
|
|
|
|
|
|
bool Model::goPath(std::string path) {
|
|
|
|
Node *node;
|
|
|
|
Dir *dir;
|
|
|
|
if(m_dir && (node = m_dir->findPath(path)) && (dir = node->getDir())) {
|
|
|
|
setDir(dir);
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2019-10-03 14:06:05 -07:00
|
|
|
}
|
|
|
|
|
2019-10-03 12:17:16 -07:00
|
|
|
// EOF
|