Refactor FileSysClient into PluginManager #4696

This commit is contained in:
Jerry (Xinyu Hou) 2015-10-19 18:07:08 -07:00
parent 6c4ee29649
commit 084e2c1e05
6 changed files with 137 additions and 235 deletions

View File

@ -1,54 +0,0 @@
/*
* synergy -- mouse and keyboard sharing utility
* Copyright (C) 2015 Synergy Si, Std.
*
* This package is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* found in the file LICENSE that should have accompanied this file.
*
* This package is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "FileSysClient.h"
#include "EditionType.h"
#include "QUtility.h"
#include <QDir>
#include <QProcess>
#include <QMessageBox>
#include <QCoreApplication>
#include <stdexcept>
void FileSysClient::queryPluginList()
{
try {
isDone(false);
QString extension = "*" + Plugin::getOsSpecificExt();
QStringList nameFilter(extension);
QString installDir(m_CoreInterface.getInstalledDir()
.append(QDir::separator())
.append(Plugin::getOsSpecificInstallerLocation()));
QString searchDirectory(installDir);
QDir directory(searchDirectory);
m_PluginList = directory.entryList(nameFilter);
isDone(true);
}
catch (std::exception& e)
{
isDone(true);
emit error(tr( "An error occurred while trying to load the "
"plugin list. Please contact the help desk, and "
"provide the following details.\n\n%1").arg(e.what()));
}
emit queryPluginDone();
return;
}

View File

@ -1,63 +0,0 @@
/*
* synergy -- mouse and keyboard sharing utility
* Copyright (C) 2015 Synergy Si, Std.
*
* This package is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* found in the file LICENSE that should have accompanied this file.
*
* This package is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef FileSysClient_H
#define FileSysClient_H
#include <QString>
#include <QStringList>
#include <QObject>
#include "Plugin.h"
#include "CoreInterface.h"
class QMessageBox;
class QWidget;
class QStringList;
class FileSysClient : public QObject
{
Q_OBJECT
public:
QStringList& getPluginList() { return m_PluginList; }
bool isDone() { return done; }
int count() { return copyCount; }
public slots:
void queryPluginList();
signals:
void error(QString e);
void queryPluginDone();
private:
void isDone(bool b) { done = b; }
QString request(const QString& email,
const QString& password,
QStringList& args);
Plugin plugin;
void count(int i) { copyCount = i; }
private:
int copyCount;
bool done;
QStringList m_PluginList;
CoreInterface m_CoreInterface;
};
#endif // FileSysClient_H

View File

@ -35,15 +35,17 @@
PluginManager::PluginManager() : PluginManager::PluginManager() :
m_FileSysPluginList() m_PluginList()
{
init();
}
PluginManager::~PluginManager()
{ {
} }
void PluginManager::initFromFileSys(QStringList pluginList) void PluginManager::init()
{ {
m_FileSysPluginList.clear();
m_FileSysPluginList.append(pluginList);
m_PluginDir = m_CoreInterface.getPluginDir(); m_PluginDir = m_CoreInterface.getPluginDir();
if (m_PluginDir.isEmpty()) { if (m_PluginDir.isEmpty()) {
emit error(tr("Failed to get plugin directory.")); emit error(tr("Failed to get plugin directory."));
@ -60,10 +62,6 @@ void PluginManager::initFromFileSys(QStringList pluginList)
} }
} }
PluginManager::~PluginManager()
{
}
bool PluginManager::exist(QString name) bool PluginManager::exist(QString name)
{ {
CoreInterface coreInterface; CoreInterface coreInterface;
@ -106,12 +104,12 @@ void PluginManager::copyPlugins()
destDir.mkpath("."); destDir.mkpath(".");
} }
// Run through the list of plugins and copy them // Run through the list of plugins and copy them
for ( int i = 0 ; i < m_FileSysPluginList.size() ; i++ ) { for ( int i = 0 ; i < m_PluginList.size() ; i++ ) {
// Get a file entry for the plugin using the full path // Get a file entry for the plugin using the full path
QFile file(srcDirName + QDir::separator() + m_FileSysPluginList.at(i)); QFile file(srcDirName + QDir::separator() + m_PluginList.at(i));
// construct the destination file name // construct the destination file name
QString newName(destDirName + QDir::separator() + m_FileSysPluginList.at(i)); QString newName(destDirName + QDir::separator() + m_PluginList.at(i));
// Check to see if the plugin already exists // Check to see if the plugin already exists
QFile newFile(newName); QFile newFile(newName);
@ -136,7 +134,7 @@ void PluginManager::copyPlugins()
emit error( emit error(
tr("Failed to copy plugin '%1' to: %2\n%3\n" tr("Failed to copy plugin '%1' to: %2\n%3\n"
"Please stop synergy and run the wizard again.") "Please stop synergy and run the wizard again.")
.arg(m_FileSysPluginList.at(i)) .arg(m_PluginList.at(i))
.arg(newName) .arg(newName)
.arg(file.errorString())); .arg(file.errorString()));
return; return;
@ -144,9 +142,9 @@ void PluginManager::copyPlugins()
else { else {
emit info( emit info(
tr("Copying '%1' plugin (%2/%3)...") tr("Copying '%1' plugin (%2/%3)...")
.arg(m_FileSysPluginList.at(i)) .arg(m_PluginList.at(i))
.arg(i+1) .arg(i+1)
.arg(m_FileSysPluginList.size())); .arg(m_PluginList.size()));
} }
} }
} }
@ -160,3 +158,30 @@ void PluginManager::copyPlugins()
emit copyFinished(); emit copyFinished();
return; return;
} }
void PluginManager::queryPluginList()
{
try {
setDone(false);
QString extension = "*" + Plugin::getOsSpecificExt();
QStringList nameFilter(extension);
QString installDir(m_CoreInterface.getInstalledDir()
.append(QDir::separator())
.append(Plugin::getOsSpecificInstallerLocation()));
QString searchDirectory(installDir);
QDir directory(searchDirectory);
m_PluginList = directory.entryList(nameFilter);
setDone(true);
}
catch (std::exception& e)
{
setDone(true);
emit error(tr( "An error occurred while trying to load the "
"plugin list. Please contact the help desk, and "
"provide the following details.\n\n%1").arg(e.what()));
}
emit queryPluginDone();
return;
}

View File

@ -35,15 +35,18 @@ public:
PluginManager(); PluginManager();
~PluginManager(); ~PluginManager();
void initFromWeb(QStringList pluginList); void init();
void initFromFileSys(QStringList pluginList);
int pluginCount() { return m_FileSysPluginList.count(); } int pluginCount() { return m_PluginList.count(); }
QStringList& getPluginList() { return m_PluginList; }
bool isDone() { return done; }
void setDone(bool b) { done = b; }
static bool exist(QString name); static bool exist(QString name);
public slots: public slots:
void copyPlugins(); void copyPlugins();
void queryPluginList();
private: private:
QString getPluginUrl(const QString& pluginName); QString getPluginUrl(const QString& pluginName);
@ -59,14 +62,16 @@ signals:
void info(QString i); void info(QString i);
void updateCopyStatus(int); void updateCopyStatus(int);
void copyFinished(); void copyFinished();
void queryPluginDone();
private: private:
QStringList m_FileSysPluginList; QStringList m_PluginList;
QString m_PluginDir; QString m_PluginDir;
QString m_ProfileDir; QString m_ProfileDir;
QString m_InstalledDir; QString m_InstalledDir;
CoreInterface m_CoreInterface; CoreInterface m_CoreInterface;
SslCertificate m_SslCertificate; SslCertificate m_SslCertificate;
bool done;
}; };
#endif // PLUGINMANAGER_H #endif // PLUGINMANAGER_H

View File

@ -64,15 +64,57 @@ void PluginWizardPage::changeEvent(QEvent *e)
} }
} }
void PluginWizardPage::showError(QString error) void PluginWizardPage::initializePage()
{ {
updateStatus(tr("Error: %1").arg(error)); QWizardPage::initializePage();
if (m_Email.isEmpty() ||
m_Password.isEmpty()) {
updateStatus(tr("Setup complete."));
showFinished(); showFinished();
return;
}
m_pLabelSpinning->show();
QThread* thread = new QThread;
connect(&m_PluginManager,
SIGNAL(error(QString)),
this,
SLOT(showError(QString)));
connect(&m_PluginManager,
SIGNAL(info(QString)),
this,
SLOT(updateStatus(QString)));
connect(&m_PluginManager,
SIGNAL(queryPluginDone()),
this,
SLOT(queryPluginDone()));
connect(&m_PluginManager,
SIGNAL(queryPluginDone()),
thread,
SLOT(quit()));
connect(&m_PluginManager,
SIGNAL(error(QString)),
thread,
SLOT(quit()));
connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
m_PluginManager.moveToThread(thread);
thread->start();
QMetaObject::invokeMethod(&m_PluginManager, "queryPluginList", Qt::QueuedConnection);
} }
void PluginWizardPage::queryPluginDone() void PluginWizardPage::queryPluginDone()
{ {
QStringList pluginList = m_pFileSysClient->getPluginList(); QStringList pluginList = m_PluginManager.getPluginList();
if (pluginList.isEmpty()) { if (pluginList.isEmpty()) {
updateStatus(tr("Setup complete.")); updateStatus(tr("Setup complete."));
showFinished(); showFinished();
@ -84,57 +126,10 @@ void PluginWizardPage::queryPluginDone()
} }
} }
void PluginWizardPage::finished()
{
// TODO: we should check if ns plugin exists
m_mainWindow.appConfig().setCryptoEnabled(true);
updateStatus(tr("Plugins installed successfully."));
showFinished();
}
void PluginWizardPage::generateCertificate()
{
connect(m_pSslCertificate,
SIGNAL(generateFinished()),
this,
SLOT(finished()));
connect(m_pSslCertificate,
SIGNAL(generateFinished()),
m_pThread,
SLOT(quit()));
updateStatus(tr("Generating SSL certificate..."));
QMetaObject::invokeMethod(
m_pSslCertificate,
"generateCertificate",
Qt::QueuedConnection);
}
void PluginWizardPage::updateStatus(QString info)
{
m_pLabelStatus->setText(info);
}
void PluginWizardPage::copyPlugins() void PluginWizardPage::copyPlugins()
{ {
QStringList pluginList = m_pFileSysClient->getPluginList();
m_PluginManager.initFromFileSys(pluginList);
m_pThread = new QThread; m_pThread = new QThread;
connect(&m_PluginManager,
SIGNAL(error(QString)),
this,
SLOT(showError(QString)));
connect(&m_PluginManager,
SIGNAL(info(QString)),
this,
SLOT(updateStatus(QString)));
connect(&m_PluginManager, connect(&m_PluginManager,
SIGNAL(copyFinished()), SIGNAL(copyFinished()),
this, this,
@ -162,6 +157,47 @@ void PluginWizardPage::copyPlugins()
Qt::QueuedConnection); Qt::QueuedConnection);
} }
void PluginWizardPage::generateCertificate()
{
connect(m_pSslCertificate,
SIGNAL(generateFinished()),
this,
SLOT(finished()));
connect(m_pSslCertificate,
SIGNAL(generateFinished()),
m_pThread,
SLOT(quit()));
updateStatus(tr("Generating SSL certificate..."));
QMetaObject::invokeMethod(
m_pSslCertificate,
"generateCertificate",
Qt::QueuedConnection);
}
void PluginWizardPage::showError(QString error)
{
updateStatus(tr("Error: %1").arg(error));
showFinished();
}
void PluginWizardPage::updateStatus(QString info)
{
m_pLabelStatus->setText(info);
}
void PluginWizardPage::finished()
{
// TODO: we should check if ns plugin exists
m_mainWindow.appConfig().setCryptoEnabled(true);
updateStatus(tr("Plugins installed successfully."));
showFinished();
}
void PluginWizardPage::showFinished() void PluginWizardPage::showFinished()
{ {
m_pLabelSpinning->hide(); m_pLabelSpinning->hide();
@ -173,50 +209,3 @@ bool PluginWizardPage::isComplete() const
{ {
return m_Finished; return m_Finished;
} }
void PluginWizardPage::initializePage()
{
QWizardPage::initializePage();
if (m_Email.isEmpty() ||
m_Password.isEmpty()) {
updateStatus(tr("Setup complete."));
showFinished();
return;
}
if (m_pFileSysClient == NULL) {
m_pFileSysClient = new FileSysClient();
m_pLabelSpinning->show();
QThread* thread = new QThread;
connect(m_pFileSysClient,
SIGNAL(error(QString)),
this,
SLOT(showError(QString)));
connect(m_pFileSysClient,
SIGNAL(queryPluginDone()),
this,
SLOT(queryPluginDone()));
connect(m_pFileSysClient,
SIGNAL(queryPluginDone()),
thread,
SLOT(quit()));
connect(m_pFileSysClient,
SIGNAL(error(QString)),
thread,
SLOT(quit()));
connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
m_pFileSysClient->moveToThread(thread);
thread->start();
QMetaObject::invokeMethod(m_pFileSysClient, "queryPluginList", Qt::QueuedConnection);
}
}

View File

@ -50,8 +50,8 @@ protected slots:
void showError(QString error); void showError(QString error);
void updateStatus(QString info); void updateStatus(QString info);
void queryPluginDone(); void queryPluginDone();
void finished();
void generateCertificate(); void generateCertificate();
void finished();
private: private:
void copyPlugins(); void copyPlugins();