File indexing completed on 2024-05-19 16:38:20

0001 /*
0002     SPDX-FileCopyrightText: 2015, 2016 Ivan Cukic <ivan.cukic(at)kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005 */
0006 
0007 #include "activitiessync_p.h"
0008 
0009 #include <QCoreApplication>
0010 
0011 namespace ActivitiesSync
0012 {
0013 typedef std::shared_ptr<KActivities::Consumer> ConsumerPtr;
0014 
0015 ConsumerPtr instance()
0016 {
0017     static std::mutex s_instanceMutex;
0018     static std::weak_ptr<KActivities::Consumer> s_instance;
0019 
0020     std::unique_lock<std::mutex> locker{s_instanceMutex};
0021 
0022     auto ptr = s_instance.lock();
0023 
0024     if (!ptr) {
0025         ptr = std::make_shared<KActivities::Consumer>();
0026         s_instance = ptr;
0027     }
0028 
0029     return ptr;
0030 }
0031 
0032 QString currentActivity(ConsumerPtr &activities)
0033 {
0034     // We need to get the current activity synchonously,
0035     // this means waiting for the service to be available.
0036     // It should not introduce blockages since there usually
0037     // is a global activity cache in applications that care
0038     // about activities.
0039 
0040     if (!activities) {
0041         activities = instance();
0042     }
0043 
0044     while (activities->serviceStatus() == KActivities::Consumer::Unknown) {
0045         QCoreApplication::instance()->processEvents();
0046     }
0047 
0048     return activities->currentActivity();
0049 }
0050 
0051 } // namespace ActivitiesSync