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

View File

@ -1,11 +1,15 @@
#include "PluginWizardPage.h" #include "PluginWizardPage.h"
#include "ui_PluginWizardPageBase.h" #include "ui_PluginWizardPageBase.h"
#include "WebClient.h"
#include <QMovie> #include <QMovie>
#include <QThread>
PluginWizardPage::PluginWizardPage(QWidget *parent) : PluginWizardPage::PluginWizardPage(QWidget *parent) :
QWizardPage(parent), QWizardPage(parent),
m_Finished(false) m_Finished(false),
m_pWebClient(NULL)
{ {
setupUi(this); setupUi(this);
@ -16,6 +20,9 @@ PluginWizardPage::PluginWizardPage(QWidget *parent) :
PluginWizardPage::~PluginWizardPage() PluginWizardPage::~PluginWizardPage()
{ {
if (m_pWebClient != NULL) {
delete m_pWebClient;
}
} }
void PluginWizardPage::changeEvent(QEvent *e) 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 bool PluginWizardPage::isComplete() const
{ {
return m_Finished; return m_Finished;
@ -38,6 +65,34 @@ bool PluginWizardPage::isComplete() const
void PluginWizardPage::initializePage() void PluginWizardPage::initializePage()
{ {
QWizardPage::initializePage(); QWizardPage::initializePage();
m_Finished = true; if (m_pWebClient == NULL) {
emit completeChanged(); 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 "ui_PluginWizardPageBase.h"
#include <QWizardPage> #include <QWizardPage>
class WebClient;
class PluginWizardPage : public QWizardPage, public Ui::PluginWizardPage { class PluginWizardPage : public QWizardPage, public Ui::PluginWizardPage {
Q_OBJECT Q_OBJECT
@ -13,6 +15,8 @@ public:
~PluginWizardPage(); ~PluginWizardPage();
void setFinished(bool b) { m_Finished = b; } void setFinished(bool b) { m_Finished = b; }
void setEmail(QString e) { m_Email = e; }
void setPassword(QString p) { m_Password = p; }
bool isComplete() const; bool isComplete() const;
void initializePage(); void initializePage();
@ -20,7 +24,16 @@ public:
protected: protected:
void changeEvent(QEvent *e); void changeEvent(QEvent *e);
protected slots:
void queryPluginDone();
private:
void updateStatus(QString info);
private: private:
bool m_Finished; bool m_Finished;
WebClient* m_pWebClient;
QString m_Email;
QString m_Password;
}; };
#endif // PLUGINWIZARDPAGE_H #endif // PLUGINWIZARDPAGE_H

View File

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

View File

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

View File

@ -34,14 +34,15 @@ int WebClient::getEdition(
QString responseJson; QString responseJson;
int edition = Unknown; int edition = Unknown;
try { try {
responseJson = request(email, password); QStringList args("--login-auth");
responseJson = request(email, password, args);
} }
catch (std::exception& e) catch (std::exception& e)
{ {
message.critical( message.critical(
w, "Error", w, "Error",
tr("Sorry, an error occured while trying to sign in. " tr("An error occured while trying to sign in. "
"Please contact the help desk, and provide the " "Please contact the helpdesk, and provide the "
"following details.\n\n%1").arg(e.what())); "following details.\n\n%1").arg(e.what()));
return edition; return edition;
} }
@ -50,7 +51,7 @@ int WebClient::getEdition(
if (resultRegex.exactMatch(responseJson)) { if (resultRegex.exactMatch(responseJson)) {
QString boolString = resultRegex.cap(1); QString boolString = resultRegex.cap(1);
if (boolString == "true") { if (boolString == "true") {
QRegExp editionRegex(".*\"edition\".*:.*\"(.+)\",.*"); QRegExp editionRegex(".*\"edition\".*:.*\"([^\"]+)\".*");
if (editionRegex.exactMatch(responseJson)) { if (editionRegex.exactMatch(responseJson)) {
QString e = editionRegex.cap(1); QString e = editionRegex.cap(1);
edition = e.toInt(); edition = e.toInt();
@ -67,7 +68,7 @@ int WebClient::getEdition(
} }
} }
else { else {
QRegExp errorRegex(".*\"error\".*:.*\"(.+)\".*"); QRegExp errorRegex(".*\"error\".*:.*\"([^\"]+)\".*");
if (errorRegex.exactMatch(responseJson)) { if (errorRegex.exactMatch(responseJson)) {
// replace "\n" with real new lines. // replace "\n" with real new lines.
@ -88,10 +89,67 @@ int WebClient::getEdition(
return edition; 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"); QString program(QCoreApplication::applicationDirPath() + "/syntool");
QStringList args("--login-auth");
QProcess process; QProcess process;
process.setReadChannel(QProcess::StandardOutput); process.setReadChannel(QProcess::StandardOutput);

View File

@ -19,10 +19,12 @@
#define WEBCLIENT_H #define WEBCLIENT_H
#include <QString> #include <QString>
#include <QStringList>
#include <QObject> #include <QObject>
class QMessageBox; class QMessageBox;
class QWidget; class QWidget;
class QStringList;
class WebClient : public QObject class WebClient : public QObject
{ {
@ -33,9 +35,27 @@ public:
const QString& password, const QString& password,
QMessageBox& message, QMessageBox& message,
QWidget* w); 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: 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 #endif // WEBCLIENT_H

View File

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

View File

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

View File

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

View File

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

View File

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