KTextAddons

textautogeneratemanager.cpp
1/*
2SPDX-FileCopyrightText: 2025 Laurent Montel <montel@kde.org>
3
4SPDX-License-Identifier: GPL-2.0-or-later
5*/
6#include "textautogeneratemanager.h"
7#include "core/textautogeneratechatmodel.h"
8#include "core/textautogenerateengineutil.h"
9
10#include <KConfigGroup>
11#include <QRegularExpression>
12
13using namespace TextAutogenerateText;
14TextAutogenerateManager::TextAutogenerateManager(QObject *parent)
15 : QObject{parent}
16 , mTextAutoGenerateChatModel(new TextAutoGenerateChatModel(this))
17{
18}
19
20TextAutogenerateManager::~TextAutogenerateManager() = default;
21
22TextAutogenerateManager *TextAutogenerateManager::self()
23{
24 static TextAutogenerateManager s_self;
25 return &s_self;
26}
27
28void TextAutogenerateManager::ask(const QString &msg)
29{
30 Q_EMIT sendMessageRequested(msg);
31}
32
33TextAutoGenerateChatModel *TextAutogenerateManager::textAutoGenerateChatModel() const
34{
35 return mTextAutoGenerateChatModel;
36}
37
38void TextAutogenerateManager::filterListMessages(QList<TextAutoGenerateMessage> &messages, bool archived) const
39{
40 std::sort(messages.begin(), messages.end(), [archived](const TextAutoGenerateMessage &left, const TextAutoGenerateMessage &right) {
41 /*
42 if (archived) {
43 if (!left.archived() || !right.archived()) {
44 return false;
45 }
46 } else {
47 if (left.archived() || right.archived()) {
48 return false;
49 }
50 }
51 */
52 if (left.dateTime() == right.dateTime()) {
53 if (left.sender() == TextAutoGenerateMessage::Sender::User) {
54 return true;
55 }
56 }
57 return left.dateTime() < right.dateTime();
58 });
59}
60
61bool TextAutogenerateManager::showArchived() const
62{
63 return mShowArchived;
64}
65
66void TextAutogenerateManager::setShowArchived(bool newShowArchived)
67{
68 mShowArchived = newShowArchived;
69}
70
71void TextAutogenerateManager::loadHistory()
72{
73 QList<TextAutoGenerateMessage> messages;
74 KSharedConfig::Ptr config;
75 const QStringList keyGroups = keyRecorderList(config);
76
77 for (const QString &groupName : keyGroups) {
78 KConfigGroup group = config->group(groupName);
79 TextAutoGenerateMessage message;
80
81 message.setContent(group.readEntry(QStringLiteral("Content")));
82 const int sender = group.readEntry(QStringLiteral("Sender"), 0);
83 message.setSender(static_cast<TextAutoGenerateMessage::Sender>(sender));
84 message.setDateTime(group.readEntry(QStringLiteral("DateTime"), 0));
85 message.setUuid(group.readEntry(QStringLiteral("Uuid"), QByteArray()));
86 message.setAnswerUuid(group.readEntry(QStringLiteral("AnswerUuid"), QByteArray()));
87 message.setTopic(group.readEntry(QStringLiteral("Topic"), QString()));
88 message.setArchived(group.readEntry(QStringLiteral("Archived"), false));
89
90 messages.append(std::move(message));
91 }
92 filterListMessages(messages, mShowArchived);
93 mTextAutoGenerateChatModel->setMessages(messages);
94}
95
96void TextAutogenerateManager::saveHistory()
97{
98 const QList<TextAutoGenerateMessage> messages = mTextAutoGenerateChatModel->messages();
99 KSharedConfig::Ptr config;
100 const QStringList filterGroups = keyRecorderList(config);
101
102 for (const QString &group : filterGroups) {
103 config->deleteGroup(group);
104 }
105 for (int i = 0, total = messages.count(); i < total; ++i) {
106 const QString groupName = QStringLiteral("AutoGenerate #%1").arg(i);
107 KConfigGroup group = config->group(groupName);
108 const TextAutoGenerateMessage &info = messages.at(i);
109 group.writeEntry(QStringLiteral("Content"), info.content());
110 group.writeEntry(QStringLiteral("Sender"), static_cast<int>(info.sender()));
111 group.writeEntry(QStringLiteral("DateTime"), info.dateTime());
112 group.writeEntry(QStringLiteral("Uuid"), info.uuid());
113 group.writeEntry(QStringLiteral("AnswerUuid"), info.answerUuid());
114 group.writeEntry(QStringLiteral("Topic"), info.topic());
115 group.writeEntry(QStringLiteral("Archived"), info.archived());
116 }
117 config->sync();
118}
119
120QStringList TextAutogenerateManager::keyRecorderList(KSharedConfig::Ptr &config) const
121{
122 config = KSharedConfig::openConfig(TextAutogenerateEngineUtil::defaultConfigFileName(), KConfig::NoGlobals);
123 const QStringList keyGroups = config->groupList().filter(QRegularExpression(QStringLiteral("AutoGenerate #\\d+")));
124 return keyGroups;
125}
126
127#include "moc_textautogeneratemanager.cpp"
KConfigGroup group(const QString &group)
void writeEntry(const char *key, const char *value, WriteConfigFlags pFlags=Normal)
QString readEntry(const char *key, const char *aDefault=nullptr) const
static KSharedConfig::Ptr openConfig(const QString &fileName=QString(), OpenFlags mode=FullConfig, QStandardPaths::StandardLocation type=QStandardPaths::GenericConfigLocation)
void append(QList< T > &&value)
const_reference at(qsizetype i) const const
iterator begin()
qsizetype count() const const
iterator end()
Q_EMITQ_EMIT
QObject * sender() const const
QString arg(Args &&... args) const const
QTextStream & left(QTextStream &stream)
QTextStream & right(QTextStream &stream)
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri May 2 2025 12:06:03 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.