QtPass Developer documentation
QtPass test coverage
Current view: top level - src/storetree.h Coverage Total Hit
Test: QtPass Lines: 100.0 % 1 1
Test Date: 2026-12-03 23:34:20 Functions: - 0 0

            Line data    Source code
       1              : // SPDX-FileCopyrightText: 2026 Anne Jan Brouwer
       2              : // SPDX-License-Identifier: GPL-3.0-or-later
       3              : /**
       4              :  * @file
       5              :  * @brief StoreTree: owns the QFileSystemModel and StoreModel behind the
       6              :  *        password tree view and maps view indexes to store paths.
       7              :  */
       8              : #ifndef SRC_STORETREE_H_
       9              : #define SRC_STORETREE_H_
      10              : 
      11              : #include "storemodel.h"
      12              : #include <QFileInfo>
      13              : #include <QFileSystemModel>
      14              : #include <QFileSystemWatcher>
      15              : #include <QModelIndex>
      16              : #include <QObject>
      17              : #include <QSet>
      18              : #include <QString>
      19              : #include <QStringList>
      20              : #include <QTimer>
      21              : #include <memory>
      22              : 
      23              : class Pass;
      24              : class QTreeView;
      25              : 
      26              : /**
      27              :  * @class StoreTree
      28              :  * @ingroup store
      29              :  * @brief The password store as shown in the tree: the QFileSystemModel, the
      30              :  *        StoreModel proxy on top of it and the QTreeView they drive, with
      31              :  *        the path questions the main window keeps asking.
      32              :  *
      33              :  * Owns the two models. setStore() is the one place that re-roots the file
      34              :  * system model, the proxy and the view together (this sequence used to be
      35              :  * spelled out five times); dirFor()/fileFor() are the two path mappings
      36              :  * that were duplicated across MainWindow and Util.
      37              :  *
      38              :  * The file system model does not watch the store itself: when files in a
      39              :  * folder it lists change, Qt finds each changed row by a linear search, so
      40              :  * a pulled commit that rewrote N entries of one folder froze the window for
      41              :  * on the order of N² comparisons (half a minute for 20000). StoreTree
      42              :  * watches the folders the model has listed instead and, once they have
      43              :  * been quiet for a moment, builds a fresh model (reload()), which costs
      44              :  * time linear in what it lists.
      45              :  */
      46              : class StoreTree : public QObject {
      47              :   Q_OBJECT
      48              : 
      49              : public:
      50              :   /**
      51              :    * @brief Attach the proxy model to the view and set its look: hidden
      52              :    *        header, indentation, scroll bars, context-menu policy. Column
      53              :    *        hiding and sorting need a source model and happen in setStore().
      54              :    * @param view The tree view to drive.
      55              :    * @param parent Owner.
      56              :    */
      57              :   explicit StoreTree(QTreeView *view, QObject *parent = nullptr);
      58              : 
      59              :   /**
      60              :    * @brief Point everything at @p path: file system root, proxy store and
      61              :    *        view root index. Also drops a search filter.
      62              :    * @param path Password store directory.
      63              :    */
      64              :   void setStore(const QString &path);
      65              : 
      66              :   /**
      67              :    * @brief List the store again in a fresh file system model and swap it in
      68              :    *        once the store root and every folder listed before (expanded
      69              :    *        or not, so a search still sees inside it) are listed again,
      70              :    *        keeping the expanded folders, the current entry and the scroll
      71              :    *        position. Runs by itself a moment after a listed folder changed.
      72              :    */
      73              :   void reload();
      74              : 
      75              :   /**
      76              :    * @brief Hand the proxy the backend it drops onto.
      77              :    * @param pass Active backend (not owned).
      78              :    */
      79              :   void setPass(Pass *pass);
      80              : 
      81              :   /**
      82              :    * @brief Filter the visible entries.
      83              :    * @param pattern Regular expression matched against entry names; an
      84              :    *                invalid or empty one shows everything.
      85              :    */
      86              :   void setFilter(const QRegularExpression &pattern);
      87              : 
      88              :   /**
      89              :    * @brief The folder an index stands for.
      90              :    * @param index View index; a file maps to its folder, an invalid index to
      91              :    *              the store root.
      92              :    * @param forPass true for a store-relative path ("" for the root),
      93              :    *                false for an absolute one.
      94              :    * @return Folder path ending in the native separator (see forPass).
      95              :    */
      96              :   [[nodiscard]] auto dirFor(const QModelIndex &index, bool forPass) const
      97              :       -> QString;
      98              : 
      99              :   /**
     100              :    * @brief dirFor() applied to the view's current index.
     101              :    * @param forPass As in dirFor().
     102              :    * @return Folder path.
     103              :    */
     104              :   [[nodiscard]] auto currentDir(bool forPass) const -> QString;
     105              : 
     106              :   /**
     107              :    * @brief The entry an index stands for.
     108              :    * @param index View index; folders and invalid indexes yield an empty
     109              :    *              string.
     110              :    * @param forPass true for the pass name (store-relative, no .gpg),
     111              :    *                false for the absolute .gpg path.
     112              :    * @return Entry name or path, empty when @p index is not a file.
     113              :    */
     114              :   [[nodiscard]] auto fileFor(const QModelIndex &index, bool forPass) const
     115              :       -> QString;
     116              : 
     117              :   /**
     118              :    * @brief fileFor() applied to the view's current index.
     119              :    * @param forPass As in fileFor().
     120              :    * @return Entry name or path.
     121              :    */
     122              :   [[nodiscard]] auto currentFile(bool forPass) const -> QString;
     123              : 
     124              :   /**
     125              :    * @brief File system information for a view index.
     126              :    * @param index View index.
     127              :    * @return QFileInfo of the underlying path.
     128              :    */
     129              :   [[nodiscard]] auto fileInfo(const QModelIndex &index) const -> QFileInfo;
     130              : 
     131              :   /**
     132              :    * @brief The view index for an absolute path, or invalid when the file
     133              :    *        system model does not (yet) know it.
     134              :    * @param path Absolute path inside the store.
     135              :    * @return Proxy index.
     136              :    */
     137              :   [[nodiscard]] auto indexFor(const QString &path) const -> QModelIndex;
     138              : 
     139              :   /**
     140              :    * @brief The proxy's root index for the current store.
     141              :    * @return Proxy index of the store root.
     142              :    */
     143              :   [[nodiscard]] auto rootIndex() -> QModelIndex;
     144              : 
     145              :   /**
     146              :    * @brief The view's current index.
     147              :    * @return Proxy index, possibly invalid.
     148              :    */
     149              :   [[nodiscard]] auto currentIndex() const -> QModelIndex;
     150              : 
     151              :   /// @return The store directory set with setStore().
     152              :   [[nodiscard]] auto store() const -> QString { return m_store; }
     153              :   /// @return The proxy the view shows.
     154           23 :   [[nodiscard]] auto proxy() -> StoreModel & { return m_proxy; }
     155              :   /// @return The file system model under the proxy; reload() replaces it.
     156              :   [[nodiscard]] auto fileSystem() -> QFileSystemModel & { return *m_fs; }
     157              :   /// @return The tree view.
     158              :   [[nodiscard]] auto view() const -> QTreeView * { return m_view; }
     159              : 
     160              : signals:
     161              :   /**
     162              :    * @brief The file system model the view shows finished listing a folder;
     163              :    *        also once for the store root after reload() swapped models.
     164              :    * @param path Absolute folder path.
     165              :    */
     166              :   void directoryLoaded(const QString &path);
     167              :   /**
     168              :    * @brief After a reload() the current entry or folder could not be shown
     169              :    *        again: it was removed or renamed, or its folder was not listed
     170              :    *        yet. Nothing is current then, so currentDir() gives the store
     171              :    *        root, and a menu opened on that entry must not act on the root
     172              :    *        instead. One that still exists is made current again if it
     173              :    *        shows once the listings still under way at the swap are in.
     174              :    * @param path Absolute path the current entry had.
     175              :    */
     176              :   void currentEntryLost(const QString &path);
     177              : 
     178              : private:
     179              :   friend class tst_storetree;
     180              :   friend class tst_mainwindow;
     181              : 
     182              :   /**
     183              :    * @brief A file system model as the tree uses it: `*.gpg` only, and not
     184              :    *        watching (see the class description).
     185              :    * @return The new model, not rooted yet.
     186              :    */
     187              :   auto makeModel() -> std::unique_ptr<QFileSystemModel>;
     188              :   /**
     189              :    * @brief Hide the size, type and date columns and sort by name, once a
     190              :    *        source model is attached.
     191              :    */
     192              :   void setUpColumns();
     193              :   /**
     194              :    * @brief Watch @p dir from now on, as the shown model has listed it.
     195              :    * @param dir Absolute folder path.
     196              :    */
     197              :   void watchDir(const QString &dir);
     198              :   /**
     199              :    * @brief Swap in the model reload() built, restoring the view.
     200              :    */
     201              :   void swapInFresh();
     202              :   /**
     203              :    * @brief The watched folders the view shows expanded, for swapInFresh().
     204              :    * @return Their absolute paths, parents before their children.
     205              :    */
     206              :   [[nodiscard]] auto expandedDirs() const -> QStringList;
     207              :   /**
     208              :    * @brief Stop watching the folders that no longer exist.
     209              :    */
     210              :   void unwatchGoneDirs();
     211              :   /**
     212              :    * @brief The shown model listed @p path: watch it, list it in a model
     213              :    *        being built too, and filter again once the listings a swap did
     214              :    *        not wait for are done.
     215              :    * @param path Absolute folder path.
     216              :    */
     217              :   void shownListed(const QString &path);
     218              :   /**
     219              :    * @brief Make the entry a swap could not show current again, once it
     220              :    *        shows. The wait ends when the listings the swap did not wait for
     221              :    *        are in, or when another entry is made current, even one
     222              :    *        deselected since.
     223              :    */
     224              :   void restoreCurrent();
     225              : 
     226              :   /** @brief The tree view this object drives; owned by the main window. */
     227              :   QTreeView *m_view;
     228              :   /**
     229              :    * @brief Source model over the store directory, filtered to `*.gpg`;
     230              :    *        replaced by setStore() and reload().
     231              :    */
     232              :   std::unique_ptr<QFileSystemModel> m_fs;
     233              :   /**
     234              :    * @brief The model reload() replaced, kept until the next one: a caller
     235              :    *        still holding it reads stale rows instead of freed memory.
     236              :    */
     237              :   std::unique_ptr<QFileSystemModel> m_retired;
     238              :   /** @brief The model reload() is building, until it is swapped in. */
     239              :   std::unique_ptr<QFileSystemModel> m_fresh;
     240              :   /** @brief Folders m_fresh still has to list before the swap. */
     241              :   QSet<QString> m_freshPending;
     242              :   /** @brief Folders still being listed when m_fresh was swapped in. */
     243              :   QSet<QString> m_stillListing;
     244              :   /** @brief The current entry a swap could not show yet. */
     245              :   QString m_restoreCurrent;
     246              :   /** @brief A reload() came while m_fresh was built: run one after it. */
     247              :   bool m_reloadAgain = false;
     248              :   /** @brief Ends the wait for m_fresh: it is swapped in as it is. */
     249              :   QTimer m_freshDeadline;
     250              :   /**
     251              :    * @brief The folders the shown model has listed; reload() lists all of
     252              :    *        them again.
     253              :    */
     254              :   QFileSystemWatcher m_watcher;
     255              :   /** @brief Waits for the watched folders to be quiet, then reload(). */
     256              :   QTimer m_reloadTimer;
     257              :   /**
     258              :    * @brief Proxy the view shows: hides `.git`, strips `.gpg`, applies the
     259              :    *        search filter and handles drag and drop.
     260              :    */
     261              :   StoreModel m_proxy;
     262              :   /** @brief Absolute store directory last passed to setStore(). */
     263              :   QString m_store;
     264              : };
     265              : 
     266              : #endif // SRC_STORETREE_H_
        

Generated by: LCOV version 2.0-1