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;
64 // Read the file into a std::string
65 std::stringstream recent;
68 size_t charsRead = fread (buf, sizeof(char), 1024, fin);
70 error << string_compose (_("Error reading recent session file %1 (%2)"), path, strerror (errno)) << endmsg;
77 recent.write (buf, charsRead);
82 pair<string,string> newpair;
84 getline(recent, newpair.first);
90 getline(recent, newpair.second);
96 rs.push_back (newpair);
99 /* display sorting should be done in the GUI, otherwise the
100 * natural order will be broken
108 ARDOUR::read_recent_templates (std::deque<std::string>& rt)
110 std::string path = Glib::build_filename (user_config_directory(), recent_templates_file_name);
112 ifstream recent (path.c_str());
115 if (errno != ENOENT) {
116 error << string_compose (_("cannot open recent template file %1 (%2)"), path, strerror (errno)) << endmsg;
125 std::string session_template_full_name;
127 getline(recent, session_template_full_name);
129 if (!recent.good()) {
133 rt.push_back (session_template_full_name);
140 ARDOUR::write_recent_sessions (RecentSessions& rs)
142 FILE* fout = g_fopen (Glib::build_filename (user_config_directory(), recent_file_name).c_str(), "wb");
150 //ofstream recent (fout);
157 for (RecentSessions::iterator i = rs.begin(); i != rs.end(); ++i) {
158 recent << (*i).first << '\n' << (*i).second << endl;
161 string recentString = recent.str();
162 size_t writeSize = recentString.length();
164 fwrite(recentString.c_str(), sizeof(char), writeSize, fout);
168 error << string_compose (_("Error writing recent sessions file %1 (%2)"), recent_file_name, strerror (errno)) << endmsg;
182 ARDOUR::write_recent_templates (std::deque<std::string>& rt)
184 std::string path = Glib::build_filename (user_config_directory(), recent_templates_file_name);
186 std::ofstream recent (path.c_str());
192 for (std::deque<std::string>::const_iterator i = rt.begin(); i != rt.end(); ++i) {
193 recent << (*i) << std::endl;
200 ARDOUR::store_recent_sessions (string name, string path)
204 if (ARDOUR::read_recent_sessions (rs) < 0) {
208 pair<string,string> newpair;
210 newpair.first = name;
211 newpair.second = path;
213 rs.erase(remove(rs.begin(), rs.end(), newpair), rs.end());
215 rs.push_front (newpair);
217 uint32_t max_recent_sessions = Config->get_max_recent_sessions();
219 if (rs.size() > max_recent_sessions) {
220 rs.erase(rs.begin()+max_recent_sessions, rs.end());
223 return ARDOUR::write_recent_sessions (rs);
227 ARDOUR::store_recent_templates (const std::string& session_template_full_name)
229 std::deque<std::string> rt;
231 if (ARDOUR::read_recent_templates (rt) < 0) {
235 rt.erase(remove (rt.begin(), rt.end(), session_template_full_name), rt.end());
237 rt.push_front (session_template_full_name);
239 uint32_t max_recent_templates = Config->get_max_recent_templates ();
241 if (rt.size() > max_recent_templates) {
242 rt.erase( rt.begin() + max_recent_templates, rt.end ());
245 return ARDOUR::write_recent_templates (rt);
249 ARDOUR::remove_recent_sessions (const string& path)
254 if (ARDOUR::read_recent_sessions (rs) < 0) {
258 for (RecentSessions::iterator i = rs.begin(); i != rs.end(); ++i) {
259 if (i->second == path) {
267 return ARDOUR::write_recent_sessions (rs);