File indexing completed on 2024-12-22 04:56:52

0001 /*
0002     SPDX-FileCopyrightText: 2023-2024 Laurent Montel <montel.org>
0003     SPDX-License-Identifier: LGPL-2.0-or-later
0004 */
0005 
0006 #include "newmailnotificationhistorywidget.h"
0007 #include "newmailnotificationhistorybrowsertext.h"
0008 #include "newmailnotificationhistorybrowsertextwidget.h"
0009 #include "newmailnotificationhistorymanager.h"
0010 #include "newmailnotifieragentsettings.h"
0011 #include <KLocalizedString>
0012 
0013 #include <TextCustomEditor/PlainTextEditor>
0014 
0015 #include <QCheckBox>
0016 #include <QScrollBar>
0017 #include <QVBoxLayout>
0018 
0019 NewMailNotificationHistoryWidget::NewMailNotificationHistoryWidget(QWidget *parent)
0020     : QWidget{parent}
0021     , mTextBrowser(new NewMailNotificationHistoryBrowserTextWidget(new NewMailNotificationHistoryBrowserText(this), this))
0022     , mEnabledHistory(new QCheckBox(i18n("Enabled"), this))
0023 {
0024     auto mainLayout = new QVBoxLayout(this);
0025     mainLayout->setObjectName(QLatin1StringView("mainLayout"));
0026     mainLayout->setContentsMargins({});
0027 
0028     mEnabledHistory->setObjectName(QLatin1StringView("mEnabledHistory"));
0029     mainLayout->addWidget(mEnabledHistory);
0030     connect(mEnabledHistory, &QCheckBox::clicked, this, &NewMailNotificationHistoryWidget::slotEnableChanged);
0031 
0032     mTextBrowser->setObjectName(QLatin1StringView("mTextBrowser"));
0033     mainLayout->addWidget(mTextBrowser);
0034 
0035     connect(NewMailNotificationHistoryManager::self(),
0036             &NewMailNotificationHistoryManager::historyAdded,
0037             this,
0038             &NewMailNotificationHistoryWidget::slotHistoryAdded);
0039 
0040     mTextBrowser->setHtml(NewMailNotificationHistoryManager::self()->history().join(QStringLiteral("<br>")));
0041     connect(mTextBrowser, &NewMailNotificationHistoryBrowserTextWidget::clearHistory, this, [this]() {
0042         NewMailNotificationHistoryManager::self()->clear();
0043         mTextBrowser->clear();
0044     });
0045     slotEnableChanged(NewMailNotifierAgentSettings::self()->enableNotificationHistory());
0046     mEnabledHistory->setChecked(NewMailNotifierAgentSettings::self()->enableNotificationHistory());
0047 }
0048 
0049 NewMailNotificationHistoryWidget::~NewMailNotificationHistoryWidget() = default;
0050 
0051 void NewMailNotificationHistoryWidget::slotHistoryAdded(const QString &str)
0052 {
0053     mTextBrowser->editor()->setHtml(str);
0054     mTextBrowser->editor()->verticalScrollBar()->setValue(mTextBrowser->editor()->verticalScrollBar()->maximum());
0055 }
0056 
0057 void NewMailNotificationHistoryWidget::slotEnableChanged(bool clicked)
0058 {
0059     mTextBrowser->setEnabled(clicked);
0060     NewMailNotifierAgentSettings::self()->setEnableNotificationHistory(clicked);
0061     NewMailNotifierAgentSettings::self()->save();
0062 }
0063 
0064 #include "moc_newmailnotificationhistorywidget.cpp"