3 Copyright (c) 2012 Jakob Leben & Tim Blechmann
4 http://www.audiosynth.com
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "session_manager.hpp"
22 #include "settings/manager.hpp"
23 #include "doc_manager.hpp"
25 #include "SC_DirUtils.h"
32 static QString
sessionFilePath( const QString
& name
)
34 char config_dir
[PATH_MAX
];
35 sc_GetUserConfigDirectory(config_dir
, PATH_MAX
);
39 if (!dir
.mkpath("sessions")) {
40 qWarning("The path to sessions does not exist and could not be created!");
46 return dir
.filePath(name
+ ".yaml");
49 SessionManager::SessionManager( DocumentManager
*docMng
, QObject
* parent
) :
55 QDir
SessionManager::sessionsDir()
57 char config_dir
[PATH_MAX
];
58 sc_GetUserConfigDirectory(config_dir
, PATH_MAX
);
62 if (dir
.mkpath("sessions"))
65 qWarning("The path to sessions does not exist and could not be created!");
69 dir
.setFilter( QDir::Files
);
70 dir
.setNameFilters( QStringList() << "*.yaml" );
75 QStringList
SessionManager::availableSessions()
77 QStringList sessions
= sessionsDir().entryList();
78 QStringList::iterator it
;
79 for ( it
= sessions
.begin(); it
!= sessions
.end(); ++it
)
80 *it
= QFileInfo(*it
).baseName();
84 QString
SessionManager::lastSession()
86 QDir dir
= sessionsDir();
87 if (dir
.path().isEmpty())
90 QString path
= QFile::symLinkTarget( dir
.filePath( ".last-session.lnk" ) );
92 return QFileInfo(path
).baseName();
95 void SessionManager::newSession()
99 QDir dir
= sessionsDir();
100 if (!dir
.path().isEmpty())
101 saveLastSession( dir
, QString() );
103 emit
switchSessionRequest(0);
106 Session
*SessionManager::openSession( const QString
& name
)
108 // NOTE: This will create a session if it doesn't exists
112 QDir dir
= sessionsDir();
113 if (dir
.path().isEmpty())
116 QString sessionFile
= dir
.filePath(name
+ ".yaml");
117 mSession
= new Session( sessionFile
, name
, Settings::serializationFormat() );
119 saveLastSession( dir
, sessionFile
);
121 emit
switchSessionRequest(mSession
);
126 void SessionManager::saveSession()
129 emit
saveSessionRequest(mSession
);
134 Session
* SessionManager::saveSessionAs( const QString
& name
)
137 // Maybe use a different data structure for Session instead of QSettings?
138 // A new class that would allow closing without saving would be nice.
145 QDir dir
= sessionsDir();
146 if (dir
.path().isEmpty()) {
147 emit
switchSessionRequest(0);
151 QString sessionFile
= dir
.filePath(name
+ ".yaml");
152 mSession
= new Session( sessionFile
, name
, Settings::serializationFormat() );
154 emit
saveSessionRequest(mSession
);
158 saveLastSession( dir
, sessionFile
);
160 emit
currentSessionNameChanged();
165 void SessionManager::closeSession()
168 emit
saveSessionRequest(mSession
);
174 void SessionManager::removeSession( const QString
& name
)
176 QDir dir
= sessionsDir();
177 if (dir
.path().isEmpty())
180 if (mSession
&& mSession
->name() == name
)
184 saveLastSession(dir
, QString());
185 emit
switchSessionRequest(0);
188 if (!QFile::remove(dir
.filePath(name
+ ".yaml")))
189 qWarning("Could not remove a session file!");
192 void SessionManager::renameSession( const QString
& oldName
, const QString
& newName
)
194 if (mSession
&& mSession
->name() == oldName
)
196 saveSessionAs(newName
);
197 removeSession(oldName
);
201 QDir dir
= sessionsDir();
202 if (dir
.path().isEmpty())
205 if (!dir
.rename(oldName
+ ".yaml", newName
+ ".yaml"))
206 qWarning("Could not rename session file!");
210 bool SessionManager::saveLastSession( const QDir
& dir
, const QString
& sessionFile
)
212 QString linkFile
= dir
.filePath(".last-session.lnk");
214 if ( QFile::exists(linkFile
) )
215 if (!QFile::remove(linkFile
)) {
216 qWarning("Could not remove old link to last session!");
220 if ( sessionFile
.isEmpty() || QFile::link( sessionFile
, linkFile
) )
223 qWarning("Could not create link to last session!");