presentation/AddressPickerModel.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #pragma once | ||
| 2 | |||
| 3 | #include <QObject> | ||
| 4 | #include <QThreadPool> | ||
| 5 | #include <QVariantList> | ||
| 6 | #include <atomic> | ||
| 7 | |||
| 8 | // Sprint 108 S108.5.3 (D-14): the rows of the composer's address book sheet. | ||
| 9 | // Without a filter the people written to most come first ("recent"), then | ||
| 10 | // every contact with an address, A–Z; a filter searches names and addresses; | ||
| 11 | // a group shows its members. Each row: {key, name, address, addresses | ||
| 12 | // (every address of the card, preferred first), book, recent, initials, | ||
| 13 | // avatarColor}. `groups` lists the groups for the sheet's filter chips: | ||
| 14 | // {bookId, name, count}. | ||
| 15 | // | ||
| 16 | // The contacts database is read on a worker (latest request wins); the | ||
| 17 | // sheet never blocks the composer while it loads. | ||
| 18 | class AddressPickerModel : public QObject { | ||
| 19 | Q_OBJECT | ||
| 20 | Q_PROPERTY(QVariantList rows READ rows NOTIFY rowsChanged) | ||
| 21 | Q_PROPERTY(QVariantList groups READ groups NOTIFY rowsChanged) | ||
| 22 | Q_PROPERTY(bool loading READ loading NOTIFY rowsChanged) | ||
| 23 | |||
| 24 | public: | ||
| 25 | static constexpr int MaxRows = 300; | ||
| 26 | static constexpr int RecentRows = 8; | ||
| 27 | |||
| 28 | explicit AddressPickerModel(QObject *parent = nullptr); | ||
| 29 | ~AddressPickerModel() override; | ||
| 30 | |||
| 31 | 795 | void setDatabasePath(const QString &path) { m_databasePath = path; } | |
| 32 | 500 | QVariantList rows() const { return m_rows; } | |
| 33 | 319 | QVariantList groups() const { return m_groups; } | |
| 34 | 143 | bool loading() const { return m_loading; } | |
| 35 | |||
| 36 | // Loads the rows for a filter text, optionally inside one group | ||
| 37 | // (bookId + name); the result arrives through rowsChanged. | ||
| 38 | Q_INVOKABLE void request(const QString &filter, qint64 groupBookId = 0, const QString &group = {}); | ||
| 39 | // Blocks until the newest request finished (tests). | ||
| 40 | bool waitForDone(int timeoutMs = 5000); | ||
| 41 | |||
| 42 | signals: | ||
| 43 | void rowsChanged(); | ||
| 44 | |||
| 45 | private: | ||
| 46 | QString m_databasePath; | ||
| 47 | QVariantList m_rows; | ||
| 48 | QVariantList m_groups; | ||
| 49 | bool m_loading = false; | ||
| 50 | std::atomic<quint64> m_generation{0}; | ||
| 51 | QThreadPool m_pool; | ||
| 52 | }; | ||
| 53 |