// SPDX-FileCopyrightText: 2026 Matvey Shcherbinin // SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL #pragma once #include #include #include #include #include #include #include // Images that live somewhere else, kept here once they have been fetched. // // The problem it answers is a list of faces and a conversation of photographs // being fetched from a homeserver again every time a delegate is built. A // timeline builds its rows on every scroll, and an Image that is asked for an // mxc:// url is a request to the server for those bytes - Qt's own pixmap cache // is in memory, and an entry in it is gone when the process is. So a row that // scrolls out and back asks again, and again, and the whole conversation costs // a download per pixel of scrolling. // // A file on disk answers the second time. This is the one place that decides // where such a file is and how it is named, so the timeline, the conversation // list and the room column all agree on the same name for the same picture // instead of each keeping a cache of its own. // // The files are in the system cache directory, which is the right place for // something this program can fetch again: whatever the operating system decides // to throw away is thrown away, and the program asks for it once more. class MediaCache : public QObject { Q_OBJECT QML_ELEMENT QML_SINGLETON // Files this account has on disk, for a window in the settings page. There // is nothing to switch off here - a cache is meant to fill - but a person // asking how much disk this program is using is entitled to an answer. Q_PROPERTY(qulonglong diskUsage READ diskUsage NOTIFY diskUsageChanged) // What the files are, as [{ name, size }], newest first. Empty is a normal // answer, not a fault: an account that has looked at nothing has no files. Q_PROPERTY(QVariantList files READ files NOTIFY diskUsageChanged) // An optional cap on the cache in megabytes; zero leaves it unbounded. Q_PROPERTY(int limitMb READ limitMb WRITE setLimitMb NOTIFY diskUsageChanged) public: static MediaCache *create(QQmlEngine *, QJSEngine *); // The file url is already in, empty when it is not. Safe to ask on every // row of a list while a delegate is being built: it is one directory entry // and no network, and an answer is the whole point of asking. Q_INVOKABLE QString localPathFor(const QUrl &url) const; // Asks for the file to be there, and does not wait: a row is drawn from // the remote url meanwhile, and from the local one the next time it is // built. Asking for a file already held costs a directory entry and no // traffic, so this is what a row may call without asking first. Q_INVOKABLE void fetch(const QUrl &url); // Frees the disk. Everything in here can be fetched again, so this is a // forget, not a delete of something that was paid for. Q_INVOKABLE void clear(); qulonglong diskUsage() const { return m_diskUsage; } int limitMb() const { return m_limitMb; } void setLimitMb(int mb); QVariantList files() const; Q_SIGNALS: void diskUsageChanged(); private: explicit MediaCache(QObject *parent = nullptr); // -, which keeps the file // recognisable in a file manager and impossible to confuse with another // url's: same picture at two sizes is two files, by design. static QString fileNameFor(const QUrl &url); static QString cacheDir(); void remeasure(); void trimToLimit(); // urls asked for and not answered yet, so that a hundred rows sharing one // avatar make one request rather than a hundred. QHash m_inFlight; qulonglong m_diskUsage = 0; int m_limitMb = 0; };