annotate base/RecentFiles.cpp @ 1300:4a2bc07ec0fb 3.0-integration

Make CSV feature writer produce the same output with Qt 5.6 and 5.7+
author Chris Cannam
date Fri, 25 Nov 2016 17:33:44 +0000
parents 6a94bb528e9d
children 452b48b29c2d
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@149 16 #include "RecentFiles.h"
Chris@149 17
Chris@277 18 #include "Preferences.h"
Chris@277 19
Chris@149 20 #include <QFileInfo>
Chris@156 21 #include <QSettings>
Chris@209 22 #include <QRegExp>
Chris@149 23
Chris@928 24 RecentFiles::RecentFiles(QString settingsGroup, int maxCount) :
Chris@166 25 m_settingsGroup(settingsGroup),
Chris@166 26 m_maxCount(maxCount)
Chris@149 27 {
Chris@166 28 read();
Chris@149 29 }
Chris@149 30
Chris@149 31 RecentFiles::~RecentFiles()
Chris@149 32 {
Chris@149 33 // nothing
Chris@149 34 }
Chris@149 35
Chris@149 36 void
Chris@166 37 RecentFiles::read()
Chris@149 38 {
Chris@166 39 m_names.clear();
Chris@156 40 QSettings settings;
Chris@166 41 settings.beginGroup(m_settingsGroup);
Chris@156 42
Chris@928 43 for (int i = 0; i < 100; ++i) {
Chris@166 44 QString key = QString("recent-%1").arg(i);
Chris@166 45 QString name = settings.value(key, "").toString();
Chris@166 46 if (name == "") break;
Chris@166 47 if (i < m_maxCount) m_names.push_back(name);
Chris@156 48 else settings.setValue(key, "");
Chris@149 49 }
Chris@156 50
Chris@156 51 settings.endGroup();
Chris@149 52 }
Chris@149 53
Chris@149 54 void
Chris@166 55 RecentFiles::write()
Chris@149 56 {
Chris@156 57 QSettings settings;
Chris@166 58 settings.beginGroup(m_settingsGroup);
Chris@156 59
Chris@928 60 for (int i = 0; i < m_maxCount; ++i) {
Chris@166 61 QString key = QString("recent-%1").arg(i);
Chris@166 62 QString name = "";
Chris@928 63 if (i < (int)m_names.size()) name = m_names[i];
Chris@166 64 settings.setValue(key, name);
Chris@149 65 }
Chris@156 66
Chris@156 67 settings.endGroup();
Chris@149 68 }
Chris@149 69
Chris@149 70 void
Chris@149 71 RecentFiles::truncateAndWrite()
Chris@149 72 {
Chris@928 73 while (int(m_names.size()) > m_maxCount) {
Chris@166 74 m_names.pop_back();
Chris@149 75 }
Chris@166 76 write();
Chris@149 77 }
Chris@149 78
Chris@149 79 std::vector<QString>
Chris@166 80 RecentFiles::getRecent() const
Chris@149 81 {
Chris@166 82 std::vector<QString> names;
Chris@928 83 for (int i = 0; i < m_maxCount; ++i) {
Chris@928 84 if (i < (int)m_names.size()) {
Chris@166 85 names.push_back(m_names[i]);
Chris@149 86 }
Chris@149 87 }
Chris@166 88 return names;
Chris@149 89 }
Chris@149 90
Chris@149 91 void
Chris@166 92 RecentFiles::add(QString name)
Chris@149 93 {
Chris@149 94 bool have = false;
Chris@928 95 for (int i = 0; i < int(m_names.size()); ++i) {
Chris@166 96 if (m_names[i] == name) {
Chris@149 97 have = true;
Chris@149 98 break;
Chris@149 99 }
Chris@149 100 }
Chris@149 101
Chris@149 102 if (!have) {
Chris@166 103 m_names.push_front(name);
Chris@149 104 } else {
Chris@166 105 std::deque<QString> newnames;
Chris@166 106 newnames.push_back(name);
Chris@928 107 for (int i = 0; i < int(m_names.size()); ++i) {
Chris@166 108 if (m_names[i] == name) continue;
Chris@166 109 newnames.push_back(m_names[i]);
Chris@149 110 }
Chris@185 111 m_names = newnames;
Chris@149 112 }
Chris@149 113
Chris@149 114 truncateAndWrite();
Chris@166 115 emit recentChanged();
Chris@149 116 }
Chris@149 117
Chris@166 118 void
Chris@166 119 RecentFiles::addFile(QString name)
Chris@166 120 {
Chris@209 121 static QRegExp schemeRE("^[a-zA-Z]{2,5}://");
Chris@277 122 static QRegExp tempRE("[\\/][Tt]e?mp[\\/]");
Chris@209 123 if (schemeRE.indexIn(name) == 0) {
Chris@209 124 add(name);
Chris@209 125 } else {
Chris@277 126 QString absPath = QFileInfo(name).absoluteFilePath();
Chris@277 127 if (tempRE.indexIn(absPath) != -1) {
Chris@277 128 Preferences *prefs = Preferences::getInstance();
Chris@277 129 if (prefs && !prefs->getOmitTempsFromRecentFiles()) {
Chris@277 130 add(absPath);
Chris@277 131 }
Chris@277 132 } else {
Chris@277 133 add(absPath);
Chris@277 134 }
Chris@209 135 }
Chris@166 136 }
Chris@149 137
Chris@166 138