Use sys::path and ARDOUR::user_config_directory in ARDOUR::read/write_recent_sessions
[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 <cerrno>
21 #include <unistd.h>
22 #include <fstream>
23 #include <algorithm>
24 #include <pbd/error.h>
25 #include <ardour/configuration.h>
26 #include <ardour/filesystem_paths.h>
27 #include <ardour/recent_sessions.h>
28 #include <ardour/utils.h>
29
30 #include "i18n.h"
31
32 using namespace std;
33 using namespace ARDOUR;
34 using namespace PBD;
35
36 namespace {
37
38         const char * const recent_file_name = "recent";
39
40 } // anonymous
41
42 int
43 ARDOUR::read_recent_sessions (RecentSessions& rs)
44 {
45         sys::path recent_file_path(user_config_directory());
46
47         recent_file_path /= recent_file_name;
48         
49         const string path = recent_file_path.to_string();
50
51         ifstream recent (path.c_str());
52         
53         if (!recent) {
54                 if (errno != ENOENT) {
55                         error << string_compose (_("cannot open recent session file %1 (%2)"), path, strerror (errno)) << endmsg;
56                         return -1;
57                 } else {
58                         return 1;
59                 }
60         }
61
62         while (true) {
63
64                 pair<string,string> newpair;
65
66                 getline(recent, newpair.first);
67
68                 if (!recent.good()) {
69                         break;
70                 }
71                 
72                 getline(recent, newpair.second);
73
74                 if (!recent.good()) {
75                         break;
76                 }
77
78                 if (!access(newpair.second.c_str(), R_OK)) {
79                         rs.push_back (newpair);
80                 }
81         }
82
83         // This deletes any missing sessions
84         ARDOUR::write_recent_sessions (rs);
85
86         /* display sorting should be done in the GUI, otherwise the
87          * natural order will be broken
88          */
89
90         return 0;
91 }
92
93 int
94 ARDOUR::write_recent_sessions (RecentSessions& rs)
95 {
96         sys::path recent_file_path(user_config_directory());
97
98         recent_file_path /= recent_file_name;
99         
100         const string path = recent_file_path.to_string();
101
102         ofstream recent (path.c_str());
103
104         if (!recent) {
105                 return -1;
106         }
107
108         for (RecentSessions::iterator i = rs.begin(); i != rs.end(); ++i) {
109                 recent << (*i).first << '\n' << (*i).second << endl;
110         }
111         
112         return 0;
113 }
114         
115 int
116 ARDOUR::store_recent_sessions (string name, string path)
117 {
118         RecentSessions rs;
119
120         if (ARDOUR::read_recent_sessions (rs) < 0) {
121                 return -1;
122         }
123
124         pair<string,string> newpair;
125
126         newpair.first = name;
127         newpair.second = path;
128
129         rs.erase(remove(rs.begin(), rs.end(), newpair), rs.end());
130         
131         rs.push_front (newpair);
132
133         if (rs.size() > 10) {
134                 rs.erase(rs.begin()+10, rs.end());
135         }
136
137         return ARDOUR::write_recent_sessions (rs);
138 }
139