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
|
|
|
#include <QMetaEnum>
|
|
|
|
|
|
|
|
Arc::FileType Arc::getFileType(int n) {
|
|
|
|
if(int t = QMetaEnum::fromType<Arc::FileType>().value(n); n != -1) {
|
|
|
|
return Arc::FileType(t);
|
|
|
|
} else {
|
2019-10-04 10:37:34 -07:00
|
|
|
throw EnumError("invalid file type");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Arc::ArchiveType Arc::getArchiveType(std::istream &st) noexcept {
|
|
|
|
Arc::ArchiveType ret = Arc::ArcNone;
|
|
|
|
try {
|
|
|
|
auto pos = st.tellg();
|
|
|
|
auto magic = readBytes<4>(st);
|
|
|
|
if(magic == std::array{'P', 'A', 'C', 'K'}) {
|
|
|
|
ret = Arc::ArcPack;
|
|
|
|
} else if(magic == std::array{'W', 'A', 'D', '2'}) {
|
|
|
|
ret = Arc::ArcWad2;
|
|
|
|
}
|
|
|
|
st.seekg(pos);
|
|
|
|
} catch(...) {
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ArcDir Arc::readArchive(std::istream &st) {
|
|
|
|
switch(Arc::getArchiveType(st)) {
|
|
|
|
case Arc::ArcPack: return readPack(st);
|
|
|
|
case Arc::ArcWad2: return readWad2(st);
|
|
|
|
case Arc::ArcNone: throw FileFormatError("not an archive");
|
2019-10-03 12:17:16 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-03 14:06:05 -07:00
|
|
|
ArcDir::iterator ArcDir::findNode(std::string const &name) {
|
|
|
|
return std::find_if(begin(), end(), [&name](ArcNode const &node) {
|
|
|
|
return node.name == name;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
ArcNode::ArcNode(ArcDir &&f, std::string &&n, Arc::FileType t) :
|
|
|
|
super_type(std::move(f)),
|
2019-10-03 12:17:16 -07:00
|
|
|
name(std::move(n)),
|
2019-10-03 14:06:05 -07:00
|
|
|
type(t)
|
2019-10-03 12:17:16 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-10-03 14:06:05 -07:00
|
|
|
ArcNode::ArcNode(ArcFile &&f, std::string &&n, Arc::FileType t) :
|
|
|
|
super_type(std::move(f)),
|
2019-10-03 12:17:16 -07:00
|
|
|
name(std::move(n)),
|
2019-10-03 14:06:05 -07:00
|
|
|
type(t)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-10-04 10:37:34 -07:00
|
|
|
ArcModel::ArcModel(ArcDir const *root, QObject *parent) :
|
2019-10-03 14:06:05 -07:00
|
|
|
QAbstractItemModel{parent},
|
|
|
|
m_root{root}
|
2019-10-03 12:17:16 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-10-04 10:37:34 -07:00
|
|
|
ArcModel::~ArcModel() {
|
2019-10-03 14:06:05 -07:00
|
|
|
}
|
|
|
|
|
2019-10-04 10:37:34 -07:00
|
|
|
QVariant ArcModel::data(QModelIndex const &index, int role) const {
|
2019-10-03 14:06:05 -07:00
|
|
|
if(!index.isValid()) {
|
|
|
|
return QVariant{};
|
|
|
|
}
|
|
|
|
|
2019-10-04 10:37:34 -07:00
|
|
|
auto node = static_cast<ArcNode *>(index.internalPointer());
|
2019-10-03 14:06:05 -07:00
|
|
|
|
|
|
|
switch(role) {
|
|
|
|
case Qt::DecorationRole:
|
|
|
|
if(index.column() == Arc::ColumnName) {
|
|
|
|
auto icon =
|
2019-10-04 10:37:34 -07:00
|
|
|
std::holds_alternative<ArcDir>(*node) ? "folder"
|
2019-10-03 14:06:05 -07:00
|
|
|
: "text-x-generic";
|
|
|
|
return QVariant{QIcon::fromTheme(icon)};
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Qt::DisplayRole:
|
|
|
|
switch(index.column()) {
|
|
|
|
case Arc::ColumnSize:
|
2019-10-04 10:37:34 -07:00
|
|
|
if(auto file = std::get_if<ArcFile>(node)) {
|
2019-10-03 14:06:05 -07:00
|
|
|
return QVariant{QString::number(file->size())};
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Arc::ColumnName:
|
|
|
|
return QVariant{tr(node->name.data())};
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return QVariant{};
|
|
|
|
}
|
|
|
|
|
2019-10-04 10:37:34 -07:00
|
|
|
Qt::ItemFlags ArcModel::flags(QModelIndex const &index) const {
|
2019-10-03 14:06:05 -07:00
|
|
|
if(!index.isValid()) {
|
|
|
|
return Qt::NoItemFlags;
|
|
|
|
} else {
|
|
|
|
return Qt::ItemIsSelectable |
|
|
|
|
Qt::ItemIsDragEnabled |
|
|
|
|
Qt::ItemIsEnabled |
|
|
|
|
Qt::ItemNeverHasChildren;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-04 10:37:34 -07:00
|
|
|
QVariant ArcModel::headerData(int section,
|
2019-10-03 14:06:05 -07:00
|
|
|
Qt::Orientation orientation,
|
|
|
|
int role) const {
|
|
|
|
if(orientation == Qt::Horizontal && role == Qt::DisplayRole) {
|
|
|
|
switch(section) {
|
|
|
|
case Arc::ColumnSize: return QVariant{tr("Size")};
|
|
|
|
case Arc::ColumnName: return QVariant{tr("Name")};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return QVariant{};
|
|
|
|
}
|
|
|
|
|
2019-10-04 10:37:34 -07:00
|
|
|
QModelIndex ArcModel::index(int row,
|
2019-10-03 14:06:05 -07:00
|
|
|
int col,
|
|
|
|
QModelIndex const &parent) const {
|
|
|
|
if(!hasIndex(row, col, parent) || row > m_root->size()) {
|
|
|
|
return QModelIndex{};
|
|
|
|
} else {
|
|
|
|
// despite index data being const, this function does not take a const
|
|
|
|
// pointer, which is very annoying!
|
2019-10-04 10:37:34 -07:00
|
|
|
return createIndex(row, col, const_cast<ArcNode *>(&m_root->at(row)));
|
2019-10-03 14:06:05 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-04 10:37:34 -07:00
|
|
|
QModelIndex ArcModel::parent(QModelIndex const &) const {
|
2019-10-03 14:06:05 -07:00
|
|
|
return QModelIndex{};
|
|
|
|
}
|
|
|
|
|
2019-10-04 10:37:34 -07:00
|
|
|
int ArcModel::rowCount(QModelIndex const &) const {
|
2019-10-03 14:06:05 -07:00
|
|
|
return m_root->size();
|
|
|
|
}
|
|
|
|
|
2019-10-04 10:37:34 -07:00
|
|
|
int ArcModel::columnCount(QModelIndex const &) const {
|
2019-10-03 14:06:05 -07:00
|
|
|
return Arc::ColumnMax;
|
|
|
|
}
|
|
|
|
|
2019-10-03 12:17:16 -07:00
|
|
|
// EOF
|