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

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2007-03-19
0007  * Description : Core database Url interface.
0008  *
0009  * SPDX-FileCopyrightText: 2007-2008 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 "coredburl.h"
0016 
0017 // Qt includes
0018 
0019 #include <QUrlQuery>
0020 #include <QStringList>
0021 
0022 // Local includes
0023 
0024 #include "collectionmanager.h"
0025 #include "collectionlocation.h"
0026 #include "digikam_debug.h"
0027 #include "digikam_globals.h"
0028 
0029 namespace Digikam
0030 {
0031 
0032 CoreDbUrl CoreDbUrl::fromFileUrl(const QUrl& fileUrl,
0033                                  const QUrl& albumRoot,
0034                                  const DbEngineParameters& parameters)
0035 {
0036     CollectionLocation location = CollectionManager::instance()->locationForAlbumRoot(albumRoot);
0037 
0038     return fromFileUrl(fileUrl, albumRoot, location.id(), parameters);
0039 }
0040 
0041 CoreDbUrl CoreDbUrl::fromFileUrl(const QUrl& fileUrl,
0042                                  const QUrl& albumRoot,
0043                                  int   albumRootId,
0044                                  const DbEngineParameters& parameters)
0045 {
0046     CoreDbUrl url;
0047     url.setScheme(QLatin1String("digikamalbums"));
0048 
0049     // get album root path without trailing slash
0050 
0051     QString albumRootPath = albumRoot.adjusted(QUrl::StripTrailingSlash).toLocalFile();
0052 
0053     // get the hierarchy below the album root
0054 
0055     QString pathUnderRoot = fileUrl.toLocalFile().remove(albumRootPath);
0056     url.setPath(pathUnderRoot);
0057 
0058     QUrlQuery q(url);
0059     q.addQueryItem(QLatin1String("albumRoot"),   albumRootPath);
0060     q.addQueryItem(QLatin1String("albumRootId"), QString::number(albumRootId));
0061     url.setQuery(q);
0062 
0063     url.setParameters(parameters);
0064 
0065 //    qCDebug(DIGIKAM_COREDB_LOG) << "CoreDbUrl::fromFileUrl : " << url.toDisplayString();
0066 
0067     return url;
0068 }
0069 
0070 CoreDbUrl CoreDbUrl::fromAlbumAndName(const QString& name,
0071                                       const QString& album,
0072                                       const QUrl& albumRoot,
0073                                       const DbEngineParameters& parameters)
0074 {
0075     CollectionLocation location = CollectionManager::instance()->locationForAlbumRoot(albumRoot);
0076 
0077     return fromAlbumAndName(name, album, albumRoot, location.id(), parameters);
0078 }
0079 
0080 CoreDbUrl CoreDbUrl::fromAlbumAndName(const QString& name,
0081                                       const QString& album,
0082                                       const QUrl& albumRoot,
0083                                       int   albumRootId,
0084                                       const DbEngineParameters& parameters)
0085 {
0086     CoreDbUrl url;
0087     QString path(album);
0088     url.setScheme(QLatin1String("digikamalbums"));
0089 
0090     if (path != QLatin1String("/"))
0091         path += QLatin1Char('/');
0092 
0093     url.setPath(path + name);
0094 
0095     QUrlQuery q(url);
0096     q.addQueryItem(QLatin1String("albumRoot"),   albumRoot.adjusted(QUrl::StripTrailingSlash).toLocalFile());
0097     q.addQueryItem(QLatin1String("albumRootId"), QString::number(albumRootId));
0098     url.setQuery(q);
0099     url.setParameters(parameters);
0100 
0101 //    qCDebug(DIGIKAM_COREDB_LOG) << "CoreDbUrl::fromAlbumAndName : " << url.toDisplayString();
0102 
0103     return url;
0104 }
0105 
0106 CoreDbUrl CoreDbUrl::albumUrl(const DbEngineParameters& parameters)
0107 {
0108     CoreDbUrl url;
0109     url.setScheme(QLatin1String("digikamalbums"));
0110     url.setParameters(parameters);
0111 
0112     qCDebug(DIGIKAM_COREDB_LOG) << "CoreDbUrl::albumUrl : " << url.toDisplayString();
0113     return url;
0114 }
0115 
0116 CoreDbUrl CoreDbUrl::fromTagIds(const QList<int>& tagIds,
0117                                 const DbEngineParameters& parameters)
0118 {
0119     CoreDbUrl url;
0120     url.setScheme(QLatin1String("digikamtags"));
0121 
0122     for (QList<int>::const_iterator it = tagIds.constBegin(); it != tagIds.constEnd(); ++it)
0123     {
0124         url.setPath(url.path() + QLatin1Char('/') + QString::number(*it));
0125     }
0126 
0127     url.setParameters(parameters);
0128 
0129 //    qCDebug(DIGIKAM_COREDB_LOG) << "CoreDbUrl::fromTagIds : " << url.toDisplayString();
0130 
0131     return url;
0132 }
0133 
0134 CoreDbUrl CoreDbUrl::dateUrl(const DbEngineParameters& parameters)
0135 {
0136     CoreDbUrl url;
0137     url.setScheme(QLatin1String("digikamdates"));
0138     url.setParameters(parameters);
0139 
0140 //    qCDebug(DIGIKAM_COREDB_LOG) << "CoreDbUrl::dateUrl : " << url.toDisplayString();
0141 
0142     return url;
0143 }
0144 
0145 CoreDbUrl CoreDbUrl::fromDateForMonth(const QDate& date, const DbEngineParameters& parameters)
0146 {
0147     QDate firstDayOfMonth(date.year(), date.month(), 1);
0148     QDate firstDayOfNextMonth = firstDayOfMonth.addMonths(1);
0149 
0150     return (fromDateRange(firstDayOfMonth, firstDayOfNextMonth, parameters));
0151 }
0152 
0153 CoreDbUrl CoreDbUrl::fromDateForYear(const QDate& date, const DbEngineParameters& parameters)
0154 {
0155     QDate firstDayOfYear(date.year(), 1, 1);
0156     QDate firstDayOfNextYear = firstDayOfYear.addYears(1);
0157 
0158     return (fromDateRange(firstDayOfYear, firstDayOfNextYear, parameters));
0159 }
0160 
0161 CoreDbUrl CoreDbUrl::fromDateRange(const QDate& startDate,
0162                                    const QDate& endDate,
0163                                    const DbEngineParameters& parameters)
0164 {
0165     CoreDbUrl url;
0166     url.setScheme(QLatin1String("digikamdates"));
0167     url.setPath(startDate.toString(Qt::ISODate) + QLatin1Char('/') + endDate.toString(Qt::ISODate));
0168     url.setParameters(parameters);
0169 
0170 //    qCDebug(DIGIKAM_COREDB_LOG) << "CoreDbUrl::fromDateRange : " << url.toDisplayString();
0171 
0172     return url;
0173 }
0174 
0175 CoreDbUrl CoreDbUrl::mapImagesUrl(const DbEngineParameters& parameters)
0176 {
0177     CoreDbUrl url;
0178     url.setScheme(QLatin1String("digikammapimages"));
0179     url.setParameters(parameters);
0180 
0181 //    qCDebug(DIGIKAM_COREDB_LOG) << "CoreDbUrl::mapImagesUrl : " << url.toDisplayString();
0182 
0183     return url;
0184 }
0185 
0186 CoreDbUrl CoreDbUrl::fromAreaRange(const qreal lat1, const qreal lng1,
0187                                    const qreal lat2, const qreal lng2,
0188                                    const DbEngineParameters& parameters)
0189 {
0190     CoreDbUrl url;
0191     url.setScheme(QLatin1String("digikammapimages"));
0192 
0193     QUrlQuery q(url);
0194     q.addQueryItem(QLatin1String("lat1"), QString::number(lat1));
0195     q.addQueryItem(QLatin1String("lon1"), QString::number(lng1));
0196     q.addQueryItem(QLatin1String("lat2"), QString::number(lat2));
0197     q.addQueryItem(QLatin1String("lon2"), QString::number(lng2));
0198     url.setQuery(q);
0199 
0200     url.setParameters(parameters);
0201 
0202 //    qCDebug(DIGIKAM_COREDB_LOG) << "CoreDbUrl::fromAreaRange : " << url.toDisplayString();
0203 
0204     return url;
0205 }
0206 
0207 CoreDbUrl CoreDbUrl::searchUrl(int id, const DbEngineParameters& parameters)
0208 {
0209     CoreDbUrl url;
0210     url.setScheme(QLatin1String("digikamsearch"));
0211 
0212     QUrlQuery q(url);
0213     q.addQueryItem(QLatin1String("searchId"), QString::number(id));
0214     url.setQuery(q);
0215 
0216     url.setParameters(parameters);
0217 
0218 //    qCDebug(DIGIKAM_COREDB_LOG) << "CoreDbUrl::searchUrl : " << url.toDisplayString();
0219 
0220     return url;
0221 }
0222 
0223 CoreDbUrl::CoreDbUrl(const QUrl& digikamalbumsUrl)
0224     : QUrl(digikamalbumsUrl)
0225 {
0226 }
0227 
0228 CoreDbUrl::CoreDbUrl(const CoreDbUrl& url)
0229     : QUrl(url)
0230 {
0231 }
0232 
0233 CoreDbUrl::CoreDbUrl()
0234 {
0235 }
0236 
0237 CoreDbUrl& CoreDbUrl::operator=(const QUrl& digikamalbumsUrl)
0238 {
0239     QUrl::operator=(digikamalbumsUrl);
0240 
0241     return *this;
0242 }
0243 
0244 CoreDbUrl& CoreDbUrl::operator=(const CoreDbUrl& url)
0245 {
0246     QUrl::operator=(url);
0247 
0248     return *this;
0249 }
0250 
0251 bool CoreDbUrl::operator==(const QUrl& digikamalbumsUrl) const
0252 {
0253     return ( QUrl::operator==(digikamalbumsUrl) );
0254 }
0255 
0256 /*
0257 CoreDbUrl::operator DbEngineParameters() const
0258 {
0259     return parameters();
0260 }
0261 */
0262 
0263 // --- Database parameters ---------------------------------------------------------------------
0264 
0265 DbEngineParameters CoreDbUrl::parameters() const
0266 {
0267     return DbEngineParameters(*this);
0268 }
0269 
0270 void CoreDbUrl::setParameters(const DbEngineParameters& parameters)
0271 {
0272     parameters.insertInUrl(*this);
0273 }
0274 
0275 // --- Protocol --------------------------------------------------------------------------------
0276 
0277 bool CoreDbUrl::isAlbumUrl() const
0278 {
0279     return (scheme() == QLatin1String("digikamalbums"));
0280 }
0281 
0282 bool CoreDbUrl::isTagUrl() const
0283 {
0284     return (scheme() == QLatin1String("digikamtags"));
0285 }
0286 
0287 bool CoreDbUrl::isDateUrl() const
0288 {
0289     return (scheme() == QLatin1String("digikamdates"));
0290 }
0291 
0292 bool CoreDbUrl::isMapImagesUrl() const
0293 {
0294     return (scheme() == QLatin1String("digikammapimages"));
0295 }
0296 
0297 bool CoreDbUrl::isSearchUrl() const
0298 {
0299     return (scheme() == QLatin1String("digikamsearch"));
0300 }
0301 
0302 // --- Album URL ----------------------------------------------------------------------------
0303 
0304 QUrl CoreDbUrl::albumRoot() const
0305 {
0306     QString albumRoot = QUrlQuery(*this).queryItemValue(QLatin1String("albumRoot"));
0307 
0308     if (!albumRoot.isNull())
0309     {
0310         return QUrl::fromLocalFile(albumRoot);
0311     }
0312 
0313     return QUrl();
0314 }
0315 
0316 QString CoreDbUrl::albumRootPath() const
0317 {
0318     return (QUrlQuery(*this).queryItemValue(QLatin1String("albumRoot")));
0319 }
0320 
0321 int CoreDbUrl::albumRootId() const
0322 {
0323     return (QUrlQuery(*this).queryItemValue(QLatin1String("albumRootId")).toInt());
0324 }
0325 
0326 QString CoreDbUrl::album() const
0327 {
0328     // obey trailing slash in the path - albums have a trailing slash
0329     // get result without trailing slash
0330 
0331     QUrl url = adjusted(QUrl::RemoveFilename);
0332 
0333     return (url.adjusted(QUrl::StripTrailingSlash).path());
0334 }
0335 
0336 QString CoreDbUrl::name() const
0337 {
0338     // do not ignore trailing slash in the path - albums have a trailing slash
0339     return fileName();
0340 }
0341 
0342 QUrl CoreDbUrl::fileUrl() const
0343 {
0344     return QUrl::fromLocalFile(albumRoot().toLocalFile() + path());
0345 }
0346 
0347 // --- Tag URL ------------------------------------------------------------------------
0348 
0349 int CoreDbUrl::tagId() const
0350 {
0351     if (path() == QLatin1String("/"))
0352     {
0353         return (-1);
0354     }
0355 
0356     return (fileName().toInt());
0357 }
0358 
0359 QList<int> CoreDbUrl::tagIds() const
0360 {
0361     QList<int>  ids;
0362     QStringList stringIds = path().split(QLatin1Char('/'), QT_SKIP_EMPTY_PARTS);
0363 
0364     for (int i = 0 ; i < stringIds.count() ; ++i)
0365     {
0366         ids << stringIds.at(i).toInt();
0367     }
0368 
0369     return ids;
0370 }
0371 
0372 // --- Date URL -----------------------------------------------------------------------
0373 
0374 QDate CoreDbUrl::startDate() const
0375 {
0376     QStringList dates = path().split(QLatin1Char('/'));
0377 
0378     if (dates.size() >= 1)
0379     {
0380         return QDate::fromString(dates.at(0), Qt::ISODate);
0381     }
0382 
0383     return QDate();
0384 }
0385 
0386 QDate CoreDbUrl::endDate() const
0387 {
0388     QStringList dates = path().split(QLatin1Char('/'));
0389 
0390     if (dates.size() >= 2)
0391     {
0392         return QDate::fromString(dates.at(1), Qt::ISODate);
0393     }
0394     else
0395     {
0396         return QDate();
0397     }
0398 }
0399 
0400 // --- MapImages URL --------------------------------------------------------------------------------
0401 
0402 bool CoreDbUrl::areaCoordinates(double* lat1, double* lat2, double* lon1, double* lon2) const
0403 {
0404     bool ok;
0405     bool allOk = true;
0406     *lat1      = QUrlQuery(*this).queryItemValue(QLatin1String("lat1")).toDouble(&ok);
0407     allOk      = ok && allOk;
0408     *lat2      = QUrlQuery(*this).queryItemValue(QLatin1String("lat2")).toDouble(&ok);
0409     allOk      = ok && allOk;
0410     *lon1      = QUrlQuery(*this).queryItemValue(QLatin1String("lon1")).toDouble(&ok);
0411     allOk      = ok && allOk;
0412     *lon2      = QUrlQuery(*this).queryItemValue(QLatin1String("lon2")).toDouble(&ok);
0413     allOk      = ok && allOk;
0414 
0415     return allOk;
0416 }
0417 
0418 // --- Search URL --------------------------------------------------------------------
0419 
0420 int CoreDbUrl::searchId() const
0421 {
0422     return QUrlQuery(*this).queryItemValue(QLatin1String("searchId")).toInt();
0423 }
0424 
0425 } // namespace Digikam