| 1 | /* |
| 2 | * Copyright (C) 2017 Canonical, Ltd. |
| 3 | * |
| 4 | * This program is free software: you can redistribute it and/or modify it under |
| 5 | * the terms of the GNU Lesser General Public License version 3, as published by |
| 6 | * the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 9 | * ANY WARRANTY; without even the implied warranties of MERCHANTABILITY, |
| 10 | * SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | * Lesser General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU Lesser General Public License |
| 14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 | */ |
| 16 | |
| 17 | #include "lomiriappmenuextraactionhandler.h" |
| 18 | |
| 19 | #include "gmenumodelexporter.h" |
| 20 | #include "logging.h" |
| 21 | |
| 22 | static const gchar introspection_xml[] = |
| 23 | "<node>" |
| 24 | " <interface name='qtubuntu.actions.extra'>" |
| 25 | " <method name='aboutToShow'>" |
| 26 | " <arg type='t' name='tag' direction='in'/>" |
| 27 | " </method>" |
| 28 | " </interface>" |
| 29 | "</node>"; |
| 30 | |
| 31 | static void handle_method_call (GDBusConnection *, |
| 32 | const gchar *, |
| 33 | const gchar *, |
| 34 | const gchar *, |
| 35 | const gchar *method_name, |
| 36 | GVariant *parameters, |
| 37 | GDBusMethodInvocation *invocation, |
| 38 | gpointer user_data) |
| 39 | { |
| 40 | |
| 41 | if (g_strcmp0 (method_name, "aboutToShow") == 0) |
| 42 | { |
| 43 | if (g_variant_check_format_string(parameters, "(t)", false)) { |
| 44 | auto obj = static_cast<LomiriGMenuModelExporter*>(user_data); |
| 45 | guint64 tag; |
| 46 | |
| 47 | g_variant_get (parameters, "(t)", &tag); |
| 48 | obj->aboutToShow(tag); |
| 49 | } |
| 50 | |
| 51 | g_dbus_method_invocation_return_value (invocation, NULL); |
| 52 | } else { |
| 53 | g_dbus_method_invocation_return_error(invocation, |
| 54 | G_DBUS_ERROR, |
| 55 | G_DBUS_ERROR_UNKNOWN_METHOD, |
| 56 | "Unknown method"); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | |
| 61 | static const GDBusInterfaceVTable interface_vtable = |
| 62 | { |
| 63 | handle_method_call, |
| 64 | NULL, |
| 65 | NULL, |
| 66 | NULL |
| 67 | }; |
| 68 | |
| 69 | LomiriAppMenuExtraActionHandler::LomiriAppMenuExtraActionHandler() |
| 70 | : m_registration_id(0) |
| 71 | { |
| 72 | m_introspection_data = g_dbus_node_info_new_for_xml (introspection_xml, NULL); |
| 73 | } |
| 74 | |
| 75 | LomiriAppMenuExtraActionHandler::~LomiriAppMenuExtraActionHandler() |
| 76 | { |
| 77 | g_clear_pointer(&m_introspection_data, g_dbus_node_info_unref); |
| 78 | } |
| 79 | |
| 80 | bool LomiriAppMenuExtraActionHandler::connect(GDBusConnection *connection, const QByteArray &menuPath, LomiriGMenuModelExporter *gmenuexporter) |
| 81 | { |
| 82 | if (m_registration_id != 0) { |
| 83 | qCWarning(lomiriappmenu, "Called connect in an already connected LomiriAppMenuExtraActionHandler"); |
| 84 | return false; |
| 85 | } |
| 86 | |
| 87 | GError *error = nullptr; |
| 88 | m_registration_id = g_dbus_connection_register_object (connection, menuPath.constData(), |
| 89 | m_introspection_data->interfaces[0], |
| 90 | &interface_vtable, |
| 91 | gmenuexporter, |
| 92 | nullptr, |
| 93 | &error); |
| 94 | |
| 95 | if (!m_registration_id) { |
| 96 | qCWarning(lomiriappmenu, "Failed to extra actions - %s", error ? error->message : "unknown error"); |
| 97 | g_clear_error(&error); |
| 98 | } |
| 99 | |
| 100 | return m_registration_id != 0; |
| 101 | } |
| 102 | |
| 103 | void LomiriAppMenuExtraActionHandler::disconnect(GDBusConnection *connection) { |
| 104 | if (m_registration_id) { |
| 105 | g_dbus_connection_unregister_object (connection, m_registration_id); |
| 106 | } |
| 107 | } |