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 <glibmm/miscutils.h>
27 #include "pbd/error.h"
29 #include "ardour/rc_configuration.h"
30 #include "ardour/filesystem_paths.h"
31 #include "ardour/recent_sessions.h"
36 using namespace ARDOUR;
41 const char * const recent_file_name = "recent";
42 const char * const recent_templates_file_name = "recent_templates";
47 ARDOUR::read_recent_sessions (RecentSessions& rs)
49 std::string path = Glib::build_filename (user_config_directory(), recent_file_name);
51 ifstream recent (path.c_str());
54 if (errno != ENOENT) {
55 error << string_compose (_("cannot open recent session file %1 (%2)"), path, strerror (errno)) << endmsg;
64 pair<string,string> newpair;
66 getline(recent, newpair.first);
72 getline(recent, newpair.second);
78 rs.push_back (newpair);
81 /* display sorting should be done in the GUI, otherwise the
82 * natural order will be broken
89 ARDOUR::read_recent_templates (std::deque<std::string>& rt)
91 std::string path = Glib::build_filename (user_config_directory(), recent_templates_file_name);
93 ifstream recent (path.c_str());
96 if (errno != ENOENT) {
97 error << string_compose (_("cannot open recent template file %1 (%2)"), path, strerror (errno)) << endmsg;
106 std::string session_template_full_name;
108 getline(recent, session_template_full_name);
110 if (!recent.good()) {
114 rt.push_back (session_template_full_name);
121 ARDOUR::write_recent_sessions (RecentSessions& rs)
123 std::string path = Glib::build_filename (user_config_directory(), recent_file_name);
125 ofstream recent (path.c_str());
131 for (RecentSessions::iterator i = rs.begin(); i != rs.end(); ++i) {
132 recent << (*i).first << '\n' << (*i).second << endl;
139 ARDOUR::write_recent_templates (std::deque<std::string>& rt)
141 std::string path = Glib::build_filename (user_config_directory(), recent_templates_file_name);
143 std::ofstream recent (path.c_str());
149 for (std::deque<std::string>::const_iterator i = rt.begin(); i != rt.end(); ++i) {
150 recent << (*i) << std::endl;
157 ARDOUR::store_recent_sessions (string name, string path)
161 if (ARDOUR::read_recent_sessions (rs) < 0) {
165 pair<string,string> newpair;
167 newpair.first = name;
168 newpair.second = path;
170 rs.erase(remove(rs.begin(), rs.end(), newpair), rs.end());
172 rs.push_front (newpair);
174 uint32_t max_recent_sessions = Config->get_max_recent_sessions();
176 if (rs.size() > max_recent_sessions) {
177 rs.erase(rs.begin()+max_recent_sessions, rs.end());
180 return ARDOUR::write_recent_sessions (rs);
184 ARDOUR::store_recent_templates (const std::string& session_template_full_name)
186 std::deque<std::string> rt;
188 if (ARDOUR::read_recent_templates (rt) < 0) {
192 rt.erase(remove (rt.begin(), rt.end(), session_template_full_name), rt.end());
194 rt.push_front (session_template_full_name);
196 uint32_t max_recent_templates = Config->get_max_recent_templates ();
198 if (rt.size() > max_recent_templates) {
199 rt.erase( rt.begin() + max_recent_templates, rt.end ());
202 return ARDOUR::write_recent_templates (rt);
206 ARDOUR::remove_recent_sessions (const string& path)
211 if (ARDOUR::read_recent_sessions (rs) < 0) {
215 for (RecentSessions::iterator i = rs.begin(); i != rs.end(); ++i) {
216 if (i->second == path) {
224 return ARDOUR::write_recent_sessions (rs);