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.
27 #include <glib/gstdio.h>
28 #include <glibmm/miscutils.h>
30 #include "pbd/error.h"
32 #include "ardour/rc_configuration.h"
33 #include "ardour/filesystem_paths.h"
34 #include "ardour/recent_sessions.h"
39 using namespace ARDOUR;
44 const char * const recent_file_name = "recent";
45 const char * const recent_templates_file_name = "recent_templates";
50 ARDOUR::read_recent_sessions (RecentSessions& rs)
52 std::string path = Glib::build_filename (user_config_directory(), recent_file_name);
53 FILE* fin = g_fopen (path.c_str(), "rb");
56 if (errno != ENOENT) {
57 error << string_compose (_("cannot open recent session file %1 (%2)"), path, strerror (errno)) << endmsg;
66 // Read the file into a std::string;
67 std::stringstream recent;
68 char temporaryBuffer[1024];
71 size_t charsRead = fread(&temporaryBuffer[0], sizeof(char), 1024, fin);
72 if (charsRead != 1024 && ferror(fin))
74 error << string_compose (_("Error reading recent session file %1 (%2)"), path, strerror (errno)) << endmsg;
78 recent << &temporaryBuffer[0];
83 //ifstream recent (fin);
87 pair<string,string> newpair;
89 getline(recent, newpair.first);
95 getline(recent, newpair.second);
101 rs.push_back (newpair);
104 /* display sorting should be done in the GUI, otherwise the
105 * natural order will be broken
113 ARDOUR::read_recent_templates (std::deque<std::string>& rt)
115 std::string path = Glib::build_filename (user_config_directory(), recent_templates_file_name);
117 ifstream recent (path.c_str());
120 if (errno != ENOENT) {
121 error << string_compose (_("cannot open recent template file %1 (%2)"), path, strerror (errno)) << endmsg;
130 std::string session_template_full_name;
132 getline(recent, session_template_full_name);
134 if (!recent.good()) {
138 rt.push_back (session_template_full_name);
145 ARDOUR::write_recent_sessions (RecentSessions& rs)
147 FILE* fout = g_fopen (Glib::build_filename (user_config_directory(), recent_file_name).c_str(), "wb");
155 //ofstream recent (fout);
162 for (RecentSessions::iterator i = rs.begin(); i != rs.end(); ++i) {
163 recent << (*i).first << '\n' << (*i).second << endl;
166 string recentString = recent.str();
167 size_t writeSize = recentString.length();
169 fwrite(recentString.c_str(), sizeof(char), writeSize, fout);
173 error << string_compose (_("Error writing recent sessions file %1 (%2)"), recent_file_name, strerror (errno)) << endmsg;
187 ARDOUR::write_recent_templates (std::deque<std::string>& rt)
189 std::string path = Glib::build_filename (user_config_directory(), recent_templates_file_name);
191 std::ofstream recent (path.c_str());
197 for (std::deque<std::string>::const_iterator i = rt.begin(); i != rt.end(); ++i) {
198 recent << (*i) << std::endl;
205 ARDOUR::store_recent_sessions (string name, string path)
209 if (ARDOUR::read_recent_sessions (rs) < 0) {
213 pair<string,string> newpair;
215 newpair.first = name;
216 newpair.second = path;
218 rs.erase(remove(rs.begin(), rs.end(), newpair), rs.end());
220 rs.push_front (newpair);
222 uint32_t max_recent_sessions = Config->get_max_recent_sessions();
224 if (rs.size() > max_recent_sessions) {
225 rs.erase(rs.begin()+max_recent_sessions, rs.end());
228 return ARDOUR::write_recent_sessions (rs);
232 ARDOUR::store_recent_templates (const std::string& session_template_full_name)
234 std::deque<std::string> rt;
236 if (ARDOUR::read_recent_templates (rt) < 0) {
240 rt.erase(remove (rt.begin(), rt.end(), session_template_full_name), rt.end());
242 rt.push_front (session_template_full_name);
244 uint32_t max_recent_templates = Config->get_max_recent_templates ();
246 if (rt.size() > max_recent_templates) {
247 rt.erase( rt.begin() + max_recent_templates, rt.end ());
250 return ARDOUR::write_recent_templates (rt);
254 ARDOUR::remove_recent_sessions (const string& path)
259 if (ARDOUR::read_recent_sessions (rs) < 0) {
263 for (RecentSessions::iterator i = rs.begin(); i != rs.end(); ++i) {
264 if (i->second == path) {
272 return ARDOUR::write_recent_sessions (rs);