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