summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexandr Akulich <akulichalexander@gmail.com>2016-07-15 08:40:16 (GMT)
committerAlexandr Akulich <akulichalexander@gmail.com>2016-08-08 13:27:40 (GMT)
commit78a720b656a84340d026dc9085864a325c431e37 (patch)
treeef8116b7ce4e61b5f05312d4a5a623b9853e9308
parentf8f3729be03aee93352a3dbd65f45df8acf6aec9 (diff)
[filters] Added Geopoint filter plugin.
-rw-r--r--filters/CMakeLists.txt1
-rw-r--r--filters/geopoint/CMakeLists.txt21
-rw-r--r--filters/geopoint/geopoint-filter.cpp76
-rw-r--r--filters/geopoint/geopoint-filter.h32
-rw-r--r--filters/geopoint/ktptextui_message_filter_geopoint.desktop.cmake17
5 files changed, 147 insertions, 0 deletions
diff --git a/filters/CMakeLists.txt b/filters/CMakeLists.txt
index 8118b13..ce06ba7 100644
--- a/filters/CMakeLists.txt
+++ b/filters/CMakeLists.txt
@@ -1,6 +1,7 @@
add_subdirectory(bugzilla)
add_subdirectory(emoticons)
add_subdirectory(formatting)
+add_subdirectory(geopoint)
add_subdirectory(images)
add_subdirectory(latex)
add_subdirectory(searchexpansion)
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 <akulichalexander@gmail.com>
+ *
+ * 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 <KPluginFactory>
+
+#include <QRegularExpression>
+#include <QRegularExpressionMatch>
+
+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("<a href=\"%1\">%1</a>");
+ static const QString mapTemplate = QStringLiteral(
+ "<iframe width=\"420\" height=\"350\" scrolling=\"no\" marginheight=\"0\" marginwidth=\"0\" src=\""
+ "https://www.openlinkmap.org/small.php?lat=%1&lon=%2&zoom=%3\" "
+ "style=\"border: 1px solid black\" "
+ "frameborder=\"0\"></iframe>"
+ );
+
+ static const QRegularExpression geoTextExpression(QStringLiteral(
+ "geo:"
+ "(?<latitude>[\\+\\-]?\\d{1,3}(\\.[0-9]+)?),"
+ "(?<longitude>[\\+\\-]?\\d{1,3}(\\.[0-9]+)?)"
+ "(\\?z=(?<zoom>\\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("<br/>") + 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<GeopointFilter>();)
+
+#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 <akulichalexander@gmail.com>
+ *
+ * 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 <KTp/abstract-message-filter.h>
+
+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.