summaryrefslogtreecommitdiffstats
path: root/src/MainWindow.cc
blob: 39fb1d2a3cf204a5f926e4ef4ed36cb0440b499d (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
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
 * nheko Copyright (C) 2017  Konstantinos Sideris <siderisk@auth.gr>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "Config.h"
#include "MainWindow.h"

#include <QApplication>
#include <QLayout>
#include <QNetworkReply>
#include <QSettings>
#include <QShortcut>
#include <QSystemTrayIcon>

MainWindow *MainWindow::instance_ = nullptr;

MainWindow::MainWindow(QWidget *parent)
  : QMainWindow(parent)
  , progress_modal_{ nullptr }
  , spinner_{ nullptr }
{
        QSizePolicy sizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
        setSizePolicy(sizePolicy);
        setWindowTitle("nheko");
        setObjectName("MainWindow");
        setStyleSheet("QWidget#MainWindow {background-color: #fff}");

        restoreWindowSize();
        setMinimumSize(QSize(conf::window::minWidth, conf::window::minHeight));

        QFont font("Open Sans");
        font.setPixelSize(conf::fontSize);
        font.setStyleStrategy(QFont::PreferAntialias);
        setFont(font);

        client_   = QSharedPointer<MatrixClient>(new MatrixClient("matrix.org"));
        trayIcon_ = new TrayIcon(":/logos/nheko-32.png", this);

        welcome_page_  = new WelcomePage(this);
        login_page_    = new LoginPage(client_, this);
        register_page_ = new RegisterPage(client_, this);
        chat_page_     = new ChatPage(client_, this);

        // Initialize sliding widget manager.
        pageStack_ = new QStackedWidget(this);
        pageStack_->addWidget(welcome_page_);
        pageStack_->addWidget(login_page_);
        pageStack_->addWidget(register_page_);
        pageStack_->addWidget(chat_page_);

        setCentralWidget(pageStack_);

        connect(welcome_page_, SIGNAL(userLogin()), this, SLOT(showLoginPage()));
        connect(welcome_page_, SIGNAL(userRegister()), this, SLOT(showRegisterPage()));

        connect(login_page_, SIGNAL(backButtonClicked()), this, SLOT(showWelcomePage()));
        connect(register_page_, SIGNAL(backButtonClicked()), this, SLOT(showWelcomePage()));

        connect(chat_page_, SIGNAL(close()), this, SLOT(showWelcomePage()));
        connect(
          chat_page_, SIGNAL(changeWindowTitle(QString)), this, SLOT(setWindowTitle(QString)));
        connect(chat_page_, SIGNAL(unreadMessages(int)), trayIcon_, SLOT(setUnreadCount(int)));

        connect(trayIcon_,
                SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
                this,
                SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));

        connect(chat_page_, SIGNAL(contentLoaded()), this, SLOT(removeOverlayProgressBar()));

        connect(client_.data(),
                SIGNAL(loginSuccess(QString, QString, QString)),
                this,
                SLOT(showChatPage(QString, QString, QString)));

        QShortcut *quitShortcut = new QShortcut(QKeySequence::Quit, this);
        connect(quitShortcut, &QShortcut::activated, this, QApplication::quit);

        QSettings settings;

        if (hasActiveUser()) {
                QString token       = settings.value("auth/access_token").toString();
                QString home_server = settings.value("auth/home_server").toString();
                QString user_id     = settings.value("auth/user_id").toString();

                showChatPage(user_id, home_server, token);
        }
}

void
MainWindow::restoreWindowSize()
{
        QSettings settings;
        int savedWidth  = settings.value("window/width").toInt();
        int savedheight = settings.value("window/height").toInt();

        if (savedWidth == 0 || savedheight == 0)
                resize(conf::window::width, conf::window::height);
        else
                resize(savedWidth, savedheight);
}

void
MainWindow::saveCurrentWindowSize()
{
        QSettings settings;
        QSize current = size();

        settings.setValue("window/width", current.width());
        settings.setValue("window/height", current.height());
}

void
MainWindow::removeOverlayProgressBar()
{
        QTimer *timer = new QTimer(this);
        timer->setSingleShot(true);

        connect(timer, &QTimer::timeout, [=]() {
                timer->deleteLater();

                if (progress_modal_ != nullptr) {
                        progress_modal_->deleteLater();
                        progress_modal_->fadeOut();
                }

                if (spinner_ != nullptr)
                        spinner_->deleteLater();

                spinner_->stop();

                progress_modal_ = nullptr;
                spinner_        = nullptr;
        });

        timer->start(500);
}

void
MainWindow::showChatPage(QString userid, QString homeserver, QString token)
{
        QSettings settings;
        settings.setValue("auth/access_token", token);
        settings.setValue("auth/home_server", homeserver);
        settings.setValue("auth/user_id", userid);

        int modalOpacityDuration = 300;

        // If we go directly from the welcome page don't show an animation.
        if (pageStack_->currentIndex() == 0)
                modalOpacityDuration = 0;

        QTimer::singleShot(
          modalOpacityDuration + 100, this, [=]() { pageStack_->setCurrentWidget(chat_page_); });

        if (spinner_ == nullptr) {
                spinner_ = new LoadingIndicator(this);
                spinner_->setColor("#acc7dc");
                spinner_->setFixedHeight(120);
                spinner_->setFixedWidth(120);
                spinner_->start();
        }

        if (progress_modal_ == nullptr) {
                progress_modal_ = new OverlayModal(this, spinner_);
                progress_modal_->fadeIn();
                progress_modal_->setDuration(modalOpacityDuration);
        }

        login_page_->reset();
        chat_page_->bootstrap(userid, homeserver, token);

        instance_ = this;
}

void
MainWindow::showWelcomePage()
{
        pageStack_->setCurrentWidget(welcome_page_);
}

void
MainWindow::showLoginPage()
{
        pageStack_->setCurrentWidget(login_page_);
}

void
MainWindow::showRegisterPage()
{
        pageStack_->setCurrentWidget(register_page_);
}

void
MainWindow::closeEvent(QCloseEvent *event)
{
        if (isVisible()) {
                event->ignore();
                hide();
        }
}

void
MainWindow::iconActivated(QSystemTrayIcon::ActivationReason reason)
{
        switch (reason) {
        case QSystemTrayIcon::Trigger:
                if (!isVisible()) {
                        show();
                } else {
                        hide();
                }
                break;
        default:
                break;
        }
}

bool
MainWindow::hasActiveUser()
{
        QSettings settings;

        return settings.contains("auth/access_token") && settings.contains("auth/home_server") &&
               settings.contains("auth/user_id");
}

MainWindow *
MainWindow::instance()
{
        return instance_;
}

MainWindow::~MainWindow()
{
}