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