added login window #4168

This commit is contained in:
XinyuHou 2015-01-30 16:04:27 +00:00
parent 9835c7206d
commit ccc5834757
14 changed files with 589 additions and 17 deletions

View File

@ -14,7 +14,8 @@ FORMS += res/MainWindowBase.ui \
res/HotkeyDialogBase.ui \
res/SettingsDialogBase.ui \
res/SetupWizardBase.ui \
res/AddClientDialogBase.ui
res/AddClientDialogBase.ui \
res/LoginWindowBase.ui
SOURCES += src/main.cpp \
src/MainWindow.cpp \
src/AboutDialog.cpp \
@ -50,7 +51,9 @@ SOURCES += src/main.cpp \
src/ZeroconfService.cpp \
src/DataDownloader.cpp \
src/AddClientDialog.cpp \
src/CommandProcess.cpp
src/CommandProcess.cpp \
src/LoginWindow.cpp \
src/LoginAuth.cpp
HEADERS += src/MainWindow.h \
src/AboutDialog.h \
src/ServerConfig.h \
@ -86,7 +89,9 @@ HEADERS += src/MainWindow.h \
src/ZeroconfService.h \
src/DataDownloader.h \
src/AddClientDialog.h \
src/CommandProcess.h
src/CommandProcess.h \
src/LoginWindow.h \
src/LoginAuth.h
RESOURCES += res/Synergy.qrc
RC_FILE = res/win/Synergy.rc
macx {

22
src/gui/res/LoginWindow.h Normal file
View File

@ -0,0 +1,22 @@
#ifndef LOGINWINDOW_H
#define LOGINWINDOW_H
#include <QMainWindow>
#include "ui_LoginWindowBase.h"
class LoginWindow : public QMainWindow, public Ui::LoginWindowBase
{
Q_OBJECT
public:
LoginWindow(QWidget *parent = 0);
~LoginWindow();
protected:
void changeEvent(QEvent *e);
private:
Ui::LoginWindow *ui;
};
#endif // LOGINWINDOW_H

View File

@ -0,0 +1,149 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>LoginWindow</class>
<widget class="QMainWindow" name="LoginWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>200</height>
</rect>
</property>
<property name="windowTitle">
<string>Synergy</string>
</property>
<widget class="QWidget" name="centralwidget">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QVBoxLayout" name="verticalLayout_2">
<property name="leftMargin">
<number>10</number>
</property>
<item>
<widget class="QLabel" name="label">
<property name="text">
<string/>
</property>
<property name="pixmap">
<pixmap resource="Synergy.qrc">:/res/image/about.png</pixmap>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QFormLayout" name="formLayout">
<property name="leftMargin">
<number>20</number>
</property>
<property name="rightMargin">
<number>20</number>
</property>
<item row="0" column="0">
<widget class="QLabel" name="m_pLabelEmail">
<property name="text">
<string>Email:</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLineEdit" name="m_pLineEditEmail">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="m_pLabelPassword">
<property name="text">
<string>Password:</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLineEdit" name="m_pLineEditPassword">
<property name="inputMethodHints">
<set>Qt::ImhHiddenText</set>
</property>
<property name="echoMode">
<enum>QLineEdit::Password</enum>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<property name="leftMargin">
<number>20</number>
</property>
<property name="rightMargin">
<number>20</number>
</property>
<item>
<widget class="QLabel" name="m_pLabelRegister">
<property name="text">
<string>&lt;a href=&quot;http://synergy-project.org/&quot;&gt;Register&lt;/a&gt;</string>
</property>
<property name="openExternalLinks">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="m_pPushButtonLogin">
<property name="sizePolicy">
<sizepolicy hsizetype="Maximum" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Login</string>
</property>
<property name="shortcut">
<string>Return</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="m_pPushButtonCancel">
<property name="sizePolicy">
<sizepolicy hsizetype="Maximum" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Cancel</string>
</property>
<property name="shortcut">
<string>Esc</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>21</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources>
<include location="Synergy.qrc"/>
</resources>
<connections/>
</ui>

102
src/gui/src/LoginAuth.cpp Normal file
View File

@ -0,0 +1,102 @@
#include "LoginAuth.h"
#include "LoginWindow.h"
#include <QProcess>
#include <QCoreApplication>
#include <QCryptographicHash>
#include <stdexcept>
void LoginAuth::checkUserType()
{
int result = doCheckUserType();
m_pLoginWindow->setLoginResult(result);
emit finished();
}
int LoginAuth::doCheckUserType()
{
QString responseJson;
try
{
responseJson = request(m_Email, m_Password);
}
catch (std::exception& e)
{
m_pLoginWindow->setError(e.what());
return ExceptionError;
}
QRegExp resultRegex(".*\"result\".*:.*(true|false).*");
if (resultRegex.exactMatch(responseJson)) {
QString boolString = resultRegex.cap(1);
if (boolString == "true") {
return Home;
}
else if (boolString == "false") {
return InvalidEmailPassword;
}
}
else {
QRegExp errorRegex(".*\"error\".*:.*\"(.+)\".*");
if (errorRegex.exactMatch(responseJson)) {
// replace "\n" with real new lines.
QString error = errorRegex.cap(1).replace("\\n", "\n");
m_pLoginWindow->setError(error);
return Error;
}
}
m_pLoginWindow->setError(responseJson);
return ServerResponseError;
}
QString LoginAuth::request(const QString& email, const QString& password)
{
QString program(QCoreApplication::applicationDirPath() + "/syntool");
QStringList args("--login-auth");
QProcess process;
process.setReadChannel(QProcess::StandardOutput);
process.start(program, args);
bool success = process.waitForStarted();
QString out, error;
if (success)
{
// hash password in case it contains interesting chars.
QString credentials(email + ":" + hash(password) + "\n");
process.write(credentials.toStdString().c_str());
if (process.waitForFinished()) {
out = process.readAllStandardOutput();
error = process.readAllStandardError();
}
}
out = out.trimmed();
error = error.trimmed();
if (out.isEmpty() ||
!error.isEmpty() ||
!success ||
process.exitCode() != 0)
{
throw std::runtime_error(
QString("Code: %1\nError: %2")
.arg(process.exitCode())
.arg(error.isEmpty() ? "Unknown" : error)
.toStdString());
}
return out;
}
QString LoginAuth::hash(const QString& string)
{
QByteArray data = string.toUtf8();
QByteArray hash = QCryptographicHash::hash(data, QCryptographicHash::Md5);
return hash.toHex();
}

47
src/gui/src/LoginAuth.h Normal file
View File

@ -0,0 +1,47 @@
#ifndef LOGINAUTH_H
#define LOGINAUTH_H
#include <QString>
#include <QObject>
class LoginWindow;
enum qUserType {
Student,
Home,
Professional,
Error,
ExceptionError,
InvalidEmailPassword,
ServerResponseError,
Unknown
};
class LoginAuth : public QObject
{
Q_OBJECT
public:
int doCheckUserType();
void setEmail(QString email) { m_Email = email; }
void setPassword(QString password) { m_Password = password; }
void setLoginWindow(LoginWindow* w) { m_pLoginWindow = w; }
public slots:
void checkUserType();
signals:
void finished();
private:
QString request(const QString& email, const QString& password);
QString hash(const QString& string);
private:
QString m_Email;
QString m_Password;
LoginWindow* m_pLoginWindow;
};
#endif // LOGINAUTH_H

144
src/gui/src/LoginWindow.cpp Normal file
View File

@ -0,0 +1,144 @@
#include "LoginWindow.h"
#include "ui_LoginWindowBase.h"
#include "MainWindow.h"
#include "SetupWizard.h"
#include "LoginAuth.h"
#include <QMessageBox>
#include <QCloseEvent>
#include <QThread>
LoginWindow::LoginWindow(
MainWindow* mainWindow,
SetupWizard* setupWizard,
bool wizardShouldRun,
QWidget *parent) :
QMainWindow(parent),
m_pMainWindow(mainWindow),
m_pSetupWizard(setupWizard),
m_WizardShouldRun(wizardShouldRun),
m_pLoginAuth(NULL),
m_LoginResult(Unknown)
{
setupUi(this);
}
LoginWindow::~LoginWindow()
{
if (m_pLoginAuth != NULL) {
delete m_pLoginAuth;
}
}
void LoginWindow::showNext()
{
if (m_LoginResult == ExceptionError) {
QMessageBox::critical(
this,
tr("Error"),
tr("Sorry, an error occured while trying to sign in. "
"Please contact the help desk, and provide the "
"following details.\n\n%1").arg(m_Error));
}
else if (m_LoginResult == InvalidEmailPassword) {
QMessageBox::critical(
this,
tr("Error"),
tr("Login failed, invalid email or password."));
}
else if (m_LoginResult == Error) {
QMessageBox::critical(
this,
tr("Error"),
tr("Login failed, an error occurred.\n\n%1").arg(m_Error));
}
else if (m_LoginResult == ServerResponseError) {
QMessageBox::critical(
this,
"Error",
tr("Login failed, an error occurred.\n\nServer response:\n\n%1")
.arg(m_Error));
}
else {
hide();
if (m_WizardShouldRun) {
m_pSetupWizard->show();
}
else {
m_pMainWindow->setLoginResult(m_LoginResult);
m_pMainWindow->show();
}
}
delete m_pLoginAuth;
m_pLoginAuth = NULL;
m_LoginResult = Unknown;
m_pPushButtonLogin->setText("Login");
m_pPushButtonLogin->setDefault(true);
}
bool LoginWindow::validEmailPassword()
{
if (m_pLineEditEmail->text().isEmpty() ||
m_pLineEditPassword->text().isEmpty()) {
QMessageBox::warning(
this,
"Warning",
tr("Please fill in your email and password."));
return false;
}
return true;
}
void LoginWindow::changeEvent(QEvent *e)
{
QMainWindow::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
retranslateUi(this);
break;
default:
break;
}
}
void LoginWindow::closeEvent(QCloseEvent *event)
{
event->accept();
showNext();
}
void LoginWindow::on_m_pPushButtonLogin_clicked()
{
if (validEmailPassword()) {
if (m_pLoginAuth == NULL) {
m_pLoginAuth = new LoginAuth();
m_pLoginAuth->setLoginWindow(this);
}
m_pPushButtonLogin->setText("Logging...");
QString email = m_pLineEditEmail->text();
QString password = m_pLineEditPassword->text();
m_pLoginAuth->setEmail(email);
m_pLoginAuth->setPassword(password);
QThread* thread = new QThread;
connect(m_pLoginAuth, SIGNAL(finished()), this, SLOT(showNext()));
connect(m_pLoginAuth, SIGNAL(finished()), thread, SLOT(quit()));
connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
m_pLoginAuth->moveToThread(thread);
thread->start();
QMetaObject::invokeMethod(m_pLoginAuth, "checkUserType", Qt::QueuedConnection);
}
}
void LoginWindow::on_m_pPushButtonCancel_clicked()
{
showNext();
}

