Messagelib

templatesutil.cpp
1/*
2 SPDX-FileCopyrightText: 2011-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "templatesutil.h"
8#include "templatesutil_p.h"
9
10#include <KConfigGroup>
11#include <KEmailAddress>
12#include <KSharedConfig>
13#include <QRegularExpression>
14#include <QStringList>
15
16using namespace TemplateParser;
17
18void TemplateParser::Util::deleteTemplate(const QString &id)
19{
20 KSharedConfig::Ptr config = KSharedConfig::openConfig(QStringLiteral("templatesconfigurationrc"), KConfig::NoGlobals);
21
22 const QString key = QStringLiteral("Templates #%1").arg(id);
23 if (config->hasGroup(key)) {
24 KConfigGroup group = config->group(key);
25 group.deleteGroup();
26 group.sync();
27 }
28}
29
30QStringList TemplateParser::Util::keywordsWithArgs()
31{
32 const QStringList keywordsWithArgs = QStringList()
33 << QStringLiteral("%REM=\"\"%-") << QStringLiteral("%INSERT=\"\"") << QStringLiteral("%SYSTEM=\"\"") << QStringLiteral("%QUOTEPIPE=\"\"")
34 << QStringLiteral("%MSGPIPE=\"\"") << QStringLiteral("%BODYPIPE=\"\"") << QStringLiteral("%CLEARPIPE=\"\"") << QStringLiteral("%TEXTPIPE=\"\"")
35 << QStringLiteral("%OHEADER=\"\"") << QStringLiteral("%HEADER=\"\"") << QStringLiteral("%DICTIONARYLANGUAGE=\"\"") << QStringLiteral("%LANGUAGE=\"\"");
36 return keywordsWithArgs;
37}
38
39QStringList TemplateParser::Util::keywords()
40{
41 const QStringList keywords =
42 QStringList() << QStringLiteral("%QUOTE") << QStringLiteral("%FORCEDPLAIN") << QStringLiteral("%FORCEDHTML") << QStringLiteral("%QHEADERS")
43 << QStringLiteral("%HEADERS") << QStringLiteral("%TEXT") << QStringLiteral("%OTEXTSIZE") << QStringLiteral("%OTEXT")
44 << QStringLiteral("%OADDRESSEESADDR") << QStringLiteral("%CCADDR") << QStringLiteral("%CCNAME") << QStringLiteral("%CCFNAME")
45 << QStringLiteral("%CCLNAME") << QStringLiteral("%TOADDR") << QStringLiteral("%TONAME") << QStringLiteral("%TOFNAME")
46 << QStringLiteral("%TOLNAME") << QStringLiteral("%TOLIST") << QStringLiteral("%FROMADDR") << QStringLiteral("%FROMNAME")
47 << QStringLiteral("%FROMFNAME") << QStringLiteral("%FROMLNAME") << QStringLiteral("%FULLSUBJECT") << QStringLiteral("%MSGID")
48 << QStringLiteral("%HEADER\\( ") << QStringLiteral("%OCCADDR") << QStringLiteral("%OCCNAME") << QStringLiteral("%OCCFNAME")
49 << QStringLiteral("%OCCLNAME") << QStringLiteral("%OTOADDR") << QStringLiteral("%OTONAME") << QStringLiteral("%OTOFNAME")
50 << QStringLiteral("%OTOLNAME") << QStringLiteral("%OTOLIST") << QStringLiteral("%OTO") << QStringLiteral("%OFROMADDR")
51 << QStringLiteral("%OFROMNAME") << QStringLiteral("%OFROMFNAME") << QStringLiteral("%OFROMLNAME") << QStringLiteral("%OFULLSUBJECT")
52 << QStringLiteral("%OFULLSUBJ") << QStringLiteral("%OMSGID") << QStringLiteral("%DATEEN") << QStringLiteral("%DATESHORT")
53 << QStringLiteral("%DATE") << QStringLiteral("%DOW") << QStringLiteral("%TIMELONGEN") << QStringLiteral("%TIMELONG")
54 << QStringLiteral("%TIME") << QStringLiteral("%ODATEEN") << QStringLiteral("%ODATESHORT") << QStringLiteral("%ODATE")
55 << QStringLiteral("%ODOW") << QStringLiteral("%OTIMELONGEN") << QStringLiteral("%OTIMELONG") << QStringLiteral("%OTIME")
56 << QStringLiteral("%BLANK") << QStringLiteral("%NOP") << QStringLiteral("%CLEAR") << QStringLiteral("%DEBUGOFF")
57 << QStringLiteral("%DEBUG") << QStringLiteral("%CURSOR") << QStringLiteral("%SIGNATURE");
58 return keywords;
59}
60
61QString TemplateParser::Util::getFirstNameFromEmail(const QString &str)
62{
63 // simple logic:
64 // 1. If there is ',' in name, than format is 'Last, First'. If the first name consists
65 // of several words, all parts are returned.
66 // 2. If there is no ',' in the name, the format is 'First Last'. If the first name consists
67 // of several words, there is not way to decided whether the middle names are part of the
68 // the first or the last name, so we return only the first word.
69 // 3. If the display name is empty, return 'name' from 'name@domain'.
70
71 QString res;
74 if (!name.isEmpty()) {
75 // we have a display name, look for a comma
76 int nameLength = name.length();
77 int sep_pos = -1;
78 int i;
79 if ((sep_pos = name.indexOf(QLatin1Char(','))) < 0) {
80 // no comma, start at the beginning of the string and return the first sequence
81 // of non-whitespace characters
82 for (i = 0; i < nameLength; i++) {
83 const QChar c = name.at(i);
84 if (!c.isSpace()) {
85 res.append(c);
86 } else {
87 break;
88 }
89 }
90 } else {
91 // found a comma, first name is everything after that comma
92 res = QStringView(name).mid(sep_pos + 1).trimmed().toString();
93 }
94 } else if (!mail.isEmpty()) {
95 // extract the part of the mail address before the '@'
96 int sep_pos = -1;
97 if ((sep_pos = mail.indexOf(QLatin1Char('@'))) < 0) {
98 // no '@', this should actually never happen, but just in case we return the
99 // full address
100 sep_pos = mail.length();
101 }
102 res = mail.left(sep_pos);
103 }
104
105 return res;
106}
107
108QString TemplateParser::Util::getLastNameFromEmail(const QString &str)
109{
110 // simple logic:
111 // 1. If there is ',' in name, than format is 'Last, First'. If the last name consists
112 // of several words, all parts are returned (i.e. everything before the ',' is returned).
113 // 2. If there is no ',' in the name, the format is 'First Last'. If the last name consists
114 // of several words, there is not way to decided whether the middle names are part of the
115 // the first or the last name, so we return only the last word.
116
117 QString res;
120 if (!name.isEmpty()) {
121 // we have a display name, look for a comma
122 int nameLength = name.length();
123 int sep_pos = -1;
124 int i;
125 if ((sep_pos = name.indexOf(QLatin1Char(','))) < 0) {
126 // no comma, start at the end of the string and return the last sequence
127 // of non-whitespace characters
128 for (i = nameLength - 1; i >= 0; i--) {
129 const QChar c = name.at(i);
130 if (!c.isSpace()) {
131 res.prepend(c);
132 } else {
133 break;
134 }
135 }
136 } else {
137 // found a comma, last name is everything before that comma
138 res = QStringView(name).left(sep_pos).trimmed().toString();
139 }
140 }
141
142 return res;
143}
144
145QString TemplateParser::Util::removeSpaceAtBegin(const QString &selection)
146{
147 QString content = selection;
148 // Remove blank lines at the beginning:
149 const int firstNonWS = content.indexOf(QRegularExpression(QStringLiteral("\\S")));
150 const int lineStart = content.lastIndexOf(QLatin1Char('\n'), firstNonWS);
151 if (lineStart >= 0) {
152 content.remove(0, lineStart);
153 }
154 return content;
155}
KConfigGroup group(const QString &group)
void deleteGroup(const QString &group, WriteConfigFlags flags=Normal)
bool sync() override
static KSharedConfig::Ptr openConfig(const QString &fileName=QString(), OpenFlags mode=FullConfig, QStandardPaths::StandardLocation type=QStandardPaths::GenericConfigLocation)
KCODECS_EXPORT bool extractEmailAddressAndName(const QString &aStr, QString &mail, QString &name)
QString name(GameStandardAction id)
QAction * mail(const QObject *recvr, const char *slot, QObject *parent)
bool isSpace(char32_t ucs4)
QString & append(QChar ch)
QString arg(Args &&... args) const const
const QChar at(qsizetype position) const const
qsizetype indexOf(QChar ch, qsizetype from, Qt::CaseSensitivity cs) const const
bool isEmpty() const const
qsizetype lastIndexOf(QChar ch, Qt::CaseSensitivity cs) const const
qsizetype length() const const
QString & prepend(QChar ch)
QString & remove(QChar ch, Qt::CaseSensitivity cs)
QStringView left(qsizetype length) const const
QStringView mid(qsizetype start, qsizetype length) const const
QString toString() const const
QStringView trimmed() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 29 2024 11:50:55 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.