1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
|
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
/*
Sonic Visualiser
An audio file viewer and annotation editor.
Centre for Digital Music, Queen Mary, University of London.
This file copyright 2006 Chris Cannam.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version. See the file
COPYING included with this distribution for more information.
*/
#include "RecentFiles.h"
#include "Preferences.h"
#include <QFileInfo>
#include <QSettings>
#include <QRegExp>
RecentFiles::RecentFiles(QString settingsGroup, int maxCount) :
m_settingsGroup(settingsGroup),
m_maxCount(maxCount)
{
read();
}
RecentFiles::~RecentFiles()
{
// nothing
}
void
RecentFiles::read()
{
m_names.clear();
QSettings settings;
settings.beginGroup(m_settingsGroup);
for (int i = 0; i < 100; ++i) {
QString key = QString("recent-%1").arg(i);
QString name = settings.value(key, "").toString();
if (name == "") break;
if (i < m_maxCount) m_names.push_back(name);
else settings.setValue(key, "");
}
settings.endGroup();
}
void
RecentFiles::write()
{
QSettings settings;
settings.beginGroup(m_settingsGroup);
for (int i = 0; i < m_maxCount; ++i) {
QString key = QString("recent-%1").arg(i);
QString name = "";
if (i < (int)m_names.size()) name = m_names[i];
settings.setValue(key, name);
}
settings.endGroup();
}
void
RecentFiles::truncateAndWrite()
{
while (int(m_names.size()) > m_maxCount) {
m_names.pop_back();
}
write();
}
std::vector<QString>
RecentFiles::getRecent() const
{
std::vector<QString> names;
for (int i = 0; i < m_maxCount; ++i) {
if (i < (int)m_names.size()) {
names.push_back(m_names[i]);
}
}
return names;
}
void
RecentFiles::add(QString name)
{
bool have = false;
for (int i = 0; i < int(m_names.size()); ++i) {
if (m_names[i] == name) {
have = true;
break;
}
}
if (!have) {
m_names.push_front(name);
} else {
std::deque<QString> newnames;
newnames.push_back(name);
for (int i = 0; i < int(m_names.size()); ++i) {
if (m_names[i] == name) continue;
newnames.push_back(m_names[i]);
}
m_names = newnames;
}
truncateAndWrite();
emit recentChanged();
}
void
RecentFiles::addFile(QString name)
{
static QRegExp schemeRE("^[a-zA-Z]{2,5}://");
static QRegExp tempRE("[\\/][Tt]e?mp[\\/]");
if (schemeRE.indexIn(name) == 0) {
add(name);
} else {
QString absPath = QFileInfo(name).absoluteFilePath();
if (tempRE.indexIn(absPath) != -1) {
Preferences *prefs = Preferences::getInstance();
if (prefs && !prefs->getOmitTempsFromRecentFiles()) {
add(absPath);
}
} else {
add(absPath);
}
}
}
|