| 1 | /* |
| 2 | * Copyright (C) 2016 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 | // Local |
| 18 | #include "gmenumodelexporter.h" |
| 19 | #include "registry.h" |
| 20 | #include "logging.h" |
| 21 | #include "lomiriappmenuextraactionhandler.h" |
| 22 | |
| 23 | #include <QDebug> |
| 24 | #include <QTimerEvent> |
| 25 | |
| 26 | #include <functional> |
| 27 | |
| 28 | namespace { |
| 29 | |
| 30 | // Derive an action name from the label by removing spaces and Capitilizing the words. |
| 31 | // Also remove mnemonics from the label. |
| 32 | inline QString getActionString(QString label) |
| 33 | { |
| 34 | QRegExp re("\\W"); |
| 35 | label = label.replace(QRegExp("(&|_)"), ""); |
| 36 | QStringList parts = label.split(re, Qt::SkipEmptyParts); |
| 37 | |
| 38 | QString result; |
| 39 | Q_FOREACH(const QString& part, parts) { |
| 40 | result += part[0].toUpper(); |
| 41 | result += part.right(part.length()-1); |
| 42 | } |
| 43 | return result; |
| 44 | } |
| 45 | |
| 46 | static void activate_cb(GSimpleAction *action, GVariant *, gpointer user_data) |
| 47 | { |
| 48 | qCDebug(lomiriappmenu, "Activate menu action '%s'", g_action_get_name(G_ACTION(action))); |
| 49 | auto item = static_cast<LomiriPlatformMenuItem*>(user_data); |
| 50 | item->activated(); |
| 51 | } |
| 52 | |
| 53 | static uint s_menuId = 0; |
| 54 | |
| 55 | #define MENU_OBJECT_PATH "/com/lomiri/Menu/%1" |
| 56 | |
| 57 | } // namespace |
| 58 | |
| 59 | |
| 60 | LomiriMenuBarExporter::LomiriMenuBarExporter(LomiriPlatformMenuBar * bar) |
| 61 | : LomiriGMenuModelExporter(bar) |
| 62 | { |
| 63 | qCDebug(lomiriappmenu, "LomiriMenuBarExporter::LomiriMenuBarExporter"); |
| 64 | |
| 65 | connect(bar, &LomiriPlatformMenuBar::structureChanged, this, [this]() { |
| 66 | m_structureTimer.start(); |
| 67 | }); |
| 68 | connect(&m_structureTimer, &QTimer::timeout, this, [this, bar]() { |
| 69 | clear(); |
| 70 | Q_FOREACH(QPlatformMenu *platformMenu, bar->menus()) { |
| 71 | GMenuItem* item = createSubmenu(platformMenu, nullptr); |
| 72 | if (item) { |
| 73 | g_menu_append_item(m_gmainMenu, item); |
| 74 | g_object_unref(item); |
| 75 | } |
| 76 | |
| 77 | LomiriPlatformMenu* gplatformMenu = static_cast<LomiriPlatformMenu*>(platformMenu); |
| 78 | if (gplatformMenu) { |
| 79 | // Sadly we don't have a better way to propagate a enabled change in a top level menu |
| 80 | // than reseting the whole menubar |
| 81 | connect(gplatformMenu, &LomiriPlatformMenu::enabledChanged, bar, &LomiriPlatformMenuBar::structureChanged); |
| 82 | } |
| 83 | } |
| 84 | }); |
| 85 | |
| 86 | connect(bar, &LomiriPlatformMenuBar::ready, this, [this]() { |
| 87 | exportModels(); |
| 88 | }); |
| 89 | } |
| 90 | |
| 91 | LomiriMenuBarExporter::~LomiriMenuBarExporter() |
| 92 | { |
| 93 | qCDebug(lomiriappmenu, "LomiriMenuBarExporter::~LomiriMenuBarExporter"); |
| 94 | } |
| 95 | |
| 96 | LomiriMenuExporter::LomiriMenuExporter(LomiriPlatformMenu *menu) |
| 97 | : LomiriGMenuModelExporter(menu) |
| 98 | { |
| 99 | qCDebug(lomiriappmenu, "LomiriMenuExporter::LomiriMenuExporter"); |
| 100 | |
| 101 | connect(menu, &LomiriPlatformMenu::structureChanged, this, [this]() { |
| 102 | m_structureTimer.start(); |
| 103 | }); |
| 104 | connect(&m_structureTimer, &QTimer::timeout, this, [this, menu]() { |
| 105 | clear(); |
| 106 | addSubmenuItems(menu, m_gmainMenu); |
| 107 | }); |
| 108 | addSubmenuItems(menu, m_gmainMenu); |
| 109 | } |
| 110 | |
| 111 | LomiriMenuExporter::~LomiriMenuExporter() |
| 112 | { |
| 113 | qCDebug(lomiriappmenu, "LomiriMenuExporter::~LomiriMenuExporter"); |
| 114 | } |
| 115 | |
| 116 | LomiriGMenuModelExporter::LomiriGMenuModelExporter(QObject *parent) |
| 117 | : QObject(parent) |
| 118 | , m_connection(nullptr) |
| 119 | , m_gmainMenu(g_menu_new()) |
| 120 | , m_gactionGroup(g_simple_action_group_new()) |
| 121 | , m_exportedModel(0) |
| 122 | , m_exportedActions(0) |
| 123 | , m_lomiriAppMenuExtraHandler(nullptr) |
| 124 | , m_menuPath(QStringLiteral(MENU_OBJECT_PATH).arg(s_menuId++)) |
| 125 | { |
| 126 | m_structureTimer.setSingleShot(true); |
| 127 | m_structureTimer.setInterval(0); |
| 128 | } |
| 129 | |
| 130 | LomiriGMenuModelExporter::~LomiriGMenuModelExporter() |
| 131 | { |
| 132 | unexportModels(); |
| 133 | clear(); |
| 134 | |
| 135 | g_object_unref(m_gmainMenu); |
| 136 | g_object_unref(m_gactionGroup); |
| 137 | } |
| 138 | |
| 139 | // Clear the menu and actions that have been created. |
| 140 | void LomiriGMenuModelExporter::clear() |
| 141 | { |
| 142 | Q_FOREACH(const QVector<QMetaObject::Connection>& menuPropertyConnections, m_propertyConnections) { |
| 143 | Q_FOREACH(const QMetaObject::Connection& connection, menuPropertyConnections) { |
| 144 | QObject::disconnect(connection); |
| 145 | } |
| 146 | } |
| 147 | m_propertyConnections.clear(); |
| 148 | |
| 149 | g_menu_remove_all(m_gmainMenu); |
| 150 | |
| 151 | Q_FOREACH(const QSet<QByteArray>& menuActions, m_actions) { |
| 152 | Q_FOREACH(const QByteArray& action, menuActions) { |
| 153 | g_action_map_remove_action(G_ACTION_MAP(m_gactionGroup), action.constData()); |
| 154 | } |
| 155 | } |
| 156 | m_actions.clear(); |
| 157 | |
| 158 | m_gmenusForMenus.clear(); |
| 159 | } |
| 160 | |
| 161 | void LomiriGMenuModelExporter::timerEvent(QTimerEvent *e) |
| 162 | { |
| 163 | // Find the menu, it's a very short list |
| 164 | auto it = m_reloadMenuTimers.begin(); |
| 165 | for (; it != m_reloadMenuTimers.end(); ++it) { |
| 166 | if (e->timerId() == it.value()) |
| 167 | break; |
| 168 | } |
| 169 | |
| 170 | if (it != m_reloadMenuTimers.end()) { |
| 171 | LomiriPlatformMenu* gplatformMenu = it.key(); |
| 172 | GMenu *menu = m_gmenusForMenus.value(gplatformMenu); |
| 173 | if (menu) { |
| 174 | Q_FOREACH(const QMetaObject::Connection& connection, m_propertyConnections[menu]) { |
| 175 | QObject::disconnect(connection); |
| 176 | } |
| 177 | m_propertyConnections.remove(menu); |
| 178 | Q_FOREACH(const QByteArray& action, m_actions[menu]) { |
| 179 | g_action_map_remove_action(G_ACTION_MAP(m_gactionGroup), action.constData()); |
| 180 | } |
| 181 | m_actions.remove(menu); |
| 182 | g_menu_remove_all(menu); |
| 183 | addSubmenuItems(gplatformMenu, menu); |
| 184 | } else { |
| 185 | qWarning() << "Got an update timer for a menu that has no GMenu" << gplatformMenu; |
| 186 | } |
| 187 | |
| 188 | m_reloadMenuTimers.erase(it); |
| 189 | } else { |
| 190 | qWarning() << "Got an update timer for a timer that was not running"; |
| 191 | } |
| 192 | killTimer(e->timerId()); |
| 193 | } |
| 194 | |
| 195 | // Export the model on dbus |
| 196 | void LomiriGMenuModelExporter::exportModels() |
| 197 | { |
| 198 | GError *error = nullptr; |
| 199 | m_connection = g_bus_get_sync (G_BUS_TYPE_SESSION, nullptr, &error); |
| 200 | if (!m_connection) { |
| 201 | qCWarning(lomiriappmenu, "Failed to retrieve session bus - %s", error ? error->message : "unknown error"); |
| 202 | g_error_free (error); |
| 203 | return; |
| 204 | } |
| 205 | |
| 206 | QByteArray menuPath(m_menuPath.toUtf8()); |
| 207 | |
| 208 | if (m_exportedModel == 0) { |
| 209 | m_exportedModel = g_dbus_connection_export_menu_model(m_connection, menuPath.constData(), G_MENU_MODEL(m_gmainMenu), &error); |
| 210 | if (m_exportedModel == 0) { |
| 211 | qCWarning(lomiriappmenu, "Failed to export menu - %s", error ? error->message : "unknown error"); |
| 212 | g_error_free (error); |
| 213 | error = nullptr; |
| 214 | } else { |
| 215 | qCDebug(lomiriappmenu, "Exported menu on %s", g_dbus_connection_get_unique_name(m_connection)); |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | if (m_exportedActions == 0) { |
| 220 | m_exportedActions = g_dbus_connection_export_action_group(m_connection, menuPath.constData(), G_ACTION_GROUP(m_gactionGroup), &error); |
| 221 | if (m_exportedActions == 0) { |
| 222 | qCWarning(lomiriappmenu, "Failed to export actions - %s", error ? error->message : "unknown error"); |
| 223 | g_error_free (error); |
| 224 | error = nullptr; |
| 225 | } else { |
| 226 | qCDebug(lomiriappmenu, "Exported actions on %s", g_dbus_connection_get_unique_name(m_connection)); |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | if (!m_lomiriAppMenuExtraHandler) { |
| 231 | m_lomiriAppMenuExtraHandler = new LomiriAppMenuExtraActionHandler(); |
| 232 | if (!m_lomiriAppMenuExtraHandler->connect(m_connection, menuPath, this)) { |
| 233 | delete m_lomiriAppMenuExtraHandler; |
| 234 | m_lomiriAppMenuExtraHandler = nullptr; |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | void LomiriGMenuModelExporter::aboutToShow(quint64 tag) |
| 240 | { |
| 241 | LomiriPlatformMenu* gplatformMenu = m_submenusWithTag.value(tag); |
| 242 | if (!gplatformMenu) { |
| 243 | qWarning() << "Got an aboutToShow call with an unknown tag"; |
| 244 | return; |
| 245 | } |
| 246 | |
| 247 | gplatformMenu->aboutToShow(); |
| 248 | } |
| 249 | |
| 250 | // Unexport the model |
| 251 | void LomiriGMenuModelExporter::unexportModels() |
| 252 | { |
| 253 | GError *error = nullptr; |
| 254 | if (!m_connection) { |
| 255 | qCWarning(lomiriappmenu, "Failed to retrieve session bus - %s", error ? error->message : "unknown error"); |
| 256 | return; |
| 257 | } |
| 258 | |
| 259 | if (m_exportedModel != 0) { |
| 260 | g_dbus_connection_unexport_menu_model(m_connection, m_exportedModel); |
| 261 | m_exportedModel = 0; |
| 262 | } |
| 263 | if (m_exportedActions != 0) { |
| 264 | g_dbus_connection_unexport_action_group(m_connection, m_exportedActions); |
| 265 | m_exportedActions = 0; |
| 266 | } |
| 267 | if (m_lomiriAppMenuExtraHandler) { |
| 268 | m_lomiriAppMenuExtraHandler->disconnect(m_connection); |
| 269 | delete m_lomiriAppMenuExtraHandler; |
| 270 | m_lomiriAppMenuExtraHandler = nullptr; |
| 271 | } |
| 272 | g_object_unref(m_connection); |
| 273 | m_connection = nullptr; |
| 274 | } |
| 275 | |
| 276 | // Create a submenu for the given platform menu. |
| 277 | // Returns a gmenuitem entry for the menu, which must be cleaned up using g_object_unref. |
| 278 | // If forItem is suplied, use it's label. |
| 279 | GMenuItem *LomiriGMenuModelExporter::createSubmenu(QPlatformMenu *platformMenu, LomiriPlatformMenuItem *forItem) |
| 280 | { |
| 281 | LomiriPlatformMenu* gplatformMenu = static_cast<LomiriPlatformMenu*>(platformMenu); |
| 282 | if (!gplatformMenu) return nullptr; |
| 283 | GMenu* menu = g_menu_new(); |
| 284 | |
| 285 | m_gmenusForMenus.insert(gplatformMenu, menu); |
| 286 | |
| 287 | QByteArray label; |
| 288 | bool enabled; |
| 289 | if (forItem) { |
| 290 | label = LomiriPlatformMenuItem::get_text(forItem).toUtf8(); |
| 291 | enabled = LomiriPlatformMenuItem::get_enabled(forItem); |
| 292 | } else { |
| 293 | label = LomiriPlatformMenu::get_text(gplatformMenu).toUtf8(); |
| 294 | enabled = LomiriPlatformMenu::get_enabled(gplatformMenu); |
| 295 | } |
| 296 | |
| 297 | addSubmenuItems(gplatformMenu, menu); |
| 298 | |
| 299 | Q_FOREACH(QPlatformMenuItem *childItem, gplatformMenu->menuItems()) { |
| 300 | LomiriPlatformMenuItem* gplatformMenuItem = static_cast<LomiriPlatformMenuItem*>(childItem); |
| 301 | if (!gplatformMenuItem) continue; |
| 302 | |
| 303 | // Sadly we don't have a better way to propagate a enabled change in a item-that-is-submenu |
| 304 | // than reseting the whole parent menu |
| 305 | if (gplatformMenuItem->menu()) { |
| 306 | connect(gplatformMenuItem, &LomiriPlatformMenuItem::enabledChanged, gplatformMenu, &LomiriPlatformMenu::structureChanged); |
| 307 | } |
| 308 | connect(gplatformMenuItem, &LomiriPlatformMenuItem::visibleChanged, gplatformMenu, &LomiriPlatformMenu::structureChanged); |
| 309 | } |
| 310 | |
| 311 | GMenuItem* gmenuItem = g_menu_item_new_submenu(label.constData(), G_MENU_MODEL(menu)); |
| 312 | const quint64 tag = gplatformMenu->tag(); |
| 313 | if (tag != 0) { |
| 314 | g_menu_item_set_attribute_value(gmenuItem, "qtubuntu-tag", g_variant_new_uint64 (tag)); |
| 315 | m_submenusWithTag.insert(gplatformMenu->tag(), gplatformMenu); |
| 316 | } |
| 317 | g_object_unref(menu); |
| 318 | |
| 319 | g_menu_item_set_attribute_value(gmenuItem, "submenu-enabled", g_variant_new_boolean(enabled)); |
| 320 | |
| 321 | connect(gplatformMenu, &LomiriPlatformMenu::structureChanged, this, [this, gplatformMenu] |
| 322 | { |
| 323 | if (!m_reloadMenuTimers.contains(gplatformMenu)) { |
| 324 | const int timerId = startTimer(0); |
| 325 | m_reloadMenuTimers.insert(gplatformMenu, timerId); |
| 326 | } |
| 327 | }); |
| 328 | |
| 329 | connect(gplatformMenu, &LomiriPlatformMenu::destroyed, this, [this, tag, gplatformMenu] |
| 330 | { |
| 331 | m_submenusWithTag.remove(tag); |
| 332 | m_gmenusForMenus.remove(gplatformMenu); |
| 333 | auto timerIdIt = m_reloadMenuTimers.find(gplatformMenu); |
| 334 | if (timerIdIt != m_reloadMenuTimers.end()) { |
| 335 | killTimer(*timerIdIt); |
| 336 | m_reloadMenuTimers.erase(timerIdIt); |
| 337 | } |
| 338 | }); |
| 339 | |
| 340 | return gmenuItem; |
| 341 | } |
| 342 | |
| 343 | // Add a platform menu's items to the given gmenu. |
| 344 | // The items are inserted into menus sections, split by the menu separators. |
| 345 | void LomiriGMenuModelExporter::addSubmenuItems(LomiriPlatformMenu* gplatformMenu, GMenu* menu) |
| 346 | { |
| 347 | auto iter = gplatformMenu->menuItems().begin(); |
| 348 | auto lastSectionStart = iter; |
| 349 | // Iterate through all the menu items adding sections when a separator is found. |
| 350 | for (; iter != gplatformMenu->menuItems().end(); ++iter) { |
| 351 | LomiriPlatformMenuItem* gplatformMenuItem = static_cast<LomiriPlatformMenuItem*>(*iter); |
| 352 | if (!gplatformMenuItem) continue; |
| 353 | |
| 354 | // don't add a section until we have separator |
| 355 | if (LomiriPlatformMenuItem::get_separator(gplatformMenuItem)) { |
| 356 | if (lastSectionStart != gplatformMenu->menuItems().begin()) { |
| 357 | GMenuItem* section = createSection(lastSectionStart, iter); |
| 358 | g_menu_append_item(menu, section); |
| 359 | g_object_unref(section); |
| 360 | } |
| 361 | lastSectionStart = iter + 1; |
| 362 | } else if (lastSectionStart == gplatformMenu->menuItems().begin()) { |
| 363 | processItemForGMenu(gplatformMenuItem, menu); |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | // Add the last section |
| 368 | if (lastSectionStart != gplatformMenu->menuItems().begin() && |
| 369 | lastSectionStart != gplatformMenu->menuItems().end()) { |
| 370 | GMenuItem* gsectionItem = createSection(lastSectionStart, gplatformMenu->menuItems().end()); |
| 371 | g_menu_append_item(menu, gsectionItem); |
| 372 | g_object_unref(gsectionItem); |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | // Create and return a gmenu item for the given platform menu item. |
| 377 | // Returned GMenuItem must be cleaned up using g_object_unref |
| 378 | GMenuItem *LomiriGMenuModelExporter::createMenuItem(QPlatformMenuItem *platformMenuItem, GMenu *parentMenu) |
| 379 | { |
| 380 | LomiriPlatformMenuItem* gplatformMenuItem = static_cast<LomiriPlatformMenuItem*>(platformMenuItem); |
| 381 | if (!gplatformMenuItem) return nullptr; |
| 382 | |
| 383 | if (!LomiriPlatformMenuItem::get_visible(gplatformMenuItem)) |
| 384 | return nullptr; |
| 385 | |
| 386 | QByteArray label(LomiriPlatformMenuItem::get_text(gplatformMenuItem).toUtf8()); |
| 387 | QByteArray actionLabel(getActionString(LomiriPlatformMenuItem::get_text(gplatformMenuItem)).toUtf8()); |
| 388 | QByteArray shortcut(LomiriPlatformMenuItem::get_shortcut(gplatformMenuItem).toString(QKeySequence::NativeText).toUtf8()); |
| 389 | |
| 390 | GMenuItem* gmenuItem = g_menu_item_new(label.constData(), nullptr); |
| 391 | g_menu_item_set_attribute(gmenuItem, "accel", "s", shortcut.constData()); |
| 392 | g_menu_item_set_detailed_action(gmenuItem, ("unity." + actionLabel).constData()); |
| 393 | |
| 394 | addAction(actionLabel, gplatformMenuItem, parentMenu); |
| 395 | return gmenuItem; |
| 396 | } |
| 397 | |
| 398 | // Create a menu section for a section of separated menu items. |
| 399 | // Returned GMenuItem must be cleaned up using g_object_unref |
| 400 | GMenuItem *LomiriGMenuModelExporter::createSection(QList<QPlatformMenuItem *>::const_iterator iter, QList<QPlatformMenuItem *>::const_iterator end) |
| 401 | { |
| 402 | GMenu* gsectionMenu = g_menu_new(); |
| 403 | for (; iter != end; ++iter) { |
| 404 | processItemForGMenu(*iter, gsectionMenu); |
| 405 | } |
| 406 | GMenuItem* gsectionItem = g_menu_item_new_section("", G_MENU_MODEL(gsectionMenu)); |
| 407 | g_object_unref(gsectionMenu); |
| 408 | return gsectionItem; |
| 409 | } |
| 410 | |
| 411 | // Add the given platform menu item to the menu. |
| 412 | // If it has an attached submenu, then create and add the submenu. |
| 413 | void LomiriGMenuModelExporter::processItemForGMenu(QPlatformMenuItem *platformMenuItem, GMenu *gmenu) |
| 414 | { |
| 415 | LomiriPlatformMenuItem* gplatformMenuItem = static_cast<LomiriPlatformMenuItem*>(platformMenuItem); |
| 416 | if (!gplatformMenuItem) return; |
| 417 | |
| 418 | GMenuItem* gmenuItem = gplatformMenuItem->menu() ? createSubmenu(gplatformMenuItem->menu(), gplatformMenuItem) : |
| 419 | createMenuItem(gplatformMenuItem, gmenu); |
| 420 | if (gmenuItem) { |
| 421 | g_menu_append_item(gmenu, gmenuItem); |
| 422 | g_object_unref(gmenuItem); |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | // Create and add an action for a menu item. |
| 427 | void LomiriGMenuModelExporter::addAction(const QByteArray &name, LomiriPlatformMenuItem *gplatformMenuItem, GMenu *parentMenu) |
| 428 | { |
| 429 | disconnect(gplatformMenuItem, &LomiriPlatformMenuItem::checkedChanged, this, 0); |
| 430 | disconnect(gplatformMenuItem, &LomiriPlatformMenuItem::enabledChanged, this, 0); |
| 431 | |
| 432 | QSet<QByteArray> &actions = m_actions[parentMenu]; |
| 433 | QVector<QMetaObject::Connection> &propertyConnections = m_propertyConnections[parentMenu]; |
| 434 | |
| 435 | if (actions.contains(name)) { |
| 436 | g_action_map_remove_action(G_ACTION_MAP(m_gactionGroup), name.constData()); |
| 437 | actions.remove(name); |
| 438 | } |
| 439 | |
| 440 | bool checkable = LomiriPlatformMenuItem::get_checkable(gplatformMenuItem); |
| 441 | |
| 442 | GSimpleAction* action = nullptr; |
| 443 | if (checkable) { |
| 444 | bool checked = LomiriPlatformMenuItem::get_checked(gplatformMenuItem); |
| 445 | action = g_simple_action_new_stateful(name.constData(), nullptr, g_variant_new_boolean(checked)); |
| 446 | |
| 447 | std::function<void(bool)> updateChecked = [gplatformMenuItem, action](bool checked) { |
| 448 | auto type = g_action_get_state_type(G_ACTION(action)); |
| 449 | if (type && g_variant_type_equal(type, G_VARIANT_TYPE_BOOLEAN)) { |
| 450 | g_simple_action_set_state(action, g_variant_new_boolean(checked ? TRUE : FALSE)); |
| 451 | } |
| 452 | }; |
| 453 | // save the connection to disconnect in LomiriGMenuModelExporter::clear() |
| 454 | propertyConnections << connect(gplatformMenuItem, &LomiriPlatformMenuItem::checkedChanged, this, updateChecked); |
| 455 | } else { |
| 456 | action = g_simple_action_new(name.constData(), nullptr); |
| 457 | } |
| 458 | |
| 459 | // Enabled update |
| 460 | std::function<void(bool)> updateEnabled = [gplatformMenuItem, action](bool enabled) { |
| 461 | GValue value = G_VALUE_INIT; |
| 462 | g_value_init (&value, G_TYPE_BOOLEAN); |
| 463 | g_value_set_boolean(&value, enabled ? TRUE : FALSE); |
| 464 | g_object_set_property(G_OBJECT(action), "enabled", &value); |
| 465 | }; |
| 466 | updateEnabled(LomiriPlatformMenuItem::get_enabled(gplatformMenuItem)); |
| 467 | // save the connection to disconnect in LomiriGMenuModelExporter::clear() |
| 468 | propertyConnections << connect(gplatformMenuItem, &LomiriPlatformMenuItem::enabledChanged, this, updateEnabled); |
| 469 | |
| 470 | g_signal_connect(action, "activate", G_CALLBACK(activate_cb), gplatformMenuItem); |
| 471 | |
| 472 | actions.insert(name); |
| 473 | g_action_map_add_action(G_ACTION_MAP(m_gactionGroup), G_ACTION(action)); |
| 474 | g_object_unref(action); |
| 475 | } |