File indexing completed on 2025-01-19 03:53:24

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2007-04-09
0007  * Description : Collection location management
0008  *
0009  * SPDX-FileCopyrightText: 2007-2009 by Marcel Wiesweg <marcel dot wiesweg at gmx dot de>
0010  *
0011  * SPDX-License-Identifier: GPL-2.0-or-later
0012  *
0013  * ============================================================ */
0014 
0015 #include "collectionmanager_p.h"
0016 
0017 /**
0018  * NOTE: This is because of the CollectionManager private slot.
0019  */
0020 #include "moc_collectionmanager.cpp"
0021 
0022 namespace Digikam
0023 {
0024 
0025 CollectionManager* CollectionManager::m_instance = nullptr;
0026 
0027 CollectionManager* CollectionManager::instance()
0028 {
0029     if (!m_instance)
0030     {
0031         m_instance = new CollectionManager;
0032     }
0033 
0034     return m_instance;
0035 }
0036 
0037 void CollectionManager::cleanUp()
0038 {
0039     delete m_instance;
0040     m_instance = nullptr;
0041 }
0042 
0043 CollectionManager::CollectionManager()
0044     : d(new Private(this))
0045 {
0046     qRegisterMetaType<CollectionLocation>("CollectionLocation");
0047 
0048     connect(Solid::DeviceNotifier::instance(), SIGNAL(deviceAdded(QString)),
0049             this, SLOT(deviceAdded(QString)));
0050 
0051     connect(Solid::DeviceNotifier::instance(), SIGNAL(deviceRemoved(QString)),
0052             this, SLOT(deviceRemoved(QString)));
0053 
0054     // CoreDbWatch slot is connected at construction of CoreDbWatch, which may be later.
0055 }
0056 
0057 CollectionManager::~CollectionManager()
0058 {
0059     qDeleteAll(d->locations);
0060     delete d;
0061 }
0062 
0063 void CollectionManager::refresh()
0064 {
0065     clearLocations();
0066     updateLocations();
0067 }
0068 
0069 void CollectionManager::setWatchDisabled()
0070 {
0071     d->watchEnabled = false;
0072 }
0073 
0074 void CollectionManager::deviceAdded(const QString& udi)
0075 {
0076     if (!d->watchEnabled)
0077     {
0078         return;
0079     }
0080 
0081     Solid::Device device(udi);
0082 
0083     if (device.is<Solid::StorageAccess>())
0084     {
0085         updateLocations();
0086     }
0087 }
0088 
0089 void CollectionManager::deviceRemoved(const QString& udi)
0090 {
0091     if (!d->watchEnabled)
0092     {
0093         return;
0094     }
0095 
0096     // we can't access the Solid::Device to check because it is removed
0097     {
0098         QReadLocker readLocker(&d->lock);
0099 
0100         if (!d->udisToWatch.contains(udi))
0101         {
0102             return;
0103         }
0104     }
0105 
0106     updateLocations();
0107 }
0108 
0109 void CollectionManager::accessibilityChanged(bool accessible, const QString& udi)
0110 {
0111     Q_UNUSED(accessible);
0112     Q_UNUSED(udi);
0113     updateLocations();
0114 }
0115 
0116 void CollectionManager::clearLocations()
0117 {
0118     QMap<int, AlbumRootLocation*> oldLocations;
0119 
0120     {
0121         QWriteLocker locker(&d->lock);
0122 
0123         oldLocations = d->locations;
0124         d->locations.clear();
0125     }
0126 
0127     Q_FOREACH (AlbumRootLocation* const location, oldLocations)
0128     {
0129         CollectionLocation::Status statusOld = location->status();
0130         location->setStatus(CollectionLocation::LocationDeleted);
0131         Q_EMIT locationStatusChanged(*location, statusOld);
0132 
0133         delete location;
0134     }
0135 }
0136 
0137 } // namespace Digikam