summaryrefslogtreecommitdiff
path: root/libs/ardour/recent_sessions.cc
blob: c2c8d1a19a5094535e14d1b81a6daa2eb8d1b944 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*
 * Copyright (C) 2005-2016 Paul Davis <paul@linuxaudiosystems.com>
 * Copyright (C) 2005 Taybin Rutkin <taybin@taybin.com>
 * Copyright (C) 2006-2012 Tim Mayberry <mojofunk@gmail.com>
 * Copyright (C) 2008-2012 David Robillard <d@drobilla.net>
 * Copyright (C) 2015 John Emmas <john@creativepost.co.uk>
 * Copyright (C) 2015 Robin Gareus <robin@gareus.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <cstring>
#include <cerrno>
#include <sstream>
#include <algorithm>

#include "pbd/gstdio_compat.h"
#include <glibmm/miscutils.h>

#include "pbd/error.h"

#include "ardour/rc_configuration.h"
#include "ardour/filesystem_paths.h"
#include "ardour/recent_sessions.h"

#include "pbd/i18n.h"

using namespace std;
using namespace ARDOUR;
using namespace PBD;

namespace {

	const char * const recent_file_name = "recent";
	const char * const recent_templates_file_name = "recent_templates";

} // anonymous

int
ARDOUR::read_recent_sessions (RecentSessions& rs)
{
	std::string path = Glib::build_filename (user_config_directory(), recent_file_name);
	FILE* fin = g_fopen (path.c_str(), "rb");

	if (!fin) {
		if (errno != ENOENT) {
			error << string_compose (_("cannot open recent session file %1 (%2)"), path, strerror (errno)) << endmsg;
			return -1;
		} else {
			return 1;
		}
	}

	// Read the file into a std::string
	std::stringstream recent;
	while (!feof (fin)) {
		char buf[1024];
		size_t charsRead = fread (buf, sizeof(char), 1024, fin);
		if (ferror (fin)) {
			error << string_compose (_("Error reading recent session file %1 (%2)"), path, strerror (errno)) << endmsg;
			fclose(fin);
			return -1;
		}
		if (charsRead == 0) {
			break;
		}
		recent.write (buf, charsRead);
	}

	while (true) {

		pair<string,string> newpair;

		getline(recent, newpair.first);

		if (!recent.good()) {
			break;
		}

		getline(recent, newpair.second);

		if (!recent.good()) {
			break;
		}

		rs.push_back (newpair);
	}

	/* display sorting should be done in the GUI, otherwise the
	 * natural order will be broken
	 */

	fclose (fin);
	return 0;
}

int
ARDOUR::read_recent_templates (std::deque<std::string>& rt)
{
	std::string path = Glib::build_filename (user_config_directory(), recent_templates_file_name);
	FILE* fin = g_fopen (path.c_str(), "rb");

	if (!fin) {
		if (errno != ENOENT) {
			error << string_compose (_("Cannot open recent template file %1 (%2)"), path, strerror (errno)) << endmsg;
			return -1;
		} else {
			return 1;
		}
	}

	// Copy the file contents into a std::stringstream
	std::stringstream recent;
	while (!feof (fin)) {
		char buf[1024];
		size_t charsRead = fread (buf, sizeof(char), 1024, fin);
		if (ferror (fin)) {
			error << string_compose (_("Error reading recent session file %1 (%2)"), path, strerror (errno)) << endmsg;
			fclose(fin);
			return -1;
		}
		if (charsRead == 0) {
			break;
		}
		recent.write (buf, charsRead);
	}

	while (true) {

		std::string session_template_full_name;

		getline(recent, session_template_full_name);

		if (!recent.good()) {
			break;
		}

		rt.push_back (session_template_full_name);
	}

	fclose (fin);
	return 0;
}

int
ARDOUR::write_recent_sessions (RecentSessions& rs)
{
	FILE* fout = g_fopen (Glib::build_filename (user_config_directory(), recent_file_name).c_str(), "wb");

	if (!fout) {
		return -1;
	}

	{
		stringstream recent;

		for (RecentSessions::iterator i = rs.begin(); i != rs.end(); ++i) {
			recent << (*i).first << '\n' << (*i).second << endl;
		}

		string recentString = recent.str();
		size_t writeSize = recentString.length();

		fwrite(recentString.c_str(), sizeof(char), writeSize, fout);

		if (ferror(fout))
		{
			error << string_compose (_("Error writing recent sessions file %1 (%2)"), recent_file_name, strerror (errno)) << endmsg;
			fclose(fout);
			return -1;
		}
	}



	fclose (fout);

	return 0;
}

int
ARDOUR::write_recent_templates (std::deque<std::string>& rt)
{
	FILE* fout = g_fopen (Glib::build_filename (user_config_directory(), recent_templates_file_name).c_str(), "wb");

	if (!fout) {
		return -1;
	}

	stringstream recent;

	for (std::deque<std::string>::const_iterator i = rt.begin(); i != rt.end(); ++i) {
		recent << (*i) << std::endl;
	}

	string recentString = recent.str();
	size_t writeSize = recentString.length();

	fwrite(recentString.c_str(), sizeof(char), writeSize, fout);

	if (ferror(fout))
	{
		error << string_compose (_("Error writing saved template file %1 (%2)"), recent_templates_file_name, strerror (errno)) << endmsg;
		fclose(fout);
		return -1;
	}

	fclose (fout);

	return 0;
}

int
ARDOUR::store_recent_sessions (string name, string path)
{
	RecentSessions rs;

	if (ARDOUR::read_recent_sessions (rs) < 0) {
		return -1;
	}

	pair<string,string> newpair;

	newpair.first = name;
	newpair.second = path;

	rs.erase(remove(rs.begin(), rs.end(), newpair), rs.end());

	rs.push_front (newpair);

	uint32_t max_recent_sessions = Config->get_max_recent_sessions();

	if (rs.size() > max_recent_sessions) {
		rs.erase(rs.begin()+max_recent_sessions, rs.end());
	}

	return ARDOUR::write_recent_sessions (rs);
}

int
ARDOUR::store_recent_templates (const std::string& session_template_full_name)
{
	std::deque<std::string> rt;

	if (ARDOUR::read_recent_templates (rt) < 0) {
		return -1;
	}

	rt.erase(remove (rt.begin(), rt.end(), session_template_full_name), rt.end());

	rt.push_front (session_template_full_name);

	uint32_t max_recent_templates = Config->get_max_recent_templates ();

	if (rt.size() > max_recent_templates) {
		rt.erase( rt.begin() + max_recent_templates, rt.end ());
	}

	return ARDOUR::write_recent_templates (rt);
}

int
ARDOUR::remove_recent_sessions (const string& path)
{
	RecentSessions rs;
	bool write = false;

	if (ARDOUR::read_recent_sessions (rs) < 0) {
		return -1;
	}

	for (RecentSessions::iterator i = rs.begin(); i != rs.end(); ++i) {
		if (i->second == path) {
			rs.erase (i);
			write = true;
			break;
		}
	}

	if (write) {
		return ARDOUR::write_recent_sessions (rs);
	} else {
		return 1;
	}
}