Merge branch 'selection_fixes' of https://github.com/nmains/ardour into cairocanvas
[ardour.git] / libs / ardour / recent_sessions.cc
1 /*
2     Copyright (C) 2004 Paul Davis
3
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.
8
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.
13
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.
17
18 */
19
20 #include <cstring>
21 #include <cerrno>
22 #include <fstream>
23 #include <algorithm>
24
25 #include <glibmm/miscutils.h>
26
27 #include "pbd/error.h"
28
29 #include "ardour/rc_configuration.h"
30 #include "ardour/filesystem_paths.h"
31 #include "ardour/recent_sessions.h"
32
33 #include "i18n.h"
34
35 using namespace std;
36 using namespace ARDOUR;
37 using namespace PBD;
38
39 namespace {
40
41         const char * const recent_file_name = "recent";
42
43 } // anonymous
44
45 int
46 ARDOUR::read_recent_sessions (RecentSessions& rs)
47 {
48         std::string path = Glib::build_filename (user_config_directory(), recent_file_name);
49
50         ifstream recent (path.c_str());
51
52         if (!recent) {
53                 if (errno != ENOENT) {
54                         error << string_compose (_("cannot open recent session file %1 (%2)"), path, strerror (errno)) << endmsg;
55                         return -1;
56                 } else {
57                         return 1;
58                 }
59         }
60
61         while (true) {
62
63                 pair<string,string> newpair;
64
65                 getline(recent, newpair.first);
66
67                 if (!recent.good()) {
68                         break;
69                 }
70
71                 getline(recent, newpair.second);
72
73                 if (!recent.good()) {
74                         break;
75                 }
76
77                 rs.push_back (newpair);
78         }
79
80         /* display sorting should be done in the GUI, otherwise the
81          * natural order will be broken
82          */
83
84         return 0;
85 }
86
87 int
88 ARDOUR::write_recent_sessions (RecentSessions& rs)
89 {
90         std::string path = Glib::build_filename (user_config_directory(), recent_file_name);
91
92         ofstream recent (path.c_str());
93
94         if (!recent) {
95                 return -1;
96         }
97
98         for (RecentSessions::iterator i = rs.begin(); i != rs.end(); ++i) {
99                 recent << (*i).first << '\n' << (*i).second << endl;
100         }
101
102         return 0;
103 }
104
105 int
106 ARDOUR::store_recent_sessions (string name, string path)
107 {
108         RecentSessions rs;
109
110         if (ARDOUR::read_recent_sessions (rs) < 0) {
111                 return -1;
112         }
113
114         pair<string,string> newpair;
115
116         newpair.first = name;
117         newpair.second = path;
118
119         rs.erase(remove(rs.begin(), rs.end(), newpair), rs.end());
120
121         rs.push_front (newpair);
122
123         uint32_t max_recent_sessions = Config->get_max_recent_sessions();
124
125         if (rs.size() > max_recent_sessions) {
126                 rs.erase(rs.begin()+max_recent_sessions, rs.end());
127         }
128
129         return ARDOUR::write_recent_sessions (rs);
130 }
131
132 int
133 ARDOUR::remove_recent_sessions (const string& path)
134 {
135         RecentSessions rs;
136         bool write = false;
137
138         if (ARDOUR::read_recent_sessions (rs) < 0) {
139                 return -1;
140         }
141
142         for (RecentSessions::iterator i = rs.begin(); i != rs.end(); ++i) {
143                 if (i->second == path) {
144                         rs.erase (i);
145                         write = true;
146                         break;
147                 }
148         }
149
150         if (write) {
151                 return ARDOUR::write_recent_sessions (rs);
152         } else {
153                 return 1;
154         }
155 }
156