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