File indexing completed on 2023-05-30 09:17:32

0001 /**
0002  * SPDX-FileCopyrightText: 2019 Simon Redman <simon@ergotech.com>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005  */
0006 
0007 #ifndef SMSHELPER_H
0008 #define SMSHELPER_H
0009 
0010 #include <QIcon>
0011 #include <QJSEngine>
0012 #include <QQmlEngine>
0013 #include <QSharedPointer>
0014 
0015 #include <KPeople/PersonData>
0016 
0017 #include "interfaces/conversationmessage.h"
0018 
0019 #include "smsapp/smscharcount.h"
0020 
0021 class PersonsCache;
0022 
0023 class SmsHelper : public QObject
0024 {
0025     Q_OBJECT
0026 public:
0027     SmsHelper() = default;
0028     ~SmsHelper() override = default;
0029 
0030     static QObject *singletonProvider(QQmlEngine *engine, QJSEngine *scriptEngine);
0031 
0032     enum CountryCode {
0033         Australia,
0034         CzechRepublic,
0035         Other, // I only care about a few country codes
0036     };
0037 
0038     /**
0039      * Return true to indicate the two phone numbers should be considered the same, false otherwise
0040      */
0041     Q_INVOKABLE static bool isPhoneNumberMatch(const QString &phone1, const QString &phone2);
0042 
0043     /**
0044      * Return true to indicate the two phone numbers should be considered the same, false otherwise
0045      * Requires canonicalized phone numbers as inputs
0046      */
0047     Q_INVOKABLE static bool isPhoneNumberMatchCanonicalized(const QString &canonicalPhone1, const QString &canonicalPhone2);
0048 
0049     /**
0050      * See inline comments for how short codes are determined
0051      * All information from https://en.wikipedia.org/wiki/Short_code
0052      */
0053     Q_INVOKABLE static bool isShortCode(const QString &canonicalNumber, const CountryCode &country);
0054 
0055     /**
0056      * Try to guess the country code from the passed number
0057      */
0058     static CountryCode determineCountryCode(const QString &canonicalNumber);
0059 
0060     /**
0061      * Simplify a phone number to a known form
0062      */
0063     Q_INVOKABLE static QString canonicalizePhoneNumber(const QString &phoneNumber);
0064 
0065     /**
0066      * Get the data for a particular person given their contact address
0067      */
0068     Q_INVOKABLE static QSharedPointer<KPeople::PersonData> lookupPersonByAddress(const QString &address);
0069 
0070     /**
0071      * Make an icon which combines the many icons
0072      *
0073      * This mimics what Android does:
0074      * If there is only one icon, use that one
0075      * If there are two icons, put one in the top-left corner and one in the bottom right
0076      * If there are three, put one in the middle of the top and the remaining two in the bottom
0077      * If there are four or more, put one in each corner (If more than four, some will be left out)
0078      */
0079     Q_INVOKABLE static QIcon combineIcons(const QList<QPixmap> &icons);
0080 
0081     /**
0082      * Get a combination of all the addresses as a comma-separated list of:
0083      *  - The KPeople contact's name (if known)
0084      *  - The address (if the contact is not known)
0085      */
0086     Q_INVOKABLE static QString getTitleForAddresses(const QList<ConversationAddress> &addresses);
0087 
0088     /**
0089      * Get a combined icon for all contacts by finding:
0090      *  - The KPeople contact's icon (if known)
0091      *  - A generic icon
0092      * and then using SmsHelper::combineIcons
0093      */
0094     Q_INVOKABLE static QIcon getIconForAddresses(const QList<ConversationAddress> &addresses);
0095 
0096     /**
0097      * Put the specified text into the system clipboard
0098      */
0099     Q_INVOKABLE static void copyToClipboard(const QString &text);
0100 
0101     /**
0102      * Get the data for all persons currently stored on device
0103      */
0104     static QList<QSharedPointer<KPeople::PersonData>> getAllPersons();
0105 
0106     /**
0107      * Get SMS character count status of SMS. It contains number of remaining characters
0108      * in current SMS (automatically selects 7-bit, 8-bit or 16-bit mode), octet count and
0109      * number of messages in concatenated SMS.
0110      */
0111     Q_INVOKABLE static SmsCharCount getCharCount(const QString &message);
0112 
0113     /**
0114      * Used to validate arbitrary phone number entered by the user
0115      */
0116     Q_INVOKABLE static bool isAddressValid(const QString &address);
0117 
0118     /**
0119      * Return the total size of the message
0120      */
0121     Q_INVOKABLE static quint64 totalMessageSize(const QList<QUrl> &urls, const QString &text);
0122 
0123     /**
0124      * Gets a thumbnail for the given attachment
0125      */
0126     Q_INVOKABLE static QIcon getThumbnailForAttachment(const Attachment &attachment);
0127 
0128 private:
0129     static bool isInGsmAlphabet(const QChar &ch);
0130     static bool isInGsmAlphabetExtension(const QChar &ch);
0131 };
0132 
0133 #endif // SMSHELPER_H