File indexing completed on 2024-05-05 04:47:05

0001 // Copyright 2018-2020 Camilo Higuita <milo.h@aol.com>
0002 // Copyright 2018-2020 Nitrux Latinoamericana S.C.
0003 //
0004 // SPDX-License-Identifier: GPL-3.0-or-later
0005 
0006 /***
0007 Pix  Copyright (C) 2018  Camilo Higuita
0008 This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
0009 This is free software, and you are welcome to redistribute it
0010 under certain conditions; type `show c' for details.
0011 
0012  This program is free software: you can redistribute it and/or modify
0013 it under the terms of the GNU General Public License as published by
0014 the Free Software Foundation, either version 3 of the License, or
0015 (at your option) any later version.
0016 
0017 This program is distributed in the hope that it will be useful,
0018 but WITHOUT ANY WARRANTY; without even the implied warranty of
0019 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0020 GNU General Public License for more details.
0021 
0022 You should have received a copy of the GNU General Public License
0023 along with this program.  If not, see <http://www.gnu.org/licenses/>.
0024 ***/
0025 
0026 #include "pix.h"
0027 #include <QDesktopServices>
0028 #include <QDebug>
0029 #include <QCoreApplication>
0030 #include <QSettings>
0031 
0032 #include <MauiKit3/Core/fmh.h>
0033 
0034 #include <MauiKit3/FileBrowsing/tagging.h>
0035 #include <MauiKit3/FileBrowsing/fmstatic.h>
0036 
0037 #include "models/gallery/gallery.h"
0038 
0039 Pix *Pix::m_instance = nullptr;
0040 
0041 Pix::Pix(QObject *parent) : QObject(parent)
0042   ,m_allImagesModel(nullptr)
0043 {
0044     connect(qApp, &QCoreApplication::aboutToQuit, []()
0045     {
0046         delete m_instance;
0047         m_instance = nullptr;
0048     });
0049 }
0050 
0051 QUrl Pix::cameraPath()
0052 {
0053     const static auto paths = QStringList{FMStatic::HomePath + "/DCIM/Camera", FMStatic::HomePath + "/Camera", FMStatic::PicturesPath+"/Camera", FMStatic::PicturesPath+"/camera"};
0054 
0055     for (const auto &path : paths)
0056     {
0057         if (FMH::fileExists(path))
0058             return QUrl(path);
0059     }
0060 
0061     return QUrl();
0062 }
0063 
0064 QUrl Pix::screenshotsPath()
0065 {
0066     const static auto paths = QStringList{FMStatic::HomePath + "/DCIM/Screenshots", FMStatic::HomePath + "/Screenshots",FMStatic::PicturesPath+"/screenshots" , FMStatic::PicturesPath+"/Screenshots"};
0067 
0068     for (const auto &path : paths) {
0069         if (FMH::fileExists(path))
0070             return QUrl(path);
0071     }
0072 
0073     return QUrl();
0074 }
0075 
0076 Gallery *Pix::allImagesModel()
0077 {
0078     if(!m_allImagesModel)
0079     {
0080         m_allImagesModel = new Gallery(this);
0081         m_allImagesModel->setUrls(QUrl::fromStringList(sources()));
0082         m_allImagesModel->setRecursive(true);
0083         m_allImagesModel->componentComplete(); //call this to actually get the data
0084         connect(this, &Pix::sourcesChanged, [this]()
0085         {
0086             m_allImagesModel->setUrls(QUrl::fromStringList(sources()));
0087         });
0088     }
0089     return m_allImagesModel;
0090 }
0091 
0092 const QStringList Pix::getSourcePaths()
0093 {
0094     static const auto defaultSources = QStringList{FMStatic::PicturesPath, FMStatic::DownloadsPath} << cameraPath().toString() + screenshotsPath().toString();
0095     QSettings settings;
0096     settings.beginGroup("Settings");
0097     const auto sources = settings.value("Sources", defaultSources).toStringList();
0098     settings.endGroup();
0099     qDebug() << "SOURCES" << sources;
0100     return sources;
0101 }
0102 
0103 void Pix::saveSourcePath(const QStringList &paths)
0104 {
0105     auto sources = getSourcePaths();
0106 
0107     sources << paths;
0108     sources.removeDuplicates();
0109 
0110     QSettings settings;
0111     settings.beginGroup("Settings");
0112     settings.setValue("Sources", sources);
0113     settings.endGroup();
0114 }
0115 
0116 void Pix::removeSourcePath(const QString &path)
0117 {
0118     auto sources = getSourcePaths();
0119     sources.removeOne(path);
0120 
0121     QSettings settings;
0122     settings.beginGroup("Settings");
0123     settings.setValue("Sources", sources);
0124     settings.endGroup();
0125 }
0126 
0127 QVariantList Pix::sourcesModel() const
0128 {
0129     QVariantList res;
0130     const auto sources = getSourcePaths();
0131     return std::accumulate(sources.constBegin(), sources.constEnd(), res, [](QVariantList &res, const QString &url) {
0132         res << FMStatic::getFileInfo(url);
0133         return res;
0134     });
0135 }
0136 
0137 QStringList Pix::sources() const
0138 {
0139     return getSourcePaths();
0140 }
0141 
0142 void Pix::openPics(const QList<QUrl> &pics)
0143 {
0144     Q_EMIT this->viewPics(QUrl::toStringList(pics));
0145 }
0146 
0147 void Pix::showInFolder(const QStringList &urls)
0148 {
0149     for (const auto &url : urls)
0150         QDesktopServices::openUrl(FMStatic::fileDir(url));
0151 }
0152 
0153 void Pix::addSources(const QStringList &paths)
0154 {
0155     saveSourcePath(paths);
0156     Q_EMIT sourcesChanged();
0157 }
0158 
0159 void Pix::removeSources(const QString &path)
0160 {
0161     removeSourcePath(path);
0162     Q_EMIT sourcesChanged();
0163 }