data/SpellLanguageMemory.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #pragma once | ||
| 2 | |||
| 3 | #include <QHash> | ||
| 4 | #include <QString> | ||
| 5 | |||
| 6 | // Sprint 108 S108.4.2 (D-12): the spelling language last used with a | ||
| 7 | // recipient — a reply or a new mail to the same person is checked in that | ||
| 8 | // language. Keys are SHA-256 hashes of the lower-case address, so the file | ||
| 9 | // names no one. Stored in <profile>/spell-languages.json (owner-only, | ||
| 10 | // atomic, bounded): at most MaxEntries, the least recently used dropped. | ||
| 11 | class SpellLanguageMemory { | ||
| 12 | public: | ||
| 13 | static constexpr int MaxEntries = 500; | ||
| 14 | static constexpr qint64 MaxFileBytes = 256 * 1024; | ||
| 15 | |||
| 16 | 1593 | explicit SpellLanguageMemory(QString path = {}) : m_path(std::move(path)) {} | |
| 17 | static QString pathFor(const QString &profileDirectory); | ||
| 18 | |||
| 19 | // A missing file is empty; a malformed or oversized one is ignored (the | ||
| 20 | // memory starts empty and is written anew). | ||
| 21 | void load(); | ||
| 22 | bool save() const; | ||
| 23 | // "" when nothing is remembered for `address`. | ||
| 24 | QString languageFor(const QString &address) const; | ||
| 25 | // Remembers and saves; an empty address or language changes nothing. | ||
| 26 | void remember(const QString &address, const QString &language); | ||
| 27 | 3 | int size() const { return int(m_entries.size()); } | |
| 28 | |||
| 29 | private: | ||
| 30 | struct Entry { | ||
| 31 | QString language; | ||
| 32 | qint64 used = 0; // ordering counter, larger = more recent | ||
| 33 | }; | ||
| 34 | QString m_path; | ||
| 35 | QHash<QString, Entry> m_entries; | ||
| 36 | qint64 m_counter = 0; | ||
| 37 | static QString keyOf(const QString &address); | ||
| 38 | }; | ||
| 39 |