Mercurial > hg > easyhg
comparison recentfiles.cpp @ 63:2340b00561d2
* Add (but don't yet use) RecentFiles class to replace inline MRU logic
| author | Chris Cannam |
|---|---|
| date | Wed, 17 Nov 2010 14:26:27 +0000 |
| parents | |
| children | 8fd71f570884 |
comparison
equal
deleted
inserted
replaced
| 62:68aebc316898 | 63:2340b00561d2 |
|---|---|
| 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ | |
| 2 | |
| 3 /* | |
| 4 EasyMercurial | |
| 5 | |
| 6 Based on HgExplorer by Jari Korhonen | |
| 7 Copyright (c) 2010 Jari Korhonen | |
| 8 Copyright (c) 2010 Chris Cannam | |
| 9 Copyright (c) 2010 Queen Mary, University of London | |
| 10 | |
| 11 This program is free software; you can redistribute it and/or | |
| 12 modify it under the terms of the GNU General Public License as | |
| 13 published by the Free Software Foundation; either version 2 of the | |
| 14 License, or (at your option) any later version. See the file | |
| 15 COPYING included with this distribution for more information. | |
| 16 */ | |
| 17 | |
| 18 #include "recentfiles.h" | |
| 19 | |
| 20 #include <QFileInfo> | |
| 21 #include <QSettings> | |
| 22 #include <QRegExp> | |
| 23 | |
| 24 RecentFiles::RecentFiles(QString settingsGroup, size_t maxCount, | |
| 25 bool ignoreTemporaries) : | |
| 26 m_settingsGroup(settingsGroup), | |
| 27 m_maxCount(maxCount), | |
| 28 m_ignoreTemporaries(ignoreTemporaries) | |
| 29 { | |
| 30 read(); | |
| 31 } | |
| 32 | |
| 33 RecentFiles::~RecentFiles() | |
| 34 { | |
| 35 // nothing | |
| 36 } | |
| 37 | |
| 38 void | |
| 39 RecentFiles::read() | |
| 40 { | |
| 41 m_names.clear(); | |
| 42 QSettings settings; | |
| 43 settings.beginGroup(m_settingsGroup); | |
| 44 | |
| 45 for (size_t i = 0; i < 100; ++i) { | |
| 46 QString key = QString("recent-%1").arg(i); | |
| 47 QString name = settings.value(key, "").toString(); | |
| 48 if (name == "") break; | |
| 49 if (i < m_maxCount) m_names.push_back(name); | |
| 50 else settings.setValue(key, ""); | |
| 51 } | |
| 52 | |
| 53 settings.endGroup(); | |
| 54 } | |
| 55 | |
| 56 void | |
| 57 RecentFiles::write() | |
| 58 { | |
| 59 QSettings settings; | |
| 60 settings.beginGroup(m_settingsGroup); | |
| 61 | |
| 62 for (size_t i = 0; i < m_maxCount; ++i) { | |
| 63 QString key = QString("recent-%1").arg(i); | |
| 64 QString name = ""; | |
| 65 if (i < m_names.size()) name = m_names[i]; | |
| 66 settings.setValue(key, name); | |
| 67 } | |
| 68 | |
| 69 settings.endGroup(); | |
| 70 } | |
| 71 | |
| 72 void | |
| 73 RecentFiles::truncateAndWrite() | |
| 74 { | |
| 75 while (m_names.size() > m_maxCount) { | |
| 76 m_names.pop_back(); | |
| 77 } | |
| 78 write(); | |
| 79 } | |
| 80 | |
| 81 QStringList | |
| 82 RecentFiles::getRecent() const | |
| 83 { | |
| 84 QStringList names; | |
| 85 for (size_t i = 0; i < m_maxCount; ++i) { | |
| 86 if (i < m_names.size()) { | |
| 87 names.push_back(m_names[i]); | |
| 88 } | |
| 89 } | |
| 90 return names; | |
| 91 } | |
| 92 | |
| 93 void | |
| 94 RecentFiles::add(QString name) | |
| 95 { | |
| 96 bool have = false; | |
| 97 for (size_t i = 0; i < m_names.size(); ++i) { | |
| 98 if (m_names[i] == name) { | |
| 99 have = true; | |
| 100 break; | |
| 101 } | |
| 102 } | |
| 103 | |
| 104 if (!have) { | |
| 105 m_names.push_front(name); | |
| 106 } else { | |
| 107 std::deque<QString> newnames; | |
| 108 newnames.push_back(name); | |
| 109 for (size_t i = 0; i < m_names.size(); ++i) { | |
| 110 if (m_names[i] == name) continue; | |
| 111 newnames.push_back(m_names[i]); | |
| 112 } | |
| 113 m_names = newnames; | |
| 114 } | |
| 115 | |
| 116 truncateAndWrite(); | |
| 117 emit recentChanged(); | |
| 118 } | |
| 119 | |
| 120 void | |
| 121 RecentFiles::addFile(QString name) | |
| 122 { | |
| 123 static QRegExp schemeRE("^[a-zA-Z]{2,5}://"); | |
| 124 static QRegExp tempRE("[\\/][Tt]e?mp[\\/]"); | |
| 125 if (schemeRE.indexIn(name) == 0) { | |
| 126 add(name); | |
| 127 } else { | |
| 128 QString absPath = QFileInfo(name).absoluteFilePath(); | |
| 129 if (tempRE.indexIn(absPath) != -1) { | |
| 130 if (!m_ignoreTemporaries) { | |
| 131 add(absPath); | |
| 132 } | |
| 133 } else { | |
| 134 add(absPath); | |
| 135 } | |
| 136 } | |
| 137 } | |
| 138 | |
| 139 |
