RecentFiles.cpp
Go to the documentation of this file.
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2 
3 /*
4  Sonic Visualiser
5  An audio file viewer and annotation editor.
6  Centre for Digital Music, Queen Mary, University of London.
7  This file copyright 2006 Chris Cannam.
8 
9  This program is free software; you can redistribute it and/or
10  modify it under the terms of the GNU General Public License as
11  published by the Free Software Foundation; either version 2 of the
12  License, or (at your option) any later version. See the file
13  COPYING included with this distribution for more information.
14 */
15 
16 #include "RecentFiles.h"
17 
18 #include "Preferences.h"
19 
20 #include <QFileInfo>
21 #include <QSettings>
22 #include <QRegExp>
23 #include <QMutexLocker>
24 
25 RecentFiles::RecentFiles(QString settingsGroup, int maxCount) :
26  m_settingsGroup(settingsGroup),
27  m_maxCount(maxCount)
28 {
29  read();
30 }
31 
33 {
34  // nothing
35 }
36 
37 void
39 {
40  // Private method - called only from constructor - no mutex lock required
41 
42  m_entries.clear();
43  QSettings settings;
44  settings.beginGroup(m_settingsGroup);
45 
46  for (int i = 0; i < 100; ++i) {
47 
48  QString idKey = QString("recent-%1").arg(i);
49  QString identifier = settings.value(idKey, "").toString();
50  if (identifier == "") break;
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  }
60  }
61 
62  settings.endGroup();
63 }
64 
65 void
67 {
68  // Private method - must be serialised at call site
69 
70  QSettings settings;
71  settings.beginGroup(m_settingsGroup);
72 
73  for (int i = 0; i < m_maxCount; ++i) {
74  QString idKey = QString("recent-%1").arg(i);
75  QString labelKey = QString("recent-%1-label").arg(i);
76  QString identifier;
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);
84  }
85 
86  settings.endGroup();
87 }
88 
89 void
91 {
92  // Private method - must be serialised at call site
93 
94  while (int(m_entries.size()) > m_maxCount) {
95  m_entries.pop_back();
96  }
97  write();
98 }
99 
100 std::vector<QString>
102 {
103  QMutexLocker locker(&m_mutex);
104 
105  std::vector<QString> identifiers;
106  for (int i = 0; i < m_maxCount; ++i) {
107  if (i < (int)m_entries.size()) {
108  identifiers.push_back(m_entries[i].first);
109  }
110  }
111 
112  return identifiers;
113 }
114 
115 std::vector<std::pair<QString, QString>>
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;
128 }
129 
130 void
131 RecentFiles::add(QString identifier, QString label)
132 {
133  {
134  QMutexLocker locker(&m_mutex);
135 
136  bool have = false;
137  for (int i = 0; i < int(m_entries.size()); ++i) {
138  if (m_entries[i].first == identifier) {
139  have = true;
140  break;
141  }
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 
157  }
158 
159  emit recentChanged();
160 }
161 
162 void
163 RecentFiles::addFile(QString filepath, QString label)
164 {
165  static QRegExp schemeRE("^[a-zA-Z]{2,5}://");
166  static QRegExp tempRE("[\\/][Tt]e?mp[\\/]");
167  if (schemeRE.indexIn(filepath) == 0) {
168  add(filepath, label);
169  } else {
170  QString absPath = QFileInfo(filepath).absoluteFilePath();
171  if (tempRE.indexIn(absPath) != -1) {
173  if (prefs && !prefs->getOmitTempsFromRecentFiles()) {
174  add(absPath, label);
175  }
176  } else {
177  add(absPath, label);
178  }
179  }
180 }
181 
182 
void recentChanged()
std::vector< std::pair< QString, QString > > getRecentEntries() const
Return the list of recent identifiers, with labels.
const int m_maxCount
Definition: RecentFiles.h:116
QMutex m_mutex
Definition: RecentFiles.h:113
void add(QString identifier, QString label="")
Add a literal identifier, optionally with a label.
static Preferences * getInstance()
Definition: Preferences.cpp:31
virtual ~RecentFiles()
Definition: RecentFiles.cpp:32
void addFile(QString filepath, QString label="")
Add a name that is known to be either a file path or a URL, optionally with a label.
void write()
Definition: RecentFiles.cpp:66
const QString m_settingsGroup
Definition: RecentFiles.h:115
bool getOmitTempsFromRecentFiles() const
Definition: Preferences.h:64
std::deque< std::pair< QString, QString > > m_entries
Definition: RecentFiles.h:118
std::vector< QString > getRecentIdentifiers() const
Return the list of recent identifiers, without labels.
bool in_range_for(const C &container, T i)
Check whether an integer index is in range for a container, avoiding overflows and signed/unsigned co...
Definition: BaseTypes.h:37
void truncateAndWrite()
Definition: RecentFiles.cpp:90
RecentFiles(QString settingsGroup="RecentFiles", int maxCount=10)
Construct a RecentFiles object that saves and restores in the given QSettings group and truncates whe...
Definition: RecentFiles.cpp:25