7458c32ff8ac979ed7e937aacd3006c68be77e67
[ardour.git] / libs / ardour / recent_sessions.cc
1 /*
2     Copyright (C) 2004 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <cstring>
21 #include <cerrno>
22 #include <fstream>
23 #include <algorithm>
24
25 #include <glibmm/miscutils.h>
26
27 #include "pbd/error.h"
28
29 #include "ardour/rc_configuration.h"
30 #include "ardour/filesystem_paths.h"
31 #include "ardour/recent_sessions.h"
32
33 #include "i18n.h"
34
35 using namespace std;
36 using namespace ARDOUR;
37 using namespace PBD;
38
39 namespace {
40
41         const char * const recent_file_name = "recent";
42         const char * const recent_templates_file_name = "recent_templates";
43
44 } // anonymous
45
46 int
47 ARDOUR::read_recent_sessions (RecentSessions& rs)
48 {
49         std::string path = Glib::build_filename (user_config_directory(), recent_file_name);
50
51         ifstream recent (path.c_str());
52
53         if (!recent) {
54                 if (errno != ENOENT) {
55                         error << string_compose (_("cannot open recent session file %1 (%2)"), path, strerror (errno)) << endmsg;
56                         return -1;
57                 } else {
58                         return 1;
59                 }
60         }
61
62         while (true) {
63
64                 pair<string,string> newpair;
65
66                 getline(recent, newpair.first);
67
68                 if (!recent.good()) {
69                         break;
70                 }
71
72                 getline(recent, newpair.second);
73
74                 if (!recent.good()) {
75                         break;
76                 }
77
78                 rs.push_back (newpair);
79         }
80
81         /* display sorting should be done in the GUI, otherwise the
82          * natural order will be broken
83          */
84
85         return 0;
86 }
87
88 int
89 ARDOUR::read_recent_templates (std::deque<std::string>& rt)
90 {
91         std::string path = Glib::build_filename (user_config_directory(), recent_templates_file_name);
92
93         ifstream recent (path.c_str());
94
95         if (!recent) {
96                 if (errno != ENOENT) {
97                         error << string_compose (_("cannot open recent template file %1 (%2)"), path, strerror (errno)) << endmsg;
98                         return -1;
99                 } else {
100                         return 1;
101                 }
102         }
103
104         while (true) {
105
106                 std::string session_template_full_name;
107
108                 getline(recent, session_template_full_name);
109
110                 if (!recent.good()) {
111                         break;
112                 }
113
114                 rt.push_back (session_template_full_name);
115         }
116
117         return 0;
118 }
119
120 int
121 ARDOUR::write_recent_sessions (RecentSessions& rs)
122 {
123         std::string path = Glib::build_filename (user_config_directory(), recent_file_name);
124
125         ofstream recent (path.c_str());
126
127         if (!recent) {
128                 return -1;
129         }
130
131         for (RecentSessions::iterator i = rs.begin(); i != rs.end(); ++i) {
132                 recent << (*i).first << '\n' << (*i).second << endl;
133         }
134
135         return 0;
136 }
137
138 int
139 ARDOUR::write_recent_templates (std::deque<std::string>& rt)
140 {
141         std::string path = Glib::build_filename (user_config_directory(), recent_templates_file_name);
142
143         std::ofstream recent (path.c_str());
144
145         if (!recent) {
146                 return -1;
147         }
148
149         for (std::deque<std::string>::const_iterator i = rt.begin(); i != rt.end(); ++i) {
150                 recent << (*i) << std::endl;
151         }
152
153         return 0;
154 }
155
156 int
157 ARDOUR::store_recent_sessions (string name, string path)
158 {
159         RecentSessions rs;
160
161         if (ARDOUR::read_recent_sessions (rs) < 0) {
162                 return -1;
163         }
164
165         pair<string,string> newpair;
166
167         newpair.first = name;
168         newpair.second = path;
169
170         rs.erase(remove(rs.begin(), rs.end(), newpair), rs.end());
171
172         rs.push_front (newpair);
173
174         uint32_t max_recent_sessions = Config->get_max_recent_sessions();
175
176         if (rs.size() > max_recent_sessions) {
177                 rs.erase(rs.begin()+max_recent_sessions, rs.end());
178         }
179
180         return ARDOUR::write_recent_sessions (rs);
181 }
182
183 int
184 ARDOUR::store_recent_templates (const std::string& session_template_full_name)
185 {
186         std::deque<std::string> rt;
187
188         if (ARDOUR::read_recent_templates (rt) < 0) {
189                 return -1;
190         }
191
192         rt.erase(remove (rt.begin(), rt.end(), session_template_full_name), rt.end());
193
194         rt.push_front (session_template_full_name);
195
196         uint32_t max_recent_templates = Config->get_max_recent_templates ();
197
198         if (rt.size() > max_recent_templates) {
199                 rt.erase( rt.begin() + max_recent_templates, rt.end ());
200         }
201
202         return ARDOUR::write_recent_templates (rt);
203 }
204
205 int
206 ARDOUR::remove_recent_sessions (const string& path)
207 {
208         RecentSessions rs;
209         bool write = false;
210
211         if (ARDOUR::read_recent_sessions (rs) < 0) {
212                 return -1;
213         }
214
215         for (RecentSessions::iterator i = rs.begin(); i != rs.end(); ++i) {
216                 if (i->second == path) {
217                         rs.erase (i);
218                         write = true;
219                         break;
220                 }
221         }
222
223         if (write) {
224                 return ARDOUR::write_recent_sessions (rs);
225         } else {
226                 return 1;
227         }
228 }
229