/* * Copyright 2013 Canonical Ltd. * * This program 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; version 3. * * This program 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 program. If not, see . * * Authors: * Nick Dedekind */ #include "unitymenumodelcache.h" #include #include UnityMenuModelCache* UnityMenuModelCache::theCache = nullptr; UnityMenuModelCache* UnityMenuModelCache::singleton() { if (!theCache) { theCache = new UnityMenuModelCache(); } return theCache; } UnityMenuModelCache::UnityMenuModelCache(QObject* parent) : QObject(parent) { } QSharedPointer UnityMenuModelCache::model(const QByteArray& bus, const QByteArray& path, const QVariantMap& actions) { if (m_registry.contains(path)) return m_registry[path]; UnityMenuModel* model = new UnityMenuModel; QQmlEngine::setObjectOwnership(model, QQmlEngine::CppOwnership); QSharedPointer menuModel(model); connect(model, &QObject::destroyed, this, [this] { QMutableHashIterator> iter(m_registry); while(iter.hasNext()) { auto keyVal = iter.next(); if (keyVal.value().toStrongRef().isNull()) { iter.remove(); } } }); m_registry[path] = menuModel.toWeakRef(); menuModel->setBusName(bus); menuModel->setMenuObjectPath(path); menuModel->setActions(actions); return menuModel; } bool UnityMenuModelCache::contains(const QByteArray& path) { return m_registry.contains(path); }