File indexing completed on 2024-12-08 04:34:13

0001 /*
0002    SPDX-FileCopyrightText: 2023-2024 Laurent Montel <montel.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "exploredatabasewidget.h"
0008 #include "model/messagesmodel.h"
0009 #include "rocketchataccount.h"
0010 #include "room/messagelistview.h"
0011 #include <KLineEditEventHandler>
0012 #include <KMessageBox>
0013 #include <QCheckBox>
0014 #include <QDateTimeEdit>
0015 #include <QLabel>
0016 #include <QLineEdit>
0017 #include <QPushButton>
0018 #include <QSpinBox>
0019 #include <QVBoxLayout>
0020 
0021 // debug dialogbox => don't translate it
0022 ExploreDatabaseWidget::ExploreDatabaseWidget(RocketChatAccount *account, QWidget *parent)
0023     : QWidget{parent}
0024     , mRocketChatAccount(account)
0025     , mMessageListView(new MessageListView(account, MessageListView::Mode::Viewing, this))
0026     , mLocalMessageDatabase(new LocalMessageDatabase())
0027     , mRoomName(new QLineEdit(this))
0028     , mNumberOfMessages(new QSpinBox(this))
0029     , mUseStartDateTime(new QCheckBox(QStringLiteral("Start"), this))
0030     , mStartDateTime(new QDateTimeEdit(this))
0031     , mUseEndDateTime(new QCheckBox(QStringLiteral("End"), this))
0032     , mEndDateTime(new QDateTimeEdit(this))
0033     , mMessageModel(new MessagesModel(QStringLiteral("no_room"), account, nullptr, this))
0034 {
0035     mNumberOfMessages->setObjectName(QStringLiteral("mNumberOfMessages"));
0036     mNumberOfMessages->setRange(-1, 9999);
0037     mNumberOfMessages->setValue(-1);
0038     mNumberOfMessages->setSpecialValueText(QStringLiteral("All messages"));
0039 
0040     mUseStartDateTime->setObjectName(QStringLiteral("mUseStartDateTime"));
0041     mStartDateTime->setObjectName(QStringLiteral("mStartDateTime"));
0042     mUseEndDateTime->setObjectName(QStringLiteral("mUseEndDateTime"));
0043     mEndDateTime->setObjectName(QStringLiteral("mEndDateTime"));
0044 
0045     mMessageListView->setObjectName(QStringLiteral("mMessageListView"));
0046     auto mainLayout = new QVBoxLayout(this);
0047     mainLayout->setObjectName(QStringLiteral("mainLayout"));
0048     mainLayout->setContentsMargins({});
0049 
0050     auto hboxLayout = new QHBoxLayout;
0051     hboxLayout->setContentsMargins({});
0052     hboxLayout->setObjectName(QStringLiteral("hboxLayout"));
0053     mainLayout->addLayout(hboxLayout);
0054 
0055     mRoomName->setObjectName(QStringLiteral("mRoomName"));
0056     mRoomName->setClearButtonEnabled(true);
0057     KLineEditEventHandler::catchReturnKey(mRoomName);
0058     auto label = new QLabel(QStringLiteral("Room name:"), this);
0059     label->setObjectName(QStringLiteral("label"));
0060     hboxLayout->addWidget(label);
0061     hboxLayout->addWidget(mRoomName);
0062 
0063     hboxLayout->addWidget(mUseStartDateTime);
0064     hboxLayout->addWidget(mStartDateTime);
0065     hboxLayout->addWidget(mUseEndDateTime);
0066     hboxLayout->addWidget(mEndDateTime);
0067 
0068     mStartDateTime->setEnabled(false);
0069     mEndDateTime->setEnabled(false);
0070 
0071     connect(mUseStartDateTime, &QCheckBox::clicked, mStartDateTime, &QDateTimeEdit::setEnabled);
0072     connect(mUseEndDateTime, &QCheckBox::clicked, mEndDateTime, &QDateTimeEdit::setEnabled);
0073 
0074     hboxLayout->addWidget(new QLabel(QStringLiteral("Number Of Elements"), this));
0075     hboxLayout->addWidget(mNumberOfMessages);
0076 
0077     auto pushButton = new QPushButton(QStringLiteral("Load"), this);
0078     pushButton->setObjectName(QStringLiteral("pushButton"));
0079     hboxLayout->addWidget(pushButton);
0080     connect(pushButton, &QPushButton::clicked, this, &ExploreDatabaseWidget::slotLoad);
0081     connect(mRoomName, &QLineEdit::returnPressed, this, &ExploreDatabaseWidget::slotLoad);
0082 
0083     mMessageListView->setModel(mMessageModel);
0084     mainLayout->addWidget(mMessageListView);
0085 }
0086 
0087 ExploreDatabaseWidget::~ExploreDatabaseWidget() = default;
0088 
0089 void ExploreDatabaseWidget::slotLoad()
0090 {
0091     const QString roomName = mRoomName->text().trimmed();
0092     if (!roomName.isEmpty()) {
0093         qint64 startId = -1;
0094         qint64 endId = -1;
0095         if (mUseStartDateTime->isChecked()) {
0096             startId = mStartDateTime->dateTime().toMSecsSinceEpoch();
0097         }
0098         if (mUseEndDateTime->isChecked()) {
0099             endId = mEndDateTime->dateTime().toMSecsSinceEpoch();
0100         }
0101         const QVector<Message> listMessages = mLocalMessageDatabase->loadMessages(mRocketChatAccount, roomName, startId, endId, mNumberOfMessages->value());
0102         mMessageModel->clear();
0103         mMessageModel->addMessages(listMessages);
0104     } else {
0105         KMessageBox::error(this, QStringLiteral("Room '%1' does not have database").arg(roomName), QStringLiteral("Database empty"));
0106     }
0107 }
0108 
0109 #include "moc_exploredatabasewidget.cpp"