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/>.
|
|
|
|
*/
|
|
|
|
|
2017-10-07 20:09:34 +03:00
|
|
|
#include "MainWindow.h"
|
2017-10-08 22:38:38 +03:00
|
|
|
#include "Config.h"
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2017-09-24 17:39:06 +03:00
|
|
|
#include <QApplication>
|
2017-04-06 02:06:42 +03:00
|
|
|
#include <QLayout>
|
|
|
|
#include <QNetworkReply>
|
|
|
|
#include <QSettings>
|
2017-09-24 13:58:36 +03:00
|
|
|
#include <QShortcut>
|
2017-09-24 17:39:06 +03:00
|
|
|
#include <QSystemTrayIcon>
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2017-06-02 18:04:51 +03:00
|
|
|
MainWindow *MainWindow::instance_ = nullptr;
|
|
|
|
|
2017-04-06 02:06:42 +03:00
|
|
|
MainWindow::MainWindow(QWidget *parent)
|
2017-08-20 13:47:22 +03:00
|
|
|
: QMainWindow(parent)
|
2017-10-07 20:09:34 +03:00
|
|
|
, progressModal_{ nullptr }
|
2017-08-20 13:47:22 +03:00
|
|
|
, spinner_{ nullptr }
|
2017-04-06 02:06:42 +03:00
|
|
|
{
|
2017-09-10 12:58:00 +03:00
|
|
|
QSizePolicy sizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
|
|
|
|
setSizePolicy(sizePolicy);
|
|
|
|
setWindowTitle("nheko");
|
|
|
|
setObjectName("MainWindow");
|
2017-09-16 17:43:34 +03:00
|
|
|
setStyleSheet("QWidget#MainWindow {background-color: #fff}");
|
2017-05-14 21:10:03 +03:00
|
|
|
|
2017-09-10 12:58:00 +03:00
|
|
|
restoreWindowSize();
|
|
|
|
setMinimumSize(QSize(conf::window::minWidth, conf::window::minHeight));
|
2017-05-14 21:10:03 +03:00
|
|
|
|
2017-09-10 12:58:00 +03:00
|
|
|
QFont font("Open Sans");
|
|
|
|
font.setPixelSize(conf::fontSize);
|
|
|
|
font.setStyleStrategy(QFont::PreferAntialias);
|
|
|
|
setFont(font);
|
2017-05-14 21:10:03 +03:00
|
|
|
|
2017-09-10 12:58:00 +03:00
|
|
|
client_ = QSharedPointer<MatrixClient>(new MatrixClient("matrix.org"));
|
|
|
|
trayIcon_ = new TrayIcon(":/logos/nheko-32.png", this);
|
2017-04-11 17:45:47 +03:00
|
|
|
|
2017-09-10 12:58:00 +03:00
|
|
|
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
|
|
|
|
2017-09-10 12:58:00 +03:00
|
|
|
// Initialize sliding widget manager.
|
2017-09-30 17:05:05 +03:00
|
|
|
pageStack_ = new QStackedWidget(this);
|
|
|
|
pageStack_->addWidget(welcome_page_);
|
|
|
|
pageStack_->addWidget(login_page_);
|
|
|
|
pageStack_->addWidget(register_page_);
|
|
|
|
pageStack_->addWidget(chat_page_);
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2017-09-30 17:05:05 +03:00
|
|
|
setCentralWidget(pageStack_);
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2017-09-10 12:58:00 +03:00
|
|
|
connect(welcome_page_, SIGNAL(userLogin()), this, SLOT(showLoginPage()));
|
|
|
|
connect(welcome_page_, SIGNAL(userRegister()), this, SLOT(showRegisterPage()));
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2017-09-10 12:58:00 +03:00
|
|
|
connect(login_page_, SIGNAL(backButtonClicked()), this, SLOT(showWelcomePage()));
|
|
|
|
connect(register_page_, SIGNAL(backButtonClicked()), this, SLOT(showWelcomePage()));
|
2017-04-06 02:06:42 +03:00
|
|
|
|
2017-09-10 12:58:00 +03:00
|
|
|
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)));
|
2017-05-21 16:36:06 +03:00
|
|
|
|
2017-09-10 12:58:00 +03:00
|
|
|
connect(trayIcon_,
|
|
|
|
SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
|
|
|
|
this,
|
|
|
|
SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));
|
2017-04-09 02:17:04 +03:00
|
|
|
|
2017-09-10 12:58:00 +03:00
|
|
|
connect(chat_page_, SIGNAL(contentLoaded()), this, SLOT(removeOverlayProgressBar()));
|
2017-04-26 02:23:12 +03:00
|
|
|
|
2017-09-10 12:58:00 +03:00
|
|
|
connect(client_.data(),
|
|
|
|
SIGNAL(loginSuccess(QString, QString, QString)),
|
|
|
|
this,
|
|
|
|
SLOT(showChatPage(QString, QString, QString)));
|
2017-05-22 18:03:37 +03:00
|
|
|
|
2017-09-24 13:58:36 +03:00
|
|
|
QShortcut *quitShortcut = new QShortcut(QKeySequence::Quit, this);
|
|
|
|
connect(quitShortcut, &QShortcut::activated, this, QApplication::quit);
|
|
|
|
|
2017-09-10 12:58:00 +03:00
|
|
|
QSettings settings;
|
2017-05-22 18:03:37 +03:00
|
|
|
|
2017-09-10 12:58:00 +03:00
|
|
|
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();
|
2017-05-22 18:03:37 +03:00
|
|
|
|
2017-09-10 12:58:00 +03:00
|
|
|
showChatPage(user_id, home_server, token);
|
|
|
|
}
|
2017-04-06 02:06:42 +03:00
|
|
|
}
|
|
|
|
|
2017-08-20 13:47:22 +03:00
|
|
|
void
|
|
|
|
MainWindow::restoreWindowSize()
|
2017-07-30 13:59:28 +03:00
|
|
|
{
|
2017-09-10 12:58:00 +03:00
|
|
|
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);
|
2017-07-30 13:59:28 +03:00
|
|
|
}
|
|
|
|
|
2017-08-20 13:47:22 +03:00
|
|
|
void
|
|
|
|
MainWindow::saveCurrentWindowSize()
|
2017-07-30 13:59:28 +03:00
|
|
|
{
|
2017-09-10 12:58:00 +03:00
|
|
|
QSettings settings;
|
|
|
|
QSize current = size();
|
2017-07-30 13:59:28 +03:00
|
|
|
|
2017-09-10 12:58:00 +03:00
|
|
|
settings.setValue("window/width", current.width());
|
|
|
|
settings.setValue("window/height", current.height());
|
2017-07-30 13:59:28 +03:00
|
|
|
}
|
|
|
|
|
2017-08-20 13:47:22 +03:00
|
|
|
void
|
|
|
|
MainWindow::removeOverlayProgressBar()
|
2017-04-26 02:23:12 +03:00
|
|
|
{
|
2017-09-10 12:58:00 +03:00
|
|
|
QTimer *timer = new QTimer(this);
|
|
|
|
timer->setSingleShot(true);
|
2017-04-26 02:23:12 +03:00
|
|
|
|
2017-09-10 12:58:00 +03:00
|
|
|
connect(timer, &QTimer::timeout, [=]() {
|
|
|
|
timer->deleteLater();
|
2017-04-26 02:23:12 +03:00
|
|
|
|
2017-10-07 20:09:34 +03:00
|
|
|
if (!progressModal_.isNull())
|
|
|
|
progressModal_->fadeOut();
|
2017-04-26 02:23:12 +03:00
|
|
|
|
2017-10-07 20:09:34 +03:00
|
|
|
if (!spinner_.isNull())
|
|
|
|
spinner_->stop();
|
2017-04-26 02:23:12 +03:00
|
|
|
|
2017-10-07 20:09:34 +03:00
|
|
|
progressModal_.reset();
|
|
|
|
spinner_.reset();
|
2017-09-10 12:58:00 +03:00
|
|
|
});
|
|
|
|
|
2017-10-08 22:38:38 +03:00
|
|
|
// FIXME: Snackbar doesn't work if it's initialized in the constructor.
|
|
|
|
QTimer::singleShot(100, this, [=]() {
|
|
|
|
snackBar_ = QSharedPointer<SnackBar>(new SnackBar(this));
|
|
|
|
connect(chat_page_,
|
|
|
|
&ChatPage::showNotification,
|
|
|
|
snackBar_.data(),
|
|
|
|
&SnackBar::showMessage);
|
|
|
|
});
|
|
|
|
|
2017-09-10 12:58:00 +03:00
|
|
|
timer->start(500);
|
2017-04-26 02:23:12 +03:00
|
|
|
}
|
|
|
|
|
2017-08-20 13:47:22 +03:00
|
|
|
void
|
|
|
|
MainWindow::showChatPage(QString userid, QString homeserver, QString token)
|
2017-04-06 02:06:42 +03:00
|
|
|
{
|
2017-09-10 12:58:00 +03:00
|
|
|
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.
|
2017-09-30 17:05:05 +03:00
|
|
|
if (pageStack_->currentIndex() == 0)
|
2017-09-10 12:58:00 +03:00
|
|
|
modalOpacityDuration = 0;
|
2017-09-30 17:05:05 +03:00
|
|
|
|
|
|
|
QTimer::singleShot(
|
|
|
|
modalOpacityDuration + 100, this, [=]() { pageStack_->setCurrentWidget(chat_page_); });
|
2017-09-10 12:58:00 +03:00
|
|
|
|
2017-10-07 20:09:34 +03:00
|
|
|
if (spinner_.isNull()) {
|
|
|
|
spinner_ = QSharedPointer<LoadingIndicator>(
|
|
|
|
new LoadingIndicator(this),
|
|
|
|
[=](LoadingIndicator *indicator) { indicator->deleteLater(); });
|
2017-09-10 12:58:00 +03:00
|
|
|
spinner_->setColor("#acc7dc");
|
2017-10-15 22:08:51 +03:00
|
|
|
spinner_->setFixedHeight(100);
|
|
|
|
spinner_->setFixedWidth(100);
|
2017-09-10 12:58:00 +03:00
|
|
|
spinner_->start();
|
|
|
|
}
|
|
|
|
|
2017-10-07 20:09:34 +03:00
|
|
|
if (progressModal_.isNull()) {
|
|
|
|
progressModal_ =
|
|
|
|
QSharedPointer<OverlayModal>(new OverlayModal(this, spinner_.data()),
|
|
|
|
[=](OverlayModal *modal) { modal->deleteLater(); });
|
|
|
|
progressModal_->fadeIn();
|
|
|
|
progressModal_->setDuration(modalOpacityDuration);
|
2017-09-10 12:58:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
login_page_->reset();
|
|
|
|
chat_page_->bootstrap(userid, homeserver, token);
|
|
|
|
|
|
|
|
instance_ = this;
|
2017-04-06 02:06:42 +03:00
|
|
|
}
|
|
|
|
|
2017-08-20 13:47:22 +03:00
|
|
|
void
|
|
|
|
MainWindow::showWelcomePage()
|
2017-04-06 02:06:42 +03:00
|
|
|
{
|
2017-09-30 17:05:05 +03:00
|
|
|
pageStack_->setCurrentWidget(welcome_page_);
|
2017-04-06 02:06:42 +03:00
|
|
|
}
|
|
|
|
|
2017-08-20 13:47:22 +03:00
|
|
|
void
|
|
|
|
MainWindow::showLoginPage()
|
2017-04-06 02:06:42 +03:00
|
|
|
{
|
2017-09-30 17:05:05 +03:00
|
|
|
pageStack_->setCurrentWidget(login_page_);
|
2017-04-06 02:06:42 +03:00
|
|
|
}
|
|
|
|
|
2017-08-20 13:47:22 +03:00
|
|
|
void
|
|
|
|
MainWindow::showRegisterPage()
|
2017-04-06 02:06:42 +03:00
|
|
|
{
|
2017-09-30 17:05:05 +03:00
|
|
|
pageStack_->setCurrentWidget(register_page_);
|
2017-04-06 02:06:42 +03:00
|
|
|
}
|
|
|
|
|
2017-08-20 13:47:22 +03:00
|
|
|
void
|
|
|
|
MainWindow::closeEvent(QCloseEvent *event)
|
2017-05-21 16:36:06 +03:00
|
|
|
{
|
2017-09-10 12:58:00 +03:00
|
|
|
if (isVisible()) {
|
|
|
|
event->ignore();
|
|
|
|
hide();
|
|
|
|
}
|
2017-05-21 16:36:06 +03:00
|
|
|
}
|
|
|
|
|
2017-08-20 13:47:22 +03:00
|
|
|
void
|
|
|
|
MainWindow::iconActivated(QSystemTrayIcon::ActivationReason reason)
|
2017-05-21 16:36:06 +03:00
|
|
|
{
|
2017-09-10 12:58:00 +03:00
|
|
|
switch (reason) {
|
|
|
|
case QSystemTrayIcon::Trigger:
|
|
|
|
if (!isVisible()) {
|
|
|
|
show();
|
|
|
|
} else {
|
|
|
|
hide();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2017-05-21 16:36:06 +03:00
|
|
|
}
|
|
|
|
|
2017-08-20 13:47:22 +03:00
|
|
|
bool
|
|
|
|
MainWindow::hasActiveUser()
|
2017-05-22 18:03:37 +03:00
|
|
|
{
|
2017-09-10 12:58:00 +03:00
|
|
|
QSettings settings;
|
2017-05-22 18:03:37 +03:00
|
|
|
|
2017-09-10 12:58:00 +03:00
|
|
|
return settings.contains("auth/access_token") && settings.contains("auth/home_server") &&
|
|
|
|
settings.contains("auth/user_id");
|
2017-05-22 18:03:37 +03:00
|
|
|
}
|
|
|
|
|
2017-08-20 13:47:22 +03:00
|
|
|
MainWindow *
|
|
|
|
MainWindow::instance()
|
2017-06-02 18:04:51 +03:00
|
|
|
{
|
2017-09-10 12:58:00 +03:00
|
|
|
return instance_;
|
2017-06-02 18:04:51 +03:00
|
|
|
}
|
|
|
|
|
2017-10-01 12:11:33 +03:00
|
|
|
MainWindow::~MainWindow() {}
|