Chris@149: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
Chris@149: 
Chris@149: /*
Chris@149:     Sonic Visualiser
Chris@149:     An audio file viewer and annotation editor.
Chris@149:     Centre for Digital Music, Queen Mary, University of London.
Chris@149:     This file copyright 2006 Chris Cannam.
Chris@149:     
Chris@149:     This program is free software; you can redistribute it and/or
Chris@149:     modify it under the terms of the GNU General Public License as
Chris@149:     published by the Free Software Foundation; either version 2 of the
Chris@149:     License, or (at your option) any later version.  See the file
Chris@149:     COPYING included with this distribution for more information.
Chris@149: */
Chris@149: 
Chris@149: #include "RecentFiles.h"
Chris@149: 
Chris@277: #include "Preferences.h"
Chris@277: 
Chris@149: #include <QFileInfo>
Chris@156: #include <QSettings>
Chris@209: #include <QRegExp>
Chris@149: 
Chris@928: RecentFiles::RecentFiles(QString settingsGroup, int maxCount) :
Chris@166:     m_settingsGroup(settingsGroup),
Chris@166:     m_maxCount(maxCount)
Chris@149: {
Chris@166:     read();
Chris@149: }
Chris@149: 
Chris@149: RecentFiles::~RecentFiles()
Chris@149: {
Chris@149:     // nothing
Chris@149: }
Chris@149: 
Chris@149: void
Chris@166: RecentFiles::read()
Chris@149: {
Chris@166:     m_names.clear();
Chris@156:     QSettings settings;
Chris@166:     settings.beginGroup(m_settingsGroup);
Chris@156: 
Chris@928:     for (int i = 0; i < 100; ++i) {
Chris@166:         QString key = QString("recent-%1").arg(i);
Chris@166:         QString name = settings.value(key, "").toString();
Chris@166:         if (name == "") break;
Chris@166:         if (i < m_maxCount) m_names.push_back(name);
Chris@156:         else settings.setValue(key, "");
Chris@149:     }
Chris@156: 
Chris@156:     settings.endGroup();
Chris@149: }
Chris@149: 
Chris@149: void
Chris@166: RecentFiles::write()
Chris@149: {
Chris@156:     QSettings settings;
Chris@166:     settings.beginGroup(m_settingsGroup);
Chris@156: 
Chris@928:     for (int i = 0; i < m_maxCount; ++i) {
Chris@166:         QString key = QString("recent-%1").arg(i);
Chris@166:         QString name = "";
Chris@928:         if (i < (int)m_names.size()) name = m_names[i];
Chris@166:         settings.setValue(key, name);
Chris@149:     }
Chris@156: 
Chris@156:     settings.endGroup();
Chris@149: }
Chris@149: 
Chris@149: void
Chris@149: RecentFiles::truncateAndWrite()
Chris@149: {
Chris@928:     while (int(m_names.size()) > m_maxCount) {
Chris@166:         m_names.pop_back();
Chris@149:     }
Chris@166:     write();
Chris@149: }
Chris@149: 
Chris@149: std::vector<QString>
Chris@166: RecentFiles::getRecent() const
Chris@149: {
Chris@166:     std::vector<QString> names;
Chris@928:     for (int i = 0; i < m_maxCount; ++i) {
Chris@928:         if (i < (int)m_names.size()) {
Chris@166:             names.push_back(m_names[i]);
Chris@149:         }
Chris@149:     }
Chris@166:     return names;
Chris@149: }
Chris@149: 
Chris@149: void
Chris@166: RecentFiles::add(QString name)
Chris@149: {
Chris@149:     bool have = false;
Chris@928:     for (int i = 0; i < int(m_names.size()); ++i) {
Chris@166:         if (m_names[i] == name) {
Chris@149:             have = true;
Chris@149:             break;
Chris@149:         }
Chris@149:     }
Chris@149:     
Chris@149:     if (!have) {
Chris@166:         m_names.push_front(name);
Chris@149:     } else {
Chris@166:         std::deque<QString> newnames;
Chris@166:         newnames.push_back(name);
Chris@928:         for (int i = 0; i < int(m_names.size()); ++i) {
Chris@166:             if (m_names[i] == name) continue;
Chris@166:             newnames.push_back(m_names[i]);
Chris@149:         }
Chris@185:         m_names = newnames;
Chris@149:     }
Chris@149: 
Chris@149:     truncateAndWrite();
Chris@166:     emit recentChanged();
Chris@149: }
Chris@149: 
Chris@166: void
Chris@166: RecentFiles::addFile(QString name)
Chris@166: {
Chris@209:     static QRegExp schemeRE("^[a-zA-Z]{2,5}://");
Chris@277:     static QRegExp tempRE("[\\/][Tt]e?mp[\\/]");
Chris@209:     if (schemeRE.indexIn(name) == 0) {
Chris@209:         add(name);
Chris@209:     } else {
Chris@277:         QString absPath = QFileInfo(name).absoluteFilePath();
Chris@277:         if (tempRE.indexIn(absPath) != -1) {
Chris@277:             Preferences *prefs = Preferences::getInstance();
Chris@277:             if (prefs && !prefs->getOmitTempsFromRecentFiles()) {
Chris@277:                 add(absPath);
Chris@277:             }
Chris@277:         } else {
Chris@277:             add(absPath);
Chris@277:         }
Chris@209:     }
Chris@166: }
Chris@149: 
Chris@166: