2013-06-16 13:17:46 +00:00
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// pgAdmin 4 - PostgreSQL Tools
|
|
|
|
//
|
|
|
|
// Copyright (C) 2013, The pgAdmin Development Team
|
|
|
|
// This software is released under the PostgreSQL Licence
|
|
|
|
//
|
|
|
|
// BrowserWindow.cpp - Implementation of the main window class
|
|
|
|
//
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2013-06-21 22:21:11 +00:00
|
|
|
#include "pgAdmin4.h"
|
2013-06-16 13:17:46 +00:00
|
|
|
|
2013-06-21 22:21:11 +00:00
|
|
|
// QT headers
|
2013-06-16 13:17:46 +00:00
|
|
|
#include <QtGlobal>
|
|
|
|
|
|
|
|
#if QT_VERSION >= 0x050000
|
|
|
|
#include <QtWidgets>
|
|
|
|
#include <QtWebKitWidgets>
|
|
|
|
#else
|
|
|
|
#include <QtWebKit>
|
|
|
|
#include <QAction>
|
|
|
|
#include <QMenu>
|
|
|
|
#include <QMenuBar>
|
|
|
|
#include <QMessageBox>
|
|
|
|
#include <QInputDialog>
|
|
|
|
#include <QLineEdit>
|
|
|
|
#endif
|
|
|
|
|
2013-06-21 22:21:11 +00:00
|
|
|
// App headers
|
2013-06-16 13:17:46 +00:00
|
|
|
#include "BrowserWindow.h"
|
|
|
|
|
2013-06-21 23:49:40 +00:00
|
|
|
|
2013-06-16 13:17:46 +00:00
|
|
|
// Constructor
|
2013-10-04 16:12:10 +00:00
|
|
|
BrowserWindow::BrowserWindow(quint16 port)
|
2013-06-16 13:17:46 +00:00
|
|
|
{
|
2013-10-04 16:12:10 +00:00
|
|
|
// Setup the URL the browser will connect to
|
|
|
|
m_appServerUrl = QString("http://localhost:%1").arg(port);
|
|
|
|
|
|
|
|
// Setup the UI
|
2013-06-16 13:17:46 +00:00
|
|
|
createActions();
|
|
|
|
createMenus();
|
|
|
|
|
|
|
|
// Create the WebKit control
|
|
|
|
webView = new QWebView(this);
|
|
|
|
setCentralWidget(webView);
|
2013-06-21 23:49:40 +00:00
|
|
|
connect(webView, SIGNAL(loadFinished(bool)), SLOT(finishLoading(bool)));
|
2013-06-16 13:17:46 +00:00
|
|
|
|
2013-10-04 14:08:18 +00:00
|
|
|
// Restore the geometry
|
|
|
|
QSettings settings;
|
|
|
|
restoreGeometry(settings.value("geometry").toByteArray());
|
|
|
|
restoreState(settings.value("windowState").toByteArray());
|
|
|
|
|
2013-06-21 22:21:11 +00:00
|
|
|
// Display the app
|
2013-10-04 16:12:10 +00:00
|
|
|
m_initialLoad = true;
|
|
|
|
m_loadAttempt = 1;
|
|
|
|
webView->setUrl(QString("http://localhost/:%1").arg(9876));
|
2013-06-16 13:17:46 +00:00
|
|
|
}
|
|
|
|
|
2013-06-21 23:49:40 +00:00
|
|
|
|
2013-10-04 14:08:18 +00:00
|
|
|
// Save the window geometry on close
|
|
|
|
void BrowserWindow::closeEvent(QCloseEvent *event)
|
|
|
|
{
|
|
|
|
QSettings settings;
|
|
|
|
settings.setValue("geometry", saveGeometry());
|
|
|
|
settings.setValue("windowState", saveState());
|
|
|
|
QMainWindow::closeEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-16 13:17:46 +00:00
|
|
|
// Create the actions for the window
|
|
|
|
void BrowserWindow::createActions()
|
|
|
|
{
|
|
|
|
// Open an arbitrary URL
|
|
|
|
openUrlAction = new QAction(tr("&Open URL..."), this);
|
|
|
|
openUrlAction->setShortcut(tr("Ctrl+U"));
|
|
|
|
openUrlAction->setStatusTip(tr("Open a URL"));
|
|
|
|
connect(openUrlAction, SIGNAL(triggered()), this, SLOT(openUrl()));
|
|
|
|
|
|
|
|
// Exit the app
|
|
|
|
exitAction = new QAction(tr("E&xit"), this);
|
|
|
|
exitAction->setStatusTip(tr("Exit the application"));
|
|
|
|
exitAction->setShortcuts(QKeySequence::Quit);
|
|
|
|
connect(exitAction, SIGNAL(triggered()), this, SLOT(close()));
|
|
|
|
|
|
|
|
// About box
|
|
|
|
aboutAction = new QAction(tr("&About"), this);
|
|
|
|
aboutAction->setStatusTip(tr("Show the application's About box"));
|
|
|
|
connect(aboutAction, SIGNAL(triggered()), this, SLOT(about()));
|
|
|
|
}
|
|
|
|
|
2013-06-21 23:49:40 +00:00
|
|
|
|
2013-06-16 13:17:46 +00:00
|
|
|
// Create the application menus
|
|
|
|
void BrowserWindow::createMenus()
|
|
|
|
{
|
|
|
|
// File
|
|
|
|
fileMenu = menuBar()->addMenu(tr("&File"));
|
|
|
|
fileMenu->addAction(openUrlAction);
|
|
|
|
fileMenu->addSeparator();
|
|
|
|
fileMenu->addAction(exitAction);
|
|
|
|
|
|
|
|
menuBar()->addSeparator();
|
|
|
|
|
|
|
|
// Help
|
|
|
|
helpMenu = menuBar()->addMenu(tr("&Help"));
|
|
|
|
helpMenu->addAction(aboutAction);
|
|
|
|
}
|
|
|
|
|
2013-06-21 23:49:40 +00:00
|
|
|
|
|
|
|
// Process loading finished signals from the web view.
|
|
|
|
void BrowserWindow::finishLoading(bool ok)
|
|
|
|
{
|
2013-10-04 16:12:10 +00:00
|
|
|
if (m_initialLoad && !ok)
|
2013-06-21 23:49:40 +00:00
|
|
|
{
|
|
|
|
// The load attempt failed. Try again up to 4 times with an
|
|
|
|
// incremental backoff.
|
2013-10-04 16:12:10 +00:00
|
|
|
if (m_loadAttempt < 5)
|
2013-06-21 23:49:40 +00:00
|
|
|
{
|
2013-10-04 16:12:10 +00:00
|
|
|
if (m_loadAttempt > 1)
|
2013-06-21 23:59:34 +00:00
|
|
|
{
|
2013-10-04 16:12:10 +00:00
|
|
|
qDebug() << "Initial connection failed. Retrying in" << m_loadAttempt << "seconds.";
|
|
|
|
webView->setHtml(QString(tr("<p>Failed to connect to the pgAdmin application server. Retrying in %1 seconds, ") +
|
|
|
|
tr("or click <a href=\"%2\">here</a> to try again now.</p>")).arg(m_loadAttempt).arg(m_appServerUrl));
|
2013-06-21 23:59:34 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-10-04 16:12:10 +00:00
|
|
|
webView->setHtml(QString(tr("<p>Connecting to the application server...</p>")));
|
2013-06-21 23:59:34 +00:00
|
|
|
}
|
2013-06-21 23:49:40 +00:00
|
|
|
|
2013-10-04 16:12:10 +00:00
|
|
|
pause(m_loadAttempt);
|
|
|
|
webView->setUrl(m_appServerUrl);
|
|
|
|
m_loadAttempt++;
|
2013-06-21 23:59:34 +00:00
|
|
|
|
2013-06-21 23:49:40 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-10-04 16:12:10 +00:00
|
|
|
qDebug() << "Initial connection failed after multiple attempts. Aborting.";
|
2013-06-21 23:49:40 +00:00
|
|
|
webView->setHtml(QString(tr("<p>Failed to connect to the pgAdmin application server. ") +
|
2013-10-04 16:12:10 +00:00
|
|
|
tr("Click <a href=\"%1\">here</a> to try again.</p>")).arg(m_appServerUrl));
|
2013-06-21 23:49:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-04 16:12:10 +00:00
|
|
|
m_initialLoad = false;
|
2013-06-21 23:49:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Pause for n seconds, without freezing the UI.
|
|
|
|
void BrowserWindow::pause(int seconds)
|
|
|
|
{
|
|
|
|
QTime dieTime = QTime::currentTime().addSecs(seconds);
|
|
|
|
|
|
|
|
while (QTime::currentTime() < dieTime)
|
|
|
|
QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-16 13:17:46 +00:00
|
|
|
// Display the about box
|
|
|
|
void BrowserWindow::about()
|
|
|
|
{
|
|
|
|
QMessageBox::about(this, tr("About pgAdmin 4"), tr("pgAdmin 4 - PostgreSQL Tools"));
|
|
|
|
}
|
|
|
|
|
2013-06-21 23:49:40 +00:00
|
|
|
|
2013-06-16 13:17:46 +00:00
|
|
|
// Open an arbitrary URL
|
|
|
|
void BrowserWindow::openUrl()
|
|
|
|
{
|
|
|
|
bool ok;
|
|
|
|
QString url = QInputDialog::getText(this, tr("Enter a URL"), tr("URL:"), QLineEdit::Normal, "http://", &ok);
|
|
|
|
|
|
|
|
if (ok && !url.isEmpty())
|
|
|
|
webView->setUrl(url);
|
|
|
|
}
|
|
|
|
|