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 <KContacts/PhoneNumber>
17 #include <KDialogJobUiDelegate>
18 #include <KIO/CommandLauncherJob>
19 #include <KLocalizedString>
20 #include <KMessageBox>
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::error(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::error(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 }
std::optional< QSqlQuery > query(const QString &queryStatement)
QString trimmed() const const
QString number() const
bool openUrl(const QUrl &url)
void setScheme(const QString &scheme)
QString i18n(const char *text, const TYPE &arg...)
bool isEmpty() const const
void error(QWidget *parent, const QString &text, const QString &title, const KGuiItem &buttonOk, Options options=Notify)
QString & replace(int position, int n, QChar after)
void setQuery(const QString &query, QUrl::ParsingMode mode)
QString normalizedNumber() const
void setPath(const QString &path, QUrl::ParsingMode mode)
KIOCORE_EXPORT QString number(KIO::filesize_t size)
QString message
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Sat Apr 1 2023 04:09:05 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.