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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
|
#include "QtLoginWindow.h"
#include "QtAboutWidget.h"
#include "QtSwiftUtil.h"
#include "QtMainWindow.h"
#include <QApplication>
#include <QBoxLayout>
#include <QFileDialog>
#include <QStatusBar>
#include <QToolButton>
#include <QLabel>
#include <QMenuBar>
#include <cassert>
namespace Swift{
QtLoginWindow::QtLoginWindow() : QMainWindow() {
setWindowTitle("Swift");
resize(200, 500);
setContentsMargins(0,0,0,0);
QWidget *centralWidget = new QWidget(this);
setCentralWidget(centralWidget);
QBoxLayout *topLayout = new QBoxLayout(QBoxLayout::TopToBottom, centralWidget);
stack_ = new QStackedWidget(centralWidget);
topLayout->addWidget(stack_);
topLayout->setMargin(0);
QWidget *wrapperWidget = new QWidget(this);
wrapperWidget->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding));
QBoxLayout *layout = new QBoxLayout(QBoxLayout::TopToBottom, wrapperWidget);
layout->addStretch();
QLabel* logo = new QLabel(this);
logo->setPixmap(QPixmap(":/logo-shaded-text.256.png"));
logo->setScaledContents(true);
logo->setFixedSize(192,192);
layout->addWidget(logo);
layout->addStretch();
username_ = new QLineEdit(this);
layout->addWidget(username_);
QWidget* w = new QWidget(this);
w->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding));
layout->addWidget(w);
QHBoxLayout* credentialsLayout = new QHBoxLayout(w);
credentialsLayout->setMargin(0);
credentialsLayout->setSpacing(3);
password_ = new QLineEdit(this);
password_->setEchoMode(QLineEdit::Password);
connect(password_, SIGNAL(returnPressed()), this, SLOT(loginClicked()));
connect(username_, SIGNAL(returnPressed()), password_, SLOT(setFocus()));
connect(username_, SIGNAL(returnPressed()), password_, SLOT(selectAll()));
credentialsLayout->addWidget(password_);
certificateButton_ = new QToolButton(this);
certificateButton_->setCheckable(true);
certificateButton_->setIcon(QIcon(":/icons/certificate.png"));
credentialsLayout->addWidget(certificateButton_);
connect(certificateButton_, SIGNAL(clicked(bool)), SLOT(handleCertficateChecked(bool)));
loginButton_ = new QPushButton(this);
loginButton_->setText(tr("Connect"));
loginButton_->setAutoDefault(true);
loginButton_->setDefault(true);
layout->addWidget(loginButton_);
message_ = new QLabel(this);
message_->setTextFormat(Qt::RichText);
message_->setWordWrap(true);
layout->addWidget(message_);
layout->addStretch();
remember_ = new QCheckBox(tr("Remember Password?"), this);
layout->addWidget(remember_);
connect(loginButton_, SIGNAL(clicked()), SLOT(loginClicked()));
stack_->addWidget(wrapperWidget);
#ifdef SWIFTEN_PLATFORM_MACOSX
menuBar_ = new QMenuBar(NULL);
#else
menuBar_ = menuBar();
#endif
QApplication::setQuitOnLastWindowClosed(false);
swiftMenu_ = new QMenu(tr("Swift"), this);
QAction* aboutAction = new QAction("About Swift", this);
connect(aboutAction, SIGNAL(activated()), SLOT(handleAbout()));
swiftMenu_->addAction(aboutAction);
QAction* quitAction = new QAction("Quit", this);
connect(quitAction, SIGNAL(activated()), SLOT(handleQuit()));
swiftMenu_->addAction(quitAction);
setInitialMenus();
this->show();
}
void QtLoginWindow::addAvailableAccount(const String& defaultJID, const String& defaultPassword, const String& defaultCertificate) {
username_->setText(P2QSTRING(defaultJID));
password_->setText(P2QSTRING(defaultPassword));
certificateFile_ = P2QSTRING(defaultCertificate);
if (!certificateFile_.isEmpty()) {
certificateButton_->setChecked(true);
}
remember_->setChecked(defaultPassword != "");
}
void QtLoginWindow::loggedOut() {
if (stack_->count() > 1) {
QWidget* current = stack_->currentWidget();
stack_->setCurrentIndex(0);
stack_->removeWidget(current);
}
setInitialMenus();
setEnabled(true);
}
void QtLoginWindow::loginClicked() {
setEnabled(false);
onLoginRequest(Q2PSTRING(username_->text()), Q2PSTRING(password_->text()), Q2PSTRING(certificateFile_), remember_->isChecked());
}
void QtLoginWindow::handleCertficateChecked(bool checked) {
if (checked) {
certificateFile_ = QFileDialog::getOpenFileName(this, "Select an authentication certificate", QString(), QString("*.cert"));
if (certificateFile_.isEmpty()) {
certificateButton_->setChecked(false);
}
}
else {
certificateFile_ = "";
}
}
void QtLoginWindow::handleAbout() {
if (!aboutDialog_) {
aboutDialog_ = new QtAboutWidget();
aboutDialog_->show();
}
else {
aboutDialog_->show();
aboutDialog_->raise();
aboutDialog_->activateWindow();
}
}
void QtLoginWindow::handleQuit() {
QApplication::quit();
}
void QtLoginWindow::setInitialMenus() {
menuBar_->clear();
menuBar_->addMenu(swiftMenu_);
}
void QtLoginWindow::morphInto(MainWindow *mainWindow) {
QtMainWindow *qtMainWindow = dynamic_cast<QtMainWindow*>(mainWindow);
assert(qtMainWindow);
stack_->addWidget(qtMainWindow);
stack_->setCurrentWidget(qtMainWindow);
setEnabled(true);
setInitialMenus();
foreach (QMenu* menu, qtMainWindow->getMenus()) {
menuBar_->addMenu(menu);
}
}
void QtLoginWindow::setMessage(const String& message) {
if (!message.isEmpty()) {
message_->setText("<center><font color=\"red\">" + P2QSTRING(message) + "</font></center>");
}
else {
message_->setText("");
}
}
void QtLoginWindow::bringToFront() {
if (isHidden()) {
showNormal();
raise();
activateWindow();
}
else {
hide();
}
}
}
|