File indexing completed on 2024-06-23 05:18:38

0001 /*
0002    SPDX-FileCopyrightText: 2019-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "convertsnippetvariablesjob.h"
0008 #include "composer/composerviewinterface.h"
0009 #include "snippet/convertsnippetvariablesutil.h"
0010 #include <KEmailAddress>
0011 #include <KMime/Types>
0012 #include <QDebug>
0013 #include <TemplateParser/TemplatesUtil>
0014 using namespace MessageComposer;
0015 ConvertSnippetVariablesJob::ConvertSnippetVariablesJob(QObject *parent)
0016     : QObject(parent)
0017 {
0018 }
0019 
0020 ConvertSnippetVariablesJob::~ConvertSnippetVariablesJob()
0021 {
0022     delete mComposerViewInterface;
0023 }
0024 
0025 void ConvertSnippetVariablesJob::setText(const QString &str)
0026 {
0027     mText = str;
0028 }
0029 
0030 bool ConvertSnippetVariablesJob::canStart() const
0031 {
0032     if (mText.isEmpty() || !mComposerViewInterface) {
0033         return false;
0034     }
0035     return true;
0036 }
0037 
0038 void ConvertSnippetVariablesJob::start()
0039 {
0040     if (!canStart()) {
0041         Q_EMIT textConverted(QString());
0042         deleteLater();
0043         return;
0044     }
0045     Q_EMIT textConverted(convertVariables(mComposerViewInterface, mText));
0046     deleteLater();
0047 }
0048 
0049 QString ConvertSnippetVariablesJob::text() const
0050 {
0051     return mText;
0052 }
0053 
0054 MessageComposer::ComposerViewInterface *ConvertSnippetVariablesJob::composerViewInterface() const
0055 {
0056     return mComposerViewInterface;
0057 }
0058 
0059 void ConvertSnippetVariablesJob::setComposerViewInterface(MessageComposer::ComposerViewInterface *composerViewInterface)
0060 {
0061     mComposerViewInterface = composerViewInterface;
0062 }
0063 
0064 QString ConvertSnippetVariablesJob::convertVariables(const QString &cmd, int &i, QChar c)
0065 {
0066     QString result;
0067     if (cmd.startsWith(QLatin1StringView("LASTYEAR"))) {
0068         i += strlen("LASTYEAR");
0069         result.append(MessageComposer::ConvertSnippetVariablesUtil::lastYear());
0070     } else if (cmd.startsWith(QLatin1StringView("NEXTYEAR"))) {
0071         i += strlen("NEXTYEAR");
0072         result.append(MessageComposer::ConvertSnippetVariablesUtil::nextYear());
0073     } else if (cmd.startsWith(QLatin1StringView("MONTHNUMBER"))) {
0074         i += strlen("MONTHNUMBER");
0075         result.append(MessageComposer::ConvertSnippetVariablesUtil::monthNumber());
0076     } else if (cmd.startsWith(QLatin1StringView("DAYOFMONTH"))) {
0077         i += strlen("DAYOFMONTH");
0078         result.append(MessageComposer::ConvertSnippetVariablesUtil::dayOfMonth());
0079     } else if (cmd.startsWith(QLatin1StringView("WEEKNUMBER"))) {
0080         i += strlen("WEEKNUMBER");
0081         result.append(MessageComposer::ConvertSnippetVariablesUtil::weekNumber());
0082     } else if (cmd.startsWith(QLatin1StringView("MONTHNAMESHORT"))) {
0083         i += strlen("MONTHNAMESHORT");
0084         result.append(MessageComposer::ConvertSnippetVariablesUtil::monthNameShort());
0085     } else if (cmd.startsWith(QLatin1StringView("MONTHNAMELONG"))) {
0086         i += strlen("MONTHNAMELONG");
0087         result.append(MessageComposer::ConvertSnippetVariablesUtil::monthNameLong());
0088     } else if (cmd.startsWith(QLatin1StringView("DAYOFWEEKNAMESHORT"))) {
0089         i += strlen("DAYOFWEEKNAMESHORT");
0090         result.append(MessageComposer::ConvertSnippetVariablesUtil::dayOfWeekNameShort());
0091     } else if (cmd.startsWith(QLatin1StringView("DAYOFWEEKNAMELONG"))) {
0092         i += strlen("DAYOFWEEKNAMELONG");
0093         result.append(MessageComposer::ConvertSnippetVariablesUtil::dayOfWeekNameLong());
0094     } else if (cmd.startsWith(QLatin1StringView("YEARLASTMONTH"))) {
0095         i += strlen("YEARLASTMONTH");
0096         result.append(MessageComposer::ConvertSnippetVariablesUtil::yearLastMonth());
0097     } else if (cmd.startsWith(QLatin1StringView("YEAR"))) {
0098         i += strlen("YEAR");
0099         result.append(MessageComposer::ConvertSnippetVariablesUtil::year());
0100     } else if (cmd.startsWith(QLatin1StringView("DAYOFWEEK"))) {
0101         i += strlen("DAYOFWEEK");
0102         result.append(MessageComposer::ConvertSnippetVariablesUtil::dayOfWeek());
0103     } else if (cmd.startsWith(QLatin1StringView("LASTMONTHNAMELONG"))) {
0104         i += strlen("LASTMONTHNAMELONG");
0105         result.append(MessageComposer::ConvertSnippetVariablesUtil::lastMonthNameLong());
0106     } else {
0107         result.append(c);
0108     }
0109     return result;
0110 }
0111 
0112 QString ConvertSnippetVariablesJob::convertVariables(MessageComposer::ComposerViewInterface *composerView, const QString &text)
0113 {
0114     QString result;
0115     const int tmpl_len = text.length();
0116     for (int i = 0; i < tmpl_len; ++i) {
0117         const QChar c = text[i];
0118         if (c == QLatin1Char('%')) {
0119             const QString cmd = text.mid(i + 1);
0120             if (composerView) {
0121                 if (cmd.startsWith(QLatin1StringView("CCADDR"))) {
0122                     i += strlen("CCADDR");
0123                     const QString str = composerView->cc();
0124                     result.append(str);
0125                 } else if (cmd.startsWith(QLatin1StringView("CCFNAME"))) {
0126                     i += strlen("CCFNAME");
0127                     const QString str = getFirstNameFromEmail(composerView->cc());
0128                     result.append(str);
0129                 } else if (cmd.startsWith(QLatin1StringView("CCLNAME"))) {
0130                     i += strlen("CCLNAME");
0131                     const QString str = getLastNameFromEmail(composerView->cc());
0132                     result.append(str);
0133                 } else if (cmd.startsWith(QLatin1StringView("CCNAME"))) {
0134                     i += strlen("CCNAME");
0135                     const QString str = getNameFromEmail(composerView->cc());
0136                     result.append(str);
0137                 } else if (cmd.startsWith(QLatin1StringView("BCCADDR"))) {
0138                     i += strlen("BCCADDR");
0139                     const QString str = composerView->bcc();
0140                     result.append(str);
0141                 } else if (cmd.startsWith(QLatin1StringView("BCCFNAME"))) {
0142                     i += strlen("BCCFNAME");
0143                     const QString str = getFirstNameFromEmail(composerView->bcc());
0144                     result.append(str);
0145                 } else if (cmd.startsWith(QLatin1StringView("BCCLNAME"))) {
0146                     i += strlen("BCCLNAME");
0147                     const QString str = getLastNameFromEmail(composerView->bcc());
0148                     result.append(str);
0149                 } else if (cmd.startsWith(QLatin1StringView("BCCNAME"))) {
0150                     i += strlen("BCCNAME");
0151                     const QString str = getNameFromEmail(composerView->bcc());
0152                     result.append(str);
0153                 } else if (cmd.startsWith(QLatin1StringView("FULLSUBJECT"))) {
0154                     i += strlen("FULLSUBJECT");
0155                     const QString str = composerView->subject();
0156                     result.append(str);
0157                 } else if (cmd.startsWith(QLatin1StringView("TOADDR"))) {
0158                     i += strlen("TOADDR");
0159                     const QString str = composerView->to();
0160                     result.append(str);
0161                 } else if (cmd.startsWith(QLatin1StringView("TOFNAME"))) {
0162                     i += strlen("TOFNAME");
0163                     const QString str = getFirstNameFromEmail(composerView->to());
0164                     result.append(str);
0165                 } else if (cmd.startsWith(QLatin1StringView("TOLNAME"))) {
0166                     i += strlen("TOLNAME");
0167                     const QString str = getLastNameFromEmail(composerView->to());
0168                     result.append(str);
0169                 } else if (cmd.startsWith(QLatin1StringView("TONAME"))) {
0170                     i += strlen("TONAME");
0171                     const QString str = getNameFromEmail(composerView->to());
0172                     result.append(str);
0173                 } else if (cmd.startsWith(QLatin1StringView("FROMADDR"))) {
0174                     i += strlen("FROMADDR");
0175                     const QString str = composerView->from();
0176                     result.append(str);
0177                 } else if (cmd.startsWith(QLatin1StringView("FROMFNAME"))) {
0178                     i += strlen("FROMFNAME");
0179                     const QString str = getFirstNameFromEmail(composerView->from());
0180                     result.append(str);
0181                 } else if (cmd.startsWith(QLatin1StringView("FROMLNAME"))) {
0182                     i += strlen("FROMLNAME");
0183                     const QString str = getLastNameFromEmail(composerView->from());
0184                     result.append(str);
0185                 } else if (cmd.startsWith(QLatin1StringView("FROMNAME"))) {
0186                     i += strlen("FROMNAME");
0187                     const QString str = getNameFromEmail(composerView->from());
0188                     result.append(str);
0189                 } else if (cmd.startsWith(QLatin1StringView("DOW"))) {
0190                     i += strlen("DOW");
0191                     const QString str = composerView->insertDayOfWeek();
0192                     result.append(str);
0193                 } else if (cmd.startsWith(QLatin1StringView("DATE"))) {
0194                     i += strlen("DATE");
0195                     const QString str = composerView->longDate();
0196                     result.append(str);
0197                 } else if (cmd.startsWith(QLatin1StringView("SHORTDATE"))) {
0198                     i += strlen("SHORTDATE");
0199                     const QString str = composerView->shortDate();
0200                     result.append(str);
0201                 } else if (cmd.startsWith(QLatin1StringView("TIME"))) {
0202                     i += strlen("TIME");
0203                     const QString str = composerView->shortTime();
0204                     result.append(str);
0205                 } else if (cmd.startsWith(QLatin1StringView("TIMELONG"))) {
0206                     i += strlen("TIMELONG");
0207                     const QString str = composerView->longTime();
0208                     result.append(str);
0209                 } else if (cmd.startsWith(QLatin1StringView("ATTACHMENTCOUNT"))) {
0210                     i += strlen("ATTACHMENTCOUNT");
0211                     const QString str = QString::number(composerView->attachments().count());
0212                     result.append(str);
0213                 } else if (cmd.startsWith(QLatin1StringView("ATTACHMENTNAMES"))) {
0214                     i += strlen("ATTACHMENTNAMES");
0215                     const QString str = composerView->attachments().names().join(QLatin1Char(','));
0216                     result.append(str);
0217                 } else if (cmd.startsWith(QLatin1StringView("ATTACHMENTFILENAMES"))) {
0218                     i += strlen("ATTACHMENTFILENAMES");
0219                     const QString str = composerView->attachments().fileNames().join(QLatin1Char(','));
0220                     result.append(str);
0221                 } else if (cmd.startsWith(QLatin1StringView("ATTACHMENTNAMESANDSIZES"))) {
0222                     i += strlen("ATTACHMENTNAMESANDSIZES");
0223                     const QString str = composerView->attachments().namesAndSize().join(QLatin1Char(','));
0224                     result.append(str);
0225                 } else {
0226                     result.append(convertVariables(cmd, i, c));
0227                 }
0228             } else {
0229                 result.append(convertVariables(cmd, i, c));
0230             }
0231         } else {
0232             result.append(c);
0233         }
0234     }
0235     return result;
0236 }
0237 
0238 QString ConvertSnippetVariablesJob::getNameFromEmail(const QString &address)
0239 {
0240     const QStringList lst = address.split(QStringLiteral(", "));
0241     QStringList resultName;
0242     for (const QString &str : lst) {
0243         KMime::Types::Mailbox mailBoxAddress;
0244         mailBoxAddress.fromUnicodeString(KEmailAddress::normalizeAddressesAndEncodeIdn(str));
0245         const QString firstName = mailBoxAddress.name();
0246         if (!firstName.isEmpty()) {
0247             resultName << firstName;
0248         }
0249     }
0250 
0251     const QString str = resultName.isEmpty() ? QString() : resultName.join(QStringLiteral(", "));
0252     return str;
0253 }
0254 
0255 QString ConvertSnippetVariablesJob::getFirstNameFromEmail(const QString &address)
0256 {
0257     const QStringList lst = KEmailAddress::splitAddressList(address);
0258     QStringList resultName;
0259     for (const QString &str : lst) {
0260         KMime::Types::Mailbox mailBoxAddress;
0261         mailBoxAddress.fromUnicodeString(KEmailAddress::normalizeAddressesAndEncodeIdn(str));
0262         const QString firstName = TemplateParser::Util::getFirstNameFromEmail(mailBoxAddress.name());
0263         if (!firstName.isEmpty()) {
0264             resultName << firstName;
0265         }
0266     }
0267 
0268     const QString str = resultName.isEmpty() ? QString() : resultName.join(QStringLiteral(", "));
0269     return str;
0270 }
0271 
0272 QString ConvertSnippetVariablesJob::getLastNameFromEmail(const QString &address)
0273 {
0274     const QStringList lst = KEmailAddress::splitAddressList(address);
0275     QStringList resultName;
0276     for (const QString &str : lst) {
0277         KMime::Types::Mailbox newAddress;
0278         newAddress.fromUnicodeString(KEmailAddress::normalizeAddressesAndEncodeIdn(str));
0279         qDebug() << "newAddress.name()  " << newAddress.name();
0280         const QString lastName = TemplateParser::Util::getLastNameFromEmail(newAddress.name());
0281         if (!lastName.isEmpty()) {
0282             resultName << lastName;
0283         }
0284     }
0285 
0286     const QString str = resultName.isEmpty() ? QString() : resultName.join(QStringLiteral(", "));
0287     return str;
0288 }
0289 
0290 #include "moc_convertsnippetvariablesjob.cpp"