1) Fixed pgAdmin hang issue when the user clicks on 'View Log' menu option.

2) Fixed some code smell in runtime code.
This commit is contained in:
Akshay Joshi
2020-07-29 15:31:38 +05:30
parent f715373218
commit 510dd5c047
15 changed files with 92 additions and 85 deletions
-2
View File
@@ -25,8 +25,6 @@ class ConfigWindow : public QDialog
public: public:
explicit ConfigWindow(QWidget *parent = Q_NULLPTR); explicit ConfigWindow(QWidget *parent = Q_NULLPTR);
bool NeedRestart() { return m_needRestart; };
signals: signals:
void accepted(bool needRestart); void accepted(bool needRestart);
void closing(bool accepted); void closing(bool accepted);
+16 -2
View File
@@ -74,11 +74,11 @@ void FloatingWindow::createActions()
connect(m_copyUrlAction, SIGNAL(triggered()), m_menuActions, SLOT(onCopyUrl())); connect(m_copyUrlAction, SIGNAL(triggered()), m_menuActions, SLOT(onCopyUrl()));
m_configAction = new QAction(tr("C&onfigure..."), this); m_configAction = new QAction(tr("C&onfigure..."), this);
m_configAction->setEnabled(true); m_configAction->setEnabled(false);
connect(m_configAction, SIGNAL(triggered()), m_menuActions, SLOT(onConfig())); connect(m_configAction, SIGNAL(triggered()), m_menuActions, SLOT(onConfig()));
m_logAction = new QAction(tr("&View log..."), this); m_logAction = new QAction(tr("&View log..."), this);
m_logAction->setEnabled(true); m_logAction->setEnabled(false);
connect(m_logAction, SIGNAL(triggered()), m_menuActions, SLOT(onLog())); connect(m_logAction, SIGNAL(triggered()), m_menuActions, SLOT(onLog()));
m_quitAction = new QAction(tr("&Shut down server"), this); m_quitAction = new QAction(tr("&Shut down server"), this);
@@ -110,6 +110,20 @@ void FloatingWindow::setMenuActions(MenuActions * menuActions)
m_menuActions = menuActions; m_menuActions = menuActions;
} }
// Enable the View Log option
void FloatingWindow::enableViewLogOption()
{
if (m_logAction != Q_NULLPTR)
m_logAction->setEnabled(true);
}
// Disable the View Log option
void FloatingWindow::disableViewLogOption()
{
if (m_logAction != Q_NULLPTR)
m_logAction->setEnabled(false);
}
void FloatingWindow::closeEvent(QCloseEvent * event) void FloatingWindow::closeEvent(QCloseEvent * event)
{ {
+2
View File
@@ -29,6 +29,8 @@ public:
bool Init(); bool Init();
void enablePostStartOptions(); void enablePostStartOptions();
void enableViewLogOption();
void disableViewLogOption();
void setMenuActions(MenuActions * menuActions); void setMenuActions(MenuActions * menuActions);
private: private:
+5 -5
View File
@@ -37,11 +37,11 @@ void LogWindow::LoadLog()
ui->lblStatus->setText(tr("Loading logfiles...")); ui->lblStatus->setText(tr("Loading logfiles..."));
ui->lblStartupLog->setText(tr("Startup Log (%1):").arg(getStartupLogFile())); ui->lblStartupLog->setText(tr("Startup Log (%1):").arg(g_startupLogFile));
ui->lblServerLog->setText(tr("Server Log (%1):").arg(getServerLogFile())); ui->lblServerLog->setText(tr("Server Log (%1):").arg(g_serverLogFile));
startupLines = this->readLog(getStartupLogFile(), ui->textStartupLog); startupLines = this->readLog(g_startupLogFile, ui->textStartupLog);
serverLines = this->readLog(getServerLogFile(), ui->textServerLog); serverLines = this->readLog(g_serverLogFile, ui->textServerLog);
ui->lblStatus->setText(QString(tr("Loaded startup log (%1 lines) and server log (%2 lines).")).arg(startupLines).arg(serverLines)); ui->lblStatus->setText(QString(tr("Loaded startup log (%1 lines) and server log (%2 lines).")).arg(startupLines).arg(serverLines));
} }
@@ -73,7 +73,7 @@ int LogWindow::readLog(QString logFile, QPlainTextEdit *logWidget)
log = fopen(logFile.toUtf8().data(), "r"); log = fopen(logFile.toUtf8().data(), "r");
if (log == Q_NULLPTR) if (log == Q_NULLPTR)
{ {
logWidget->setPlainText(QString(tr("The log file (%1) could not be opened.")).arg(getServerLogFile())); logWidget->setPlainText(QString(tr("The log file (%1) could not be opened.")).arg(g_serverLogFile));
this->setDisabled(false); this->setDisabled(false);
QApplication::restoreOverrideCursor(); QApplication::restoreOverrideCursor();
return 0; return 0;
+1 -1
View File
@@ -33,7 +33,7 @@ Logger* Logger::GetLogger()
{ {
m_pThis = new Logger(); m_pThis = new Logger();
m_Logfile = new QFile; m_Logfile = new QFile;
m_Logfile->setFileName(getStartupLogFile()); m_Logfile->setFileName(g_startupLogFile);
m_Logfile->open(QIODevice::WriteOnly | QIODevice::Text); m_Logfile->open(QIODevice::WriteOnly | QIODevice::Text);
m_Logfile->setPermissions(QFile::ReadOwner|QFile::WriteOwner); m_Logfile->setPermissions(QFile::ReadOwner|QFile::WriteOwner);
} }
+6 -7
View File
@@ -75,15 +75,14 @@ void MenuActions::onConfig()
} }
void MenuActions::onConfigDone(bool needRestart) void MenuActions::onConfigDone(bool needRestart) const
{ {
if (needRestart) if (needRestart && QMessageBox::Yes == QMessageBox::question(Q_NULLPTR,
tr("Shut down server?"),
tr("The pgAdmin 4 server must be restarted for changes to take effect. Do you want to shut down the server now?"),
QMessageBox::Yes | QMessageBox::No))
{ {
if (QMessageBox::Yes == QMessageBox::question(Q_NULLPTR, exit(0);
tr("Shut down server?"),
tr("The pgAdmin 4 server must be restarted for changes to take effect. Do you want to shut down the server now?"),
QMessageBox::Yes | QMessageBox::No))
exit(0);
} }
} }
+1 -1
View File
@@ -30,7 +30,7 @@ private:
ConfigWindow *m_configWindow = Q_NULLPTR; ConfigWindow *m_configWindow = Q_NULLPTR;
public slots: public slots:
void onConfigDone(bool needRestart); void onConfigDone(bool needRestart) const;
protected slots: protected slots:
void onNew(); void onNew();
+22 -10
View File
@@ -208,7 +208,7 @@ bool Runtime::alreadyRunning()
if (is_running) if (is_running)
{ {
QFile addressFile(getAddressFile()); QFile addressFile(g_addressFile);
addressFile.open(QIODevice::ReadOnly | QIODevice::Text); addressFile.open(QIODevice::ReadOnly | QIODevice::Text);
QTextStream in(&addressFile); QTextStream in(&addressFile);
QString url = in.readLine(); QString url = in.readLine();
@@ -273,7 +273,7 @@ QSplashScreen * Runtime::displaySplash(QApplication *app)
// Get the port number we're going to use // Get the port number we're going to use
quint16 Runtime::getPort() quint16 Runtime::getPort() const
{ {
quint16 port = 0L; quint16 port = 0L;
@@ -347,7 +347,7 @@ FloatingWindow * Runtime::createFloatingWindow(QSplashScreen *splash, MenuAction
// Server startup loop // Server startup loop
Server * Runtime::startServerLoop(QSplashScreen *splash, FloatingWindow *floatingWindow, TrayIcon *trayIcon, int port, QString key) Server * Runtime::startServerLoop(QSplashScreen *splash, FloatingWindow *floatingWindow, TrayIcon *trayIcon, quint16 port, QString key)
{ {
bool done = false; bool done = false;
Server *server; Server *server;
@@ -374,6 +374,12 @@ Server * Runtime::startServerLoop(QSplashScreen *splash, FloatingWindow *floatin
delete server; delete server;
// Enable the View Log option for diagnostics
if (floatingWindow)
floatingWindow->enableViewLogOption();
if (trayIcon)
trayIcon->enableViewLogOption();
// Allow the user to tweak the Python Path if needed // Allow the user to tweak the Python Path if needed
m_configDone = false; m_configDone = false;
@@ -387,6 +393,12 @@ Server * Runtime::startServerLoop(QSplashScreen *splash, FloatingWindow *floatin
// Wait for configuration to be completed // Wait for configuration to be completed
while (!m_configDone) while (!m_configDone)
delay(100); delay(100);
// Disable the View Log option again
if (floatingWindow)
floatingWindow->disableViewLogOption();
if (trayIcon)
trayIcon->disableViewLogOption();
} }
else else
{ {
@@ -410,16 +422,16 @@ void Runtime::onConfigDone(bool accepted)
// Start the server // Start the server
Server * Runtime::startServer(QSplashScreen *splash, int port, QString key) Server * Runtime::startServer(QSplashScreen *splash, quint16 port, QString key)
{ {
Server *server; Server *server;
splash->showMessage(QString(QWidget::tr("Starting pgAdmin4 server...")), Qt::AlignBottom | Qt::AlignCenter); splash->showMessage(QString(QWidget::tr("Starting pgAdmin4 server...")), Qt::AlignBottom | Qt::AlignCenter);
Logger::GetLogger()->Log("Starting pgAdmin4 server..."); Logger::GetLogger()->Log("Starting pgAdmin4 server...");
QString msg = QString(QWidget::tr("Creating server object, port:%1, key:%2, logfile:%3")).arg(port).arg(key).arg(getServerLogFile()); QString msg = QString(QWidget::tr("Creating server object, port:%1, key:%2, logfile:%3")).arg(port).arg(key).arg(g_serverLogFile);
Logger::GetLogger()->Log(msg); Logger::GetLogger()->Log(msg);
server = new Server(this, port, key, getServerLogFile()); server = new Server(this, port, key, g_serverLogFile);
Logger::GetLogger()->Log("Initializing server..."); Logger::GetLogger()->Log("Initializing server...");
if (!server->Init()) if (!server->Init())
@@ -504,9 +516,9 @@ void Runtime::checkServer(QSplashScreen *splash, QString url)
// Create the address file // Create the address file
void Runtime::createAddressFile(QString url) void Runtime::createAddressFile(QString url) const
{ {
QFile addressFile(getAddressFile()); QFile addressFile(g_addressFile);
if (addressFile.open(QIODevice::WriteOnly)) if (addressFile.open(QIODevice::WriteOnly))
{ {
addressFile.setPermissions(QFile::ReadOwner|QFile::WriteOwner); addressFile.setPermissions(QFile::ReadOwner|QFile::WriteOwner);
@@ -517,7 +529,7 @@ void Runtime::createAddressFile(QString url)
// Open a browser tab // Open a browser tab
void Runtime::openBrowserTab(QString url) void Runtime::openBrowserTab(QString url) const
{ {
QString cmd = m_settings.value("BrowserCommand").toString(); QString cmd = m_settings.value("BrowserCommand").toString();
@@ -594,7 +606,7 @@ bool Runtime::shutdownServer(QUrl url)
} }
void Runtime::delay(int milliseconds) void Runtime::delay(int milliseconds) const
{ {
QTime endTime = QTime::currentTime().addMSecs(milliseconds); QTime endTime = QTime::currentTime().addMSecs(milliseconds);
while(QTime::currentTime() < endTime) while(QTime::currentTime() < endTime)
+6 -6
View File
@@ -36,7 +36,7 @@ public:
bool alreadyRunning(); bool alreadyRunning();
bool go(int argc, char *argv[]); bool go(int argc, char *argv[]);
void delay(int milliseconds); void delay(int milliseconds) const;
bool shutdownServer(QUrl url); bool shutdownServer(QUrl url);
private: private:
@@ -47,14 +47,14 @@ private:
void setupStyling(QApplication *app); void setupStyling(QApplication *app);
void configureProxy(); void configureProxy();
QSplashScreen *displaySplash(QApplication *app); QSplashScreen *displaySplash(QApplication *app);
quint16 getPort(); quint16 getPort() const;
TrayIcon *createTrayIcon(QSplashScreen *splash, MenuActions *menuActions); TrayIcon *createTrayIcon(QSplashScreen *splash, MenuActions *menuActions);
FloatingWindow *createFloatingWindow(QSplashScreen *splash, MenuActions *menuActions); FloatingWindow *createFloatingWindow(QSplashScreen *splash, MenuActions *menuActions);
Server *startServerLoop(QSplashScreen *splash, FloatingWindow *floatingWindow, TrayIcon *trayIcon, int port, QString key); Server *startServerLoop(QSplashScreen *splash, FloatingWindow *floatingWindow, TrayIcon *trayIcon, quint16 port, QString key);
Server *startServer(QSplashScreen *splash, int port, QString key); Server *startServer(QSplashScreen *splash, quint16 port, QString key);
void checkServer(QSplashScreen *splash, QString url); void checkServer(QSplashScreen *splash, QString url);
void createAddressFile(QString url); void createAddressFile(QString url) const;
void openBrowserTab(QString url); void openBrowserTab(QString url) const;
QString serverRequest(QUrl url, QString path); QString serverRequest(QUrl url, QString path);
bool pingServer(QUrl url); bool pingServer(QUrl url);
+2 -5
View File
@@ -68,10 +68,8 @@ Server::Server(Runtime *runtime, quint16 port, QString key, QString logFileName)
Py_NoUserSiteDirectory=1; Py_NoUserSiteDirectory=1;
Py_DontWriteBytecodeFlag=1; Py_DontWriteBytecodeFlag=1;
PGA_APP_NAME_UTF8 = QString("pgAdmin 4").toUtf8();
// Python3 requires conversion of char * to wchar_t *, so... // Python3 requires conversion of char * to wchar_t *, so...
const char *appName = PGA_APP_NAME_UTF8.data(); const char *appName = QString("pgAdmin 4").toUtf8();
const size_t cSize = strlen(appName)+1; const size_t cSize = strlen(appName)+1;
m_wcAppName = new wchar_t[cSize]; m_wcAppName = new wchar_t[cSize];
mbstowcs (m_wcAppName, appName, cSize); mbstowcs (m_wcAppName, appName, cSize);
@@ -177,8 +175,7 @@ Server::Server(Runtime *runtime, quint16 port, QString key, QString logFileName)
if (!pythonHome.isEmpty()) if (!pythonHome.isEmpty())
{ {
pythonHome_utf8 = pythonHome.toUtf8(); const char *python_home = pythonHome.toUtf8().data();
const char *python_home = pythonHome_utf8.data();
const size_t home_size = strlen(python_home) + 1; const size_t home_size = strlen(python_home) + 1;
m_wcPythonHome = new wchar_t[home_size]; m_wcPythonHome = new wchar_t[home_size];
mbstowcs (m_wcPythonHome, python_home, home_size); mbstowcs (m_wcPythonHome, python_home, home_size);
-2
View File
@@ -47,11 +47,9 @@ private:
// Application name in UTF-8 for Python // Application name in UTF-8 for Python
wchar_t *m_wcAppName = Q_NULLPTR; wchar_t *m_wcAppName = Q_NULLPTR;
QByteArray PGA_APP_NAME_UTF8;
// PythonHome for Python // PythonHome for Python
wchar_t *m_wcPythonHome = Q_NULLPTR; wchar_t *m_wcPythonHome = Q_NULLPTR;
QByteArray pythonHome_utf8;
}; };
#endif // SERVER_H #endif // SERVER_H
+17 -2
View File
@@ -78,11 +78,11 @@ void TrayIcon::createActions()
connect(m_copyUrlAction, SIGNAL(triggered()), m_menuActions, SLOT(onCopyUrl())); connect(m_copyUrlAction, SIGNAL(triggered()), m_menuActions, SLOT(onCopyUrl()));
m_configAction = new QAction(tr("&Configure..."), this); m_configAction = new QAction(tr("&Configure..."), this);
m_configAction->setEnabled(true); m_configAction->setEnabled(false);
connect(m_configAction, SIGNAL(triggered()), m_menuActions, SLOT(onConfig())); connect(m_configAction, SIGNAL(triggered()), m_menuActions, SLOT(onConfig()));
m_logAction = new QAction(tr("&View log..."), this); m_logAction = new QAction(tr("&View log..."), this);
m_logAction->setEnabled(true); m_logAction->setEnabled(false);
connect(m_logAction, SIGNAL(triggered()), m_menuActions, SLOT(onLog())); connect(m_logAction, SIGNAL(triggered()), m_menuActions, SLOT(onLog()));
m_quitAction = new QAction(tr("&Shut down server"), this); m_quitAction = new QAction(tr("&Shut down server"), this);
@@ -109,6 +109,21 @@ void TrayIcon::enablePostStartOptions()
m_quitAction->setEnabled(true); m_quitAction->setEnabled(true);
} }
// Enable the View Log option
void TrayIcon::enableViewLogOption()
{
if (m_logAction != Q_NULLPTR)
m_logAction->setEnabled(true);
}
// Disable the View Log option
void TrayIcon::disableViewLogOption()
{
if (m_logAction != Q_NULLPTR)
m_logAction->setEnabled(false);
}
void TrayIcon::setMenuActions(MenuActions * menuActions) void TrayIcon::setMenuActions(MenuActions * menuActions)
{ {
m_menuActions = menuActions; m_menuActions = menuActions;
+2
View File
@@ -26,6 +26,8 @@ public:
void Init(); void Init();
void enablePostStartOptions(); void enablePostStartOptions();
void enableViewLogOption();
void disableViewLogOption();
void setMenuActions(MenuActions * menuActions); void setMenuActions(MenuActions * menuActions);
private: private:
+8 -39
View File
@@ -15,9 +15,9 @@
#include <stdlib.h> #include <stdlib.h>
// Global vars for caching and avoing shutdown issues // Global vars for caching and avoing shutdown issues
QString g_startupLogFile; const QString g_startupLogFile = QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation) + ("/pgadmin4.startup.log");
QString g_serverLogFile; const QString g_serverLogFile = QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation) + (QString("/pgadmin4.%1.log").arg(getExeHash()));
QString g_addressFile; const QString g_addressFile = QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation) + (QString("/pgadmin4.%1.addr").arg(getExeHash()));
int main(int argc, char * argv[]) int main(int argc, char * argv[])
{ {
@@ -30,46 +30,15 @@ int main(int argc, char * argv[])
return runtime->go(argc, argv); return runtime->go(argc, argv);
} }
// Get the filename for the startup log
QString getStartupLogFile()
{
if (g_startupLogFile == "")
g_startupLogFile = QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation) + ("/pgadmin4.startup.log");
return g_startupLogFile;
}
// Get the filename for the server log
QString getServerLogFile()
{
if (g_serverLogFile == "")
g_serverLogFile = QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation) + (QString("/pgadmin4.%1.log").arg(getExeHash()));
return g_serverLogFile;
}
// Get the filename for the address file
QString getAddressFile()
{
if (g_addressFile == "")
g_addressFile = QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation) + (QString("/pgadmin4.%1.addr").arg(getExeHash()));
return g_addressFile;
}
// Cleanup the address and log files // Cleanup the address and log files
void cleanup() void cleanup()
{ {
qDebug() << "Removing:" << getAddressFile(); qDebug() << "Removing:" << g_addressFile;
QFile addrFile(getAddressFile()); QFile addrFile(g_addressFile);
addrFile.remove(); addrFile.remove();
qDebug() << "Removing:" << getServerLogFile(); qDebug() << "Removing:" << g_serverLogFile;
QFile logFile(getServerLogFile()); QFile logFile(g_serverLogFile);
logFile.remove(); logFile.remove();
} }
@@ -78,4 +47,4 @@ void cleanup()
QString getExeHash() QString getExeHash()
{ {
return QString(QCryptographicHash::hash(QCoreApplication::applicationFilePath().toUtf8(),QCryptographicHash::Md5).toHex()); return QString(QCryptographicHash::hash(QCoreApplication::applicationFilePath().toUtf8(),QCryptographicHash::Md5).toHex());
} }
+4 -3
View File
@@ -18,9 +18,10 @@
// Global function prototypes // Global function prototypes
int main(int argc, char * argv[]); int main(int argc, char * argv[]);
QString getStartupLogFile(); extern const QString g_startupLogFile;
QString getServerLogFile(); extern const QString g_serverLogFile;
QString getAddressFile(); extern const QString g_addressFile;
QString getExeHash(); QString getExeHash();
void cleanup(); void cleanup();