fix up file renaming code a little bit
[ArdourMidi.git] / libs / ardour / recent_sessions.cc
blobdfe85190b3e12410f67ec8fe9810eff5a2333c35
1 /*
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.
20 #include <cstring>
21 #include <cerrno>
22 #include <unistd.h>
23 #include <fstream>
24 #include <algorithm>
26 #include <glibmm/miscutils.h>
28 #include "pbd/error.h"
30 #include "ardour/configuration.h"
31 #include "ardour/filesystem_paths.h"
32 #include "ardour/recent_sessions.h"
33 #include "ardour/utils.h"
35 #include "i18n.h"
37 using namespace std;
38 using namespace ARDOUR;
39 using namespace PBD;
41 namespace {
43 const char * const recent_file_name = "recent";
45 } // anonymous
47 int
48 ARDOUR::read_recent_sessions (RecentSessions& rs)
50 sys::path recent_file_path(user_config_directory());
52 recent_file_path /= recent_file_name;
54 const string path = recent_file_path.to_string();
56 ifstream recent (path.c_str());
58 if (!recent) {
59 if (errno != ENOENT) {
60 error << string_compose (_("cannot open recent session file %1 (%2)"), path, strerror (errno)) << endmsg;
61 return -1;
62 } else {
63 return 1;
67 while (true) {
69 pair<string,string> newpair;
71 getline(recent, newpair.first);
73 if (!recent.good()) {
74 break;
77 getline(recent, newpair.second);
79 if (!recent.good()) {
80 break;
83 rs.push_back (newpair);
86 /* display sorting should be done in the GUI, otherwise the
87 * natural order will be broken
90 return 0;
93 int
94 ARDOUR::write_recent_sessions (RecentSessions& rs)
96 sys::path recent_file_path(user_config_directory());
98 recent_file_path /= recent_file_name;
100 const string path = recent_file_path.to_string();
102 ofstream recent (path.c_str());
104 if (!recent) {
105 return -1;
108 for (RecentSessions::iterator i = rs.begin(); i != rs.end(); ++i) {
109 recent << (*i).first << '\n' << (*i).second << endl;
112 return 0;
116 ARDOUR::store_recent_sessions (string name, string path)
118 RecentSessions rs;
120 if (ARDOUR::read_recent_sessions (rs) < 0) {
121 return -1;
124 pair<string,string> newpair;
126 newpair.first = name;
127 newpair.second = path;
129 rs.erase(remove(rs.begin(), rs.end(), newpair), rs.end());
131 rs.push_front (newpair);
133 if (rs.size() > 10) {
134 rs.erase(rs.begin()+10, rs.end());
137 return ARDOUR::write_recent_sessions (rs);