File indexing completed on 2025-01-05 04:49:29

0001 /*
0002    SPDX-FileCopyrightText: 2019-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 #include "quicktextwidget.h"
0007 #include "quicktextmanager.h"
0008 #include "quicktexttreewidget.h"
0009 #include <KLocalizedString>
0010 #include <KMessageBox>
0011 #include <MailCommon/SnippetWidget>
0012 #include <MailCommon/SnippetsModel>
0013 #include <QHBoxLayout>
0014 #include <QStackedWidget>
0015 
0016 QuickTextWidget::QuickTextWidget(QWidget *parent)
0017     : QWidget(parent)
0018 {
0019     auto mainLayout = new QHBoxLayout(this);
0020     mainLayout->setObjectName(QLatin1StringView("mainLayout"));
0021     mainLayout->setContentsMargins({});
0022 
0023     mSnippetsManager = new QuicktextManager(this, this);
0024 
0025     mTreeWidget = new QuicktextTreeWidget(mSnippetsManager, this);
0026     mTreeWidget->setObjectName(QLatin1StringView("treewidget"));
0027     mainLayout->addWidget(mTreeWidget);
0028 
0029     mStackedWidget = new QStackedWidget(this);
0030     mStackedWidget->setObjectName(QLatin1StringView("stackedwidget"));
0031     mainLayout->addWidget(mStackedWidget);
0032 
0033     mSnippetWidget = new MailCommon::SnippetWidget(this);
0034     mSnippetWidget->setObjectName(QLatin1StringView("snippetwidget"));
0035     mStackedWidget->addWidget(mSnippetWidget);
0036 
0037     mEmptyWidget = new QWidget(this);
0038     mStackedWidget->addWidget(mEmptyWidget);
0039 
0040     connect(mTreeWidget, &QuicktextTreeWidget::addSnippet, this, &QuickTextWidget::addSnippet);
0041     connect(mTreeWidget, &QuicktextTreeWidget::editSnippet, this, &QuickTextWidget::editSnippet);
0042     connect(mTreeWidget, &QuicktextTreeWidget::addSnippetGroup, this, &QuickTextWidget::addSnippetGroup);
0043     connect(mTreeWidget, &QuicktextTreeWidget::editSnippetGroup, this, &QuickTextWidget::editSnippetGroup);
0044     connect(mSnippetsManager->selectionModel(), &QItemSelectionModel::selectionChanged, this, [this]() {
0045         save();
0046         if (mSnippetsManager->selectionModel()->selectedIndexes().isEmpty()) {
0047             mStackedWidget->setCurrentWidget(mEmptyWidget);
0048             return;
0049         }
0050         mStackedWidget->setCurrentWidget(mSnippetWidget);
0051         const QModelIndex index = mSnippetsManager->selectionModel()->selectedIndexes().first();
0052         const bool isGroup = index.data(MailCommon::SnippetsModel::IsGroupRole).toBool();
0053         if (isGroup) {
0054             editSnippetGroup();
0055         } else {
0056             editSnippet();
0057         }
0058     });
0059     mStackedWidget->setCurrentWidget(mEmptyWidget);
0060 }
0061 
0062 QuickTextWidget::~QuickTextWidget() = default;
0063 
0064 void QuickTextWidget::save()
0065 {
0066     if (mSnippetWidget->wasChanged()) {
0067         const int answer = KMessageBox::warningTwoActions(this,
0068                                                           i18n("QuickText was changed. Do you want to save it?"),
0069                                                           i18nc("@title:window", "Save"),
0070                                                           KStandardGuiItem::save(),
0071                                                           KStandardGuiItem::discard());
0072         if (answer == KMessageBox::ButtonCode::PrimaryAction) {
0073             switch (mMode) {
0074             case EditMode::AddSnippet:
0075                 saveAddSnippet();
0076                 break;
0077             case EditMode::EditSnippet:
0078                 saveEditSnippet();
0079                 break;
0080             case EditMode::AddGroup:
0081                 saveAddGroup();
0082                 break;
0083             case EditMode::EditGroup:
0084                 saveEditGroup();
0085                 break;
0086             case EditMode::Unknown:
0087                 break;
0088             }
0089         }
0090     }
0091     mMode = EditMode::Unknown;
0092     mSnippetWidget->clear();
0093     mSnippetWidget->setWasChanged(false);
0094 }
0095 
0096 void QuickTextWidget::addSnippet()
0097 {
0098     mStackedWidget->setCurrentWidget(mSnippetWidget);
0099     mMode = EditMode::AddSnippet;
0100     mSnippetWidget->clear();
0101     mSnippetWidget->setGroupSelected(false);
0102     const bool noGroupAvailable = (mSnippetsManager->model()->rowCount() == 0);
0103 
0104     if (noGroupAvailable) {
0105         // create a 'General' snippet group
0106         if (!mSnippetsManager->model()->insertRow(mSnippetsManager->model()->rowCount(), QModelIndex())) {
0107             return;
0108         }
0109 
0110         const QModelIndex groupIndex = mSnippetsManager->model()->index(mSnippetsManager->model()->rowCount() - 1, 0, QModelIndex());
0111         mSnippetsManager->model()->setData(groupIndex, i18n("General"), MailCommon::SnippetsModel::NameRole);
0112 
0113         mSnippetsManager->selectionModel()->select(groupIndex, QItemSelectionModel::ClearAndSelect);
0114         mSnippetWidget->setGroupIndex(mSnippetsManager->currentGroupIndex());
0115         mSnippetWidget->setText(QString());
0116     }
0117     mSnippetWidget->setGroupModel(mSnippetsManager->model());
0118     mSnippetWidget->setWasChanged(false);
0119 }
0120 
0121 void QuickTextWidget::editSnippet()
0122 {
0123     mStackedWidget->setCurrentWidget(mSnippetWidget);
0124     mMode = EditMode::EditSnippet;
0125     mSnippetWidget->clear();
0126     mSnippetWidget->setGroupSelected(false);
0127 
0128     mCurrentGroupIndex = mSnippetsManager->selectionModel()->selectedIndexes().first();
0129     if (!mCurrentGroupIndex.isValid() || mCurrentGroupIndex.data(MailCommon::SnippetsModel::IsGroupRole).toBool()) {
0130         return;
0131     }
0132 
0133     const QModelIndex oldGroupIndex = mSnippetsManager->currentGroupIndex();
0134 
0135     const QString oldSnippetName = mCurrentGroupIndex.data(MailCommon::SnippetsModel::NameRole).toString();
0136     mSnippetWidget->setGroupModel(mSnippetsManager->model());
0137     mSnippetWidget->setGroupIndex(oldGroupIndex);
0138     mSnippetWidget->setName(oldSnippetName);
0139     mSnippetWidget->setTo(mCurrentGroupIndex.data(MailCommon::SnippetsModel::ToRole).toString());
0140     mSnippetWidget->setSubject(mCurrentGroupIndex.data(MailCommon::SnippetsModel::SubjectRole).toString());
0141     mSnippetWidget->setCc(mCurrentGroupIndex.data(MailCommon::SnippetsModel::CcRole).toString());
0142     mSnippetWidget->setBcc(mCurrentGroupIndex.data(MailCommon::SnippetsModel::BccRole).toString());
0143     mSnippetWidget->setText(mCurrentGroupIndex.data(MailCommon::SnippetsModel::TextRole).toString());
0144     mSnippetWidget->setKeyword(mCurrentGroupIndex.data(MailCommon::SnippetsModel::KeywordRole).toString());
0145     mSnippetWidget->setAttachment(mCurrentGroupIndex.data(MailCommon::SnippetsModel::AttachmentRole).toString());
0146     mSnippetWidget->setKeySequence(QKeySequence::fromString(mCurrentGroupIndex.data(MailCommon::SnippetsModel::KeySequenceRole).toString()));
0147     mSnippetWidget->setWasChanged(false);
0148 }
0149 
0150 void QuickTextWidget::addSnippetGroup()
0151 {
0152     mStackedWidget->setCurrentWidget(mSnippetWidget);
0153     mMode = EditMode::AddGroup;
0154     mSnippetWidget->clear();
0155     mSnippetWidget->setGroupSelected(true);
0156     mSnippetWidget->setWasChanged(false);
0157 }
0158 
0159 void QuickTextWidget::editSnippetGroup()
0160 {
0161     mStackedWidget->setCurrentWidget(mSnippetWidget);
0162     mMode = EditMode::EditGroup;
0163     mSnippetWidget->clear();
0164     mCurrentGroupIndex = mSnippetsManager->currentGroupIndex();
0165     if (!mCurrentGroupIndex.isValid() || !mCurrentGroupIndex.data(MailCommon::SnippetsModel::IsGroupRole).toBool()) {
0166         return;
0167     }
0168     mSnippetWidget->setGroupSelected(true);
0169     const QString oldGroupName = mCurrentGroupIndex.data(MailCommon::SnippetsModel::NameRole).toString();
0170     mSnippetWidget->setName(oldGroupName);
0171     mSnippetWidget->setWasChanged(false);
0172 }
0173 
0174 void QuickTextWidget::saveAddSnippet()
0175 {
0176     const QModelIndex groupIndex = mSnippetWidget->groupIndex();
0177 
0178     if (!mSnippetsManager->model()->insertRow(mSnippetsManager->model()->rowCount(groupIndex), groupIndex)) {
0179         return;
0180     }
0181 
0182     const QModelIndex index = mSnippetsManager->model()->index(mSnippetsManager->model()->rowCount(groupIndex) - 1, 0, groupIndex);
0183     saveModelIndex(index);
0184 }
0185 
0186 void QuickTextWidget::saveEditSnippet()
0187 {
0188     const QModelIndex newGroupIndex = mSnippetWidget->groupIndex();
0189     const QModelIndex oldGroupIndex = mSnippetsManager->currentGroupIndex();
0190 
0191     if (oldGroupIndex != newGroupIndex) {
0192         mSnippetsManager->model()->removeRow(mCurrentGroupIndex.row(), oldGroupIndex);
0193         mSnippetsManager->model()->insertRow(mSnippetsManager->model()->rowCount(newGroupIndex), newGroupIndex);
0194 
0195         mCurrentGroupIndex = mSnippetsManager->model()->index(mSnippetsManager->model()->rowCount(newGroupIndex) - 1, 0, newGroupIndex);
0196     }
0197 
0198     saveModelIndex(mCurrentGroupIndex);
0199 }
0200 
0201 void QuickTextWidget::saveModelIndex(const QModelIndex &index)
0202 {
0203     mSnippetsManager->model()->setData(index, mSnippetWidget->name(), MailCommon::SnippetsModel::NameRole);
0204     mSnippetsManager->model()->setData(index, mSnippetWidget->text(), MailCommon::SnippetsModel::TextRole);
0205     mSnippetsManager->model()->setData(index, mSnippetWidget->keySequence().toString(), MailCommon::SnippetsModel::KeySequenceRole);
0206     mSnippetsManager->model()->setData(index, mSnippetWidget->keyword(), MailCommon::SnippetsModel::KeywordRole);
0207     mSnippetsManager->model()->setData(index, mSnippetWidget->subject(), MailCommon::SnippetsModel::SubjectRole);
0208     mSnippetsManager->model()->setData(index, mSnippetWidget->cc(), MailCommon::SnippetsModel::CcRole);
0209     mSnippetsManager->model()->setData(index, mSnippetWidget->bcc(), MailCommon::SnippetsModel::BccRole);
0210     mSnippetsManager->model()->setData(index, mSnippetWidget->to(), MailCommon::SnippetsModel::ToRole);
0211     mSnippetsManager->model()->setData(index, mSnippetWidget->attachment(), MailCommon::SnippetsModel::AttachmentRole);
0212 
0213     mSnippetsManager->save();
0214 }
0215 
0216 void QuickTextWidget::saveAddGroup()
0217 {
0218     if (!mSnippetsManager->model()->insertRow(mSnippetsManager->model()->rowCount(), QModelIndex())) {
0219         return;
0220     }
0221 
0222     const QModelIndex groupIndex = mSnippetsManager->model()->index(mSnippetsManager->model()->rowCount() - 1, 0, QModelIndex());
0223     mSnippetsManager->model()->setData(groupIndex, mSnippetWidget->name(), MailCommon::SnippetsModel::NameRole);
0224     mSnippetsManager->save();
0225 }
0226 
0227 void QuickTextWidget::saveEditGroup()
0228 {
0229     const QString oldGroupName = mCurrentGroupIndex.data(MailCommon::SnippetsModel::NameRole).toString();
0230 
0231     if (oldGroupName == mSnippetWidget->name()) {
0232         return;
0233     }
0234 
0235     mSnippetsManager->model()->setData(mCurrentGroupIndex, mSnippetWidget->name(), MailCommon::SnippetsModel::NameRole);
0236     mSnippetsManager->save();
0237 }
0238 
0239 void QuickTextWidget::importQuickText()
0240 {
0241     mSnippetsManager->importQuickText();
0242 }
0243 
0244 void QuickTextWidget::exportQuickText()
0245 {
0246     mSnippetsManager->exportQuickText();
0247 }
0248 
0249 #include "moc_quicktextwidget.cpp"