2 * Copyright (C) 2005-2016 Paul Davis <paul@linuxaudiosystems.com>
3 * Copyright (C) 2005 Taybin Rutkin <taybin@taybin.com>
4 * Copyright (C) 2006-2012 Tim Mayberry <mojofunk@gmail.com>
5 * Copyright (C) 2008-2012 David Robillard <d@drobilla.net>
6 * Copyright (C) 2015 John Emmas <john@creativepost.co.uk>
7 * Copyright (C) 2015 Robin Gareus <robin@gareus.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
29 #include "pbd/gstdio_compat.h"
30 #include <glibmm/miscutils.h>
32 #include "pbd/error.h"
34 #include "ardour/rc_configuration.h"
35 #include "ardour/filesystem_paths.h"
36 #include "ardour/recent_sessions.h"
41 using namespace ARDOUR;
46 const char * const recent_file_name = "recent";
47 const char * const recent_templates_file_name = "recent_templates";
52 ARDOUR::read_recent_sessions (RecentSessions& rs)
54 std::string path = Glib::build_filename (user_config_directory(), recent_file_name);
55 FILE* fin = g_fopen (path.c_str(), "rb");
58 if (errno != ENOENT) {
59 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;
70 size_t charsRead = fread (buf, sizeof(char), 1024, fin);
72 error << string_compose (_("Error reading recent session file %1 (%2)"), path, strerror (errno)) << endmsg;
79 recent.write (buf, charsRead);
84 pair<string,string> newpair;
86 getline(recent, newpair.first);
92 getline(recent, newpair.second);
98 rs.push_back (newpair);
101 /* display sorting should be done in the GUI, otherwise the
102 * natural order will be broken
110 ARDOUR::read_recent_templates (std::deque<std::string>& rt)
112 std::string path = Glib::build_filename (user_config_directory(), recent_templates_file_name);
113 FILE* fin = g_fopen (path.c_str(), "rb");
116 if (errno != ENOENT) {
117 error << string_compose (_("Cannot open recent template file %1 (%2)"), path, strerror (errno)) << endmsg;
124 // Copy the file contents into a std::stringstream
125 std::stringstream recent;
126 while (!feof (fin)) {
128 size_t charsRead = fread (buf, sizeof(char), 1024, fin);
130 error << string_compose (_("Error reading recent session file %1 (%2)"), path, strerror (errno)) << endmsg;
134 if (charsRead == 0) {
137 recent.write (buf, charsRead);
142 std::string session_template_full_name;
144 getline(recent, session_template_full_name);
146 if (!recent.good()) {
150 rt.push_back (session_template_full_name);
158 ARDOUR::write_recent_sessions (RecentSessions& rs)
160 FILE* fout = g_fopen (Glib::build_filename (user_config_directory(), recent_file_name).c_str(), "wb");
169 for (RecentSessions::iterator i = rs.begin(); i != rs.end(); ++i) {
170 recent << (*i).first << '\n' << (*i).second << endl;
173 string recentString = recent.str();
174 size_t writeSize = recentString.length();
176 fwrite(recentString.c_str(), sizeof(char), writeSize, fout);
180 error << string_compose (_("Error writing recent sessions file %1 (%2)"), recent_file_name, strerror (errno)) << endmsg;
194 ARDOUR::write_recent_templates (std::deque<std::string>& rt)
196 FILE* fout = g_fopen (Glib::build_filename (user_config_directory(), recent_templates_file_name).c_str(), "wb");
204 for (std::deque<std::string>::const_iterator i = rt.begin(); i != rt.end(); ++i) {
205 recent << (*i) << std::endl;
208 string recentString = recent.str();
209 size_t writeSize = recentString.length();
211 fwrite(recentString.c_str(), sizeof(char), writeSize, fout);
215 error << string_compose (_("Error writing saved template file %1 (%2)"), recent_templates_file_name, strerror (errno)) << endmsg;
226 ARDOUR::store_recent_sessions (string name, string path)
230 if (ARDOUR::read_recent_sessions (rs) < 0) {
234 pair<string,string> newpair;
236 newpair.first = name;
237 newpair.second = path;
239 rs.erase(remove(rs.begin(), rs.end(), newpair), rs.end());
241 rs.push_front (newpair);
243 uint32_t max_recent_sessions = Config->get_max_recent_sessions();
245 if (rs.size() > max_recent_sessions) {
246 rs.erase(rs.begin()+max_recent_sessions, rs.end());
249 return ARDOUR::write_recent_sessions (rs);
253 ARDOUR::store_recent_templates (const std::string& session_template_full_name)
255 std::deque<std::string> rt;
257 if (ARDOUR::read_recent_templates (rt) < 0) {
261 rt.erase(remove (rt.begin(), rt.end(), session_template_full_name), rt.end());
263 rt.push_front (session_template_full_name);
265 uint32_t max_recent_templates = Config->get_max_recent_templates ();
267 if (rt.size() > max_recent_templates) {
268 rt.erase( rt.begin() + max_recent_templates, rt.end ());
271 return ARDOUR::write_recent_templates (rt);
275 ARDOUR::remove_recent_sessions (const string& path)
280 if (ARDOUR::read_recent_sessions (rs) < 0) {
284 for (RecentSessions::iterator i = rs.begin(); i != rs.end(); ++i) {
285 if (i->second == path) {
293 return ARDOUR::write_recent_sessions (rs);