annotate base/RecentFiles.cpp @ 153:8d92611901d3

...
author Chris Cannam
date Wed, 02 Aug 2006 16:42:17 +0000
parents 4b2ea82fd0ed
children 059b0322009c
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 #include "ConfigFile.h"
Chris@149 18
Chris@150 19 #include "Preferences.h"
Chris@149 20
Chris@149 21 #include <QFileInfo>
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@149 50 ConfigFile *cf = Preferences::getInstance()->getConfigFile();
Chris@149 51 for (unsigned int i = 0; i < 100; ++i) {
Chris@149 52 QString key = QString("recent-file-%1").arg(i);
Chris@149 53 QString filename = cf->get(key);
Chris@149 54 if (filename == "") break;
Chris@149 55 if (i < m_maxFileCount) m_files.push_back(filename);
Chris@149 56 else cf->set(key, "");
Chris@149 57 }
Chris@149 58 cf->commit();
Chris@149 59 }
Chris@149 60
Chris@149 61 void
Chris@149 62 RecentFiles::writeFiles()
Chris@149 63 {
Chris@149 64 ConfigFile *cf = Preferences::getInstance()->getConfigFile();
Chris@149 65 for (unsigned int i = 0; i < m_maxFileCount; ++i) {
Chris@149 66 QString key = QString("recent-file-%1").arg(i);
Chris@149 67 QString filename = "";
Chris@149 68 if (i < m_files.size()) filename = m_files[i];
Chris@149 69 cf->set(key, filename);
Chris@149 70 }
Chris@149 71 cf->commit();
Chris@149 72 }
Chris@149 73
Chris@149 74 void
Chris@149 75 RecentFiles::truncateAndWrite()
Chris@149 76 {
Chris@149 77 while (m_files.size() > m_maxFileCount) {
Chris@149 78 m_files.pop_back();
Chris@149 79 }
Chris@149 80 writeFiles();
Chris@149 81 }
Chris@149 82
Chris@149 83 std::vector<QString>
Chris@149 84 RecentFiles::getRecentFiles() const
Chris@149 85 {
Chris@149 86 std::vector<QString> files;
Chris@149 87 for (unsigned int i = 0; i < m_maxFileCount; ++i) {
Chris@149 88 if (i < m_files.size()) {
Chris@149 89 files.push_back(m_files[i]);
Chris@149 90 }
Chris@149 91 }
Chris@149 92 return files;
Chris@149 93 }
Chris@149 94
Chris@149 95 void
Chris@149 96 RecentFiles::addFile(QString filename)
Chris@149 97 {
Chris@149 98 filename = QFileInfo(filename).absoluteFilePath();
Chris@149 99
Chris@149 100 bool have = false;
Chris@149 101 for (unsigned int i = 0; i < m_files.size(); ++i) {
Chris@149 102 if (m_files[i] == filename) {
Chris@149 103 have = true;
Chris@149 104 break;
Chris@149 105 }
Chris@149 106 }
Chris@149 107
Chris@149 108 if (!have) {
Chris@149 109 m_files.push_front(filename);
Chris@149 110 } else {
Chris@149 111 std::deque<QString> newfiles;
Chris@149 112 newfiles.push_back(filename);
Chris@149 113 for (unsigned int i = 0; i < m_files.size(); ++i) {
Chris@149 114 if (m_files[i] == filename) continue;
Chris@149 115 newfiles.push_back(m_files[i]);
Chris@149 116 }
Chris@149 117 }
Chris@149 118
Chris@149 119 truncateAndWrite();
Chris@149 120 emit recentFilesChanged();
Chris@149 121 }
Chris@149 122
Chris@149 123