Chris@148: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@148: Chris@148: /* Chris@148: Sonic Visualiser Chris@148: An audio file viewer and annotation editor. Chris@148: Centre for Digital Music, Queen Mary, University of London. Chris@148: This file copyright 2006 Chris Cannam. Chris@148: Chris@148: This program is free software; you can redistribute it and/or Chris@148: modify it under the terms of the GNU General Public License as Chris@148: published by the Free Software Foundation; either version 2 of the Chris@148: License, or (at your option) any later version. See the file Chris@148: COPYING included with this distribution for more information. Chris@148: */ Chris@148: Chris@148: #include "RecentFiles.h" Chris@148: #include "ConfigFile.h" Chris@148: Chris@148: #include "base/Preferences.h" Chris@148: Chris@148: #include Chris@148: Chris@148: RecentFiles * Chris@148: RecentFiles::m_instance = 0; Chris@148: Chris@148: RecentFiles * Chris@148: RecentFiles::getInstance(int maxFileCount) Chris@148: { Chris@148: if (!m_instance) { Chris@148: m_instance = new RecentFiles(maxFileCount); Chris@148: } Chris@148: return m_instance; Chris@148: } Chris@148: Chris@148: RecentFiles::RecentFiles(int maxFileCount) : Chris@148: m_maxFileCount(maxFileCount) Chris@148: { Chris@148: readFiles(); Chris@148: } Chris@148: Chris@148: RecentFiles::~RecentFiles() Chris@148: { Chris@148: // nothing Chris@148: } Chris@148: Chris@148: void Chris@148: RecentFiles::readFiles() Chris@148: { Chris@148: m_files.clear(); Chris@148: ConfigFile *cf = Preferences::getInstance()->getConfigFile(); Chris@148: for (unsigned int i = 0; i < 100; ++i) { Chris@148: QString key = QString("recent-file-%1").arg(i); Chris@148: QString filename = cf->get(key); Chris@148: if (filename == "") break; Chris@148: if (i < m_maxFileCount) m_files.push_back(filename); Chris@148: else cf->set(key, ""); Chris@148: } Chris@148: cf->commit(); Chris@148: } Chris@148: Chris@148: void Chris@148: RecentFiles::writeFiles() Chris@148: { Chris@148: ConfigFile *cf = Preferences::getInstance()->getConfigFile(); Chris@148: for (unsigned int i = 0; i < m_maxFileCount; ++i) { Chris@148: QString key = QString("recent-file-%1").arg(i); Chris@148: QString filename = ""; Chris@148: if (i < m_files.size()) filename = m_files[i]; Chris@148: cf->set(key, filename); Chris@148: } Chris@148: cf->commit(); Chris@148: } Chris@148: Chris@148: void Chris@148: RecentFiles::truncateAndWrite() Chris@148: { Chris@148: while (m_files.size() > m_maxFileCount) { Chris@148: m_files.pop_back(); Chris@148: } Chris@148: writeFiles(); Chris@148: } Chris@148: Chris@148: std::vector Chris@148: RecentFiles::getRecentFiles() const Chris@148: { Chris@148: std::vector files; Chris@148: for (unsigned int i = 0; i < m_maxFileCount; ++i) { Chris@148: if (i < m_files.size()) { Chris@148: files.push_back(m_files[i]); Chris@148: } Chris@148: } Chris@148: return files; Chris@148: } Chris@148: Chris@148: void Chris@148: RecentFiles::addFile(QString filename) Chris@148: { Chris@148: filename = QFileInfo(filename).absoluteFilePath(); Chris@148: Chris@148: bool have = false; Chris@148: for (unsigned int i = 0; i < m_files.size(); ++i) { Chris@148: if (m_files[i] == filename) { Chris@148: have = true; Chris@148: break; Chris@148: } Chris@148: } Chris@148: Chris@148: if (!have) { Chris@148: m_files.push_front(filename); Chris@148: } else { Chris@148: std::deque newfiles; Chris@148: newfiles.push_back(filename); Chris@148: for (unsigned int i = 0; i < m_files.size(); ++i) { Chris@148: if (m_files[i] == filename) continue; Chris@148: newfiles.push_back(m_files[i]); Chris@148: } Chris@148: } Chris@148: Chris@148: truncateAndWrite(); Chris@148: emit recentFilesChanged(); Chris@148: } Chris@148: Chris@148: