File indexing completed on 2025-01-05 03:57:55

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2004-11-22
0007  * Description : stand alone digiKam image editor - Open files
0008  *
0009  * SPDX-FileCopyrightText: 2004-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0010  *
0011  * SPDX-License-Identifier: GPL-2.0-or-later
0012  *
0013  * ============================================================ */
0014 
0015 #include "showfoto_p.h"
0016 
0017 namespace ShowFoto
0018 {
0019 
0020 void Showfoto::slotOpenFile()
0021 {
0022     if (!d->thumbBar->currentInfo().isNull() && !promptUserSave(d->thumbBar->currentUrl()))
0023     {
0024         return;
0025     }
0026 
0027     QList<QUrl> urls = Digikam::ImageDialog::getImageURLs(this, d->lastOpenedDirectory);
0028 
0029     if      (urls.count() > 1)
0030     {
0031         slotClearThumbBar();
0032 
0033         openUrls(urls);
0034         Q_EMIT signalInfoList(d->infoList);
0035 
0036         slotOpenUrl(d->thumbBar->currentInfo());
0037         toggleNavigation(1);
0038     }
0039     else if (urls.count() == 1)
0040     {
0041         slotClearThumbBar();
0042 
0043         openFolder(urls.first().adjusted(QUrl::RemoveFilename));
0044         Q_EMIT signalInfoList(d->infoList);
0045 
0046         slotOpenUrl(d->thumbBar->findItemByUrl(urls.first()));
0047         d->thumbBar->setCurrentUrl(urls.first());
0048     }
0049 }
0050 
0051 void Showfoto::slotOpenFolder()
0052 {
0053     if (!d->thumbBar->currentInfo().isNull() && !promptUserSave(d->thumbBar->currentUrl()))
0054     {
0055         return;
0056     }
0057 
0058     QUrl url = DFileDialog::getExistingDirectoryUrl(this, i18nc("@title:window", "Open Images from Folder"),
0059                                                     d->lastOpenedDirectory);
0060     if (!url.isEmpty())
0061     {
0062         slotClearThumbBar();
0063 
0064         openFolder(url);
0065         Q_EMIT signalInfoList(d->infoList);
0066 
0067         slotOpenUrl(d->thumbBar->currentInfo());
0068         toggleNavigation(1);
0069     }
0070 }
0071 
0072 void Showfoto::slotOpenFolderFromPath(const QString& path)
0073 {
0074     qCDebug(DIGIKAM_SHOWFOTO_LOG) << "Open folder from path =" << path;
0075 
0076     QFileInfo inf(path);
0077     slotClearThumbBar();
0078 
0079     if      (inf.isFile())
0080     {
0081         slotDroppedUrls(QList<QUrl>() << QUrl::fromLocalFile(inf.absolutePath()), false, QUrl());
0082         d->thumbBar->setCurrentUrl(QUrl::fromLocalFile(path));
0083         slotOpenUrl(d->thumbBar->currentInfo());
0084     }
0085     else if (inf.isDir())
0086     {
0087         QString dpath(path.endsWith(QLatin1Char('/')) ? path : path + QLatin1Char('/'));
0088         slotDroppedUrls(QList<QUrl>() << QUrl::fromLocalFile(dpath), false, QUrl());
0089         QList<QUrl> urls = d->thumbBar->urls();
0090 
0091         if (!urls.isEmpty())
0092         {
0093             d->thumbBar->setCurrentUrl(urls.first());
0094             slotOpenUrl(d->thumbBar->currentInfo());
0095         }
0096     }
0097     else
0098     {
0099         return;
0100     }
0101 
0102     applySortSettings();
0103 }
0104 
0105 void Showfoto::openUrls(const QList<QUrl>& urls)
0106 {
0107     if (urls.isEmpty())
0108     {
0109         return;
0110     }
0111 
0112     ShowfotoItemInfo iteminfo;
0113 
0114     for (QList<QUrl>::const_iterator it = urls.constBegin() ; it != urls.constEnd() ; ++it)
0115     {
0116         QFileInfo fi((*it).toLocalFile());
0117         iteminfo = ShowfotoItemInfo::itemInfoFromFile(fi);
0118 
0119         if (!d->infoList.contains(iteminfo))
0120         {
0121             d->infoList << iteminfo;
0122         }
0123     }
0124 }
0125 
0126 void Showfoto::openFolder(const QUrl& url)
0127 {
0128     if (!url.isValid() || !url.isLocalFile())
0129     {
0130         return;
0131     }
0132 
0133     d->lastOpenedDirectory = url;
0134 
0135     // Parse image IO mime types registration to get files filter pattern.
0136 
0137     QString filter;
0138     QStringList mimeTypes = supportedImageMimeTypes(QIODevice::ReadOnly, filter);
0139     QString patterns      = filter.toLower();
0140     patterns.append(QLatin1Char(' '));
0141     patterns.append(filter.toUpper());
0142 
0143     qCDebug(DIGIKAM_SHOWFOTO_LOG) << "patterns=" << patterns;
0144 
0145     // Get all image files from directory.
0146 
0147     QDir dir(url.toLocalFile(), patterns);
0148     dir.setFilter(QDir::Files);
0149 
0150     if (!dir.exists())
0151     {
0152         return;
0153     }
0154 
0155     QApplication::setOverrideCursor(Qt::WaitCursor);
0156 
0157     QFileInfoList fileinfolist = dir.entryInfoList();
0158 
0159     if (fileinfolist.isEmpty())
0160     {
0161         QApplication::restoreOverrideCursor();
0162         return;
0163     }
0164 
0165     QFileInfoList::const_iterator fi;
0166     ShowfotoItemInfo iteminfo;
0167 
0168     // And open all items in image editor.
0169 
0170     for (fi = fileinfolist.constBegin() ; fi != fileinfolist.constEnd() ; ++fi)
0171     {
0172         iteminfo = ShowfotoItemInfo::itemInfoFromFile(*fi);
0173 
0174         if (!d->infoList.contains(iteminfo))
0175         {
0176             d->infoList << iteminfo;
0177         }
0178     }
0179 
0180     QApplication::restoreOverrideCursor();
0181 }
0182 
0183 void Showfoto::slotOpenFilesfromPath(const QStringList& files, const QString& current)
0184 {
0185     if (files.isEmpty())
0186     {
0187         return;
0188     }
0189 
0190     slotClearThumbBar();
0191     QList<QUrl> urls;
0192 
0193     Q_FOREACH (const QString& path, files)
0194     {
0195         urls << QUrl::fromLocalFile(path);
0196     }
0197 
0198     qCDebug(DIGIKAM_GENERAL_LOG) << "slotOpenFilesfromPath:: urls list" << urls;
0199 
0200     openUrls(urls);
0201 
0202     Q_EMIT signalInfoList(d->infoList);
0203 
0204     QUrl curl = QUrl::fromLocalFile(current);
0205     slotOpenUrl(d->thumbBar->showfotoItemModel()->showfotoItemInfo(curl));
0206 
0207     int index = d->thumbBar->thumbnailIndexForUrl(curl);
0208     toggleNavigation(index);
0209 
0210     qCDebug(DIGIKAM_GENERAL_LOG) << "slotOpenFilesfromPath:: switch to thumb index" << index;
0211 
0212     applySortSettings();
0213     d->thumbBar->setCurrentUrl(curl);
0214 }
0215 
0216 void Showfoto::slotAppendFilesfromPath(const QStringList& files, const QString& current)
0217 {
0218     if (files.isEmpty())
0219     {
0220         return;
0221     }
0222 
0223     QList<QUrl> urls;
0224 
0225     Q_FOREACH (const QString& file, files)
0226     {
0227         urls << QUrl::fromLocalFile(file);
0228     }
0229 
0230     slotDroppedUrls(urls, false, QUrl::fromLocalFile(current));
0231     applySortSettings();
0232 }
0233 
0234 void Showfoto::slotDroppedUrls(const QList<QUrl>& droppedUrls, bool dropped, const QUrl& current)
0235 {
0236     if (droppedUrls.isEmpty())
0237     {
0238         return;
0239     }
0240 
0241     QList<QUrl> imagesUrls;
0242     QList<QUrl> foldersUrls;
0243 
0244     Q_FOREACH (const QUrl& drop, droppedUrls)
0245     {
0246         if (drop.isValid())
0247         {
0248             QFileInfo info(drop.toLocalFile());
0249             QUrl url(QUrl::fromLocalFile(info.canonicalFilePath()));
0250 
0251             if (isReadableImageFile(info.absoluteFilePath()))
0252             {
0253                 imagesUrls << url;
0254             }
0255 
0256             if (info.isDir())
0257             {
0258                 foldersUrls << url;
0259             }
0260         }
0261     }
0262 
0263     if (!imagesUrls.isEmpty())
0264     {
0265         openUrls(imagesUrls);
0266     }
0267 
0268     if (!foldersUrls.isEmpty())
0269     {
0270         Q_FOREACH (const QUrl& fUrl, foldersUrls)
0271         {
0272             openFolder(fUrl);
0273         }
0274     }
0275 
0276     if (!d->infoList.isEmpty())
0277     {
0278         if (!dropped && foldersUrls.isEmpty() && (imagesUrls.count() == 1))
0279         {
0280             openFolder(imagesUrls.first().adjusted(QUrl::RemoveFilename));
0281             d->model->clearShowfotoItemInfos();
0282             Q_EMIT signalInfoList(d->infoList);
0283 
0284             slotOpenUrl(d->thumbBar->findItemByUrl(current.isValid() ? current : imagesUrls.first()));
0285             d->thumbBar->setCurrentUrl(current.isValid() ? current : imagesUrls.first());
0286 
0287             return;
0288         }
0289 
0290         d->model->clearShowfotoItemInfos();
0291 
0292         Q_EMIT signalInfoList(d->infoList);
0293 
0294         slotOpenUrl(d->thumbBar->currentInfo());
0295     }
0296     else
0297     {
0298         QMessageBox::information(this, qApp->applicationName(),
0299                                  i18n("There is no item to process."));
0300         qCWarning(DIGIKAM_SHOWFOTO_LOG) << "infolist is empty..";
0301     }
0302 }
0303 
0304 void Showfoto::slotOpenUrl(const ShowfotoItemInfo& info)
0305 {
0306     if (d->thumbBar->currentInfo().isNull() || info.isNull())
0307     {
0308         return;
0309     }
0310 
0311     QString localFile;
0312 
0313     if (info.url.isLocalFile())
0314     {
0315         // file protocol. We do not need the network
0316 
0317         localFile = info.url.toLocalFile();
0318     }
0319     else
0320     {
0321         QMessageBox::critical(this, i18nc("@title:window", "Error Loading File"),
0322                               i18n("Failed to load file: \"%1\"\n"
0323                                    "Remote file handling is not supported",
0324                                    info.url.fileName()));
0325         return;
0326     }
0327 
0328     d->currentLoadedUrl = info.url;
0329     d->folderView->setCurrentPath(localFile);
0330     m_canvas->load(localFile, m_IOFileSettings);
0331 
0332     //TODO : add preload here like in ImageWindow::slotLoadCurrent() ???
0333 }
0334 
0335 void Showfoto::slotAddedDropedItems(QDropEvent* e)
0336 {
0337     QList<QUrl> list = e->mimeData()->urls();
0338     QList<QUrl> urls;
0339 
0340     Q_FOREACH (const QUrl& url, list)
0341     {
0342         QFileInfo fi(url.toLocalFile());
0343 
0344         if (fi.exists())
0345         {
0346             urls.append(url);
0347         }
0348     }
0349 
0350     e->accept();
0351 
0352     if (!urls.isEmpty())
0353     {
0354         slotDroppedUrls(urls, true, QUrl());
0355     }
0356 }
0357 
0358 void Showfoto::slotFileWithDefaultApplication()
0359 {
0360     Digikam::DFileOperations::openFilesWithDefaultApplication(QList<QUrl>() << d->thumbBar->currentUrl());
0361 }
0362 
0363 void Showfoto::slotOpenWith(QAction* action)
0364 {
0365     openWith(d->thumbBar->currentUrl(), action);
0366 }
0367 
0368 } // namespace ShowFoto