1 // Copyright (c) 2012 Dennco Project
3 // This program is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
13 // You should have received a copy of the GNU General Public License
14 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 // Created by tkawata on 2/25/2012.
19 #include "mainwindow.h"
20 #include "ui_mainwindow.h"
22 #include <QFileDialog>
26 #include <QLocalSocket>
32 #include "TKConsole.h"
33 #include "dnwebinterface.h"
34 #include "DNSettings.h"
35 #include "versioninfo.h"
38 QColor MainWindow::NORMAL_COLOR(0,0,0);
40 QColor MainWindow::WARN_COLOR(0xff, 0x95, 0x2b);
42 QColor MainWindow::ERROR_COLOR(0xff, 0,0);
44 QWidget *MainWindow::instance = NULL;
46 MainWindow::MainWindow(QWidget *parent) :
48 ui(new Ui::MainWindow),
49 mEngine(NULL), mWebInterface(NULL), mControlledByCreator(false), mCreatorIPCServer(NULL)
51 QStringList arguments = qApp->arguments();
52 for (int i = 0; i < arguments.length(); i++)
54 if (!mControlledByCreator && arguments.at(i) == "-creatorControlled")
57 if (i < arguments.length() && arguments.at(i).indexOf("denncoCreator_") == 0)
59 mCreatorIPCServer = new QLocalServer(this);
60 if (mCreatorIPCServer->listen(arguments.at(i)))
62 mControlledByCreator = true;
71 QString windowTitle = "dennco engine - ";
72 windowTitle.append(ENGINE_VERSION);
74 this->setWindowTitle(windowTitle);
76 if (!mControlledByCreator)
78 QString defaultPath(QDir::homePath());
79 QString contentPath = QString::fromStdString(DNSettings::getValue(DNSettings::CONTEXT_PATH, defaultPath.toStdString()));
81 ui->filePath->setText(contentPath);
83 ui->console->document()->setMaximumBlockCount(500);
84 QFont monofont("Courier");
85 monofont.setStyleHint(QFont::Monospace);
86 ui->console->setFont(monofont);
87 TKLog::setDestination(this);
88 connect(this, SIGNAL(consoleUpdated()), this, SLOT(update()), Qt::AutoConnection);
90 std::string savedWindowStateStr = DNSettings::getValue(DNSettings::WINDOWLAYOUT,"");
91 QByteArray savedWindowStateQArray(savedWindowStateStr.c_str());
92 restoreState(QByteArray::fromHex(savedWindowStateQArray));
94 std::string savedWindowGeometoryStr = DNSettings::getValue(DNSettings::WINDOWGEOMETORY,"");
95 QByteArray savedWindowGeometoryQArray(savedWindowGeometoryStr.c_str());
96 restoreGeometry(QByteArray::fromHex(savedWindowGeometoryQArray));
100 ui->filePath->setText("");
101 ui->filePath->setEnabled(false);
102 ui->chooseDirButton->setVisible(false);
104 ui->console->document()->setMaximumBlockCount(500);
105 QFont monofont("Courier");
106 monofont.setStyleHint(QFont::Monospace);
107 ui->console->setFont(monofont);
108 TKLog::setDestination(this);
109 connect(this, SIGNAL(consoleUpdated()), this, SLOT(update()), Qt::AutoConnection);
111 std::string savedWindowStateStr = DNSettings::getValue(DNSettings::CREATORCONTROLLED_WINDOWLAYOUT,"");
112 QByteArray savedWindowStateQArray(savedWindowStateStr.c_str());
113 restoreState(QByteArray::fromHex(savedWindowStateQArray));
115 std::string savedWindowGeometoryStr = DNSettings::getValue(DNSettings::CREATORCONTROLLED_WINDOWGEOMETORY,"");
116 QByteArray savedWindowGeometoryQArray(savedWindowGeometoryStr.c_str());
117 restoreGeometry(QByteArray::fromHex(savedWindowGeometoryQArray));
119 connect(mCreatorIPCServer, SIGNAL(newConnection()), this, SLOT(on_newRequestFromCreator()));
123 MainWindow::~MainWindow()
125 if (mCreatorIPCServer)
126 delete mCreatorIPCServer;
132 void MainWindow::closeEvent(QCloseEvent *event)
134 if (!mControlledByCreator)
136 QByteArray savingWindowStateQArray = saveState().toHex();
137 DNSettings::setValue(DNSettings::WINDOWLAYOUT, savingWindowStateQArray.constData());
139 QByteArray savingWindowGeometoryQArray = saveGeometry().toHex();
140 DNSettings::setValue(DNSettings::WINDOWGEOMETORY, savingWindowGeometoryQArray.constData());
144 QByteArray savingWindowStateQArray = saveState().toHex();
145 DNSettings::setValue(DNSettings::CREATORCONTROLLED_WINDOWLAYOUT, savingWindowStateQArray.constData());
147 QByteArray savingWindowGeometoryQArray = saveGeometry().toHex();
148 DNSettings::setValue(DNSettings::CREATORCONTROLLED_WINDOWGEOMETORY, savingWindowGeometoryQArray.constData());
152 deleteCurrentEngine();
153 QMainWindow::closeEvent(event);
157 void MainWindow::on_chooseDirButton_clicked()
159 if (QString::compare(ui->startButton->text(), "Stop", Qt::CaseInsensitive) == 0)
161 DNAlert::show("Please stop engine", "Dennco engine is running.\nPlease stop engine before switching directory.");
164 QString dirPath = QFileDialog::getExistingDirectory(this,"Set dennco setting file",ui->filePath->text());
166 if (dirPath.length() > 0)
168 ui->filePath->setText(dirPath);
170 DNSettings::setValue(DNSettings::CONTEXT_PATH, dirPath.toStdString());
175 void MainWindow::on_startButton_clicked()
177 qDebug()<< "start button clicked";
178 if (QString::compare(ui->startButton->text(), "Start", Qt::CaseInsensitive) == 0)
180 deleteCurrentEngine();
184 ui->startButton->setText("Stop");
190 ui->startButton->setText("Start");
194 bool MainWindow::startEngine()
196 bool initializationFailed = false;
198 mEngine = new DNEngine(ui->filePath->text().toLocal8Bit().data());
199 if (!dnGlobal()->isErrorStatusNormal())
201 DNAlert::show(dnGlobal()->getMessage1(), dnGlobal()->getMessage2());
202 dnGlobal()->resetErrorStatus();
203 initializationFailed = true;
205 else if (!mEngine->isValid())
207 DNAlert::show("DNEngine initialize error", "ERROR. DNEngine not configured properly");
208 initializationFailed = true;
211 if (!initializationFailed)
213 mEngine->startEngine();
216 return !initializationFailed;
219 bool MainWindow::stopEngine()
223 mEngine->stopEngine();
228 void MainWindow::loadUI()
230 QString path = "file:///";
231 path.append(ui->filePath->text().append(QString::fromStdString(mEngine->getUIPath())));
233 ui->webView->load(pathUrl);
234 mWebInterface = new DNWebInterface(mEngine);
235 attachWebInterface();
238 void MainWindow::attachWebInterface()
243 QWebPage *page = ui->webView->page();
246 QWebFrame *frame = page->mainFrame();
249 frame->addToJavaScriptWindowObject("engine", mWebInterface);
250 connect(frame, SIGNAL(javaScriptWindowObjectCleared()), this, SLOT(attachWebInterface()));
251 connect(frame, SIGNAL(initialLayoutCompleted()), this, SLOT(adjustWindowSize()));
256 void MainWindow::adjustWindowSize()
258 int hadjust = ui->webView->page()->mainFrame()->scrollBarMaximum(Qt::Horizontal);
259 int vadjust = ui->webView->page()->mainFrame()->scrollBarMaximum(Qt::Vertical);
260 hadjust = hadjust > 0 ? hadjust : 0;
261 vadjust = vadjust > 0 ? vadjust : 0;
262 QSize currentSize = size();
263 resize(currentSize.width() + hadjust, currentSize.height() + vadjust);
266 void MainWindow::deleteCurrentEngine()
275 delete mWebInterface;
276 mWebInterface = NULL;
280 void MainWindow::paintEvent( QPaintEvent * event )
283 while (!mPendingConsoleMessages.isEmpty())
285 PendingConsoleMessage aMessage = mPendingConsoleMessages.dequeue();
286 switch(aMessage.messageType)
289 ui->console->setTextColor(NORMAL_COLOR);
291 case TKLog::WARNING :
292 ui->console->setTextColor(WARN_COLOR);
295 ui->console->setTextColor(ERROR_COLOR);
298 ui->console->append(aMessage.text);
300 mConsoleLock.unlock();
302 QMainWindow::paintEvent(event);
306 void MainWindow::vprintf(TKLog::MessageType type, const char *fmt, va_list ap)
308 QString msg = QString().vsprintf(fmt,ap);
310 qDebug() << msg << endl;
313 PendingConsoleMessage newMessage;
314 newMessage.messageType = type;
315 newMessage.text = msg;
316 mPendingConsoleMessages.enqueue(newMessage);
317 mConsoleLock.unlock();
318 emit consoleUpdated();
321 void MainWindow::vDebugPrintf(const char *fmt, va_list ap)
324 QString msg = QString().vsprintf(fmt,ap);
325 qDebug() << "DEBUG:" << msg << endl;
329 void MainWindow::on_newRequestFromCreator()
331 QLocalSocket *clientConnection = mCreatorIPCServer->nextPendingConnection();
332 connect(clientConnection, SIGNAL(disconnected()), clientConnection, SLOT(deleteLater()));
334 while(clientConnection->state() == QLocalSocket::ConnectedState || clientConnection->state() == QLocalSocket::ConnectingState)
336 clientConnection->waitForReadyRead();
337 QString readString = clientConnection->readAll();
338 QStringList requestData = readString.split(",");
340 if (requestData.length() < 1)
343 if (requestData.at(0) == "load" && requestData.length() == 2)
346 qDebug() << "DEBUG: recieved load request from denncoCreator. New contant path:" << requestData.at(1) << endl;
348 ui->filePath->setText(requestData.at(1));
350 clientConnection->disconnectFromServer();
352 if (QString::compare(ui->startButton->text(), "Start", Qt::CaseInsensitive) != 0)
357 deleteCurrentEngine();
361 ui->startButton->setText("Stop");
365 else if (requestData.at(0) == "close")