Mercurial > hg > svcore
comparison base/RecentFiles.cpp @ 1704:452b48b29c2d single-point
Associate a label with each recent files entry, as well as the identifier (filename)
author | Chris Cannam |
---|---|
date | Mon, 13 May 2019 15:43:23 +0100 |
parents | 6a94bb528e9d |
children |
comparison
equal
deleted
inserted
replaced
1703:b17fb3a4560c | 1704:452b48b29c2d |
---|---|
18 #include "Preferences.h" | 18 #include "Preferences.h" |
19 | 19 |
20 #include <QFileInfo> | 20 #include <QFileInfo> |
21 #include <QSettings> | 21 #include <QSettings> |
22 #include <QRegExp> | 22 #include <QRegExp> |
23 #include <QMutexLocker> | |
23 | 24 |
24 RecentFiles::RecentFiles(QString settingsGroup, int maxCount) : | 25 RecentFiles::RecentFiles(QString settingsGroup, int maxCount) : |
25 m_settingsGroup(settingsGroup), | 26 m_settingsGroup(settingsGroup), |
26 m_maxCount(maxCount) | 27 m_maxCount(maxCount) |
27 { | 28 { |
34 } | 35 } |
35 | 36 |
36 void | 37 void |
37 RecentFiles::read() | 38 RecentFiles::read() |
38 { | 39 { |
39 m_names.clear(); | 40 // Private method - called only from constructor - no mutex lock required |
41 | |
42 m_entries.clear(); | |
40 QSettings settings; | 43 QSettings settings; |
41 settings.beginGroup(m_settingsGroup); | 44 settings.beginGroup(m_settingsGroup); |
42 | 45 |
43 for (int i = 0; i < 100; ++i) { | 46 for (int i = 0; i < 100; ++i) { |
44 QString key = QString("recent-%1").arg(i); | 47 |
45 QString name = settings.value(key, "").toString(); | 48 QString idKey = QString("recent-%1").arg(i); |
46 if (name == "") break; | 49 QString identifier = settings.value(idKey, "").toString(); |
47 if (i < m_maxCount) m_names.push_back(name); | 50 if (identifier == "") break; |
48 else settings.setValue(key, ""); | 51 |
52 QString labelKey = QString("recent-%1-label").arg(i); | |
53 QString label = settings.value(labelKey, "").toString(); | |
54 | |
55 if (i < m_maxCount) m_entries.push_back({ identifier, label }); | |
56 else { | |
57 settings.setValue(idKey, ""); | |
58 settings.setValue(labelKey, ""); | |
59 } | |
49 } | 60 } |
50 | 61 |
51 settings.endGroup(); | 62 settings.endGroup(); |
52 } | 63 } |
53 | 64 |
54 void | 65 void |
55 RecentFiles::write() | 66 RecentFiles::write() |
56 { | 67 { |
68 // Private method - must be serialised at call site | |
69 | |
57 QSettings settings; | 70 QSettings settings; |
58 settings.beginGroup(m_settingsGroup); | 71 settings.beginGroup(m_settingsGroup); |
59 | 72 |
60 for (int i = 0; i < m_maxCount; ++i) { | 73 for (int i = 0; i < m_maxCount; ++i) { |
61 QString key = QString("recent-%1").arg(i); | 74 QString idKey = QString("recent-%1").arg(i); |
62 QString name = ""; | 75 QString labelKey = QString("recent-%1-label").arg(i); |
63 if (i < (int)m_names.size()) name = m_names[i]; | 76 QString identifier; |
64 settings.setValue(key, name); | 77 QString label; |
78 if (in_range_for(m_entries, i)) { | |
79 identifier = m_entries[i].first; | |
80 label = m_entries[i].second; | |
81 } | |
82 settings.setValue(idKey, identifier); | |
83 settings.setValue(labelKey, label); | |
65 } | 84 } |
66 | 85 |
67 settings.endGroup(); | 86 settings.endGroup(); |
68 } | 87 } |
69 | 88 |
70 void | 89 void |
71 RecentFiles::truncateAndWrite() | 90 RecentFiles::truncateAndWrite() |
72 { | 91 { |
73 while (int(m_names.size()) > m_maxCount) { | 92 // Private method - must be serialised at call site |
74 m_names.pop_back(); | 93 |
94 while (int(m_entries.size()) > m_maxCount) { | |
95 m_entries.pop_back(); | |
75 } | 96 } |
76 write(); | 97 write(); |
77 } | 98 } |
78 | 99 |
79 std::vector<QString> | 100 std::vector<QString> |
80 RecentFiles::getRecent() const | 101 RecentFiles::getRecentIdentifiers() const |
81 { | 102 { |
82 std::vector<QString> names; | 103 QMutexLocker locker(&m_mutex); |
104 | |
105 std::vector<QString> identifiers; | |
83 for (int i = 0; i < m_maxCount; ++i) { | 106 for (int i = 0; i < m_maxCount; ++i) { |
84 if (i < (int)m_names.size()) { | 107 if (i < (int)m_entries.size()) { |
85 names.push_back(m_names[i]); | 108 identifiers.push_back(m_entries[i].first); |
86 } | 109 } |
87 } | 110 } |
88 return names; | 111 |
112 return identifiers; | |
113 } | |
114 | |
115 std::vector<std::pair<QString, QString>> | |
116 RecentFiles::getRecentEntries() const | |
117 { | |
118 QMutexLocker locker(&m_mutex); | |
119 | |
120 std::vector<std::pair<QString, QString>> entries; | |
121 for (int i = 0; i < m_maxCount; ++i) { | |
122 if (i < (int)m_entries.size()) { | |
123 entries.push_back(m_entries[i]); | |
124 } | |
125 } | |
126 | |
127 return entries; | |
89 } | 128 } |
90 | 129 |
91 void | 130 void |
92 RecentFiles::add(QString name) | 131 RecentFiles::add(QString identifier, QString label) |
93 { | 132 { |
94 bool have = false; | 133 { |
95 for (int i = 0; i < int(m_names.size()); ++i) { | 134 QMutexLocker locker(&m_mutex); |
96 if (m_names[i] == name) { | 135 |
97 have = true; | 136 bool have = false; |
98 break; | 137 for (int i = 0; i < int(m_entries.size()); ++i) { |
138 if (m_entries[i].first == identifier) { | |
139 have = true; | |
140 break; | |
141 } | |
99 } | 142 } |
143 | |
144 if (!have) { | |
145 m_entries.push_front({ identifier, label }); | |
146 } else { | |
147 std::deque<std::pair<QString, QString>> newEntries; | |
148 newEntries.push_back({ identifier, label }); | |
149 for (int i = 0; in_range_for(m_entries, i); ++i) { | |
150 if (m_entries[i].first == identifier) continue; | |
151 newEntries.push_back(m_entries[i]); | |
152 } | |
153 m_entries = newEntries; | |
154 } | |
155 | |
156 truncateAndWrite(); | |
100 } | 157 } |
101 | 158 |
102 if (!have) { | |
103 m_names.push_front(name); | |
104 } else { | |
105 std::deque<QString> newnames; | |
106 newnames.push_back(name); | |
107 for (int i = 0; i < int(m_names.size()); ++i) { | |
108 if (m_names[i] == name) continue; | |
109 newnames.push_back(m_names[i]); | |
110 } | |
111 m_names = newnames; | |
112 } | |
113 | |
114 truncateAndWrite(); | |
115 emit recentChanged(); | 159 emit recentChanged(); |
116 } | 160 } |
117 | 161 |
118 void | 162 void |
119 RecentFiles::addFile(QString name) | 163 RecentFiles::addFile(QString filepath, QString label) |
120 { | 164 { |
121 static QRegExp schemeRE("^[a-zA-Z]{2,5}://"); | 165 static QRegExp schemeRE("^[a-zA-Z]{2,5}://"); |
122 static QRegExp tempRE("[\\/][Tt]e?mp[\\/]"); | 166 static QRegExp tempRE("[\\/][Tt]e?mp[\\/]"); |
123 if (schemeRE.indexIn(name) == 0) { | 167 if (schemeRE.indexIn(filepath) == 0) { |
124 add(name); | 168 add(filepath, label); |
125 } else { | 169 } else { |
126 QString absPath = QFileInfo(name).absoluteFilePath(); | 170 QString absPath = QFileInfo(filepath).absoluteFilePath(); |
127 if (tempRE.indexIn(absPath) != -1) { | 171 if (tempRE.indexIn(absPath) != -1) { |
128 Preferences *prefs = Preferences::getInstance(); | 172 Preferences *prefs = Preferences::getInstance(); |
129 if (prefs && !prefs->getOmitTempsFromRecentFiles()) { | 173 if (prefs && !prefs->getOmitTempsFromRecentFiles()) { |
130 add(absPath); | 174 add(absPath, label); |
131 } | 175 } |
132 } else { | 176 } else { |
133 add(absPath); | 177 add(absPath, label); |
134 } | 178 } |
135 } | 179 } |
136 } | 180 } |
137 | 181 |
138 | 182 |