Commit ff7b36f9 authored by Volker Krause's avatar Volker Krause
Browse files

Use geo: URIs to show an address on a map

Handlers for those are now configurable in System Settings, making the
application specific configuration here unnecessary.
parent 12f6d5b5
Pipeline #133169 passed with stage
in 1 minute and 39 seconds
......@@ -23,99 +23,6 @@
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QGroupBox" name="groupBox">
<property name="title">
<string>Show Address</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QComboBox" name="ShowAddressAction"/>
</item>
<item>
<widget class="QStackedWidget" name="stackedWidget">
<property name="currentIndex">
<number>0</number>
</property>
<widget class="QWidget" name="page">
<layout class="QHBoxLayout" name="horizontalLayout">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QLabel" name="label_3">
<property name="text">
<string>Url:</string>
</property>
</widget>
</item>
<item>
<widget class="KLineEdit" name="kcfg_AddressUrl">
<property name="trapReturnKey" stdset="0">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</widget>
<widget class="QWidget" name="page_2">
<layout class="QHBoxLayout" name="horizontalLayout_2">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QLabel" name="label_4">
<property name="text">
<string>Command:</string>
</property>
</widget>
</item>
<item>
<widget class="KLineEdit" name="kcfg_AddressCommand">
<property name="trapReturnKey" stdset="0">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</widget>
<widget class="QWidget" name="page_8"/>
</widget>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>9</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QGroupBox" name="groupBox_2">
<property name="title">
......
......@@ -48,30 +48,10 @@ KCMAkonadiContactActions::KCMAkonadiContactActions(QWidget *parent, const QVaria
ui.SendSmsAction->addItem(i18n("SflPhone"), ContactActionsSettings::UseSflPhoneSms);
ui.SendSmsAction->addItem(i18n("External Application"), ContactActionsSettings::UseExternalSmsApplication);
connect(ui.SendSmsAction, qOverload<int>(&QComboBox::currentIndexChanged), this, &KCMAkonadiContactActions::slotSmsPhoneNumberActionChanged);
ui.ShowAddressAction->addItem(i18n("Web Browser"), ContactActionsSettings::UseBrowser);
ui.ShowAddressAction->addItem(i18n("External Application"), ContactActionsSettings::UseExternalAddressApplication);
ui.ShowAddressAction->addItem(i18n("Google map"), ContactActionsSettings::UseGooglemap);
ui.ShowAddressAction->addItem(i18n("Map quest"), ContactActionsSettings::UseMapquest);
ui.ShowAddressAction->addItem(i18n("OpenStreetMap"), ContactActionsSettings::UseOpenStreetMap);
connect(ui.ShowAddressAction, qOverload<int>(&QComboBox::currentIndexChanged), this, &KCMAkonadiContactActions::slotShowAddressActionChanged);
load();
}
void KCMAkonadiContactActions::slotShowAddressActionChanged(int value)
{
ContactActionsSettings::EnumShowAddressAction enumValue =
static_cast<ContactActionsSettings::EnumShowAddressAction>(ui.ShowAddressAction->itemData(value).toInt());
if (enumValue == ContactActionsSettings::UseBrowser) {
ui.stackedWidget->setCurrentIndex(0);
} else if (enumValue == ContactActionsSettings::UseExternalAddressApplication) {
ui.stackedWidget->setCurrentIndex(1);
} else {
ui.stackedWidget->setCurrentIndex(2);
}
Q_EMIT changed(true);
}
void KCMAkonadiContactActions::slotSmsPhoneNumberActionChanged(int value)
{
ContactActionsSettings::EnumSendSmsAction enumValue = static_cast<ContactActionsSettings::EnumSendSmsAction>(ui.SendSmsAction->itemData(value).toInt());
......@@ -99,10 +79,6 @@ void KCMAkonadiContactActions::load()
{
mConfigManager->updateWidgets();
auto enumValueAddress = static_cast<ContactActionsSettings::EnumShowAddressAction>(ContactActionsSettings::self()->showAddressAction());
const int indexAddress = ui.ShowAddressAction->findData(enumValueAddress);
ui.ShowAddressAction->setCurrentIndex(indexAddress);
auto enumValue = static_cast<ContactActionsSettings::EnumDialPhoneNumberAction>(ContactActionsSettings::self()->dialPhoneNumberAction());
const int index = ui.DialPhoneNumberAction->findData(enumValue);
ui.DialPhoneNumberAction->setCurrentIndex(index);
......@@ -115,10 +91,6 @@ void KCMAkonadiContactActions::load()
void KCMAkonadiContactActions::save()
{
mConfigManager->updateSettings();
ContactActionsSettings::EnumShowAddressAction enumValueAddress =
static_cast<ContactActionsSettings::EnumShowAddressAction>(ui.ShowAddressAction->itemData(ui.ShowAddressAction->currentIndex()).toInt());
ContactActionsSettings::self()->setShowAddressAction(enumValueAddress);
ContactActionsSettings::EnumDialPhoneNumberAction enumValue =
static_cast<ContactActionsSettings::EnumDialPhoneNumberAction>(ui.DialPhoneNumberAction->itemData(ui.DialPhoneNumberAction->currentIndex()).toInt());
ContactActionsSettings::self()->setDialPhoneNumberAction(enumValue);
......@@ -135,7 +107,6 @@ void KCMAkonadiContactActions::defaults()
const bool bUseDefaults = ContactActionsSettings::self()->useDefaults(true);
ui.DialPhoneNumberAction->setCurrentIndex(ContactActionsSettings::self()->dialPhoneNumberAction());
ui.SendSmsAction->setCurrentIndex(ContactActionsSettings::self()->sendSmsAction());
ui.ShowAddressAction->setCurrentIndex(ContactActionsSettings::self()->showAddressAction());
ContactActionsSettings::self()->useDefaults(bUseDefaults);
}
......
......@@ -28,7 +28,6 @@ public:
private Q_SLOTS:
void slotDialPhoneNumberActionChanged(int);
void slotSmsPhoneNumberActionChanged(int value);
void slotShowAddressActionChanged(int value);
private:
KConfigDialogManager *mConfigManager = nullptr;
......
......@@ -47,7 +47,6 @@ install(FILES
target_sources(KF5AkonadiContact PRIVATE
actions/dialphonenumberaction.cpp
actions/showaddressaction.cpp
actions/qdialer.cpp
actions/qskypedialer.cpp
actions/sendsmsaction.cpp
......@@ -143,7 +142,6 @@ target_sources(KF5AkonadiContact PRIVATE
actions/dialphonenumberaction.h
actions/qskypedialer.h
actions/smsdialog.h
actions/showaddressaction.h
actions/sendsmsaction.h
actions/qdialer.h
abstractcontactgroupformatter.h
......
......@@ -6,40 +6,6 @@
<kcfgfile name="akonadi_contactrc"/>
<group name="Show Address Settings">
<entry type="Enum" name="ShowAddressAction">
<choices>
<choice name="UseBrowser"/>
<choice name="UseExternalAddressApplication"/>
<choice name="UseGooglemap"/>
<choice name="UseMapquest"/>
<choice name="UseOpenStreetMap"/>
</choices>
<whatsthis>Defines which application shall be used to show the postal address of a contact on a map. If 'Web Browser' is selected, an URL can be defined with placeholders for the single address parts. If 'External Application' is selected, a command with placeholders can be defined.</whatsthis>
<default>UseOpenStreetMap</default>
</entry>
<entry type="String" name="AddressUrl">
<default></default>
<whatsthis>This URL defines the website that shall be used to show a contact's postal address.</whatsthis>
<tooltip>The following placeholders can be used in the URL:
%s: Street
%r: Region
%l: Location
%z: Zip Code
%c: Country ISO Code</tooltip>
</entry>
<entry type="String" name="AddressCommand">
<label>Address Command</label>
<whatsthis>This command defines the application that shall be executed to show a contact's postal address.</whatsthis>
<tooltip>The following placeholders can be used in the command:
%s: Street
%r: Region
%l: Location
%z: Zip Code
%c: Country ISO Code</tooltip>
</entry>
</group>
<group name="Phone Dial Settings">
<entry type="Enum" name="DialPhoneNumberAction">
<choices>
......
/*
This file is part of Akonadi Contact.
SPDX-FileCopyrightText: 2009 Tobias Koenig <tokoe@kde.org>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include "showaddressaction.h"
#include "contactactionssettings.h"
#include <KContacts/Address>
#include <KDialogJobUiDelegate>
#include <KIO/CommandLauncherJob>
#include <KCountry>
#include <QDesktopServices>
using namespace Akonadi;
static void replaceArguments(QString &templateStr, const KContacts::Address &address)
{
const auto country = KCountry::fromName(address.country()); // will also handle ISO 3166-1 alpha 2 or 3 codes
templateStr.replace(QLatin1String("%s"), address.street())
.replace(QLatin1String("%r"), address.region())
.replace(QLatin1String("%l"), address.locality())
.replace(QLatin1String("%z"), address.postalCode())
.replace(QLatin1String("%n"), country.name())
.replace(QLatin1String("%c"), country.alpha2());
}
void ShowAddressAction::showAddress(const KContacts::Address &address)
{
// synchronize
ContactActionsSettings::self()->load();
if (ContactActionsSettings::self()->showAddressAction() == ContactActionsSettings::UseBrowser) {
QString urlTemplate = ContactActionsSettings::self()->addressUrl();
replaceArguments(urlTemplate, address);
if (!urlTemplate.isEmpty()) {
QDesktopServices::openUrl(QUrl(urlTemplate));
}
} else if (ContactActionsSettings::self()->showAddressAction() == ContactActionsSettings::UseExternalAddressApplication) {
QString commandTemplate = ContactActionsSettings::self()->addressCommand();
replaceArguments(commandTemplate, address);
if (!commandTemplate.isEmpty()) {
auto job = new KIO::CommandLauncherJob(commandTemplate);
job->setUiDelegate(new KDialogJobUiDelegate(KJobUiDelegate::AutoHandlingEnabled, nullptr));
job->start();
}
} else if (ContactActionsSettings::self()->showAddressAction() == ContactActionsSettings::UseGooglemap) {
QString urlTemplate = QStringLiteral("https://maps.google.com/maps?q=%s,%l,%c");
replaceArguments(urlTemplate, address);
if (!urlTemplate.isEmpty()) {
QDesktopServices::openUrl(QUrl(urlTemplate));
}
} else if (ContactActionsSettings::self()->showAddressAction() == ContactActionsSettings::UseMapquest) {
QString urlTemplate = QStringLiteral("https://www.mapquest.com/search/result?query=%s,%l,%c");
replaceArguments(urlTemplate, address);
if (!urlTemplate.isEmpty()) {
QDesktopServices::openUrl(QUrl(urlTemplate));
}
} else if (ContactActionsSettings::self()->showAddressAction() == ContactActionsSettings::UseOpenStreetMap) {
QString urlTemplate = QStringLiteral("https://www.openstreetmap.org/search?query=%s,%l,%c");
replaceArguments(urlTemplate, address);
if (!urlTemplate.isEmpty()) {
QDesktopServices::openUrl(QUrl(urlTemplate));
}
}
}
/*
This file is part of Akonadi Contact.
SPDX-FileCopyrightText: 2009 Tobias Koenig <tokoe@kde.org>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#pragma once
namespace KContacts
{
class Address;
}
namespace Akonadi
{
/**
* @short A contact action to show the address of a contact on a map.
*
* This class provides the functionality to show the postal address
* of a contact on a map in a webbrowser.
*
* Example:
*
* @code
*
* const KContacts::Addressee contact = ...
*
* const KContacts::Address::List addresses = contact.addresses();
* if ( !addresses.isEmpty() ) {
* Akonadi::ShowAddressAction action;
* action.showAddress( addresses.first() );
* }
*
* @endcode
*
* @author Tobias Koenig <tokoe@kde.org>
*/
class ShowAddressAction
{
public:
void showAddress(const KContacts::Address &address);
};
}
......@@ -10,13 +10,16 @@
#include "actions/dialphonenumberaction.h"
#include "actions/sendsmsaction.h"
#include "actions/showaddressaction.h"
#include <KContacts/Address>
#include <KContacts/Addressee>
#include <KContacts/PhoneNumber>
#include <KCountry>
#include <QDesktopServices>
#include <QUrl>
#include <QUrlQuery>
using namespace Akonadi;
......@@ -89,6 +92,33 @@ void ContactDefaultActions::sendSms(const KContacts::PhoneNumber &number)
void ContactDefaultActions::showAddress(const KContacts::Address &address)
{
ShowAddressAction action;
action.showAddress(address);
// ### move geo: URI generation to KContacts::Address?
QUrl url;
url.setScheme(QStringLiteral("geo"));
if (address.geo().isValid()) {
url.setPath(QString::number(address.geo().latitude()) + QLatin1Char(',') + QString::number(address.geo().longitude()));
} else if (!address.isEmpty()) {
url.setPath(QStringLiteral("0,0"));
QStringList q;
if (!address.street().isEmpty()) {
q.push_back(address.street());
}
if (!address.locality().isEmpty()) {
q.push_back(address.postalCode().isEmpty() ? address.locality() : (address.postalCode() + QLatin1Char(' ') + address.locality()));
}
if (!address.region().isEmpty()) {
q.push_back(address.region());
}
if (!address.country().isEmpty()) {
const auto c = KCountry::fromName(address.country());
q.push_back(c.alpha2());
}
QUrlQuery query;
query.addQueryItem(QStringLiteral("q"), q.join(QLatin1String(", ")));
url.setQuery(query);
} else {
return;
}
QDesktopServices::openUrl(url);
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment