annotate base/RecentFiles.h @ 1724:157c62ff0056

Close paren in text
author Chris Cannam
date Mon, 17 Jun 2019 11:02:03 +0100
parents 452b48b29c2d
children
rev   line source
Chris@149 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@149 2
Chris@149 3 /*
Chris@149 4 Sonic Visualiser
Chris@149 5 An audio file viewer and annotation editor.
Chris@149 6 Centre for Digital Music, Queen Mary, University of London.
Chris@149 7 This file copyright 2006 Chris Cannam.
Chris@149 8
Chris@149 9 This program is free software; you can redistribute it and/or
Chris@149 10 modify it under the terms of the GNU General Public License as
Chris@149 11 published by the Free Software Foundation; either version 2 of the
Chris@149 12 License, or (at your option) any later version. See the file
Chris@149 13 COPYING included with this distribution for more information.
Chris@149 14 */
Chris@149 15
Chris@1581 16 #ifndef SV_RECENT_FILES_H
Chris@1581 17 #define SV_RECENT_FILES_H
Chris@149 18
Chris@149 19 #include <QObject>
Chris@149 20 #include <QString>
Chris@1704 21 #include <QMutex>
Chris@149 22 #include <vector>
Chris@149 23 #include <deque>
Chris@149 24
Chris@166 25 /**
Chris@1704 26 * RecentFiles manages a list of recently-used identifier strings,
Chris@1704 27 * saving and restoring that list via QSettings. The identifiers do
Chris@1704 28 * not actually have to refer to files.
Chris@1704 29 *
Chris@1704 30 * Each entry must have a non-empty identifier, which is typically a
Chris@1704 31 * filename, path, URI, or internal id, and may optionally also have a
Chris@1704 32 * label, which is typically a user-visible convenience.
Chris@1704 33 *
Chris@1704 34 * RecentFiles is thread-safe - all access is serialised.
Chris@166 35 */
Chris@149 36 class RecentFiles : public QObject
Chris@149 37 {
Chris@149 38 Q_OBJECT
Chris@149 39
Chris@149 40 public:
Chris@166 41 /**
Chris@166 42 * Construct a RecentFiles object that saves and restores in the
Chris@166 43 * given QSettings group and truncates when the given count of
Chris@1704 44 * identifiers is reached.
Chris@166 45 */
Chris@1704 46 RecentFiles(QString settingsGroup = "RecentFiles",
Chris@1704 47 int maxCount = 10);
Chris@149 48
Chris@149 49 virtual ~RecentFiles();
Chris@149 50
Chris@1704 51 /**
Chris@1704 52 * Return the settingsGroup as passed to the constructor.
Chris@1704 53 */
Chris@1704 54 QString getSettingsGroup() const {
Chris@1704 55 return m_settingsGroup;
Chris@1704 56 }
Chris@166 57
Chris@166 58 /**
Chris@1704 59 * Return the maxCount as passed to the constructor.
Chris@166 60 */
Chris@1704 61 int getMaxCount() const {
Chris@1704 62 return m_maxCount;
Chris@1704 63 }
Chris@1704 64
Chris@1704 65 /**
Chris@1704 66 * Return the list of recent identifiers, without labels.
Chris@1704 67 */
Chris@1704 68 std::vector<QString> getRecentIdentifiers() const;
Chris@1704 69
Chris@1704 70 /**
Chris@1704 71 * Return the list of recent identifiers, without labels. This is
Chris@1704 72 * an alias for getRecentIdentifiers included for backward
Chris@1704 73 * compatibility.
Chris@1704 74 */
Chris@1704 75 std::vector<QString> getRecent() const {
Chris@1704 76 return getRecentIdentifiers();
Chris@1704 77 }
Chris@1704 78
Chris@1704 79 /**
Chris@1704 80 * Return the list of recent identifiers, with labels. Each
Chris@1704 81 * returned entry is a pair of identifier and label in that order.
Chris@1704 82 */
Chris@1704 83 std::vector<std::pair<QString, QString>> getRecentEntries() const;
Chris@149 84
Chris@166 85 /**
Chris@1704 86 * Add a literal identifier, optionally with a label.
Chris@1704 87 *
Chris@1704 88 * If the identifier already exists in the recent entries list, it
Chris@1704 89 * is moved to the front of the list and its label is replaced
Chris@1704 90 * with the given one.
Chris@166 91 */
Chris@1704 92 void add(QString identifier, QString label = "");
Chris@1704 93
Chris@1704 94 /**
Chris@1704 95 * Add a name that is known to be either a file path or a URL,
Chris@1704 96 * optionally with a label. If it looks like a URL, add it
Chris@1704 97 * literally; otherwise treat it as a file path and canonicalise
Chris@1704 98 * it appropriately. Also take into account the user preference
Chris@1704 99 * for whether to include temporary files in the recent files
Chris@1704 100 * menu: the file will not be added if the preference is set and
Chris@1704 101 * the file appears to be a temporary one.
Chris@1704 102 *
Chris@1704 103 * If the identifier derived from the file path already exists in
Chris@1704 104 * the recent entries list, it is moved to the front of the list
Chris@1704 105 * and its label is replaced with the given one.
Chris@1704 106 */
Chris@1704 107 void addFile(QString filepath, QString label = "");
Chris@149 108
Chris@149 109 signals:
Chris@166 110 void recentChanged();
Chris@149 111
Chris@1704 112 private:
Chris@1704 113 mutable QMutex m_mutex;
Chris@149 114
Chris@1704 115 const QString m_settingsGroup;
Chris@1704 116 const int m_maxCount;
Chris@1704 117
Chris@1704 118 std::deque<std::pair<QString, QString>> m_entries; // identifier, label
Chris@149 119
Chris@166 120 void read();
Chris@166 121 void write();
Chris@149 122 void truncateAndWrite();
Chris@149 123 };
Chris@149 124
Chris@149 125 #endif