queried plugin list from server #4168

This commit is contained in:
XinyuHou 2015-02-06 10:39:58 +00:00
parent 82b932b1c4
commit 05e6cb6254
12 changed files with 196 additions and 20 deletions

View File

@ -48,7 +48,7 @@
<item row="0" column="1">
<widget class="QLabel" name="m_pLabelSpinning">
<property name="text">
<string>TextLabel</string>
<string> </string>
</property>
</widget>
</item>
@ -96,9 +96,9 @@
</spacer>
</item>
<item row="0" column="1">
<widget class="QLabel" name="m_pLabelTip">
<widget class="QLabel" name="m_pLabelStatus">
<property name="text">
<string>Finalizing, please wait.</string>
<string>Please wait...</string>
</property>
</widget>
</item>

View File

@ -1,11 +1,15 @@
#include "PluginWizardPage.h"
#include "ui_PluginWizardPageBase.h"
#include "WebClient.h"
#include <QMovie>
#include <QThread>
PluginWizardPage::PluginWizardPage(QWidget *parent) :
QWizardPage(parent),
m_Finished(false)
m_Finished(false),
m_pWebClient(NULL)
{
setupUi(this);
@ -16,6 +20,9 @@ PluginWizardPage::PluginWizardPage(QWidget *parent) :
PluginWizardPage::~PluginWizardPage()
{
if (m_pWebClient != NULL) {
delete m_pWebClient;
}
}
void PluginWizardPage::changeEvent(QEvent *e)
@ -30,6 +37,26 @@ void PluginWizardPage::changeEvent(QEvent *e)
}
}
void PluginWizardPage::queryPluginDone()
{
QStringList plguinList = m_pWebClient->getPluginList();
if (plguinList.isEmpty()) {
if (!m_pWebClient->getLastError().isEmpty()) {
updateStatus(m_pWebClient->getLastError());
m_Finished = true;
emit completeChanged();
}
}
else {
updateStatus(plguinList.at(0));
}
}
void PluginWizardPage::updateStatus(QString info)
{
m_pLabelStatus->setText(info);
}
bool PluginWizardPage::isComplete() const
{
return m_Finished;
@ -38,6 +65,34 @@ bool PluginWizardPage::isComplete() const
void PluginWizardPage::initializePage()
{
QWizardPage::initializePage();
if (m_pWebClient == NULL) {
if (m_Email.isEmpty() ||
m_Password.isEmpty()) {
updateStatus("No plugin available.");
//TODO: stop spinning icon
m_Finished = true;
emit completeChanged();
return;
}
m_pWebClient = new WebClient();
m_pWebClient->setEmail(m_Email);
m_pWebClient->setPassword(m_Password);
QThread* thread = new QThread;
connect(m_pWebClient,
SIGNAL(queryPluginDone()),
this,
SLOT(queryPluginDone()));
connect(m_pWebClient, SIGNAL(queryPluginDone()), thread, SLOT(quit()));
connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
m_pWebClient->moveToThread(thread);
thread->start();
updateStatus("Querying plugin list...");
QMetaObject::invokeMethod(m_pWebClient, "queryPluginList", Qt::QueuedConnection);
}
}

View File

@ -4,6 +4,8 @@
#include "ui_PluginWizardPageBase.h"
#include <QWizardPage>
class WebClient;
class PluginWizardPage : public QWizardPage, public Ui::PluginWizardPage {
Q_OBJECT
@ -13,6 +15,8 @@ public:
~PluginWizardPage();
void setFinished(bool b) { m_Finished = b; }
void setEmail(QString e) { m_Email = e; }
void setPassword(QString p) { m_Password = p; }
bool isComplete() const;
void initializePage();
@ -20,7 +24,16 @@ public:
protected:
void changeEvent(QEvent *e);
protected slots:
void queryPluginDone();
private:
void updateStatus(QString info);
private:
bool m_Finished;
WebClient* m_pWebClient;
QString m_Email;
QString m_Password;
};
#endif // PLUGINWIZARDPAGE_H

View File

@ -21,7 +21,6 @@
#include "EditionType.h"
#include "QSynergyApplication.h"
#include "QUtility.h"
#include "PluginWizardPage.h"
#include <QMessageBox>
@ -94,6 +93,8 @@ bool SetupWizard::validateCurrentPage()
return false;
}
else {
m_pPluginPage->setEmail(m_pLineEditEmail->text());
m_pPluginPage->setPassword(m_pLineEditPassword->text());
return true;
}
}

