2013-06-16 08:17:46 -05:00
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// pgAdmin 4 - PostgreSQL Tools
|
|
|
|
//
|
|
|
|
// Copyright (C) 2013, The pgAdmin Development Team
|
|
|
|
// This software is released under the PostgreSQL Licence
|
|
|
|
//
|
|
|
|
// pgAdmin4.cpp - Main application entry point
|
|
|
|
//
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2013-06-21 17:21:11 -05:00
|
|
|
#include "pgAdmin4.h"
|
|
|
|
|
2013-06-16 08:17:46 -05:00
|
|
|
// Must be before QT
|
|
|
|
#include <Python.h>
|
|
|
|
|
2013-06-21 17:21:11 -05:00
|
|
|
// QT headers
|
2013-06-16 08:17:46 -05:00
|
|
|
#include <QtGlobal>
|
|
|
|
|
|
|
|
#if QT_VERSION >= 0x050000
|
|
|
|
#include <QtWidgets>
|
|
|
|
#else
|
|
|
|
#include <QApplication>
|
2013-06-21 17:32:32 -05:00
|
|
|
#include <QDebug>
|
2013-06-16 08:17:46 -05:00
|
|
|
#endif
|
|
|
|
|
2013-06-21 17:21:11 -05:00
|
|
|
// App headers
|
2013-06-16 08:17:46 -05:00
|
|
|
#include "BrowserWindow.h"
|
2013-06-21 17:21:11 -05:00
|
|
|
#include "Server.h"
|
2013-06-16 08:17:46 -05:00
|
|
|
|
|
|
|
int main(int argc, char * argv[])
|
|
|
|
{
|
|
|
|
// Create the QT application
|
|
|
|
QApplication app(argc, argv);
|
|
|
|
|
2013-10-04 09:08:18 -05:00
|
|
|
// Setup the settings management
|
|
|
|
QCoreApplication::setOrganizationName("pgAdmin Development Team");
|
|
|
|
QCoreApplication::setOrganizationDomain("pgadmin.org");
|
|
|
|
QCoreApplication::setApplicationName(PGA_APP_NAME);
|
|
|
|
|
2013-10-04 12:16:31 -05:00
|
|
|
// Find an unused port number. Essentially, we're just reserving one
|
2014-12-16 06:53:09 -06:00
|
|
|
// here that Flask will use when we start up the server.
|
2013-10-04 11:12:10 -05:00
|
|
|
QTcpSocket socket;
|
|
|
|
socket.bind(0, QAbstractSocket::DontShareAddress);
|
|
|
|
quint16 port = socket.localPort();
|
|
|
|
|
2013-06-21 17:21:11 -05:00
|
|
|
// Fire up the webserver
|
2013-10-04 11:12:10 -05:00
|
|
|
Server *server = new Server(port);
|
2013-06-21 17:21:11 -05:00
|
|
|
|
|
|
|
if (!server->Init())
|
|
|
|
{
|
2013-10-04 11:12:10 -05:00
|
|
|
qDebug() << server->getError();
|
2013-06-21 17:21:11 -05:00
|
|
|
|
2013-10-04 12:16:31 -05:00
|
|
|
QString error = QString(QWidget::tr("An error occurred initialising the application server:\n\n%1")).arg(server->getError());
|
|
|
|
QMessageBox::critical(NULL, QString(QWidget::tr("Fatal Error")), error);
|
2013-06-21 17:21:11 -05:00
|
|
|
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
server->start();
|
2013-06-16 08:17:46 -05:00
|
|
|
|
2014-12-16 06:53:09 -06:00
|
|
|
// This is a hack. Wait a second and then check to see if the server thread
|
|
|
|
// is still running. If it's not, we probably had a startup error
|
2015-10-29 05:24:07 -05:00
|
|
|
QThread::sleep(1);
|
2014-12-16 06:53:09 -06:00
|
|
|
|
|
|
|
// Any errors?
|
|
|
|
if (server->isFinished() || server->getError().length() > 0)
|
|
|
|
{
|
|
|
|
qDebug() << server->getError();
|
|
|
|
|
|
|
|
QString error = QString(QWidget::tr("An error occurred initialising the application server:\n\n%1")).arg(server->getError());
|
|
|
|
QMessageBox::critical(NULL, QString(QWidget::tr("Fatal Error")), error);
|
|
|
|
|
|
|
|
// Allow the user to tweak the Python Path if needed
|
|
|
|
QSettings settings;
|
|
|
|
bool ok;
|
|
|
|
|
|
|
|
QString path = QInputDialog::getText(NULL, QWidget::tr("Python Path"), QWidget::tr("Set the Python search path:"), QLineEdit::Normal, settings.value("PythonPath").toString(), &ok);
|
|
|
|
|
|
|
|
if (ok)
|
|
|
|
settings.setValue("PythonPath", path);
|
|
|
|
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2013-10-04 12:16:31 -05:00
|
|
|
// Generate the app server URL
|
2015-03-10 08:09:11 -05:00
|
|
|
QString appServerUrl = QString("http://localhost:%1/").arg(port);
|
2013-10-04 12:16:31 -05:00
|
|
|
|
|
|
|
// Now the server should be up, we'll attempt to connect and get a response.
|
|
|
|
// We'll retry in a loop a few time before aborting if necessary. The browser
|
|
|
|
// will also retry - that shouldn't (in theory) be necessary, but it won't
|
|
|
|
// hurt.
|
|
|
|
int attempt = 0;
|
2014-12-16 06:53:09 -06:00
|
|
|
while (attempt++ < 3)
|
2013-10-04 12:16:31 -05:00
|
|
|
{
|
|
|
|
bool alive = PingServer(QUrl(appServerUrl));
|
|
|
|
|
|
|
|
if (alive)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (attempt == 10)
|
|
|
|
{
|
|
|
|
QString error(QWidget::tr("The application server could not be contacted."));
|
|
|
|
QMessageBox::critical(NULL, QString(QWidget::tr("Fatal Error")), error);
|
|
|
|
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2015-10-29 05:24:07 -05:00
|
|
|
QThread::sleep(1);
|
2013-10-04 12:16:31 -05:00
|
|
|
}
|
|
|
|
|
2013-06-16 08:17:46 -05:00
|
|
|
// Create & show the main window
|
2013-10-04 12:16:31 -05:00
|
|
|
BrowserWindow browserWindow(appServerUrl);
|
2013-06-16 08:17:46 -05:00
|
|
|
browserWindow.show();
|
|
|
|
|
|
|
|
// Go!
|
|
|
|
return app.exec();
|
|
|
|
}
|
|
|
|
|
2014-03-09 09:39:12 -05:00
|
|
|
|
|
|
|
// Ping the application server to see if it's alive
|
2013-10-04 12:16:31 -05:00
|
|
|
bool PingServer(QUrl url)
|
|
|
|
{
|
|
|
|
QNetworkAccessManager manager;
|
|
|
|
QEventLoop loop;
|
|
|
|
QNetworkReply *reply;
|
|
|
|
QVariant redirectUrl;
|
|
|
|
|
2015-03-10 08:09:11 -05:00
|
|
|
url.setPath("/ping");
|
2013-10-04 12:16:31 -05:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
reply = manager.get(QNetworkRequest(url));
|
|
|
|
|
|
|
|
QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
|
|
|
|
loop.exec();
|
|
|
|
|
|
|
|
redirectUrl = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
|
|
|
|
url = redirectUrl.toUrl();
|
|
|
|
|
|
|
|
if (!redirectUrl.isNull())
|
|
|
|
delete reply;
|
|
|
|
|
|
|
|
} while (!redirectUrl.isNull());
|
|
|
|
|
|
|
|
if (reply->error() != QNetworkReply::NoError)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString response = reply->readAll();
|
|
|
|
|
|
|
|
if (response != "PING")
|
|
|
|
{
|
|
|
|
qDebug() << "Failed to connect, server response: " << response;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2014-03-09 09:39:12 -05:00
|
|
|
|