File indexing completed on 2025-01-19 04:46:40

0001 /*
0002    SPDX-FileCopyrightText: 2019-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "quicktextmanager.h"
0008 #include <KSharedConfig>
0009 #include <MailCommon/SnippetsModel>
0010 
0011 #include <KLocalizedString>
0012 #include <QFileDialog>
0013 
0014 #include <QItemSelectionModel>
0015 
0016 QModelIndex QuicktextManager::currentGroupIndex() const
0017 {
0018     if (mSelectionModel->selectedIndexes().isEmpty()) {
0019         return {};
0020     }
0021 
0022     const QModelIndex index = mSelectionModel->selectedIndexes().first();
0023     if (index.data(MailCommon::SnippetsModel::IsGroupRole).toBool()) {
0024         return index;
0025     } else {
0026         return mModel->parent(index);
0027     }
0028 }
0029 
0030 void QuicktextManager::save()
0031 {
0032     MailCommon::SnippetsModel::instance()->save();
0033 }
0034 
0035 QuicktextManager::QuicktextManager(QObject *parent, QWidget *parentWidget)
0036     : QObject(parent)
0037     , mParent(parentWidget)
0038 {
0039     mModel = MailCommon::SnippetsModel::instance();
0040     mSelectionModel = new QItemSelectionModel(mModel);
0041 }
0042 
0043 QuicktextManager::~QuicktextManager()
0044 {
0045     save();
0046 }
0047 
0048 QAbstractItemModel *QuicktextManager::model() const
0049 {
0050     return mModel;
0051 }
0052 
0053 QItemSelectionModel *QuicktextManager::selectionModel() const
0054 {
0055     return mSelectionModel;
0056 }
0057 
0058 bool QuicktextManager::snippetGroupSelected() const
0059 {
0060     if (mSelectionModel->selectedIndexes().isEmpty()) {
0061         return false;
0062     }
0063 
0064     return mSelectionModel->selectedIndexes().first().data(MailCommon::SnippetsModel::IsGroupRole).toBool();
0065 }
0066 
0067 QString QuicktextManager::selectedName() const
0068 {
0069     if (mSelectionModel->selectedIndexes().isEmpty()) {
0070         return {};
0071     }
0072 
0073     return mSelectionModel->selectedIndexes().first().data(MailCommon::SnippetsModel::NameRole).toString();
0074 }
0075 
0076 void QuicktextManager::importQuickText()
0077 {
0078     const QString filename = QFileDialog::getOpenFileName(mParent, i18n("Import QuickText"));
0079     if (!filename.isEmpty()) {
0080         mModel->load(filename);
0081     }
0082 }
0083 
0084 void QuicktextManager::exportQuickText()
0085 {
0086     const QString filename = QFileDialog::getSaveFileName(mParent, i18n("Export QuickText"));
0087     if (!filename.isEmpty()) {
0088         mModel->save(filename);
0089     }
0090 }
0091 
0092 #include "moc_quicktextmanager.cpp"