annotate base/RecentFiles.cpp @ 158:74abef65711b

* Fixes pulled across from Rosegarden
author Chris Cannam
date Wed, 16 Aug 2006 14:57:22 +0000
parents 059b0322009c
children 702fc936e6a6
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@150 18 #include "Preferences.h"
Chris@149 19
Chris@149 20 #include <QFileInfo>
Chris@156 21 #include <QSettings>
Chris@149 22
Chris@149 23 RecentFiles *
Chris@149 24 RecentFiles::m_instance = 0;
Chris@149 25
Chris@149 26 RecentFiles *
Chris@149 27 RecentFiles::getInstance(int maxFileCount)
Chris@149 28 {
Chris@149 29 if (!m_instance) {
Chris@149 30 m_instance = new RecentFiles(maxFileCount);
Chris@149 31 }
Chris@149 32 return m_instance;
Chris@149 33 }
Chris@149 34
Chris@149 35 RecentFiles::RecentFiles(int maxFileCount) :
Chris@149 36 m_maxFileCount(maxFileCount)
Chris@149 37 {
Chris@149 38 readFiles();
Chris@149 39 }
Chris@149 40
Chris@149 41 RecentFiles::~RecentFiles()
Chris@149 42 {
Chris@149 43 // nothing
Chris@149 44 }
Chris@149 45
Chris@149 46 void
Chris@149 47 RecentFiles::readFiles()
Chris@149 48 {
Chris@149 49 m_files.clear();
Chris@156 50 QSettings settings;
Chris@156 51 settings.beginGroup("RecentFiles");
Chris@156 52
Chris@149 53 for (unsigned int i = 0; i < 100; ++i) {
Chris@149 54 QString key = QString("recent-file-%1").arg(i);
Chris@156 55 QString filename = settings.value(key, "").toString();
Chris@149 56 if (filename == "") break;
Chris@149 57 if (i < m_maxFileCount) m_files.push_back(filename);
Chris@156 58 else settings.setValue(key, "");
Chris@149 59 }
Chris@156 60
Chris@156 61 settings.endGroup();
Chris@149 62 }
Chris@149 63
Chris@149 64 void
Chris@149 65 RecentFiles::writeFiles()
Chris@149 66 {
Chris@156 67 QSettings settings;
Chris@156 68 settings.beginGroup("RecentFiles");
Chris@156 69
Chris@149 70 for (unsigned int i = 0; i < m_maxFileCount; ++i) {
Chris@149 71 QString key = QString("recent-file-%1").arg(i);
Chris@149 72 QString filename = "";
Chris@149 73 if (i < m_files.size()) filename = m_files[i];
Chris@156 74 settings.setValue(key, filename);
Chris@149 75 }
Chris@156 76
Chris@156 77 settings.endGroup();
Chris@149 78 }
Chris@149 79
Chris@149 80 void
Chris@149 81 RecentFiles::truncateAndWrite()
Chris@149 82 {
Chris@149 83 while (m_files.size() > m_maxFileCount) {
Chris@149 84 m_files.pop_back();
Chris@149 85 }
Chris@149 86 writeFiles();
Chris@149 87 }
Chris@149 88
Chris@149 89 std::vector<QString>
Chris@149 90 RecentFiles::getRecentFiles() const
Chris@149 91 {
Chris@149 92 std::vector<QString> files;
Chris@149 93 for (unsigned int i = 0; i < m_maxFileCount; ++i) {
Chris@149 94 if (i < m_files.size()) {
Chris@149 95 files.push_back(m_files[i]);
Chris@149 96 }
Chris@149 97 }
Chris@149 98 return files;
Chris@149 99 }
Chris@149 100
Chris@149 101 void
Chris@149 102 RecentFiles::addFile(QString filename)
Chris@149 103 {
Chris@149 104 filename = QFileInfo(filename).absoluteFilePath();
Chris@149 105
Chris@149 106 bool have = false;
Chris@149 107 for (unsigned int i = 0; i < m_files.size(); ++i) {
Chris@149 108 if (m_files[i] == filename) {
Chris@149 109 have = true;
Chris@149 110 break;
Chris@149 111 }
Chris@149 112 }
Chris@149 113
Chris@149 114 if (!have) {
Chris@149 115 m_files.push_front(filename);
Chris@149 116 } else {
Chris@149 117 std::deque<QString> newfiles;
Chris@149 118 newfiles.push_back(filename);
Chris@149 119 for (unsigned int i = 0; i < m_files.size(); ++i) {
Chris@149 120 if (m_files[i] == filename) continue;
Chris@149 121 newfiles.push_back(m_files[i]);
Chris@149 122 }
Chris@149 123 }
Chris@149 124
Chris@149 125 truncateAndWrite();
Chris@149 126 emit recentFilesChanged();
Chris@149 127 }
Chris@149 128
Chris@149 129