2 Copyright (C) 2004 Paul Davis
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.
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.
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.
25 #include "pbd/gstdio_compat.h"
26 #include <glibmm/miscutils.h>
28 #include "pbd/error.h"
30 #include "ardour/rc_configuration.h"
31 #include "ardour/filesystem_paths.h"
32 #include "ardour/recent_sessions.h"
37 using namespace ARDOUR;
42 const char * const recent_file_name = "recent";
43 const char * const recent_templates_file_name = "recent_templates";
48 ARDOUR::read_recent_sessions (RecentSessions& rs)
50 std::string path = Glib::build_filename (user_config_directory(), recent_file_name);
51 FILE* fin = g_fopen (path.c_str(), "rb");
54 if (errno != ENOENT) {
55 error << string_compose (_("cannot open recent session file %1 (%2)"), path, strerror (errno)) << endmsg;
62 // Read the file into a std::string
63 std::stringstream recent;
66 size_t charsRead = fread (buf, sizeof(char), 1024, fin);
68 error << string_compose (_("Error reading recent session file %1 (%2)"), path, strerror (errno)) << endmsg;
75 recent.write (buf, charsRead);
80 pair<string,string> newpair;
82 getline(recent, newpair.first);
88 getline(recent, newpair.second);
94 rs.push_back (newpair);
97 /* display sorting should be done in the GUI, otherwise the
98 * natural order will be broken
106 ARDOUR::read_recent_templates (std::deque<std::string>& rt)
108 std::string path = Glib::build_filename (user_config_directory(), recent_templates_file_name);
109 FILE* fin = g_fopen (path.c_str(), "rb");
112 if (errno != ENOENT) {
113 error << string_compose (_("Cannot open recent template file %1 (%2)"), path, strerror (errno)) << endmsg;
120 // Copy the file contents into a std::stringstream
121 std::stringstream recent;
122 while (!feof (fin)) {
124 size_t charsRead = fread (buf, sizeof(char), 1024, fin);
126 error << string_compose (_("Error reading recent session file %1 (%2)"), path, strerror (errno)) << endmsg;
130 if (charsRead == 0) {
133 recent.write (buf, charsRead);
138 std::string session_template_full_name;
140 getline(recent, session_template_full_name);
142 if (!recent.good()) {
146 rt.push_back (session_template_full_name);
154 ARDOUR::write_recent_sessions (RecentSessions& rs)
156 FILE* fout = g_fopen (Glib::build_filename (user_config_directory(), recent_file_name).c_str(), "wb");
165 for (RecentSessions::iterator i = rs.begin(); i != rs.end(); ++i) {
166 recent << (*i).first << '\n' << (*i).second << endl;
169 string recentString = recent.str();
170 size_t writeSize = recentString.length();
172 fwrite(recentString.c_str(), sizeof(char), writeSize, fout);
176 error << string_compose (_("Error writing recent sessions file %1 (%2)"), recent_file_name, strerror (errno)) << endmsg;
190 ARDOUR::write_recent_templates (std::deque<std::string>& rt)
192 FILE* fout = g_fopen (Glib::build_filename (user_config_directory(), recent_templates_file_name).c_str(), "wb");
200 for (std::deque<std::string>::const_iterator i = rt.begin(); i != rt.end(); ++i) {
201 recent << (*i) << std::endl;
204 string recentString = recent.str();
205 size_t writeSize = recentString.length();
207 fwrite(recentString.c_str(), sizeof(char), writeSize, fout);
211 error << string_compose (_("Error writing saved template file %1 (%2)"), recent_templates_file_name, strerror (errno)) << endmsg;
222 ARDOUR::store_recent_sessions (string name, string path)
226 if (ARDOUR::read_recent_sessions (rs) < 0) {
230 pair<string,string> newpair;
232 newpair.first = name;
233 newpair.second = path;
235 rs.erase(remove(rs.begin(), rs.end(), newpair), rs.end());
237 rs.push_front (newpair);
239 uint32_t max_recent_sessions = Config->get_max_recent_sessions();
241 if (rs.size() > max_recent_sessions) {
242 rs.erase(rs.begin()+max_recent_sessions, rs.end());
245 return ARDOUR::write_recent_sessions (rs);
249 ARDOUR::store_recent_templates (const std::string& session_template_full_name)
251 std::deque<std::string> rt;
253 if (ARDOUR::read_recent_templates (rt) < 0) {
257 rt.erase(remove (rt.begin(), rt.end(), session_template_full_name), rt.end());
259 rt.push_front (session_template_full_name);
261 uint32_t max_recent_templates = Config->get_max_recent_templates ();
263 if (rt.size() > max_recent_templates) {
264 rt.erase( rt.begin() + max_recent_templates, rt.end ());
267 return ARDOUR::write_recent_templates (rt);
271 ARDOUR::remove_recent_sessions (const string& path)
276 if (ARDOUR::read_recent_sessions (rs) < 0) {
280 for (RecentSessions::iterator i = rs.begin(); i != rs.end(); ++i) {
281 if (i->second == path) {
289 return ARDOUR::write_recent_sessions (rs);