svcore
1.9
|
00001 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ 00002 00003 /* 00004 Sonic Visualiser 00005 An audio file viewer and annotation editor. 00006 Centre for Digital Music, Queen Mary, University of London. 00007 This file copyright 2006 Chris Cannam. 00008 00009 This program is free software; you can redistribute it and/or 00010 modify it under the terms of the GNU General Public License as 00011 published by the Free Software Foundation; either version 2 of the 00012 License, or (at your option) any later version. See the file 00013 COPYING included with this distribution for more information. 00014 */ 00015 00016 #include "RecentFiles.h" 00017 00018 #include "Preferences.h" 00019 00020 #include <QFileInfo> 00021 #include <QSettings> 00022 #include <QRegExp> 00023 00024 RecentFiles::RecentFiles(QString settingsGroup, int maxCount) : 00025 m_settingsGroup(settingsGroup), 00026 m_maxCount(maxCount) 00027 { 00028 read(); 00029 } 00030 00031 RecentFiles::~RecentFiles() 00032 { 00033 // nothing 00034 } 00035 00036 void 00037 RecentFiles::read() 00038 { 00039 m_names.clear(); 00040 QSettings settings; 00041 settings.beginGroup(m_settingsGroup); 00042 00043 for (int i = 0; i < 100; ++i) { 00044 QString key = QString("recent-%1").arg(i); 00045 QString name = settings.value(key, "").toString(); 00046 if (name == "") break; 00047 if (i < m_maxCount) m_names.push_back(name); 00048 else settings.setValue(key, ""); 00049 } 00050 00051 settings.endGroup(); 00052 } 00053 00054 void 00055 RecentFiles::write() 00056 { 00057 QSettings settings; 00058 settings.beginGroup(m_settingsGroup); 00059 00060 for (int i = 0; i < m_maxCount; ++i) { 00061 QString key = QString("recent-%1").arg(i); 00062 QString name = ""; 00063 if (i < (int)m_names.size()) name = m_names[i]; 00064 settings.setValue(key, name); 00065 } 00066 00067 settings.endGroup(); 00068 } 00069 00070 void 00071 RecentFiles::truncateAndWrite() 00072 { 00073 while (int(m_names.size()) > m_maxCount) { 00074 m_names.pop_back(); 00075 } 00076 write(); 00077 } 00078 00079 std::vector<QString> 00080 RecentFiles::getRecent() const 00081 { 00082 std::vector<QString> names; 00083 for (int i = 0; i < m_maxCount; ++i) { 00084 if (i < (int)m_names.size()) { 00085 names.push_back(m_names[i]); 00086 } 00087 } 00088 return names; 00089 } 00090 00091 void 00092 RecentFiles::add(QString name) 00093 { 00094 bool have = false; 00095 for (int i = 0; i < int(m_names.size()); ++i) { 00096 if (m_names[i] == name) { 00097 have = true; 00098 break; 00099 } 00100 } 00101 00102 if (!have) { 00103 m_names.push_front(name); 00104 } else { 00105 std::deque<QString> newnames; 00106 newnames.push_back(name); 00107 for (int i = 0; i < int(m_names.size()); ++i) { 00108 if (m_names[i] == name) continue; 00109 newnames.push_back(m_names[i]); 00110 } 00111 m_names = newnames; 00112 } 00113 00114 truncateAndWrite(); 00115 emit recentChanged(); 00116 } 00117 00118 void 00119 RecentFiles::addFile(QString name) 00120 { 00121 static QRegExp schemeRE("^[a-zA-Z]{2,5}://"); 00122 static QRegExp tempRE("[\\/][Tt]e?mp[\\/]"); 00123 if (schemeRE.indexIn(name) == 0) { 00124 add(name); 00125 } else { 00126 QString absPath = QFileInfo(name).absoluteFilePath(); 00127 if (tempRE.indexIn(absPath) != -1) { 00128 Preferences *prefs = Preferences::getInstance(); 00129 if (prefs && !prefs->getOmitTempsFromRecentFiles()) { 00130 add(absPath); 00131 } 00132 } else { 00133 add(absPath); 00134 } 00135 } 00136 } 00137 00138