View File

@ -19,12 +19,12 @@
#include "ui_SetupWizardBase.h"
#include "SynergyLocale.h"
#include "PluginWizardPage.h"
#include <QWizard>
#include <QNetworkAccessManager>
class MainWindow;
class QMessageBox;
class SetupWizard : public QWizard, public Ui::SetupWizardBase
{
@ -44,7 +44,7 @@ private:
bool m_StartMain;
SynergyLocale m_Locale;
int m_Edition;
QWizardPage* m_pPluginPage;
PluginWizardPage* m_pPluginPage;
private slots:
void on_m_pRadioButtonActivate_toggled(bool checked);

View File

@ -34,13 +34,14 @@ int WebClient::getEdition(
QString responseJson;
int edition = Unknown;
try {
responseJson = request(email, password);
QStringList args("--login-auth");
responseJson = request(email, password, args);
}
catch (std::exception& e)
{
message.critical(
w, "Error",
tr("Sorry, an error occured while trying to sign in. "
tr("An error occured while trying to sign in. "
"Please contact the helpdesk, and provide the "
"following details.\n\n%1").arg(e.what()));
return edition;
@ -50,7 +51,7 @@ int WebClient::getEdition(
if (resultRegex.exactMatch(responseJson)) {
QString boolString = resultRegex.cap(1);
if (boolString == "true") {
QRegExp editionRegex(".*\"edition\".*:.*\"(.+)\",.*");
QRegExp editionRegex(".*\"edition\".*:.*\"([^\"]+)\".*");
if (editionRegex.exactMatch(responseJson)) {
QString e = editionRegex.cap(1);
edition = e.toInt();
@ -67,7 +68,7 @@ int WebClient::getEdition(
}
}
else {
QRegExp errorRegex(".*\"error\".*:.*\"(.+)\".*");
QRegExp errorRegex(".*\"error\".*:.*\"([^\"]+)\".*");
if (errorRegex.exactMatch(responseJson)) {
// replace "\n" with real new lines.
@ -88,10 +89,67 @@ int WebClient::getEdition(
return edition;
}
QString WebClient::request(const QString& email, const QString& password)
void WebClient::queryPluginList()
{
QString responseJson;
try {
QStringList args("--get-plugin-list");
responseJson = request(m_Email, m_Password, args);
}
catch (std::exception& e)
{
m_Error = tr("An error occured while trying to query the "
"plugin list. Please contact the help desk, and provide "
"the following details.\n\n%1").arg(e.what());
emit queryPluginDone();
return;
}
QRegExp resultRegex(".*\"result\".*:.*(true|false).*");
if (resultRegex.exactMatch(responseJson)) {
QString boolString = resultRegex.cap(1);
if (boolString == "true") {
QRegExp editionRegex(".*\"plugins\".*:.*\"([^\"]+)\".*");
if (editionRegex.exactMatch(responseJson)) {
QString e = editionRegex.cap(1);
m_PluginList = e.split(",");
m_Error.clear();
emit queryPluginDone();
return;
}
}
else if (boolString == "false") {
m_Error = tr("Get plugin list failed, invalid user email "
"or password.");
emit queryPluginDone();
return;
}
}
else {
QRegExp errorRegex(".*\"error\".*:.*\"([^\"]+)\".*");
if (errorRegex.exactMatch(responseJson)) {
// replace "\n" with real new lines.
QString error = errorRegex.cap(1).replace("\\n", "\n");
m_Error = tr("Get plugin list failed, an error occurred."
"\n\n%1").arg(error);
emit queryPluginDone();
return;
}
}
m_Error = tr("Get plugin list failed, an error occurred.\n\n"
"Server response:\n\n%1").arg(responseJson);
emit queryPluginDone();
return;
}
QString WebClient::request(
const QString& email,
const QString& password,
QStringList& args)
{
QString program(QCoreApplication::applicationDirPath() + "/syntool");
QStringList args("--login-auth");
QProcess process;
process.setReadChannel(QProcess::StandardOutput);

View File

@ -19,10 +19,12 @@
#define WEBCLIENT_H
#include <QString>
#include <QStringList>
#include <QObject>
class QMessageBox;
class QWidget;
class QStringList;
class WebClient : public QObject
{
@ -33,9 +35,27 @@ public:
const QString& password,
QMessageBox& message,
QWidget* w);
void setEmail(QString& e) { m_Email = e; }
void setPassword(QString& p) { m_Password = p; }
QStringList& getPluginList() { return m_PluginList; }
QString& getLastError() { return m_Error; }
public slots:
void queryPluginList();
signals:
void queryPluginDone();
private:
QString request(const QString& email, const QString& password);
QString request(const QString& email,
const QString& password,
QStringList& args);
private:
QString m_Email;
QString m_Password;
QStringList m_PluginList;
QString m_Error;
};
#endif // WEBCLIENT_H

View File

@ -169,6 +169,10 @@ ArgParser::parseToolArgs(ToolArgs& args, int argc, const char* const* argv)
args.m_loginAuthenticate = true;
return true;
}
if (isArg(i, argc, argv, NULL, "--get-plugin-list", 0)) {
args.m_getPluginList = true;
return true;
}
else {
return false;
}

View File

@ -29,7 +29,7 @@
#include "platform/MSWindowsSession.h"
#endif
#define PREMIUM_AUTH_URL "https://synergy-project.org/premium/json/auth/"
#define JSON_URL "https://synergy-project.org/premium/json/"
enum {
kErrorOk,
@ -70,6 +70,9 @@ ToolApp::run(int argc, char** argv)
else if (m_args.m_loginAuthenticate) {
loginAuth();
}
else if (m_args.m_getPluginList) {
getPluginList();
}
else {
throw XSynergy("Nothing to do");
}
@ -102,7 +105,25 @@ ToolApp::loginAuth()
String password = credentials.substr(separator + 1, credentials.length());
std::stringstream ss;
ss << PREMIUM_AUTH_URL;
ss << JSON_URL << "auth/";
ss << "?email=" << ARCH->internet().urlEncode(email);
ss << "&password=" << password;
std::cout << ARCH->internet().get(ss.str()) << std::endl;
}
void
ToolApp::getPluginList()
{
String credentials;
std::cin >> credentials;
size_t separator = credentials.find(':');
String email = credentials.substr(0, separator);
String password = credentials.substr(separator + 1, credentials.length());
std::stringstream ss;
ss << JSON_URL << "plugins/";
ss << "?email=" << ARCH->internet().urlEncode(email);
ss << "&password=" << password;

View File

@ -29,6 +29,7 @@ public:
private:
void loginAuth();
void getPluginList();
private:
ToolArgs m_args;

View File

@ -18,6 +18,8 @@
#include "synergy/ToolArgs.h"
ToolArgs::ToolArgs() :
m_printActiveDesktopName(false)
m_printActiveDesktopName(false),
m_loginAuthenticate(false),
m_getPluginList(false)
{
}

View File

@ -26,4 +26,5 @@ public:
public:
bool m_printActiveDesktopName;
bool m_loginAuthenticate;
bool m_getPluginList;
};