kdeconnect-kde/smsapp/smshelper.h

134 lines
4.2 KiB
C
Raw Normal View History

/**
* SPDX-FileCopyrightText: 2019 Simon Redman <simon@ergotech.com>
*
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
*/
#ifndef SMSHELPER_H
#define SMSHELPER_H
#include <QIcon>
2020-05-09 17:50:08 +01:00
#include <QJSEngine>
#include <QQmlEngine>
#include <QSharedPointer>
2019-07-22 12:40:33 +01:00
#include <KPeople/PersonData>
#include "interfaces/conversationmessage.h"
Show remaining character count in SMS app ## Summary This adds character counter below the “Send” button in SMS conversation. It uses format XXX/Y where XXX is number of characters that can be added without splitting the SMS into multiple messages (see article Concatenated SMS on Wikipedia). Y is number of messages in in current concatenated SMS. The counter is not visible when insertion of 10 7-bit or 16-bit (depends on SMS encoding) does not create concatenated SMS. SMS encoding is automatically guessed. 8-bit encodings are not supported. If the message contains characters that are not supported by GSM 7-bit encoding, counter automatically switches to UCS-2. ## Test Plan Try entering some text that is longer than 150 characters in [GSM 03.38 encoding][1] or 60 characters in UCS-2. Number of remaining characters should be visible below the “Send” button. The character counter should show `0` at exactly 160 or 70 characters. Inserting one character should switch the counter to [Concatenated SMS][2] mode when number of messages is shown. It should show exactly same number as SMS app in Android. ## Screenshots These images are in APNG. ![grab.apng](/uploads/21ae23f2fa75c7aca487e61ddce94644/grab.apng) ![grab-cz.apng](/uploads/785e670a8598c5a65a4209f17e75f578/grab-cz.apng) [1]: https://en.wikipedia.org/w/index.php?oldid=932080074#GSM_7-bit_default_alphabet_and_extension_table_of_3GPP_TS_23.038_/_GSM_03.38 [2]: https://en.wikipedia.org/w/index.php?oldid=943185255#Message_size
2020-03-22 18:47:12 +00:00
#include "smsapp/smscharcount.h"
class PersonsCache;
class SmsHelper : public QObject
{
2020-05-09 17:50:08 +01:00
Q_OBJECT
public:
2020-05-09 17:50:08 +01:00
SmsHelper() = default;
2021-10-27 05:53:06 +01:00
~SmsHelper() override = default;
2020-05-09 17:50:08 +01:00
static QObject* singletonProvider(QQmlEngine *engine, QJSEngine *scriptEngine);
enum CountryCode {
Australia,
CzechRepublic,
Other, // I only care about a few country codes
};
/**
* Return true to indicate the two phone numbers should be considered the same, false otherwise
*/
2020-05-09 17:50:08 +01:00
Q_INVOKABLE static bool isPhoneNumberMatch(const QString& phone1, const QString& phone2);
/**
* Return true to indicate the two phone numbers should be considered the same, false otherwise
* Requires canonicalized phone numbers as inputs
*/
2020-05-09 17:50:08 +01:00
Q_INVOKABLE static bool isPhoneNumberMatchCanonicalized(const QString& canonicalPhone1, const QString& canonicalPhone2);
/**
* See inline comments for how short codes are determined
* All information from https://en.wikipedia.org/wiki/Short_code
*/
2020-05-09 17:50:08 +01:00
Q_INVOKABLE static bool isShortCode(const QString& canonicalNumber, const CountryCode& country);
/**
* Try to guess the country code from the passed number
*/
static CountryCode determineCountryCode(const QString& canonicalNumber);
/**
* Simplify a phone number to a known form
*/
2020-05-09 17:50:08 +01:00
Q_INVOKABLE static QString canonicalizePhoneNumber(const QString& phoneNumber);
/**
* Get the data for a particular person given their contact address
*/
2020-05-09 17:50:08 +01:00
Q_INVOKABLE static QSharedPointer<KPeople::PersonData> lookupPersonByAddress(const QString& address);
/**
* Make an icon which combines the many icons
*
* This mimics what Android does:
* If there is only one icon, use that one
* If there are two icons, put one in the top-left corner and one in the bottom right
* If there are three, put one in the middle of the top and the remaining two in the bottom
* If there are four or more, put one in each corner (If more than four, some will be left out)
*/
2020-05-09 17:50:08 +01:00
Q_INVOKABLE static QIcon combineIcons(const QList<QPixmap>& icons);
/**
* Get a combination of all the addresses as a comma-separated list of:
* - The KPeople contact's name (if known)
* - The address (if the contact is not known)
*/
2020-05-09 17:50:08 +01:00
Q_INVOKABLE static QString getTitleForAddresses(const QList<ConversationAddress>& addresses);
/**
* Get a combined icon for all contacts by finding:
* - The KPeople contact's icon (if known)
* - A generic icon
* and then using SmsHelper::combineIcons
*/
2020-05-09 17:50:08 +01:00
Q_INVOKABLE static QIcon getIconForAddresses(const QList<ConversationAddress>& addresses);
/**
* Put the specified text into the system clipboard
*/
Q_INVOKABLE static void copyToClipboard(const QString& text);
/**
* Get the data for all persons currently stored on device
*/
static QList<QSharedPointer<KPeople::PersonData>> getAllPersons();
2020-05-09 17:50:08 +01:00
Show remaining character count in SMS app ## Summary This adds character counter below the “Send” button in SMS conversation. It uses format XXX/Y where XXX is number of characters that can be added without splitting the SMS into multiple messages (see article Concatenated SMS on Wikipedia). Y is number of messages in in current concatenated SMS. The counter is not visible when insertion of 10 7-bit or 16-bit (depends on SMS encoding) does not create concatenated SMS. SMS encoding is automatically guessed. 8-bit encodings are not supported. If the message contains characters that are not supported by GSM 7-bit encoding, counter automatically switches to UCS-2. ## Test Plan Try entering some text that is longer than 150 characters in [GSM 03.38 encoding][1] or 60 characters in UCS-2. Number of remaining characters should be visible below the “Send” button. The character counter should show `0` at exactly 160 or 70 characters. Inserting one character should switch the counter to [Concatenated SMS][2] mode when number of messages is shown. It should show exactly same number as SMS app in Android. ## Screenshots These images are in APNG. ![grab.apng](/uploads/21ae23f2fa75c7aca487e61ddce94644/grab.apng) ![grab-cz.apng](/uploads/785e670a8598c5a65a4209f17e75f578/grab-cz.apng) [1]: https://en.wikipedia.org/w/index.php?oldid=932080074#GSM_7-bit_default_alphabet_and_extension_table_of_3GPP_TS_23.038_/_GSM_03.38 [2]: https://en.wikipedia.org/w/index.php?oldid=943185255#Message_size
2020-03-22 18:47:12 +00:00
/**
* Get SMS character count status of SMS. It contains number of remaining characters
* in current SMS (automatically selects 7-bit, 8-bit or 16-bit mode), octet count and
* number of messages in concatenated SMS.
*/
2020-05-09 17:50:08 +01:00
Q_INVOKABLE static SmsCharCount getCharCount(const QString& message);
2020-03-22 09:33:04 +00:00
/**
* Used to validate arbitrary phone number entered by the user
*/
2020-05-09 17:50:08 +01:00
Q_INVOKABLE static bool isAddressValid(const QString& address);
2020-03-22 09:33:04 +00:00
/**
* Return the total size of the message
*/
Q_INVOKABLE static quint64 totalMessageSize(const QList<QUrl>& urls, const QString& text);
/**
* Gets a thumbnail for the given attachment
*/
Q_INVOKABLE static QIcon getThumbnailForAttachment(const Attachment& attachment);
private:
2020-05-09 17:50:08 +01:00
static bool isInGsmAlphabet(const QChar& ch);
static bool isInGsmAlphabetExtension(const QChar& ch);
};
#endif // SMSHELPER_H