Skip to content

Commit

Permalink
Added more tests, replaced some unnecessary raw pointers with smart p…
Browse files Browse the repository at this point in the history
…ointers
  • Loading branch information
PhilInTheGaps committed Oct 2, 2023
1 parent e809a1a commit f2b5f36
Show file tree
Hide file tree
Showing 125 changed files with 2,502 additions and 1,388 deletions.
9 changes: 3 additions & 6 deletions app/ui/FileDialog/filedialogbackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ void FileDialogBackend::createFolder(const QString &folderName)
{
const auto path = FileUtils::fileInDir(folderName, currentDir());

FileAccess::getInstance()->createDirAsync(path).then(this, [this](FileResult *result) {
File::createDirAsync(path).then(this, [this](std::shared_ptr<FileResult> result) {
if (!result)
{
qCWarning(gmFileDialog()) << "Error: createDirAsync returned a null result!";
Expand All @@ -117,7 +117,6 @@ void FileDialogBackend::createFolder(const QString &folderName)
qCWarning(gmFileDialog()) << result->errorMessage();
}

result->deleteLater();
updateFileList();
});
}
Expand All @@ -132,7 +131,7 @@ void FileDialogBackend::updateFileList()
isLoading(true);

m_currentFuture = File::listAsync(currentDir(), !folderMode(), true);
m_currentFuture.then(this, [this](FileListResult *result) { onFileListReceived(result); })
m_currentFuture.then(this, [this](std::shared_ptr<FileListResult> result) { onFileListReceived(result); })
.onCanceled(this, [this]() {
qCDebug(gmFileDialog()) << "file list update was cancelled.";
isLoading(false);
Expand Down Expand Up @@ -160,7 +159,7 @@ void FileDialogBackend::stopCurrentRequest()
}
}

void FileDialogBackend::onFileListReceived(FileListResult *result)
void FileDialogBackend::onFileListReceived(std::shared_ptr<FileListResult> result)
{
auto countBefore = a_entries.count();

Expand All @@ -180,8 +179,6 @@ void FileDialogBackend::onFileListReceived(FileListResult *result)
{
emit entriesChanged();
}

result->deleteLater();
}

void FileDialogBackend::onCurrentDirChanged(const QString & /*dir*/)
Expand Down
4 changes: 2 additions & 2 deletions app/ui/FileDialog/filedialogbackend.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ public slots:
QStringList m_currentDir;
QStringList m_forwardFolders;

QFuture<FileListResult *> m_currentFuture;
QFuture<std::shared_ptr<FileListResult>> m_currentFuture;

void updateFileList();
void clearFileList();
void stopCurrentRequest();
void clearForward();

void onFileListReceived(FileListResult *result);
void onFileListReceived(std::shared_ptr<FileListResult> result);

private slots:
void onCurrentDirChanged(const QString &dir);
Expand Down
2 changes: 1 addition & 1 deletion app/ui/tools/combat_tracker/ListSpinBox.qml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Item {
padding: 0

onClosed: {
edit_mode = false
root.edit_mode = false
}

onAccepted: {
Expand Down
4 changes: 2 additions & 2 deletions misc/scripts/generate-coverage.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ filename_filtered="coverage_filtered.info"
if [ -z "$dir" ]; then echo "Please specify a directory!" && exit 1; fi

fastcov --include src/ --lcov -o $dir/$filename
lcov --remove $dir/$filename -o $dir/$filename_filtered '/usr/*' '*/**/tests/*' '*/**/thirdparty/*' '*/**/*_autogen/*'
lcov --remove $dir/$filename -o $dir/$filename_filtered '/usr/*' '*/**/tests/*' '*/**/*plugin_*.*' '*/**/*_qmltyperegistrations.cpp' '*/**/thirdparty/*' '*/**/*_autogen/*' --ignore-errors unused,unused

genhtml -o $dir/coverage_html $dir/$filename_filtered
genhtml --ignore-errors unmapped -o $dir/coverage_html $dir/$filename_filtered
2 changes: 1 addition & 1 deletion src/common/models/baseprojectitem.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class BaseProjectItem : public QObject
AUTO_PROPERTY_VAL2(bool, wasRenamed, false)

public:
explicit BaseProjectItem(const QString &name, QObject *parent = nullptr);
explicit BaseProjectItem(const QString &name, QObject *parent);

signals:
void wasEdited();
Expand Down
2 changes: 1 addition & 1 deletion src/common/models/treeitem.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class TreeItem : public BaseProjectItem
READ_PROPERTY(int, depth)

public:
explicit TreeItem(const QString &name, int depth, bool canToggle = true, QObject *parent = nullptr);
explicit TreeItem(const QString &name, int depth, bool canToggle, QObject *parent);

/// Whether a checkbox should be displayed.
[[nodiscard]] virtual auto isCheckable() const -> bool;
Expand Down
33 changes: 19 additions & 14 deletions src/common/utils/markdownutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,32 +52,37 @@ void MarkdownUtils::addMarkdownExtension(cmark_parser *parser, const QString &ex
#elif MARKDOWN_LIBRARY == MARKDOWN_DISCOUNT
auto MarkdownUtils::markdownToHtmlDiscount(const QString &markdown) -> QString
{
auto markdownStd = markdown.toStdString();
auto flags = MKD_AUTOLINK;
auto utf8 = markdown.toUtf8();

#ifdef MKD_NOLINKS
// Discount v2
auto *doc = gfm_string(markdownStd.c_str(), markdownStd.size(), flags);
auto flags = MKD_AUTOLINK;
auto *mmiot = mkd_string(utf8, utf8.size(), flags);

if (doc == nullptr) return u""_s;
if (mmiot == nullptr) return u""_s;

mkd_compile(doc, flags);
mkd_compile(mmiot, flags);
#else
// Discount v3
auto *doc = gfm_string(markdownStd.c_str(), markdownStd.size(), &flags);
auto *flags = mkd_flags();
mkd_set_flag_num(flags, MKD_AUTOLINK);
auto *mmiot = mkd_string(utf8, utf8.size(), flags);

if (doc == nullptr) return u""_s;
if (mmiot == nullptr) return u""_s;

mkd_compile(doc, &flags);
mkd_compile(mmiot, flags);
#endif

char *cstr_buff = nullptr;
const int size = mkd_document(doc, &cstr_buff);
char *cBuff = nullptr;
const int size = mkd_document(mmiot, &cBuff);

std::string html;
html.assign(cstr_buff, size);
mkd_cleanup(doc);
QByteArray html(cBuff, size); // copy raw data
mkd_cleanup(mmiot);

#ifndef MKD_NOLINKS
mkd_free_flags(flags);
#endif

return QString::fromStdString(html);
return QString(html);
}
#endif
13 changes: 0 additions & 13 deletions src/common/utils/networkutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,6 @@ void NetworkUtils::makeJsonRequest(QNetworkRequest &request)
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
}

auto NetworkUtils::queryToMap(const QUrlQuery &query) -> QMap<QString, QString>
{
QMap<QString, QString> map;
const auto itemPairs = query.queryItems();

for (const auto &pair : itemPairs)
{
map.insert(pair.first, pair.second);
}

return map;
}

auto NetworkUtils::isHttpUrl(QStringView url) -> bool
{
return url.startsWith("http://"_L1) || url.startsWith("https://"_L1);
Expand Down
2 changes: 0 additions & 2 deletions src/common/utils/networkutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,5 @@ class NetworkUtils
[[nodiscard]] static auto makeJsonRequest(const QUrl &url) -> QNetworkRequest;
static void makeJsonRequest(QNetworkRequest &request);

[[nodiscard]] static auto queryToMap(const QUrlQuery &query) -> QMap<QString, QString>;

[[nodiscard]] static auto isHttpUrl(QStringView url) -> bool;
};
22 changes: 10 additions & 12 deletions src/common/utils/stringutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,18 @@ using namespace Qt::Literals::StringLiterals;

constexpr int ROT13_PLACES = 13;

/**
* @brief Convert QPixmap to base64 encoded QString
*/
auto StringUtils::stringFromImage(const QPixmap &image) -> QString
auto StringUtils::imageFromString(const QByteArray &string) -> QImage
{
QByteArray bArray;
QBuffer buffer(&bArray);

buffer.open(QIODevice::WriteOnly);
image.save(&buffer, "JPEG");
auto data = QByteArray::fromBase64(removeFirst(string, JPG_BASE64_PREFIX.length()));
return QImage::fromData(data);
}

auto imageString = u"data:image/jpg;base64,"_s;
imageString.append(QString::fromLatin1(bArray.toBase64().data()));
return imageString;
auto StringUtils::pixmapFromString(const QByteArray &string) -> QPixmap
{
auto data = QByteArray::fromBase64(removeFirst(string, JPG_BASE64_PREFIX.length()));
QPixmap pixmap;
pixmap.loadFromData(data);
return pixmap;
}

/**
Expand Down
31 changes: 30 additions & 1 deletion src/common/utils/stringutils.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include <QImage>
#include <QLatin1StringView>
#include <QPixmap>
#include <QString>

Expand All @@ -13,7 +15,34 @@ struct ConstQString : public QLatin1String
class StringUtils
{
public:
static auto stringFromImage(const QPixmap &image) -> QString;
/// Convert QImage/QPixmap to base64 encoded QByteArray
template <typename T> static auto stringFromImage(const T &image) -> QByteArray
{
QByteArray data;
QBuffer buffer(&data);

buffer.open(QIODevice::WriteOnly);
image.save(&buffer, "JPEG");

return "data:image/jpg;base64," + data.toBase64();
}

static auto imageFromString(const QByteArray &string) -> QImage;
static auto pixmapFromString(const QByteArray &string) -> QPixmap;

template <typename T> constexpr static auto removeFirst(T &string, int n) -> T &
{
return string.last(string.length() - n);
}

template <typename T> constexpr static auto removeFirst(const T &string, int n) -> T
{
return string.last(string.length() - n);
}

static auto rot13(const QString &input) -> QString;
static auto hasWildcardMatch(const QString &string, const QString &wildcard) -> bool;

private:
constexpr static auto JPG_BASE64_PREFIX = QLatin1StringView("data:image/jpg;base64,");
};
48 changes: 21 additions & 27 deletions src/filesystem/cache/filecache.cpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
#include "filecache.h"
#include "filecacheentry.h"
#include <QLoggingCategory>

Q_LOGGING_CATEGORY(gmFileCache, "gm.filesystem.cache")

FileCache::FileCache(int expirationTimeMs) : m_expirationTimeMs(expirationTimeMs)
{
}

auto FileCache::tryGetData(const QString &path, QByteArray &data) -> bool
{
if (!m_entries.contains(path))
{
return false;
}

auto *entry = m_entries.value(path);
if (!entry->isFresh())
auto entry = m_entries.value(path);
if (!entry->isFresh(m_expirationTimeMs))
{
removeEntry(path);
return false;
Expand All @@ -24,21 +29,20 @@ auto FileCache::createOrUpdateEntry(const QString &path, const QByteArray &data)
{
if (m_entries.contains(path))
{
auto *entry = m_entries.value(path);
auto entry = m_entries.value(path);
return entry->update(data);
}

auto *entry = new FileCacheEntry(data, this);
m_entries[path] = entry;
m_entries[path] = std::make_shared<FileCacheEntry>(data);
return true;
}

auto FileCache::removeEntry(const QString &path) -> bool
{
if (m_entries.contains(path))
{
auto *entry = m_entries.take(path);
entry->deleteLater();
auto entry = m_entries.take(path);
entry.reset();
return true;
}

Expand All @@ -49,21 +53,27 @@ auto FileCache::moveEntry(const QString &oldPath, const QString &newPath) -> boo
{
if (!m_entries.contains(oldPath)) return false;

if (auto entry = m_entries.value(oldPath); !entry->isFresh(m_expirationTimeMs))
{
removeEntry(oldPath);
return false;
}

if (m_entries.contains(newPath))
{
removeEntry(newPath);
}

auto *entry = m_entries.take(oldPath);
m_entries[newPath] = entry;
auto entry = m_entries.take(oldPath);
m_entries[newPath] = std::move(entry);
return true;
}

auto FileCache::copyEntry(const QString &path, const QString &copy) -> bool
{
if (!m_entries.contains(path)) return false;

if (auto *entry = m_entries.value(path); entry->isFresh())
if (auto entry = m_entries.value(path); entry->isFresh(m_expirationTimeMs))
{
if (QByteArray data; entry->tryGetData(data))
{
Expand All @@ -80,21 +90,5 @@ auto FileCache::copyEntry(const QString &path, const QString &copy) -> bool

auto FileCache::checkEntry(const QString &path) -> bool
{
return m_entries.contains(path) && m_entries.value(path)->isFresh();
}

void FileCache::printEntries() const
{
qCDebug(gmFileCache()) << "Cache entries:";

foreach (const auto &key, m_entries.keys())
{
auto *value = m_entries[key];

QByteArray data;
value->tryGetData(data);

qCDebug(gmFileCache()) << "\t"
<< "key:" << key << "\tvalue:" << data << "\tisFresh:" << value->isFresh();
}
return m_entries.contains(path) && m_entries.value(path)->isFresh(m_expirationTimeMs);
}
28 changes: 16 additions & 12 deletions src/filesystem/cache/filecache.h
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
#pragma once

#include "filecacheentry.h"
#include <QMap>
#include <QObject>
#include <QByteArray>
#include <QHash>
#include <QString>
#include <memory>

class FileCache : public QObject
class FileCache
{
Q_OBJECT
public:
using QObject::QObject;
explicit FileCache(int expirationTimeMs = DEFAULT_EXPIRATION_TIME_MS);

bool tryGetData(const QString &path, QByteArray &data);
bool createOrUpdateEntry(const QString &path, const QByteArray &data);
bool removeEntry(const QString &path);
bool moveEntry(const QString &oldPath, const QString &newPath);
bool copyEntry(const QString &path, const QString &copy);
bool checkEntry(const QString &path);
auto tryGetData(const QString &path, QByteArray &data) -> bool;
auto createOrUpdateEntry(const QString &path, const QByteArray &data) -> bool;
auto removeEntry(const QString &path) -> bool;
auto moveEntry(const QString &oldPath, const QString &newPath) -> bool;
auto copyEntry(const QString &path, const QString &copy) -> bool;
auto checkEntry(const QString &path) -> bool;

void printEntries() const;

private:
QMap<QString, FileCacheEntry *> m_entries;
QHash<QString, std::shared_ptr<FileCacheEntry>> m_entries;
int m_expirationTimeMs;

static constexpr int DEFAULT_EXPIRATION_TIME_MS = 30000;
};
Loading

0 comments on commit f2b5f36

Please sign in to comment.