comparison base/RecentFiles.cpp @ 149:3e4c384f518e

* Reorganising code base. This revision will not compile.
author Chris Cannam
date Mon, 31 Jul 2006 12:03:45 +0000
parents
children 4b2ea82fd0ed
comparison
equal deleted inserted replaced
148:1a42221a1522 149:3e4c384f518e
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2
3 /*
4 Sonic Visualiser
5 An audio file viewer and annotation editor.
6 Centre for Digital Music, Queen Mary, University of London.
7 This file copyright 2006 Chris Cannam.
8
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License as
11 published by the Free Software Foundation; either version 2 of the
12 License, or (at your option) any later version. See the file
13 COPYING included with this distribution for more information.
14 */
15
16 #include "RecentFiles.h"
17 #include "ConfigFile.h"
18
19 #include "base/Preferences.h"
20
21 #include <QFileInfo>
22
23 RecentFiles *
24 RecentFiles::m_instance = 0;
25
26 RecentFiles *
27 RecentFiles::getInstance(int maxFileCount)
28 {
29 if (!m_instance) {
30 m_instance = new RecentFiles(maxFileCount);
31 }
32 return m_instance;
33 }
34
35 RecentFiles::RecentFiles(int maxFileCount) :
36 m_maxFileCount(maxFileCount)
37 {
38 readFiles();
39 }
40
41 RecentFiles::~RecentFiles()
42 {
43 // nothing
44 }
45
46 void
47 RecentFiles::readFiles()
48 {
49 m_files.clear();
50 ConfigFile *cf = Preferences::getInstance()->getConfigFile();
51 for (unsigned int i = 0; i < 100; ++i) {
52 QString key = QString("recent-file-%1").arg(i);
53 QString filename = cf->get(key);
54 if (filename == "") break;
55 if (i < m_maxFileCount) m_files.push_back(filename);
56 else cf->set(key, "");
57 }
58 cf->commit();
59 }
60
61 void
62 RecentFiles::writeFiles()
63 {
64 ConfigFile *cf = Preferences::getInstance()->getConfigFile();
65 for (unsigned int i = 0; i < m_maxFileCount; ++i) {
66 QString key = QString("recent-file-%1").arg(i);
67 QString filename = "";
68 if (i < m_files.size()) filename = m_files[i];
69 cf->set(key, filename);
70 }
71 cf->commit();
72 }
73
74 void
75 RecentFiles::truncateAndWrite()
76 {
77 while (m_files.size() > m_maxFileCount) {
78 m_files.pop_back();
79 }
80 writeFiles();
81 }
82
83 std::vector<QString>
84 RecentFiles::getRecentFiles() const
85 {
86 std::vector<QString> files;
87 for (unsigned int i = 0; i < m_maxFileCount; ++i) {
88 if (i < m_files.size()) {
89 files.push_back(m_files[i]);
90 }
91 }
92 return files;
93 }
94
95 void
96 RecentFiles::addFile(QString filename)
97 {
98 filename = QFileInfo(filename).absoluteFilePath();
99
100 bool have = false;
101 for (unsigned int i = 0; i < m_files.size(); ++i) {
102 if (m_files[i] == filename) {
103 have = true;
104 break;
105 }
106 }
107
108 if (!have) {
109 m_files.push_front(filename);
110 } else {
111 std::deque<QString> newfiles;
112 newfiles.push_back(filename);
113 for (unsigned int i = 0; i < m_files.size(); ++i) {
114 if (m_files[i] == filename) continue;
115 newfiles.push_back(m_files[i]);
116 }
117 }
118
119 truncateAndWrite();
120 emit recentFilesChanged();
121 }
122
123