7#include "templatelistwidget.h"
10#include "templateeditdialog.h"
13#include <KConfigGroup>
14#include <KLocalizedString>
16#include <KNSWidgets/Action>
17#include <KSharedConfig>
23#include <QListWidgetItem>
29class TemplateListWidgetPrivate
32 TemplateListWidgetPrivate(
const QString &configName, TemplateListWidget *qq)
38 ~TemplateListWidgetPrivate()
43 void createListWidgetItem(
const QString &name,
const QString &text,
bool isDefaultTemplate)
46 item->setData(TemplateListWidget::Text, text);
47 item->setData(TemplateListWidget::DefaultTemplate, isDefaultTemplate);
48 if (isDefaultTemplate) {
58 if (q->addNewTemplate(templateName, templateScript)) {
59 createListWidgetItem(templateName, templateScript,
false);
64 void slotInsertNewTemplate(
const QString &newTemplateScript)
67 QString templateScript = newTemplateScript;
68 if (q->modifyTemplate(templateName, templateScript,
false)) {
69 createListWidgetItem(templateName, templateScript,
false);
77 i18n(
"Do you want to delete selected template?"),
78 i18nc(
"@title:window",
"Delete template"),
81 if (answer == KMessageBox::ButtonCode::PrimaryAction) {
84 if (item->data(TemplateListWidget::DefaultTemplate).toBool() ==
false) {
96 const bool defaultTemplate = item->
data(TemplateListWidget::DefaultTemplate).
toBool();
100 if (q->modifyTemplate(templateName, templateScript, defaultTemplate)) {
101 if (!defaultTemplate) {
103 item->
setData(TemplateListWidget::Text, templateScript);
110 void slotInsertTemplate()
115 Q_EMIT q->insertTemplate(templateScript);
125 for (
int i = 0; i < q->
count(); ++i) {
132 newName = templateName.
arg(i);
137 createListWidgetItem(newName, templateScript,
false);
143 void slotImportTemplates()
148 loadTemplates(&conf);
152 void slotExportTemplates()
157 saveTemplates(&conf);
161 void slotContextMenu(
const QPoint &pos)
164 const bool listSelectedIsEmpty = lstSelectedItems.
isEmpty();
165 auto menu =
new QMenu(q);
167 if (!listSelectedIsEmpty) {
168 menu->addAction(
i18n(
"Insert template"), q, [
this]() {
169 slotInsertTemplate();
171 menu->addSeparator();
174 menu->addAction(
i18n(
"Add..."), q, [
this]() {
177 if (!listSelectedIsEmpty) {
178 const bool defaultTemplate = lstSelectedItems.
first()->data(TemplateListWidget::DefaultTemplate).toBool();
179 if (lstSelectedItems.
count() == 1) {
180 menu->addAction(defaultTemplate ?
i18n(
"Show...") :
i18n(
"Modify..."), q, [
this]() {
187 if (lstSelectedItems.
count() == 1 && !defaultTemplate) {
188 menu->addSeparator();
194 menu->addSeparator();
195 if (q->
count() > 0) {
196 menu->addAction(
i18n(
"Export..."), q, [
this]() {
197 slotExportTemplates();
200 menu->addAction(
i18n(
"Import..."), q, [
this]() {
201 slotImportTemplates();
205 if (!knewstuffConfigName.
isEmpty()) {
206 menu->addSeparator();
208 menu->addAction(fileGHNS);
219 for (
const PimCommon::defaultTemplate &tmp : templatesLst) {
220 createListWidgetItem(tmp.name, tmp.text,
true);
222 KConfigGroup group = config.data()->group(QStringLiteral(
"template"));
223 if (group.
hasKey(QStringLiteral(
"templateCount"))) {
224 loadTemplates(config.data());
229 void loadTemplates(
KConfig *configFile)
232 if (group.
hasKey(QStringLiteral(
"templateCount"))) {
233 const int numberTemplate = group.
readEntry(
"templateCount", 0);
234 for (
int i = 0; i < numberTemplate; ++i) {
235 KConfigGroup templateGroup = configFile->
group(QStringLiteral(
"templateDefine_%1").arg(i));
239 createListWidgetItem(name, text,
false);
246 void saveTemplates(
KConfig *configFile)
250 for (
const QString &group : lst) {
253 int numberOfTemplate = 0;
254 for (
int i = 0; i < q->
count(); ++i) {
256 if (templateItem->
data(TemplateListWidget::DefaultTemplate).
toBool() ==
false) {
257 KConfigGroup group = configFile->
group(QStringLiteral(
"templateDefine_%1").arg(numberOfTemplate));
259 group.
writeEntry(
"Text", templateItem->
data(TemplateListWidget::Text));
264 group.
writeEntry(
"templateCount", numberOfTemplate);
274 saveTemplates(config.data());
280 KSharedConfig::Ptr config;
281 TemplateListWidget *
const q;
284TemplateListWidget::TemplateListWidget(
const QString &configName,
QWidget *parent)
286 , d(new TemplateListWidgetPrivate(configName, this))
291 connect(
this, &TemplateListWidget::customContextMenuRequested,
this, [
this](
QPoint p) {
292 d->slotContextMenu(p);
294 connect(
this, &TemplateListWidget::doubleClicked,
this, [
this]() {
297 connect(
this, &TemplateListWidget::insertNewTemplate,
this, [
this](
const QString &tmp) {
298 d->slotInsertNewTemplate(tmp);
302TemplateListWidget::~TemplateListWidget() =
default;
304void TemplateListWidget::loadTemplates()
316 const QStringList lst{QStringLiteral(
"text/plain")};
327 mimeData->setText(item->
data(TemplateListWidget::Text).
toString());
331bool TemplateListWidget::addNewTemplate(
QString &templateName,
QString &templateScript)
336 templateName = dlg->templateName();
337 templateScript = dlg->script();
344bool TemplateListWidget::modifyTemplate(
QString &templateName,
QString &templateScript,
bool defaultTemplate)
347 dlg->setTemplateName(templateName);
348 dlg->setScript(templateScript);
351 if (!defaultTemplate) {
352 templateName = dlg->templateName();
353 templateScript = dlg->script();
361void TemplateListWidget::dropEvent(
QDropEvent *event)
363 if (
event->source() ==
this) {
367 if (
event->mimeData()->hasText()) {
369 Q_EMIT insertNewTemplate(
event->mimeData()->text());
375void TemplateListWidget::setKNewStuffConfigFile(
const QString &configName)
377 d->knewstuffConfigName = configName;
380void TemplateListWidget::addDefaultTemplate(
const QString &templateName,
const QString &templateScript)
382 d->createListWidgetItem(templateName, templateScript,
true);
386#include "moc_templatelistwidget.cpp"
static Q_INVOKABLE bool authorize(const QString &action)
void deleteGroup(const QString &group, WriteConfigFlags flags=Normal)
KConfigGroup group(const QString &group)
bool hasKey(const char *key) const
void writeEntry(const char *key, const char *value, WriteConfigFlags pFlags=Normal)
QString readEntry(const char *key, const char *aDefault=nullptr) const
QStringList groupList() const override
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
AKONADI_CALENDAR_EXPORT KCalendarCore::Event::Ptr event(const Akonadi::Item &item)
QString name(GameStandardAction id)
void error(QWidget *parent, const QString &text, const QString &title, const KGuiItem &buttonOk, Options options=Notify)
ButtonCode questionTwoActions(QWidget *parent, const QString &text, const QString &title, const KGuiItem &primaryAction, const KGuiItem &secondaryAction, const QString &dontAskAgainName=QString(), Options options=Notify)
QString getOpenFileName(QWidget *parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedFilter, Options options)
QString getSaveFileName(QWidget *parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedFilter, Options options)
QIcon fromTheme(const QString &name)
qsizetype count() const const
bool isEmpty() const const
void setText(const QString &text)
QString text() const const
QString & append(QChar ch)
QString arg(Args &&... args) const const
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
bool isEmpty() const const
void reserve(qsizetype size)
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
bool toBool() const const
QString toString() const const