Akonadi Contacts

sendsmsaction.cpp
1 /*
2  This file is part of Akonadi Contact.
3 
4  SPDX-FileCopyrightText: 2010 Felix Mauch ([email protected])
5 
6  SPDX-License-Identifier: LGPL-2.0-or-later
7 */
8 
9 #include "sendsmsaction.h"
10 
11 #include "contactactionssettings.h"
12 #include "qsflphonedialer.h"
13 #include "qskypedialer.h"
14 #include "smsdialog.h"
15 
16 #include <KDialogJobUiDelegate>
17 #include <KIO/CommandLauncherJob>
18 #include <KLocalizedString>
19 #include <KMessageBox>
20 #include <kcontacts/phonenumber.h>
21 
22 #include <QDesktopServices>
23 #include <QPointer>
24 #include <QUrl>
25 #include <QUrlQuery>
26 
27 #include <memory>
28 
29 void SendSmsAction::sendSms(const KContacts::PhoneNumber &phoneNumber)
30 {
31  const QString number = phoneNumber.number().trimmed();
32 
33  // synchronize
34  ContactActionsSettings::self()->load();
35 
36  // check for valid config first, so the user doesn't type the message without a way to actually send it
37  QString command = ContactActionsSettings::self()->smsCommand();
38  if (command.isEmpty() && ContactActionsSettings::self()->sendSmsAction() == ContactActionsSettings::UseExternalSmsApplication) {
39  KMessageBox::sorry(nullptr, i18n("There is no application set which could be executed.\nPlease go to the settings dialog and configure one."));
40  return;
41  }
42 
43  QPointer<SmsDialog> dlg(new SmsDialog(number));
44  if (dlg->exec() != QDialog::Accepted) { // the cancel button has been clicked
45  delete dlg;
46  return;
47  }
48  const QString message = (dlg != nullptr ? dlg->message() : QString());
49  delete dlg;
50 
51  std::unique_ptr<QDialer> dialer;
52  // we handle skype separated
53  if (ContactActionsSettings::self()->sendSmsAction() == ContactActionsSettings::UseSkypeSms) {
54  dialer = std::make_unique<QSkypeDialer>(QStringLiteral("AkonadiContacts"));
55  } else if (ContactActionsSettings::self()->sendSmsAction() == ContactActionsSettings::UseSflPhoneSms) {
56  dialer = std::make_unique<QSflPhoneDialer>(QStringLiteral("AkonadiContacts"));
57  }
58  if (dialer) {
59  if (dialer->sendSms(number, message)) {
60  // I'm not sure whether here should be a notification.
61  // Skype can do a notification itself if whished.
62  } else {
63  KMessageBox::sorry(nullptr, dialer->errorMessage());
64  }
65  }
66 
67  if (ContactActionsSettings::self()->sendSmsAction() == ContactActionsSettings::UseSystemDefaultSms) {
68  QUrl url;
69  url.setScheme(QStringLiteral("sms"));
70  url.setPath(phoneNumber.normalizedNumber());
72  query.addQueryItem(QStringLiteral("body"), message);
73  url.setQuery(query);
75  return;
76  }
77 
78  /*
79  * %N the raw number
80  * %n the number with all additional non-number characters removed
81  */
82  command.replace(QLatin1String("%N"), QStringLiteral("\"%1\"").arg(phoneNumber.number()));
83  command.replace(QLatin1String("%n"), QStringLiteral("\"%1\"").arg(phoneNumber.normalizedNumber()));
84  command.replace(QLatin1String("%t"), QStringLiteral("\"%1\"").arg(message));
85  // Bug: 293232 In KDE3 We used %F to replace text
86  command.replace(QLatin1String("%F"), message);
87 
88  auto job = new KIO::CommandLauncherJob(command);
89  job->setUiDelegate(new KDialogJobUiDelegate(KJobUiDelegate::AutoHandlingEnabled, nullptr));
90  job->start();
91 }
KCOREADDONS_EXPORT void message(KMessage::MessageType messageType, const QString &text, const QString &caption=QString())
std::optional< QSqlQuery > query(const QString &queryStatement)
QString normalizedNumber() const
void setPath(const QString &path, QUrl::ParsingMode mode)
QString number() const
void addQueryItem(const QString &key, const QString &value)
KIOCORE_EXPORT QString number(KIO::filesize_t size)
bool isEmpty() const const
QString trimmed() const const
void setScheme(const QString &scheme)
QString i18n(const char *text, const TYPE &arg...)
QString & replace(int position, int n, QChar after)
void setQuery(const QString &query, QUrl::ParsingMode mode)
bool openUrl(const QUrl &url)
void sorry(QWidget *parent, const QString &text, const QString &caption=QString(), Options options=Notify)
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Wed Jun 23 2021 23:09:25 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.