Welcome to mirror list, hosted at ThFree Co, Russian Federation.

osm_auth_dialog.cpp « qt - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 69e0e14c7be33353aeaf2b9057ef6f504d11af48 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include "qt/osm_auth_dialog.hpp"

#include "editor/osm_auth.hpp"

#include "platform/settings.hpp"

#include <QtWidgets/QDialogButtonBox>
#include <QtWidgets/QHBoxLayout>
#include <QtWidgets/QLabel>
#include <QtWidgets/QLineEdit>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QVBoxLayout>

using namespace osm;

namespace qt
{

char const * kTokenKeySetting = "OsmTokenKey";
char const * kTokenSecretSetting = "OsmTokenSecret";
char const * kLoginDialogTitle = "OpenStreetMap Login Dialog";
char const * kLogoutDialogTitle = "Logout Dialog";

OsmAuthDialog::OsmAuthDialog(QWidget * parent)
{
  string key, secret;
  bool const isLoginDialog = !settings::Get(kTokenKeySetting, key) || key.empty() ||
                             !settings::Get(kTokenSecretSetting, secret) || secret.empty();

  QVBoxLayout * vLayout = new QVBoxLayout(parent);

  QHBoxLayout * loginRow = new QHBoxLayout();
  loginRow->addWidget(new QLabel(tr("UserName or EMail:")));
  QLineEdit * loginLineEdit = new QLineEdit();
  loginLineEdit->setObjectName("login");
  if (!isLoginDialog)
    loginLineEdit->setEnabled(false);
  loginRow->addWidget(loginLineEdit);
  vLayout->addLayout(loginRow);

  QHBoxLayout * passwordRow = new QHBoxLayout();
  passwordRow->addWidget(new QLabel(tr("Password:")));
  QLineEdit * passwordLineEdit = new QLineEdit();
  passwordLineEdit->setEchoMode(QLineEdit::Password);
  passwordLineEdit->setObjectName("password");
  if (!isLoginDialog)
    passwordLineEdit->setEnabled(false);
  passwordRow->addWidget(passwordLineEdit);
  vLayout->addLayout(passwordRow);

  QPushButton * actionButton = new QPushButton(isLoginDialog ? tr("Login") : tr("Logout"));
  actionButton->setObjectName("button");
  connect(actionButton, SIGNAL(clicked()), this, SLOT(OnAction()));

  QPushButton * closeButton = new QPushButton(tr("Close"));
  connect(closeButton, SIGNAL(clicked()), this, SLOT(close()));

  QHBoxLayout * buttonsLayout = new QHBoxLayout();
  buttonsLayout->addWidget(closeButton);
  buttonsLayout->addWidget(actionButton);
  vLayout->addLayout(buttonsLayout);

  setLayout(vLayout);
  setWindowTitle(isLoginDialog ? tr(kLoginDialogTitle) : tr(kLogoutDialogTitle));
}

void SwitchToLogin(QDialog * dlg)
{
  dlg->findChild<QLineEdit *>("login")->setEnabled(true);
  dlg->findChild<QLineEdit *>("password")->setEnabled(true);
  dlg->findChild<QPushButton *>("button")->setText(dlg->tr("Login"));
  dlg->setWindowTitle(dlg->tr(kLoginDialogTitle));
}

void SwitchToLogout(QDialog * dlg)
{
  dlg->findChild<QLineEdit *>("login")->setEnabled(false);
  dlg->findChild<QLineEdit *>("password")->setEnabled(false);
  dlg->findChild<QPushButton *>("button")->setText(dlg->tr("Logout"));
  dlg->setWindowTitle(dlg->tr(kLoginDialogTitle));
}

void OsmAuthDialog::OnAction()
{
  bool const isLoginDialog = findChild<QPushButton *>("button")->text() == tr("Login");
  if (isLoginDialog)
  {
    string const login = findChild<QLineEdit *>("login")->text().toStdString();
    string const password = findChild<QLineEdit *>("password")->text().toStdString();

    if (login.empty())
    {
      setWindowTitle("Please enter Login");
      return;
    }

    if (password.empty())
    {
      setWindowTitle("Please enter Password");
      return;
    }

    OsmOAuth auth = osm::OsmOAuth::ServerAuth();
    try
    {
      if (auth.AuthorizePassword(login, password))
      {
        auto const token = auth.GetKeySecret();
        settings::Set(kTokenKeySetting, token.first);
        settings::Set(kTokenSecretSetting, token.second);

        SwitchToLogout(this);
      }
      else
      {
        setWindowTitle("Auth failed: invalid login or password");
        return;
      }
    }
    catch (std::exception const & ex)
    {
      setWindowTitle((string("Auth failed: ") + ex.what()).c_str());
      return;
    }
  }
  else
  {
    settings::Set(kTokenKeySetting, string());
    settings::Set(kTokenSecretSetting, string());

    SwitchToLogin(this);
  }
}

} // namespace qt