46
src/gui/src/LoginWindow.h Normal file
View File

@ -0,0 +1,46 @@
#ifndef LOGINWINDOW_H
#define LOGINWINDOW_H
#include <QMainWindow>
#include "ui_LoginWindowBase.h"
class MainWindow;
class SetupWizard;
class LoginAuth;
class LoginWindow : public QMainWindow, public Ui::LoginWindow
{
Q_OBJECT
public:
LoginWindow(MainWindow* mainWindow,
SetupWizard* setupWizard,
bool wizardShouldRun,
QWidget *parent = 0);
~LoginWindow();
void setLoginResult(int result) { m_LoginResult = result; }
void setError(QString error) { m_Error = error; }
protected:
void changeEvent(QEvent *e);
void closeEvent(QCloseEvent *event);
private slots:
void on_m_pPushButtonCancel_clicked();
void on_m_pPushButtonLogin_clicked();
void showNext();
private:
bool validEmailPassword();
private:
MainWindow* m_pMainWindow;
SetupWizard* m_pSetupWizard;
bool m_WizardShouldRun;
LoginAuth* m_pLoginAuth;
int m_LoginResult;
QString m_Error;
};
#endif // LOGINWINDOW_H

View File

@ -28,6 +28,7 @@
#include "ZeroconfService.h"
#include "DataDownloader.h"
#include "CommandProcess.h"
#include "LoginAuth.h"
#include <QtCore>
#include <QtGui>
@ -89,7 +90,8 @@ MainWindow::MainWindow(QSettings& settings, AppConfig& appConfig) :
m_pCancelButton(NULL),
m_SuppressAutoConfigWarning(false),
m_BonjourInstall(NULL),
m_SuppressEmptyServerWarning(false)
m_SuppressEmptyServerWarning(false),
m_LoginResult(Unknown)
{
setupUi(this);
@ -566,7 +568,10 @@ QString MainWindow::configFilename()
m_pTempConfigFile = new QTemporaryFile();
if (!m_pTempConfigFile->open())
{
QMessageBox::critical(this, tr("Cannot write configuration file"), tr("The temporary configuration file required to start synergy can not be written."));
QMessageBox::critical(
this, tr("Cannot write configuration file"),
tr("The temporary configuration file required to start synergy can not be written."));
return "";
}
@ -872,6 +877,26 @@ int MainWindow::checkWinArch()
return unknown;
}
void MainWindow::setLoginResult(int result)
{
m_LoginResult = result;
QString title;
if (result == Student) {
title = "Synergy Student";
}
else if (result == Home) {
title = "Synergy Home";
}
else if (result == Professional) {
title = "Synergy Pro";
}
else {
title = "Synergy (UNREGISTERED)";
}
setWindowTitle(title);
}
void MainWindow::on_m_pGroupClient_toggled(bool on)
{
m_pGroupServer->setChecked(!on);

View File

@ -112,6 +112,7 @@ class MainWindow : public QMainWindow, public Ui::MainWindowBase
void updateZeroconfService();
void serverDetected(const QString name);
int checkWinArch();
void setLoginResult(int result);
public slots:
void appendLogRaw(const QString& text);
@ -192,6 +193,7 @@ class MainWindow : public QMainWindow, public Ui::MainWindowBase
bool m_SuppressAutoConfigWarning;
CommandProcess* m_BonjourInstall;
bool m_SuppressEmptyServerWarning;
int m_LoginResult;
private slots:
void on_m_pCheckBoxAutoConfig_toggled(bool checked);

View File

@ -23,6 +23,7 @@
#include "MainWindow.h"
#include "AppConfig.h"
#include "SetupWizard.h"
#include "LoginWindow.h"
#include <QtCore>
#include <QtGui>
@ -93,14 +94,12 @@ int main(int argc, char* argv[])
MainWindow mainWindow(settings, appConfig);
SetupWizard setupWizard(mainWindow, true);
if (appConfig.wizardShouldRun())
{
setupWizard.show();
}
else
{
mainWindow.open();
}
LoginWindow loginWindow(
&mainWindow,
&setupWizard,
appConfig.wizardShouldRun());
loginWindow.show();
return app.exec();
}

