scide: open documents in existing IDE
[supercollider.git] / editors / sc-ide / core / main.cpp
blob969a0d20e7c1467d179b57db0aea2f14d91e994e
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 "main.hpp"
22 #include "settings/manager.hpp"
23 #include "session_manager.hpp"
24 #include "../widgets/main_window.hpp"
25 #include "../widgets/code_editor/highlighter.hpp"
26 #include "SC_DirUtils.h"
28 #include <QApplication>
29 #include <QAction>
30 #include <QBuffer>
31 #include <QDataStream>
32 #include <QDir>
33 #include <QFileOpenEvent>
34 #include <QLibraryInfo>
35 #include <QTranslator>
37 using namespace ScIDE;
39 int main( int argc, char *argv[] )
41 QApplication app(argc, argv);
43 QStringList arguments (QApplication::arguments());
44 arguments.pop_front(); // application path
46 SingleInstanceGuard guard;
47 if (!arguments.empty()) {
48 if (guard.tryConnect(arguments))
49 return 0;
52 QTranslator qtTranslator;
53 qtTranslator.load("qt_" + QLocale::system().name(), QLibraryInfo::location(QLibraryInfo::TranslationsPath));
54 app.installTranslator(&qtTranslator);
56 QTranslator scideTranslator;
57 scideTranslator.load("scide_" + QLocale::system().name());
58 app.installTranslator(&scideTranslator);
60 Main * main = Main::instance();
62 MainWindow *win = new MainWindow(main);
64 // NOTE: load session after GUI is created, so that GUI can respond
65 QString lastSession = main->sessionManager()->lastSession();
66 if (!lastSession.isEmpty()) {
67 main->sessionManager()->openSession(lastSession);
68 win->show();
70 else {
71 main->sessionManager()->newSession();
72 win->showMaximized();
75 foreach (QString argument, arguments) {
76 main->documentManager()->open(argument);
79 bool startInterpreter = main->settings()->value("IDE/interpreter/autoStart").toBool();
80 if (startInterpreter)
81 main->scProcess()->start();
83 return app.exec();
87 bool SingleInstanceGuard::tryConnect(QStringList const & arguments)
89 QLocalSocket * mSocket = new QLocalSocket(this);
90 mSocket->connectToServer("SuperColliderIDESingleton");
91 if (mSocket->waitForConnected(500)) {
92 QDataStream stream(mSocket);
93 stream.setVersion(QDataStream::Qt_4_6);
95 stream << QString("open");
96 stream << arguments;
97 mSocket->flush();
98 return true;
100 delete mSocket;
102 mIpcServer = new QLocalServer(this);
103 mIpcServer->listen("SuperColliderIDESingleton");
104 connect(mIpcServer, SIGNAL(newConnection()), this, SLOT(onNewIpcConnection()));
105 return false;
108 void SingleInstanceGuard::onIpcData()
110 QByteArray ipcData = mIpcSocket->readAll();
112 QBuffer receivedData ( &ipcData );
113 receivedData.open ( QIODevice::ReadOnly );
115 QDataStream in ( &receivedData );
116 in.setVersion ( QDataStream::Qt_4_6 );
117 QString id;
118 in >> id;
119 if ( in.status() != QDataStream::Ok )
120 return;
122 QStringList message;
123 in >> message;
124 if ( in.status() != QDataStream::Ok )
125 return;
127 if (id == QString("open")) {
128 foreach (QString path, message)
129 Main::instance()->documentManager()->open(path);
134 static QString getSettingsFile()
136 char config_dir[PATH_MAX];
137 sc_GetUserConfigDirectory(config_dir, PATH_MAX);
138 return QString(config_dir) + SC_PATH_DELIMITER + "sc_ide_conf.yaml";
141 // NOTE: mSettings must be the first to initialize,
142 // because other members use it!
144 Main::Main(void) :
145 mSettings( new Settings::Manager( getSettingsFile(), this ) ),
146 mSCResponder( new ScResponder(this) ),
147 mSCProcess( new SCProcess(this) ),
148 mSCServer( new ScServer(this) ),
149 mDocManager( new DocumentManager(this) ),
150 mSessionManager( new SessionManager(mDocManager, this) )
152 new SyntaxHighlighterGlobals(this);
154 connect(mSCProcess, SIGNAL(response(QString,QString)), mSCResponder, SLOT(onResponse(QString,QString)));
155 connect(mSCResponder, SIGNAL(serverRunningChanged(bool,QString,int)), mSCServer, SLOT(onServerRunningChanged(bool,QString,int)));
157 qApp->installEventFilter(this);
160 void Main::quit() {
161 mSessionManager->saveSession();
162 storeSettings();
163 QApplication::quit();
166 bool Main::eventFilter(QObject *object, QEvent *event)
168 if (event->type() == QEvent::FileOpen) {
169 // open the file dragged onto the application icon on Mac
170 QFileOpenEvent *openEvent = static_cast<QFileOpenEvent*>(event);
171 mDocManager->open(openEvent->file());
172 return true;
173 } else
174 return QObject::eventFilter(object, event);