Merge pull request #506 from andrewcsmith/patch-2
[supercollider.git] / editors / sc-ide / core / session_manager.cpp
blob13966aa557484fd35da764c31e7defdfed83363e
1 /*
2 SuperCollider Qt IDE
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"
27 #include <QFile>
28 #include <QFileInfo>
30 namespace ScIDE {
32 static QString sessionFilePath( const QString & name )
34 char config_dir[PATH_MAX];
35 sc_GetUserConfigDirectory(config_dir, PATH_MAX);
37 QDir dir(config_dir);
39 if (!dir.mkpath("sessions")) {
40 qWarning("The path to sessions does not exist and could not be created!");
41 return QString();
44 dir.cd("sessions");
46 return dir.filePath(name + ".yaml");
49 SessionManager::SessionManager( DocumentManager *docMng, QObject * parent ) :
50 QObject(parent),
51 mDocMng(docMng),
52 mSession(0)
55 QDir SessionManager::sessionsDir()
57 char config_dir[PATH_MAX];
58 sc_GetUserConfigDirectory(config_dir, PATH_MAX);
60 QDir dir(config_dir);
62 if (dir.mkpath("sessions"))
63 dir.cd("sessions");
64 else {
65 qWarning("The path to sessions does not exist and could not be created!");
66 return QDir();
69 dir.setFilter( QDir::Files );
70 dir.setNameFilters( QStringList() << "*.yaml" );
72 return dir;
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();
81 return sessions;
84 QString SessionManager::lastSession()
86 QDir dir = sessionsDir();
87 if (dir.path().isEmpty())
88 return QString();
90 QString path = QFile::symLinkTarget( dir.filePath( ".last-session.lnk" ) );
92 return QFileInfo(path).baseName();
95 void SessionManager::newSession()
97 closeSession();
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
110 closeSession();
112 QDir dir = sessionsDir();
113 if (dir.path().isEmpty())
114 return 0;
116 QString sessionFile = dir.filePath(name + ".yaml");
117 mSession = new Session( sessionFile, name, Settings::serializationFormat() );
119 saveLastSession( dir, sessionFile );
121 emit switchSessionRequest(mSession);
123 return mSession;
126 void SessionManager::saveSession()
128 if (mSession) {
129 emit saveSessionRequest(mSession);
130 mSession->sync();
134 Session * SessionManager::saveSessionAs( const QString & name )
136 // TODO:
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.
140 if (mSession) {
141 delete mSession;
142 mSession = 0;
145 QDir dir = sessionsDir();
146 if (dir.path().isEmpty()) {
147 emit switchSessionRequest(0);
148 return 0;
151 QString sessionFile = dir.filePath(name + ".yaml");
152 mSession = new Session( sessionFile, name, Settings::serializationFormat() );
154 emit saveSessionRequest(mSession);
156 mSession->sync();
158 saveLastSession( dir, sessionFile );
160 emit currentSessionNameChanged();
162 return mSession;
165 void SessionManager::closeSession()
167 if (mSession)
168 emit saveSessionRequest(mSession);
170 delete mSession;
171 mSession = 0;
174 void SessionManager::removeSession( const QString & name )
176 QDir dir = sessionsDir();
177 if (dir.path().isEmpty())
178 return;
180 if (mSession && mSession->name() == name)
182 delete mSession;
183 mSession = 0;
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);
199 else
201 QDir dir = sessionsDir();
202 if (dir.path().isEmpty())
203 return;
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!");
217 return false;
220 if ( sessionFile.isEmpty() || QFile::link( sessionFile, linkFile ) )
221 return true;
222 else
223 qWarning("Could not create link to last session!");
225 return false;
228 } // namespace ScIDE