View File

@ -160,17 +160,20 @@ ArgParser::parsePlatformArg(ArgsBase& argsBase, const int& argc, const char* con
bool
ArgParser::parseToolArgs(ToolArgs& args, int argc, const char* const* argv)
{
bool result = false;
for (int i = 1; i < argc; ++i) {
if (isArg(i, argc, argv, NULL, "--get-active-desktop", 0)) {
args.m_printActiveDesktopName = true;
return true;
result = true;
}
else {
return false;
if (isArg(i, argc, argv, NULL, "--login-auth", 0)) {
args.m_loginAuthenticate = true;
result = true;
}
}
return false;
return result;
}
bool

View File

@ -29,6 +29,8 @@
#include "platform/MSWindowsSession.h"
#endif
#define PREMIUM_AUTH_URL "https://synergy-project.org/premium/json/auth/"
enum {
kErrorOk,
kErrorArgs,
@ -65,6 +67,9 @@ ToolApp::run(int argc, char** argv)
}
#endif
}
else if (m_args.m_loginAuthenticate) {
loginAuth();
}
else {
throw XSynergy("Nothing to do");
}
@ -85,3 +90,21 @@ void
ToolApp::help()
{
}
void
ToolApp::loginAuth()
{
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 << PREMIUM_AUTH_URL;
ss << "?email=" << ARCH->internet().urlEncode(email);
ss << "&password=" << password;
std::cout << ARCH->internet().get(ss.str()) << std::endl;
}

View File

@ -26,6 +26,10 @@ class ToolApp : public MinimalApp
public:
UInt32 run(int argc, char** argv);
void help();
private:
void loginAuth();
private:
ToolArgs m_args;
};

View File

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