a-la-karte/src/mediamanager.cpp

146 lines
3.8 KiB
C++
Raw Normal View History

#include "mediamanager.h"
#include "game.h"
#include "screenshotmodel.h"
#include <QDateTime>
#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QStandardPaths>
MediaManager::MediaManager(QObject *parent)
: QObject(parent)
{
}
QString MediaManager::screenshotsBasePath() const
{
return QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + QStringLiteral("/media/screenshots");
}
QString MediaManager::gameId(Game *game) const
{
return game ? game->id() : QString();
}
QString MediaManager::ensureDirectory(const QString &path) const
{
if (path.isEmpty()) {
return {};
}
QDir dir(path);
if (!dir.exists()) {
QDir parent;
parent.mkpath(path);
}
return QDir(path).absolutePath();
}
QString MediaManager::gameScreenshotsFolderPath(Game *game) const
{
const QString id = gameId(game);
if (id.isEmpty()) {
return {};
}
return screenshotsBasePath() + QLatin1Char('/') + id;
}
QString MediaManager::screenshotsFolderPath(Game *game) const
{
const QString path = gameScreenshotsFolderPath(game);
return ensureDirectory(path);
}
QUrl MediaManager::screenshotsFolderUrl(Game *game) const
{
const QString path = screenshotsFolderPath(game);
if (path.isEmpty()) {
return {};
}
return QUrl::fromLocalFile(path);
}
QObject *MediaManager::screenshotsModel(Game *game)
{
const QString id = gameId(game);
if (id.isEmpty()) {
return nullptr;
}
if (m_models.contains(id) && m_models.value(id)) {
return m_models.value(id);
}
auto *model = new ScreenshotModel(this);
model->setDirectoryPath(screenshotsFolderPath(game));
m_models.insert(id, model);
return model;
}
QString MediaManager::uniqueDestinationPath(const QString &folderPath, const QString &fileName) const
{
const QString baseName = QFileInfo(fileName).completeBaseName();
const QString suffix = QFileInfo(fileName).suffix();
const QString cleanBase = baseName.isEmpty() ? QStringLiteral("screenshot") : baseName;
const QString cleanSuffix = suffix.isEmpty() ? QStringLiteral("png") : suffix;
QString candidate = folderPath + QLatin1Char('/') + cleanBase + QLatin1Char('.') + cleanSuffix;
if (!QFileInfo::exists(candidate)) {
return candidate;
}
for (int i = 1; i < 10000; ++i) {
candidate = folderPath + QLatin1Char('/') + cleanBase + QLatin1Char('_') + QString::number(i) + QLatin1Char('.') + cleanSuffix;
if (!QFileInfo::exists(candidate)) {
return candidate;
}
}
return folderPath + QLatin1Char('/') + cleanBase + QLatin1Char('_') + QString::number(QDateTime::currentMSecsSinceEpoch()) + QLatin1Char('.') + cleanSuffix;
}
bool MediaManager::importScreenshot(Game *game, const QUrl &sourceUrl)
{
if (!game) {
return false;
}
if (!sourceUrl.isLocalFile()) {
Q_EMIT importError(game, tr("Only local files are supported"));
return false;
}
const QString srcPath = sourceUrl.toLocalFile();
QFileInfo srcInfo(srcPath);
if (!srcInfo.exists() || !srcInfo.isFile()) {
Q_EMIT importError(game, tr("Source file does not exist"));
return false;
}
const QString folder = screenshotsFolderPath(game);
if (folder.isEmpty()) {
Q_EMIT importError(game, tr("Failed to create screenshots folder"));
return false;
}
const QString destPath = uniqueDestinationPath(folder, srcInfo.fileName());
if (!QFile::copy(srcPath, destPath)) {
Q_EMIT importError(game, tr("Failed to copy file"));
return false;
}
const QString id = gameId(game);
if (m_models.contains(id) && m_models.value(id)) {
m_models.value(id)->refresh();
}
Q_EMIT screenshotsImported(game, 1);
return true;
}