diff --git a/filters/CMakeLists.txt b/filters/CMakeLists.txt index 8118b13..ce06ba7 100644 --- a/filters/CMakeLists.txt +++ b/filters/CMakeLists.txt @@ -1,14 +1,15 @@ add_subdirectory(bugzilla) add_subdirectory(emoticons) add_subdirectory(formatting) +add_subdirectory(geopoint) add_subdirectory(images) add_subdirectory(latex) add_subdirectory(searchexpansion) add_subdirectory(youtube) add_subdirectory(highlight) add_subdirectory(urlexpansion) add_subdirectory(otr) if (Qt5TextToSpeech_FOUND) add_subdirectory(texttospeech) endif() \ No newline at end of file diff --git a/filters/geopoint/CMakeLists.txt b/filters/geopoint/CMakeLists.txt new file mode 100644 index 0000000..0a3414f --- /dev/null +++ b/filters/geopoint/CMakeLists.txt @@ -0,0 +1,21 @@ +add_library(ktptextui_message_filter_geopoint MODULE geopoint-filter.cpp) + +target_link_libraries(ktptextui_message_filter_geopoint + ktpchat + KF5::KIOWidgets + KTp::CommonInternals + ) + +# Install: +install(TARGETS ktptextui_message_filter_geopoint + DESTINATION ${PLUGIN_INSTALL_DIR} +) + +configure_file(${CMAKE_CURRENT_SOURCE_DIR}/ktptextui_message_filter_geopoint.desktop.cmake + ${CMAKE_CURRENT_BINARY_DIR}/ktptextui_message_filter_geopoint.desktop + @ONLY +) + +install(FILES ${CMAKE_CURRENT_BINARY_DIR}/ktptextui_message_filter_geopoint.desktop + DESTINATION ${SERVICES_INSTALL_DIR} +) diff --git a/filters/geopoint/geopoint-filter.cpp b/filters/geopoint/geopoint-filter.cpp new file mode 100644 index 0000000..40a74ae --- /dev/null +++ b/filters/geopoint/geopoint-filter.cpp @@ -0,0 +1,76 @@ +/* + * Copyright (C) 2016 Alexandr Akulich + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#include "geopoint-filter.h" + +#include + +#include +#include + +GeopointFilter::GeopointFilter(QObject *parent, const QVariantList &args) : + AbstractMessageFilter(parent) +{ + Q_UNUSED(args) +} + +void GeopointFilter::filterMessage(KTp::Message &message, const KTp::MessageContext&) +{ + static const QString linkTemplate = QStringLiteral("%1"); + static const QString mapTemplate = QStringLiteral( + "" + ); + + static const QRegularExpression geoTextExpression(QStringLiteral( + "geo:" + "(?[\\+\\-]?\\d{1,3}(\\.[0-9]+)?)," + "(?[\\+\\-]?\\d{1,3}(\\.[0-9]+)?)" + "(\\?z=(?\\d+))?")); + QRegularExpressionMatch match; + QString messageText = message.mainMessagePart(); + int index = 0; + + while ((index = messageText.indexOf(geoTextExpression, index, &match)) >= 0) { + const double latitude = match.capturedRef(QStringLiteral("latitude")).toDouble(); + const double longitude = match.capturedRef(QStringLiteral("longitude")).toDouble(); + + const QStringRef zoomStr = match.capturedRef(QStringLiteral("zoom")); + + int zoom = 12; + if (!zoomStr.isNull()) { + zoom = zoomStr.toInt(); + } + + message.appendMessagePart(QStringLiteral("
") + mapTemplate.arg(latitude).arg(longitude).arg(zoom)); + messageText.replace(index, match.capturedLength(), linkTemplate.arg(match.captured())); + + index = match.capturedEnd(QStringLiteral("longitude")); + // Shift the index by the length of added text: we added the second copy of "geo:..." text, + // plus the html code itself and minus the length of argument placeholders (%1 and %1). + index += match.capturedLength() + linkTemplate.length() - 4; + } + + message.setMainMessagePart(messageText); +} + +K_PLUGIN_FACTORY(MessageFilterFactory, registerPlugin();) + +#include "geopoint-filter.moc" diff --git a/filters/geopoint/geopoint-filter.h b/filters/geopoint/geopoint-filter.h new file mode 100644 index 0000000..376f460 --- /dev/null +++ b/filters/geopoint/geopoint-filter.h @@ -0,0 +1,32 @@ +/* + * Copyright (C) 2016 Alexandr Akulich + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#ifndef GEOPOINT_FILTER_H +#define GEOPOINT_FILTER_H + +#include + +class GeopointFilter : public KTp::AbstractMessageFilter +{ + Q_OBJECT +public: + GeopointFilter(QObject *parent, const QVariantList &args); + void filterMessage(KTp::Message &message, const KTp::MessageContext &context); +}; + +#endif // GEOPOINT_FILTER_H diff --git a/filters/geopoint/ktptextui_message_filter_geopoint.desktop.cmake b/filters/geopoint/ktptextui_message_filter_geopoint.desktop.cmake new file mode 100644 index 0000000..64658ba --- /dev/null +++ b/filters/geopoint/ktptextui_message_filter_geopoint.desktop.cmake @@ -0,0 +1,17 @@ +[Desktop Entry] +Encoding=UTF-8 +Type=Service +ServiceTypes=KTpTextUi/MessageFilter + +X-KDE-Library=ktptextui_message_filter_geopoint +X-KDE-PluginInfo-Author=Alexandr Akulich +X-KDE-PluginInfo-Email=akulichalexander@gmail.com +X-KDE-PluginInfo-Name=geopoint +X-KDE-PluginInfo-Version=@KTP_TEXT_UI_VERSION@ +X-KDE-PluginInfo-Website=http://community.kde.org/KTp +X-KDE-PluginInfo-License=GPL +X-KDE-PluginInfo-EnabledByDefault=false +X-KTp-PluginInfo-Version=@KTP_MESSAGE_FILTER_FRAMEWORK_VERSION@ + +Name=Geopoint Preview +Comment=Show geo URI on a map view.