Akonadi Contacts

showaddressaction.cpp
1 /*
2  This file is part of Akonadi Contact.
3 
4  SPDX-FileCopyrightText: 2009 Tobias Koenig <[email protected]>
5 
6  SPDX-License-Identifier: LGPL-2.0-or-later
7 */
8 
9 #include "showaddressaction.h"
10 
11 #include "contactactionssettings.h"
12 
13 #include <KDialogJobUiDelegate>
14 #include <KIO/CommandLauncherJob>
15 #include <kcontacts/address.h>
16 
17 #include <QDesktopServices>
18 
19 using namespace Akonadi;
20 
21 static void replaceArguments(QString &templateStr, const KContacts::Address &address)
22 {
23  templateStr.replace(QLatin1String("%s"), address.street())
24  .replace(QLatin1String("%r"), address.region())
25  .replace(QLatin1String("%l"), address.locality())
26  .replace(QLatin1String("%z"), address.postalCode())
27  .replace(QLatin1String("%n"), address.country())
28  .replace(QLatin1String("%c"), address.countryToISO(address.country()));
29 }
30 
31 void ShowAddressAction::showAddress(const KContacts::Address &address)
32 {
33  // synchronize
34  ContactActionsSettings::self()->load();
35 
36  if (ContactActionsSettings::self()->showAddressAction() == ContactActionsSettings::UseBrowser) {
37  QString urlTemplate = ContactActionsSettings::self()->addressUrl();
38  replaceArguments(urlTemplate, address);
39  if (!urlTemplate.isEmpty()) {
40  QDesktopServices::openUrl(QUrl(urlTemplate));
41  }
42  } else if (ContactActionsSettings::self()->showAddressAction() == ContactActionsSettings::UseExternalAddressApplication) {
43  QString commandTemplate = ContactActionsSettings::self()->addressCommand();
44  replaceArguments(commandTemplate, address);
45 
46  if (!commandTemplate.isEmpty()) {
47  auto job = new KIO::CommandLauncherJob(commandTemplate);
48  job->setUiDelegate(new KDialogJobUiDelegate(KJobUiDelegate::AutoHandlingEnabled, nullptr));
49  job->start();
50  }
51  } else if (ContactActionsSettings::self()->showAddressAction() == ContactActionsSettings::UseGooglemap) {
52  QString urlTemplate = QStringLiteral("https://maps.google.com/maps?q=%s,%l,%c");
53  replaceArguments(urlTemplate, address);
54  if (!urlTemplate.isEmpty()) {
55  QDesktopServices::openUrl(QUrl(urlTemplate));
56  }
57  } else if (ContactActionsSettings::self()->showAddressAction() == ContactActionsSettings::UseMapquest) {
58  QString urlTemplate = QStringLiteral("https://www.mapquest.com/search/result?query=%s,%l,%c");
59  replaceArguments(urlTemplate, address);
60  if (!urlTemplate.isEmpty()) {
61  QDesktopServices::openUrl(QUrl(urlTemplate));
62  }
63  } else if (ContactActionsSettings::self()->showAddressAction() == ContactActionsSettings::UseOpenStreetMap) {
64  QString urlTemplate = QStringLiteral("https://www.openstreetmap.org/search?query=%s,%l,%c");
65  replaceArguments(urlTemplate, address);
66  if (!urlTemplate.isEmpty()) {
67  QDesktopServices::openUrl(QUrl(urlTemplate));
68  }
69  }
70 }
QString street() const
QString region() const
static QString countryToISO(const QString &cname)
QString country() const
bool isEmpty() const const
QString postalCode() const
QString locality() const
const QList< QKeySequence > & replace()
QString & replace(int position, int n, QChar after)
bool openUrl(const QUrl &url)
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.