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