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