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