File indexing completed on 2024-12-01 10:40:59

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 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
0031     mMainLayout->setSpacing(0);
0032 #endif
0033 
0034     const QString newFeaturesMD5 = WhatsNewWidget::newFeaturesMD5();
0035     const bool hasNewFeature = (RuqolaGlobalConfig::self()->previousNewFeaturesMD5() != newFeaturesMD5);
0036     if (hasNewFeature) {
0037         auto whatsNewMessageWidget = new WhatsNewMessageWidget(this);
0038         whatsNewMessageWidget->setObjectName(QStringLiteral("whatsNewMessageWidget"));
0039         mMainLayout->addWidget(whatsNewMessageWidget);
0040         RuqolaGlobalConfig::self()->setPreviousNewFeaturesMD5(newFeaturesMD5);
0041         whatsNewMessageWidget->animatedShow();
0042     }
0043 
0044     mStackedWidget->setObjectName(QStringLiteral("mStackedWidget"));
0045     mMainLayout->addWidget(mStackedWidget);
0046 
0047     mRuqolaMainWidget->setObjectName(QStringLiteral("mRuqolaMainWidget"));
0048     mStackedWidget->addWidget(mRuqolaMainWidget);
0049 
0050     mRuqolaLoginWidget->setObjectName(QStringLiteral("mRuqolaLoginWidget"));
0051     mStackedWidget->addWidget(mRuqolaLoginWidget);
0052 
0053     mStackedWidget->setCurrentWidget(mRuqolaLoginWidget);
0054     connect(mRuqolaMainWidget, &RuqolaMainWidget::channelSelected, this, &RuqolaCentralWidget::channelSelected);
0055     connect(ServerErrorInfoHistoryManager::self(), &ServerErrorInfoHistoryManager::newServerErrorInfo, this, &RuqolaCentralWidget::slotNewErrorInfo);
0056 }
0057 
0058 RuqolaCentralWidget::~RuqolaCentralWidget() = default;
0059 
0060 void RuqolaCentralWidget::createServerErrorInfoMessageWidget()
0061 {
0062     mServerErrorInfoMessageWidget = new ServerErrorInfoMessageWidget(this);
0063     mServerErrorInfoMessageWidget->setObjectName(QStringLiteral("mServerErrorInfoMessageWidget"));
0064     mMainLayout->insertWidget(0, mServerErrorInfoMessageWidget);
0065 }
0066 
0067 void RuqolaCentralWidget::slotNewErrorInfo()
0068 {
0069     if (!mServerErrorInfoMessageWidget) {
0070         createServerErrorInfoMessageWidget();
0071     }
0072     mServerErrorInfoMessageWidget->animatedShow();
0073 }
0074 
0075 void RuqolaCentralWidget::slotJobFailedInfo(const QString &messageError, const QString &accountName)
0076 {
0077     ServerErrorInfo info;
0078     info.setAccountName(accountName);
0079     info.setMessage(messageError);
0080     ServerErrorInfoHistoryManager::self()->addServerErrorInfo(std::move(info));
0081 }
0082 
0083 void RuqolaCentralWidget::slotSocketError(QAbstractSocket::SocketError error, const QString &errorString)
0084 {
0085     Q_UNUSED(error)
0086     // ## let's hope this happens while the login widget is visible, but that's quite likely
0087     // Testcase: try to connect to a server that doesn't exist
0088     mRuqolaLoginWidget->showError(errorString);
0089 }
0090 
0091 Room *RuqolaCentralWidget::room() const
0092 {
0093     return mRuqolaMainWidget->room();
0094 }
0095 
0096 QString RuqolaCentralWidget::roomId() const
0097 {
0098     return mRuqolaMainWidget->roomId();
0099 }
0100 
0101 Room::RoomType RuqolaCentralWidget::roomType() const
0102 {
0103     return mRuqolaMainWidget->roomType();
0104 }
0105 
0106 void RuqolaCentralWidget::selectNextUnreadChannel()
0107 {
0108     mRuqolaMainWidget->selectNextUnreadChannel();
0109 }
0110 
0111 void RuqolaCentralWidget::setCurrentRocketChatAccount(RocketChatAccount *account)
0112 {
0113     if (mCurrentRocketChatAccount) {
0114         disconnect(mCurrentRocketChatAccount, nullptr, this, nullptr);
0115     }
0116     mCurrentRocketChatAccount = account;
0117     connect(mCurrentRocketChatAccount, &RocketChatAccount::loginStatusChanged, this, &RuqolaCentralWidget::slotLoginStatusChanged);
0118     connect(mCurrentRocketChatAccount, &RocketChatAccount::socketError, this, &RuqolaCentralWidget::slotSocketError);
0119     connect(mCurrentRocketChatAccount, &RocketChatAccount::jobFailed, this, &RuqolaCentralWidget::slotJobFailedInfo);
0120     mRuqolaMainWidget->setCurrentRocketChatAccount(mCurrentRocketChatAccount);
0121     // Check if account is connected or not.
0122     slotLoginStatusChanged();
0123 }
0124 
0125 void RuqolaCentralWidget::slotLoginStatusChanged()
0126 {
0127     const auto loginStatus = mCurrentRocketChatAccount->loginStatus();
0128     mRuqolaLoginWidget->setLoginStatus(loginStatus);
0129     bool loginPage = false;
0130     if (loginStatus == DDPAuthenticationManager::LoggedIn) {
0131         mStackedWidget->setCurrentWidget(mRuqolaMainWidget);
0132     } else {
0133         mStackedWidget->setCurrentWidget(mRuqolaLoginWidget);
0134         mRuqolaLoginWidget->setRocketChatAccount(mCurrentRocketChatAccount);
0135         loginPage = true;
0136     }
0137     Q_EMIT loginPageActivated(loginPage);
0138 }
0139 
0140 #include "moc_ruqolacentralwidget.cpp"