fix clicking when processors become active/inactive; reduce crazy 2.5sec delay for...
[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/filesystem_paths.h"
32 #include "ardour/recent_sessions.h"
33 #include "ardour/utils.h"
34
35 #include "i18n.h"
36
37 using namespace std;
38 using namespace ARDOUR;
39 using namespace PBD;
40
41 namespace {
42
43         const char * const recent_file_name = "recent";
44
45 } // anonymous
46
47 int
48 ARDOUR::read_recent_sessions (RecentSessions& rs)
49 {
50         sys::path recent_file_path(user_config_directory());
51
52         recent_file_path /= recent_file_name;
53         
54         const string path = recent_file_path.to_string();
55
56         ifstream recent (path.c_str());
57         
58         if (!recent) {
59                 if (errno != ENOENT) {
60                         error << string_compose (_("cannot open recent session file %1 (%2)"), path, strerror (errno)) << endmsg;
61                         return -1;
62                 } else {
63                         return 1;
64                 }
65         }
66
67         while (true) {
68
69                 pair<string,string> newpair;
70
71                 getline(recent, newpair.first);
72
73                 if (!recent.good()) {
74                         break;
75                 }
76                 
77                 getline(recent, newpair.second);
78
79                 if (!recent.good()) {
80                         break;
81                 }
82
83                 rs.push_back (newpair);
84         }
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