File indexing completed on 2024-04-28 05:01:52

0001 /*
0002   SPDX-FileCopyrightText: 2020-2024 Laurent Montel <montel@kde.org>
0003 
0004   SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "ruqolacentralwidget.h"
0008 #include "rocketchataccount.h"
0009 #include "ruqolaglobalconfig.h"
0010 #include "ruqolaloginwidget.h"
0011 #include "ruqolamainwidget.h"
0012 #include "servererrorinfo.h"
0013 #include "servererrorinfohistory/servererrorinfomessagewidget.h"
0014 #include "servererrorinfohistorymanager.h"
0015 #include "whatsnew/whatsnewmessagewidget.h"
0016 #include "whatsnew/whatsnewwidget.h"
0017 #include <KLocalizedString>
0018 #include <QHBoxLayout>
0019 #include <QStackedWidget>
0020 
0021 RuqolaCentralWidget::RuqolaCentralWidget(QWidget *parent)
0022     : QWidget(parent)
0023     , mStackedWidget(new QStackedWidget(this))
0024     , mRuqolaMainWidget(new RuqolaMainWidget(this))
0025     , mRuqolaLoginWidget(new RuqolaLoginWidget(this))
0026     , mMainLayout(new QVBoxLayout(this))
0027 {
0028     mMainLayout->setContentsMargins({});
0029     mMainLayout->setObjectName(QStringLiteral("mainlayout"));
0030     mMainLayout->setSpacing(0);
0031 
0032     const QString newFeaturesMD5 = WhatsNewWidget::newFeaturesMD5();
0033     const bool hasNewFeature = (RuqolaGlobalConfig::self()->previousNewFeaturesMD5() != newFeaturesMD5);
0034     if (hasNewFeature) {
0035         auto whatsNewMessageWidget = new WhatsNewMessageWidget(this);
0036         whatsNewMessageWidget->setObjectName(QStringLiteral("whatsNewMessageWidget"));
0037         mMainLayout->addWidget(whatsNewMessageWidget);
0038         RuqolaGlobalConfig::self()->setPreviousNewFeaturesMD5(newFeaturesMD5);
0039         whatsNewMessageWidget->animatedShow();
0040     }
0041 
0042     mStackedWidget->setObjectName(QStringLiteral("mStackedWidget"));
0043     mMainLayout->addWidget(mStackedWidget);
0044 
0045     mRuqolaMainWidget->setObjectName(QStringLiteral("mRuqolaMainWidget"));
0046     mStackedWidget->addWidget(mRuqolaMainWidget);
0047 
0048     mRuqolaLoginWidget->setObjectName(QStringLiteral("mRuqolaLoginWidget"));
0049     mStackedWidget->addWidget(mRuqolaLoginWidget);
0050 
0051     mStackedWidget->setCurrentWidget(mRuqolaLoginWidget);
0052     connect(mRuqolaMainWidget, &RuqolaMainWidget::channelSelected, this, &RuqolaCentralWidget::channelSelected);
0053     connect(ServerErrorInfoHistoryManager::self(), &ServerErrorInfoHistoryManager::newServerErrorInfo, this, &RuqolaCentralWidget::slotNewErrorInfo);
0054 }
0055 
0056 RuqolaCentralWidget::~RuqolaCentralWidget() = default;
0057 
0058 void RuqolaCentralWidget::createServerErrorInfoMessageWidget()
0059 {
0060     mServerErrorInfoMessageWidget = new ServerErrorInfoMessageWidget(this);
0061     mServerErrorInfoMessageWidget->setObjectName(QStringLiteral("mServerErrorInfoMessageWidget"));
0062     mMainLayout->insertWidget(0, mServerErrorInfoMessageWidget);
0063 }
0064 
0065 void RuqolaCentralWidget::slotNewErrorInfo()
0066 {
0067     if (!mServerErrorInfoMessageWidget) {
0068         createServerErrorInfoMessageWidget();
0069     }
0070     mServerErrorInfoMessageWidget->animatedShow();
0071 }
0072 
0073 void RuqolaCentralWidget::slotJobFailedInfo(const QString &messageError, const QString &accountName)
0074 {
0075     ServerErrorInfo info;
0076     info.setAccountName(accountName);
0077     info.setMessage(messageError);
0078     ServerErrorInfoHistoryManager::self()->addServerErrorInfo(std::move(info));
0079 }
0080 
0081 void RuqolaCentralWidget::slotSocketError(QAbstractSocket::SocketError error, const QString &errorString)
0082 {
0083     Q_UNUSED(error)
0084     // ## let's hope this happens while the login widget is visible, but that's quite likely
0085     // Testcase: try to connect to a server that doesn't exist
0086     mRuqolaLoginWidget->showError(errorString);
0087 }
0088 
0089 Room *RuqolaCentralWidget::room() const
0090 {
0091     return mRuqolaMainWidget->room();
0092 }
0093 
0094 QString RuqolaCentralWidget::roomId() const
0095 {
0096     return mRuqolaMainWidget->roomId();
0097 }
0098 
0099 Room::RoomType RuqolaCentralWidget::roomType() const
0100 {
0101     return mRuqolaMainWidget->roomType();
0102 }
0103 
0104 void RuqolaCentralWidget::selectNextUnreadChannel()
0105 {
0106     mRuqolaMainWidget->selectNextUnreadChannel();
0107 }
0108 
0109 void RuqolaCentralWidget::setCurrentRocketChatAccount(RocketChatAccount *account)
0110 {
0111     if (mCurrentRocketChatAccount) {
0112         disconnect(mCurrentRocketChatAccount, nullptr, this, nullptr);
0113     }
0114     mCurrentRocketChatAccount = account;
0115     connect(mCurrentRocketChatAccount, &RocketChatAccount::loginStatusChanged, this, &RuqolaCentralWidget::slotLoginStatusChanged);
0116     connect(mCurrentRocketChatAccount, &RocketChatAccount::socketError, this, &RuqolaCentralWidget::slotSocketError);
0117     connect(mCurrentRocketChatAccount, &RocketChatAccount::jobFailed, this, &RuqolaCentralWidget::slotJobFailedInfo);
0118     mRuqolaMainWidget->setCurrentRocketChatAccount(mCurrentRocketChatAccount);
0119     // Check if account is connected or not.
0120     slotLoginStatusChanged();
0121 }
0122 
0123 void RuqolaCentralWidget::slotLoginStatusChanged()
0124 {
0125     const auto loginStatus = mCurrentRocketChatAccount->loginStatus();
0126     mRuqolaLoginWidget->setLoginStatus(loginStatus);
0127     bool loginPage = false;
0128     if (loginStatus == DDPAuthenticationManager::LoggedIn) {
0129         mStackedWidget->setCurrentWidget(mRuqolaMainWidget);
0130     } else {
0131         mStackedWidget->setCurrentWidget(mRuqolaLoginWidget);
0132         mRuqolaLoginWidget->setRocketChatAccount(mCurrentRocketChatAccount);
0133         loginPage = true;
0134     }
0135     Q_EMIT loginPageActivated(loginPage);
0136 }
0137 
0138 #include "moc_ruqolacentralwidget.cpp"