matrixion/src/MainWindow.cc

210 lines
5.9 KiB
C++
Raw Normal View History

2017-04-06 02:06:42 +03:00
/*
* 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 "MainWindow.h"
#include <QLayout>
#include <QNetworkReply>
#include <QSettings>
2017-05-21 16:36:06 +03:00
#include <QSystemTrayIcon>
2017-04-06 02:06:42 +03:00
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, progress_modal_{nullptr}
, spinner_{nullptr}
2017-04-06 02:06:42 +03:00
{
2017-05-14 21:10:03 +03:00
QSizePolicy sizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
setSizePolicy(sizePolicy);
setWindowTitle("nheko");
2017-05-21 16:36:06 +03:00
setObjectName("MainWindow");
setStyleSheet("QWidget#MainWindow {background-color: #f9f9f9}");
2017-05-14 21:10:03 +03:00
resize(1066, 600); // 16:9 ratio
setMinimumSize(QSize(950, 600));
QFont font("Open Sans", 12);
font.setStyleStrategy(QFont::PreferAntialias);
setFont(font);
client_ = QSharedPointer<MatrixClient>(new MatrixClient("matrix.org"));
2017-05-21 16:36:06 +03:00
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);
2017-04-06 02:06:42 +03:00
// Initialize sliding widget manager.
sliding_stack_ = new SlidingStackWidget(this);
sliding_stack_->addWidget(welcome_page_);
sliding_stack_->addWidget(login_page_);
sliding_stack_->addWidget(register_page_);
sliding_stack_->addWidget(chat_page_);
setCentralWidget(sliding_stack_);
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)));
2017-05-21 16:36:06 +03:00
connect(chat_page_, SIGNAL(unreadMessages(int)), trayIcon_, SLOT(setUnreadCount(int)));
connect(trayIcon_,
SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
this,
SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));
connect(client_.data(),
SIGNAL(initialSyncCompleted(const SyncResponse &)),
this,
SLOT(removeOverlayProgressBar()));
connect(client_.data(),
2017-04-06 02:06:42 +03:00
SIGNAL(loginSuccess(QString, QString, QString)),
this,
SLOT(showChatPage(QString, QString, QString)));
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);
}
2017-04-06 02:06:42 +03:00
}
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();
progress_modal_ = nullptr;
spinner_ = nullptr;
});
timer->start(500);
}
2017-04-06 02:06:42 +03:00
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 index = sliding_stack_->getWidgetIndex(chat_page_);
int modalOpacityDuration = 300;
// If we go directly from the welcome page don't show an animation.
if (sliding_stack_->currentIndex() == 0) {
sliding_stack_->setCurrentIndex(index);
modalOpacityDuration = 0;
} else {
sliding_stack_->slideInIndex(index, SlidingStackWidget::AnimationDirection::LEFT_TO_RIGHT);
}
2017-04-06 02:06:42 +03:00
if (spinner_ == nullptr) {
spinner_ = new CircularProgress(this);
spinner_->setColor("#acc7dc");
spinner_->setSize(100);
}
if (progress_modal_ == nullptr) {
progress_modal_ = new OverlayModal(this, spinner_);
progress_modal_->fadeIn();
progress_modal_->setDuration(modalOpacityDuration);
}
2017-04-09 02:32:48 +03:00
login_page_->reset();
2017-04-06 02:06:42 +03:00
chat_page_->bootstrap(userid, homeserver, token);
}
void MainWindow::showWelcomePage()
{
int index = sliding_stack_->getWidgetIndex(welcome_page_);
if (sliding_stack_->currentIndex() == sliding_stack_->getWidgetIndex(login_page_))
sliding_stack_->slideInIndex(index, SlidingStackWidget::AnimationDirection::RIGHT_TO_LEFT);
else
sliding_stack_->slideInIndex(index, SlidingStackWidget::AnimationDirection::LEFT_TO_RIGHT);
}
void MainWindow::showLoginPage()
{
int index = sliding_stack_->getWidgetIndex(login_page_);
sliding_stack_->slideInIndex(index, SlidingStackWidget::AnimationDirection::LEFT_TO_RIGHT);
}
void MainWindow::showRegisterPage()
{
int index = sliding_stack_->getWidgetIndex(register_page_);
sliding_stack_->slideInIndex(index, SlidingStackWidget::AnimationDirection::RIGHT_TO_LEFT);
}
2017-05-21 16:36:06 +03:00
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");
}
2017-04-06 02:06:42 +03:00
MainWindow::~MainWindow()
{
}