File indexing completed on 2025-01-19 03:55:51

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2018-02-24
0007  * Description : Container for IOJob data.
0008  *
0009  * SPDX-FileCopyrightText: 2018 by Maik Qualmann <metzpinguin at gmail dot com>
0010  *
0011  * SPDX-License-Identifier: GPL-2.0-or-later
0012  *
0013  * ============================================================ */
0014 
0015 #include "iojobdata.h"
0016 
0017 // Qt includes
0018 
0019 #include <QRecursiveMutex>
0020 #include <QMutexLocker>
0021 
0022 // Local includes
0023 
0024 #include "album.h"
0025 #include "iteminfo.h"
0026 #include "digikam_debug.h"
0027 
0028 namespace Digikam
0029 {
0030 
0031 class Q_DECL_HIDDEN IOJobData::Private
0032 {
0033 public:
0034 
0035     explicit Private()
0036       : operation    (Unknown),
0037         fileConflict (Continue),
0038         errorOrCancel(false),
0039         srcAlbum     (nullptr),
0040         destAlbum    (nullptr),
0041         jobTime      (QDateTime::currentDateTime())
0042     {
0043     }
0044 
0045     int                operation;
0046     int                fileConflict;
0047 
0048     bool               errorOrCancel;
0049 
0050     PAlbum*            srcAlbum;
0051     PAlbum*            destAlbum;
0052 
0053     DTrashItemInfoList trashItemList;
0054     QMap<QUrl, QUrl>   changeDestMap;
0055     QList<ItemInfo>    itemInfosList;
0056     QList<QUrl>        sourceUrlList;
0057     QList<int>         sourceAlbumIds;
0058 
0059     QUrl               destUrl;
0060 
0061     QString            progressId;
0062     QDateTime          jobTime;
0063 
0064     QRecursiveMutex    mutex;
0065 };
0066 
0067 IOJobData::IOJobData(int operation,
0068                      const QList<ItemInfo>& infos,
0069                      PAlbum* const dest)
0070     : d(new Private)
0071 {
0072     d->operation = operation;
0073     d->destAlbum = dest;
0074 
0075     setItemInfos(infos);
0076 
0077     if (d->destAlbum)
0078     {
0079         d->destUrl = d->destAlbum->fileUrl();
0080     }
0081 }
0082 
0083 IOJobData::IOJobData(int operation,
0084                      const QList<ItemInfo>& infos,
0085                      const QUrl& dest)
0086     : d(new Private)
0087 {
0088     d->operation = operation;
0089     d->destUrl   = dest;
0090 
0091     setItemInfos(infos);
0092 }
0093 
0094 IOJobData::IOJobData(int operation,
0095                      const QList<QUrl>& urls,
0096                      PAlbum* const dest)
0097     : d(new Private)
0098 {
0099     d->operation     = operation;
0100     d->sourceUrlList = urls;
0101     d->destAlbum     = dest;
0102 
0103     if (d->destAlbum)
0104     {
0105         d->destUrl = d->destAlbum->fileUrl();
0106     }
0107 }
0108 
0109 IOJobData::IOJobData(int operation,
0110                      PAlbum* const src,
0111                      PAlbum* const dest)
0112     : d(new Private)
0113 {
0114     d->operation = operation;
0115     d->srcAlbum  = src;
0116     d->destAlbum = dest;
0117 
0118     if (d->srcAlbum)
0119     {
0120         d->sourceUrlList << d->srcAlbum->fileUrl();
0121     }
0122 
0123     if (d->destAlbum)
0124     {
0125         d->destUrl = d->destAlbum->fileUrl();
0126     }
0127 }
0128 
0129 IOJobData::IOJobData(int operation,
0130                      const QList<QUrl>& urls,
0131                      const QUrl& dest)
0132     : d(new Private)
0133 {
0134     d->operation     = operation;
0135     d->sourceUrlList = urls;
0136     d->destUrl       = dest;
0137 }
0138 
0139 IOJobData::IOJobData(int operation,
0140                      const ItemInfo& info,
0141                      const QString& newName,
0142                      bool overwrite)
0143     : d(new Private)
0144 {
0145     d->operation = operation;
0146 
0147     if (overwrite)
0148     {
0149         d->fileConflict = Overwrite;
0150     }
0151 
0152     setItemInfos(QList<ItemInfo>() << info);
0153 
0154     d->destUrl = info.fileUrl().adjusted(QUrl::RemoveFilename);
0155     d->destUrl.setPath(d->destUrl.path() + newName);
0156 }
0157 
0158 IOJobData::IOJobData(int operation,
0159                      const DTrashItemInfoList& infos)
0160     : d(new Private)
0161 {
0162     d->operation     = operation;
0163     d->trashItemList = infos;
0164 
0165     // We need source URLs as dummy.
0166 
0167     Q_FOREACH (const DTrashItemInfo& item, d->trashItemList)
0168     {
0169         d->sourceUrlList << QUrl::fromLocalFile(item.trashPath);
0170     }
0171 }
0172 
0173 IOJobData::~IOJobData()
0174 {
0175     delete d;
0176 }
0177 
0178 void IOJobData::setItemInfos(const QList<ItemInfo>& infos)
0179 {
0180     d->itemInfosList = infos;
0181 
0182     d->sourceUrlList.clear();
0183     d->sourceAlbumIds.clear();
0184 
0185     Q_FOREACH (const ItemInfo& info, d->itemInfosList)
0186     {
0187         d->sourceUrlList << info.fileUrl();
0188 
0189         if (!d->sourceAlbumIds.contains(info.albumId()))
0190         {
0191             d->sourceAlbumIds << info.albumId();
0192         }
0193     }
0194 }
0195 
0196 void IOJobData::setSourceUrls(const QList<QUrl>& urls)
0197 {
0198     d->sourceUrlList = urls;
0199 }
0200 
0201 void IOJobData::setDestUrl(const QUrl& srcUrl,
0202                            const QUrl& destUrl)
0203 {
0204     QMutexLocker locker(&d->mutex);
0205 
0206     d->changeDestMap.insert(srcUrl, destUrl);
0207 }
0208 
0209 void IOJobData::setProgressId(const QString& id)
0210 {
0211     d->progressId = id;
0212 }
0213 
0214 void IOJobData::setFileConflict(int fc)
0215 {
0216     d->fileConflict = fc;
0217 }
0218 
0219 void IOJobData::setErrorOrCancel(bool err)
0220 {
0221     d->errorOrCancel |= err;
0222 }
0223 
0224 int IOJobData::operation() const
0225 {
0226     return d->operation;
0227 }
0228 
0229 int IOJobData::fileConflict() const
0230 {
0231     return d->fileConflict;
0232 }
0233 
0234 bool IOJobData::errorOrCancel() const
0235 {
0236     return d->errorOrCancel;
0237 }
0238 
0239 PAlbum* IOJobData::srcAlbum() const
0240 {
0241     return d->srcAlbum;
0242 }
0243 
0244 PAlbum* IOJobData::destAlbum() const
0245 {
0246     return d->destAlbum;
0247 }
0248 
0249 QUrl IOJobData::destUrl(const QUrl& srcUrl) const
0250 {
0251     QMutexLocker locker(&d->mutex);
0252 
0253     if (srcUrl.isEmpty())
0254     {
0255         return d->destUrl;
0256     }
0257 
0258     return d->changeDestMap.value(srcUrl, d->destUrl);
0259 }
0260 
0261 QUrl IOJobData::getNextUrl() const
0262 {
0263     QMutexLocker locker(&d->mutex);
0264 
0265     QUrl url;
0266 
0267     if (!d->sourceUrlList.isEmpty())
0268     {
0269         url = d->sourceUrlList.takeFirst();
0270     }
0271 
0272     return url;
0273 }
0274 
0275 QString IOJobData::destName(const QUrl& srcUrl) const
0276 {
0277     QMutexLocker locker(&d->mutex);
0278 
0279     QUrl url = srcUrl.adjusted(QUrl::StripTrailingSlash);
0280     url      = d->changeDestMap.value(srcUrl, url);
0281 
0282     return url.fileName();
0283 }
0284 
0285 QString IOJobData::getProgressId() const
0286 {
0287     return d->progressId;
0288 }
0289 
0290 QDateTime IOJobData::jobTime() const
0291 {
0292     return d->jobTime;
0293 }
0294 
0295 ItemInfo IOJobData::findItemInfo(const QUrl& url) const
0296 {
0297     Q_FOREACH (const ItemInfo& info, d->itemInfosList)
0298     {
0299         if (info.fileUrl() == url)
0300         {    // cppcheck-suppress useStlAlgorithm
0301             return info;
0302         }
0303     }
0304 
0305     return ItemInfo();
0306 }
0307 
0308 QList<int> IOJobData::srcAlbumIds() const
0309 {
0310     return d->sourceAlbumIds;
0311 }
0312 
0313 QList<QUrl> IOJobData::sourceUrls() const
0314 {
0315     return d->sourceUrlList;
0316 }
0317 
0318 QList<ItemInfo> IOJobData::itemInfos() const
0319 {
0320     return d->itemInfosList;
0321 }
0322 
0323 DTrashItemInfoList IOJobData::trashItems() const
0324 {
0325     return d->trashItemList;
0326 }
0327 
0328 } // namespace Digikam