svcore  1.9
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 
24 RecentFiles::RecentFiles(QString settingsGroup, int maxCount) :
25  m_settingsGroup(settingsGroup),
26  m_maxCount(maxCount)
27 {
28  read();
29 }
30 
32 {
33  // nothing
34 }
35 
36 void
38 {
39  m_names.clear();
40  QSettings settings;
41  settings.beginGroup(m_settingsGroup);
42 
43  for (int i = 0; i < 100; ++i) {
44  QString key = QString("recent-%1").arg(i);
45  QString name = settings.value(key, "").toString();
46  if (name == "") break;
47  if (i < m_maxCount) m_names.push_back(name);
48  else settings.setValue(key, "");
49  }
50 
51  settings.endGroup();
52 }
53 
54 void
56 {
57  QSettings settings;
58  settings.beginGroup(m_settingsGroup);
59 
60  for (int i = 0; i < m_maxCount; ++i) {
61  QString key = QString("recent-%1").arg(i);
62  QString name = "";
63  if (i < (int)m_names.size()) name = m_names[i];
64  settings.setValue(key, name);
65  }
66 
67  settings.endGroup();
68 }
69 
70 void
72 {
73  while (int(m_names.size()) > m_maxCount) {
74  m_names.pop_back();
75  }
76  write();
77 }
78 
79 std::vector<QString>
81 {
82  std::vector<QString> names;
83  for (int i = 0; i < m_maxCount; ++i) {
84  if (i < (int)m_names.size()) {
85  names.push_back(m_names[i]);
86  }
87  }
88  return names;
89 }
90 
91 void
92 RecentFiles::add(QString name)
93 {
94  bool have = false;
95  for (int i = 0; i < int(m_names.size()); ++i) {
96  if (m_names[i] == name) {
97  have = true;
98  break;
99  }
100  }
101 
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 
115  emit recentChanged();
116 }
117 
118 void
119 RecentFiles::addFile(QString name)
120 {
121  static QRegExp schemeRE("^[a-zA-Z]{2,5}://");
122  static QRegExp tempRE("[\\/][Tt]e?mp[\\/]");
123  if (schemeRE.indexIn(name) == 0) {
124  add(name);
125  } else {
126  QString absPath = QFileInfo(name).absoluteFilePath();
127  if (tempRE.indexIn(absPath) != -1) {
129  if (prefs && !prefs->getOmitTempsFromRecentFiles()) {
130  add(absPath);
131  }
132  } else {
133  add(absPath);
134  }
135  }
136 }
137 
138 
void recentChanged()
QString m_settingsGroup
Definition: RecentFiles.h:70
std::deque< QString > m_names
Definition: RecentFiles.h:73
void addFile(QString name)
Add a name that is known to be either a file path or a URL.
static Preferences * getInstance()
Definition: Preferences.cpp:31
bool getOmitTempsFromRecentFiles() const
Definition: Preferences.h:65
virtual ~RecentFiles()
Definition: RecentFiles.cpp:31
void write()
Definition: RecentFiles.cpp:55
void add(QString name)
Add a name that should be treated as a literal string.
Definition: RecentFiles.cpp:92
std::vector< QString > getRecent() const
Definition: RecentFiles.cpp:80
int m_maxCount
Definition: RecentFiles.h:71
void truncateAndWrite()
Definition: RecentFiles.cpp:71
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:24