Added common files

This commit is contained in:
Digital Artifex
2026-06-06 22:34:19 -04:00
parent 96a7c71a29
commit fb90713022
11 changed files with 545 additions and 21 deletions
-21
View File
@@ -1,21 +0,0 @@
#ifndef WALLPAPERMETADATA_H
#define WALLPAPERMETADATA_H
#include "Komplex_global.h"
#include <QObject>
#include <QString>
#include <QByteArray>
#include <QDateTime>
struct KOMPLEX_EXPORT WallpaperMetaData {
QString uri;
QString name;
QString author;
QString description;
QString thumbnail;
QDateTime installDate;
};
Q_DECLARE_METATYPE(WallpaperMetaData)
#endif // WALLPAPERMETADATA_H
+18
View File
@@ -0,0 +1,18 @@
#include "coreservices.h"
#include "logging.h"
QNetworkAccessManager *CoreServices::m_networkAccessManager = new QNetworkAccessManager();
auto CoreServices::networkAccessManager() -> QNetworkAccessManager *
{
return m_networkAccessManager;
}
auto CoreServices::sessionToken() -> QByteArray
{
LOG_ERROR_X(true,
"CoreServices::sessionToken",
"User services not yet implements",
{}
);
}
+28
View File
@@ -0,0 +1,28 @@
#ifndef CORESERVICES_H
#define CORESERVICES_H
#include <QObject>
#include <QNetworkAccessManager>
class CoreServices
{
public:
/**
* @brief networkAccessManager
* Plugin-wide QNetworkAccessManager
* @return
*/
static auto networkAccessManager() -> QNetworkAccessManager*;
/**
* @brief getSessionToken
* Plugin-wide access to current user session token
* @return
*/
static auto sessionToken() -> QByteArray;
private:
static QNetworkAccessManager *m_networkAccessManager;
};
#endif // CORESERVICES_H
+28
View File
@@ -0,0 +1,28 @@
#ifndef FETCHRESULT_H
#define FETCHRESULT_H
#include <qtypes.h>
#include <QList>
template<typename T>
struct FetchResult
{
/**
* @brief total
* Total number of results in the query
*/
qsizetype total = 0;
/**
* @brief count
* Number of results returned in this result
*/
qsizetype count = 0;
/**
* @brief results
* Results list
*/
QList<T*> data;
};
#endif // FETCHRESULT_H
+15
View File
@@ -0,0 +1,15 @@
#ifndef KOMPLEX_GLOBAL_H
#define KOMPLEX_GLOBAL_H
#ifdef KOMPLEX_PLUGIN
#define KOMPLEX_EXPORT Q_DECL_EXPORT
#else
#define KOMPLEX_EXPORT Q_DECL_IMPORT
#endif
#define SLIDING_CACHE_WINDOW_SIZE 100
#define SLIDING_CACHE_PREFETCH_THRESHOLD 20
#define NETWORK_TIMEOUT 10000
#endif // KOMPLEX_GLOBAL_H
+49
View File
@@ -0,0 +1,49 @@
#ifndef LOGGING_H
#define LOGGING_H
// clazy:exclude=clazy-unused-headers
#include <iostream> // NOLINT(clazy-unused-headers)
#include <cstdlib> // NOLINT(clazy-unused-headers)
/**
* @brief LOG_ERROR
*
* When cond is true, logs where and what as an error
* @param cond
* @param where
* @param what
*/
#define LOG_ERROR(cond, where, what) \
do { \
if ((cond)) { \
std::cerr << "Assertion failed: (" << #cond << ")\n" \
<< " Location: " << (where) << "\n" \
<< " Message: " << (what) << "\n" \
<< " File: " << __FILE__ << ":" << __LINE__ << "\n"; \
} \
} while (0)
#define LOG_ERROR_X(cond, where, what, ret) \
do { \
if ((cond)) { \
std::cout << "Assertion failed: (" << #cond << ")\n" \
<< " Location: " << (where) << "\n" \
<< " Message: " << (what) << "\n" \
<< " File: " << __FILE__ << ":" << __LINE__ << "\n"; \
return ret; \
} \
} while (0)
#ifndef ASSERT_X
#define ASSERT_X(cond, where, what) \
do { \
if (!(cond)) { \
std::cout << "Assertion failed: (" << #cond << ")\n" \
<< " Location: " << (where) << "\n" \
<< " Message: " << (what) << "\n" \
<< " File: " << __FILE__ << ":" << __LINE__ << "\n"; \
std::abort(); \
} \
} while (0)
#endif
#endif // LOGGING_H
+85
View File
@@ -0,0 +1,85 @@
#ifndef PAGINATOR_H
#define PAGINATOR_H
#include <QObject>
#include "komplex_global.h"
#include "slidingcachecontroller.h"
template<typename T>
class KOMPLEX_EXPORT Paginator
{
public:
Paginator() = default;
~Paginator() = default;
auto next() const -> void
{
setOffset(m_offset + m_resultsPerPage);
}
auto previous() const -> void
{
setOffset(m_offset - m_resultsPerPage);
}
auto resultsPerPage() const -> qsizetype
{
return m_resultsPerPage;
}
auto setResultsPerPage(qsizetype resultsPerPage) -> void
{
m_resultsPerPage = resultsPerPage;
m_data = m_cacheController.chunk(m_offset, m_resultsPerPage);
}
auto data() const -> QList<T*>
{
return m_data;
}
auto count() const -> qsizetype
{
return m_data.count();
}
auto at(qsizetype index) const -> T*
{
if(index < 0 || index >= m_data.count())
{
return nullptr;
}
return m_data.at(index);
}
auto controller() -> SlidingCacheController<T>*
{
return &m_cacheController;
}
auto offset() const -> qsizetype
{
return m_offset;
}
auto setOffset(qsizetype offset) -> void
{
m_offset = offset;
if(m_offset < 0 || m_offset == std::numeric_limits<qsizetype>::max())
m_offset = 0;
m_data = m_cacheController.chunk(m_offset, m_resultsPerPage);
}
private:
qsizetype m_offset = 0;
qsizetype m_resultsPerPage = 0;
SlidingCacheController<T> m_cacheController;
QList<T*> m_data;
};
#endif // PAGINATOR_H
@@ -0,0 +1,284 @@
#ifndef SLIDINGCACHECONTROLLER_H
#define SLIDINGCACHECONTROLLER_H
#include <QObject>
#include <QList>
#include <functional>
#include "fetchresult.h"
#include "komplex_global.h"
template <typename T>
class KOMPLEX_EXPORT SlidingCacheController
{
public:
explicit SlidingCacheController()
{
m_cache.reserve(SLIDING_CACHE_WINDOW_SIZE);
}
~SlidingCacheController()
{
clear();
}
/**
* @brief setFetch
* Used to set the fetch method for the cache controller. Method should return a QList
* of CacheObjects based on the offset and limit provided. CacheObject must derive from
* QObject
*
* @param fetch
*/
auto setFetch(
const std::function<FetchResult<T> (qsizetype offset, qsizetype limit)> &fetch
) -> void
{
m_fetch = std::move(fetch);
}
/**
* @brief at
* Retrieves the CacheObject reference at the specified index. If data is outside the
* current window, a fetch will be attempted
* @param index
* @return CacheObject* or nullptr
*/
[[nodiscard]]
auto at(qsizetype index) const -> T*
{
//OOB check for expected index
if (index < 0 ||
(m_totalCount != std::numeric_limits<qsizetype>::infinity() &&
index >= m_totalCount))
{
return nullptr;
}
while (index >= m_windowOffset + (SLIDING_CACHE_WINDOW_SIZE - SLIDING_CACHE_PREFETCH_THRESHOLD))
{
if(!slideForward())
{
break;
}
}
while (index <= m_windowOffset - (SLIDING_CACHE_WINDOW_SIZE - SLIDING_CACHE_PREFETCH_THRESHOLD))
{
if(!slideBackward())
{
break;
}
}
index = trueIndex(index);
//OOB check for actual index
if(index < 0 || index >= m_cache.count())
{
return nullptr;
}
return m_cache.at(index);
}
/**
* @brief chunk
* Returns a chunk of data from cache. If data is outside the current window, a
* fetch will be attempted
* @param index
* @param count
* @return
*/
[[nodiscard]]
auto chunk(qsizetype index, qsizetype count) -> QList<T*>
{
QList<T*> dataChunk;
for(qsizetype i = index; i < count; ++i)
{
T* data = at(i);
if(data == nullptr)
{
break;
}
dataChunk.append(data);
}
return dataChunk;
}
/**
* @brief count
*
* @return The total number of results available
*/
[[nodiscard]]
auto count() const -> qsizetype
{
return m_totalCount;
}
/**
* @brief clear
* Clears the current cache
*/
auto clear() -> void
{
while(!m_cache.isEmpty())
{
T *object = m_cache.takeFirst();
delete object;
}
}
signals:
private:
/**
* @brief slideForward
* Attempts to slide the cache window forward
* @return true if successful
*/
auto slideForward() -> bool
{
qsizetype movementSize = SLIDING_CACHE_WINDOW_SIZE;
//after initial inflation we should only arrive here when the prefetch threshold is reached
if(!m_cache.isEmpty())
{
movementSize -= SLIDING_CACHE_PREFETCH_THRESHOLD;
}
m_windowOffset += movementSize;
QList<T*> newCache = m_fetch(
m_windowOffset,
movementSize
);
if(newCache.isEmpty())
{
m_windowOffset -= movementSize;
return false;
}
while(!newCache.isEmpty() && !m_cache.isEmpty())
{
T *object = newCache.takeFirst();
T *oldObject = m_cache.takeFirst();
delete oldObject;
m_cache.append(object);
}
if(!newCache.isEmpty())
{
LOG_ERROR_X((newCache.count() + m_cache.count()) > SLIDING_CACHE_WINDOW_SIZE,
"SlidingCacheController::slideForward",
"Cache size imbalance",
false
);
m_cache.append(newCache);
}
return true;
}
/**
* @brief slideBackward
* Attempts to slide the cache window backward
* @return true if successful
*/
auto slideBackward() -> bool
{
LOG_ERROR_X(!m_fetch,
"SlidingCacheController::slideBackward",
"Fetch function not set",
false
);
qsizetype movementSize = SLIDING_CACHE_WINDOW_SIZE - SLIDING_CACHE_PREFETCH_THRESHOLD;
m_windowOffset -= movementSize;
if(m_windowOffset < 0)
{
m_windowOffset = 0;
}
FetchResult<T> result = m_fetch(
m_windowOffset,
movementSize
);
if(result.count == 0)
{
m_windowOffset += movementSize;
return false;
}
while(!result.data.isEmpty() && !m_cache.isEmpty())
{
T *object = result.data.takeLast();
T *oldObject = m_cache.takeLast();
delete oldObject;
m_cache.push_front(object);
}
if(!result.results.isEmpty())
{
LOG_ERROR_X((result.count + m_cache.count()) > SLIDING_CACHE_WINDOW_SIZE,
"SlidingCacheController::slideBackward",
"Cache size imbalance",
false
);
m_cache.push_front(std::move(result.data));
}
return true;
}
/**
* @brief trueIndex
* Translates the global object index to the current cache index
* @param index Global object index
* @return
*/
auto trueIndex(qsizetype index) -> qsizetype
{
return index - m_windowOffset;
}
/**
* @brief m_windowOffset
* Current window offset
*/
qsizetype m_windowOffset = 0;
/**
* @brief m_totalCount
* Total number of results available. Data comes from the return data of
* m_fetch
*/
qsizetype m_totalCount = std::numeric_limits<qsizetype>::infinity();
/**
* @brief m_fetch
* The fetch function for retreiving external data. This must be set by the paginator
* or other controller class for the cachecontroller to work
*/
std::function<FetchResult<T>(qsizetype offset, qsizetype limit)> m_fetch;
/**
* @brief m_cache
* The working cache
*/
QVector<T*> m_cache;
};
#endif // SLIDINGCACHECONTROLLER_H
+38
View File
@@ -0,0 +1,38 @@
#ifndef WALLPAPERCACHE_H
#define WALLPAPERCACHE_H
#include "komplex_global.h"
#include <QObject>
#include <QString>
#include <QByteArray>
#include <QDateTime>
struct KOMPLEX_EXPORT WallpaperInstallData
{
QString uri;
QString name;
QString author;
QString description;
QString thumbnail;
QDateTime installDate;
};
Q_DECLARE_METATYPE(WallpaperInstallData)
struct KOMPLEX_EXPORT WallpaperCache
{
QString uri;
QString name;
QString author;
QString authorId;
QString description;
QString thumbnail;
QDateTime createdDate;
qreal price = 0;
QString currency;
quint64 downloadCount = 0;
qint8 type = -1;
};
Q_DECLARE_METATYPE(WallpaperCache)
#endif // WALLPAPERCACHE_H