enough with umpteen "i18n.h" files. Consolidate on pbd/i18n.h
[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 <sstream>
23 #include <algorithm>
24
25 #include "pbd/gstdio_compat.h"
26 #include <glibmm/miscutils.h>
27
28 #include "pbd/error.h"
29
30 #include "ardour/rc_configuration.h"
31 #include "ardour/filesystem_paths.h"
32 #include "ardour/recent_sessions.h"
33
34 #include "pbd/i18n.h"
35
36 using namespace std;
37 using namespace ARDOUR;
38 using namespace PBD;
39
40 namespace {
41
42         const char * const recent_file_name = "recent";
43         const char * const recent_templates_file_name = "recent_templates";
44
45 } // anonymous
46
47 int
48 ARDOUR::read_recent_sessions (RecentSessions& rs)
49 {
50         std::string path = Glib::build_filename (user_config_directory(), recent_file_name);
51         FILE* fin = g_fopen (path.c_str(), "rb");
52
53         if (!fin) {
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         // Read the file into a std::string
63         std::stringstream recent;
64         while (!feof (fin)) {
65                 char buf[1024];
66                 size_t charsRead = fread (buf, sizeof(char), 1024, fin);
67                 if (ferror (fin)) {
68                         error << string_compose (_("Error reading recent session file %1 (%2)"), path, strerror (errno)) << endmsg;
69                         fclose(fin);
70                         return -1;
71                 }
72                 if (charsRead == 0) {
73                         break;
74                 }
75                 recent.write (buf, charsRead);
76         }
77
78         while (true) {
79
80                 pair<string,string> newpair;
81
82                 getline(recent, newpair.first);
83
84                 if (!recent.good()) {
85                         break;
86                 }
87
88                 getline(recent, newpair.second);
89
90                 if (!recent.good()) {
91                         break;
92                 }
93
94                 rs.push_back (newpair);
95         }
96
97         /* display sorting should be done in the GUI, otherwise the
98          * natural order will be broken
99          */
100
101         fclose (fin);
102         return 0;
103 }
104
105 int
106 ARDOUR::read_recent_templates (std::deque<std::string>& rt)
107 {
108         std::string path = Glib::build_filename (user_config_directory(), recent_templates_file_name);
109         FILE* fin = g_fopen (path.c_str(), "rb");
110
111         if (!fin) {
112                 if (errno != ENOENT) {
113                         error << string_compose (_("Cannot open recent template file %1 (%2)"), path, strerror (errno)) << endmsg;
114                         return -1;
115                 } else {
116                         return 1;
117                 }
118         }
119
120         // Copy the file contents into a std::stringstream
121         std::stringstream recent;
122         while (!feof (fin)) {
123                 char buf[1024];
124                 size_t charsRead = fread (buf, sizeof(char), 1024, fin);
125                 if (ferror (fin)) {
126                         error << string_compose (_("Error reading recent session file %1 (%2)"), path, strerror (errno)) << endmsg;
127                         fclose(fin);
128                         return -1;
129                 }
130                 if (charsRead == 0) {
131                         break;
132                 }
133                 recent.write (buf, charsRead);
134         }
135
136         while (true) {
137
138                 std::string session_template_full_name;
139
140                 getline(recent, session_template_full_name);
141
142                 if (!recent.good()) {
143                         break;
144                 }
145
146                 rt.push_back (session_template_full_name);
147         }
148
149         fclose (fin);
150         return 0;
151 }
152
153 int
154 ARDOUR::write_recent_sessions (RecentSessions& rs)
155 {
156         FILE* fout = g_fopen (Glib::build_filename (user_config_directory(), recent_file_name).c_str(), "wb");
157
158         if (!fout) {
159                 return -1;
160         }
161
162         {
163                 stringstream recent;
164
165                 for (RecentSessions::iterator i = rs.begin(); i != rs.end(); ++i) {
166                         recent << (*i).first << '\n' << (*i).second << endl;
167                 }
168
169                 string recentString = recent.str();
170                 size_t writeSize = recentString.length();
171
172                 fwrite(recentString.c_str(), sizeof(char), writeSize, fout);
173
174                 if (ferror(fout))
175                 {
176                         error << string_compose (_("Error writing recent sessions file %1 (%2)"), recent_file_name, strerror (errno)) << endmsg;
177                         fclose(fout);
178                         return -1;
179                 }
180         }
181
182
183
184         fclose (fout);
185
186         return 0;
187 }
188
189 int
190 ARDOUR::write_recent_templates (std::deque<std::string>& rt)
191 {
192         FILE* fout = g_fopen (Glib::build_filename (user_config_directory(), recent_templates_file_name).c_str(), "wb");
193
194         if (!fout) {
195                 return -1;
196         }
197
198         stringstream recent;
199
200         for (std::deque<std::string>::const_iterator i = rt.begin(); i != rt.end(); ++i) {
201                 recent << (*i) << std::endl;
202         }
203
204         string recentString = recent.str();
205         size_t writeSize = recentString.length();
206
207         fwrite(recentString.c_str(), sizeof(char), writeSize, fout);
208
209         if (ferror(fout))
210         {
211                 error << string_compose (_("Error writing saved template file %1 (%2)"), recent_templates_file_name, strerror (errno)) << endmsg;
212                 fclose(fout);
213                 return -1;
214         }
215
216         fclose (fout);
217
218         return 0;
219 }
220
221 int
222 ARDOUR::store_recent_sessions (string name, string path)
223 {
224         RecentSessions rs;
225
226         if (ARDOUR::read_recent_sessions (rs) < 0) {
227                 return -1;
228         }
229
230         pair<string,string> newpair;
231
232         newpair.first = name;
233         newpair.second = path;
234
235         rs.erase(remove(rs.begin(), rs.end(), newpair), rs.end());
236
237         rs.push_front (newpair);
238
239         uint32_t max_recent_sessions = Config->get_max_recent_sessions();
240
241         if (rs.size() > max_recent_sessions) {
242                 rs.erase(rs.begin()+max_recent_sessions, rs.end());
243         }
244
245         return ARDOUR::write_recent_sessions (rs);
246 }
247
248 int
249 ARDOUR::store_recent_templates (const std::string& session_template_full_name)
250 {
251         std::deque<std::string> rt;
252
253         if (ARDOUR::read_recent_templates (rt) < 0) {
254                 return -1;
255         }
256
257         rt.erase(remove (rt.begin(), rt.end(), session_template_full_name), rt.end());
258
259         rt.push_front (session_template_full_name);
260
261         uint32_t max_recent_templates = Config->get_max_recent_templates ();
262
263         if (rt.size() > max_recent_templates) {
264                 rt.erase( rt.begin() + max_recent_templates, rt.end ());
265         }
266
267         return ARDOUR::write_recent_templates (rt);
268 }
269
270 int
271 ARDOUR::remove_recent_sessions (const string& path)
272 {
273         RecentSessions rs;
274         bool write = false;
275
276         if (ARDOUR::read_recent_sessions (rs) < 0) {
277                 return -1;
278         }
279
280         for (RecentSessions::iterator i = rs.begin(); i != rs.end(); ++i) {
281                 if (i->second == path) {
282                         rs.erase (i);
283                         write = true;
284                         break;
285                 }
286         }
287
288         if (write) {
289                 return ARDOUR::write_recent_sessions (rs);
290         } else {
291                 return 1;
292         }
293 }