File indexing completed on 2024-05-12 04:45:58

0001 #include "videosmodel.h"
0002 #include <QFileSystemWatcher>
0003 #include <QDebug>
0004 #include <MauiKit3/FileBrowsing/tagging.h>
0005 #include <MauiKit3/FileBrowsing/fmstatic.h>
0006 #include <MauiKit3/FileBrowsing/fileloader.h>
0007 
0008 static FMH::MODEL videoData(const QUrl &url)
0009 {
0010     FMH::MODEL model;
0011     model= FMStatic::getFileInfoModel(url);
0012     model.insert(FMH::MODEL_KEY::PREVIEW, "image://preview/"+url.toString());
0013     return model;
0014 }
0015 
0016 VideosModel::VideosModel(QObject *parent) : MauiList(parent)
0017   , m_fileLoader(new FMH::FileLoader(this))
0018   , m_watcher (new QFileSystemWatcher(this))
0019   , m_autoReload(true)
0020   , m_autoScan(true)
0021   , m_recursive (true)
0022   , m_urls(QStringList())
0023 {
0024     qDebug()<< "CREATING GALLERY LIST";
0025 
0026     m_fileLoader->informer = &videoData;
0027     connect(m_fileLoader, &FMH::FileLoader::finished,[this](FMH::MODEL_LIST items)
0028     {
0029         qDebug() << "Items finished" << items.size();
0030         Q_EMIT this->filesChanged();
0031     });
0032 
0033     connect(m_fileLoader, &FMH::FileLoader::itemsReady,[this](FMH::MODEL_LIST items)
0034     {
0035         Q_EMIT this->preListChanged();
0036         this-> list << items;
0037         Q_EMIT this->postListChanged();
0038         Q_EMIT this->countChanged();
0039     });
0040 
0041     connect(m_fileLoader, &FMH::FileLoader::itemReady,[this](FMH::MODEL item)
0042     {
0043         this->insertFolder(item[FMH::MODEL_KEY::SOURCE]);
0044     });
0045 
0046     connect(m_watcher, &QFileSystemWatcher::directoryChanged, [this](QString dir)
0047     {
0048         qDebug()<< "Dir changed" << dir;
0049         this->rescan();
0050         //        this->scan({QUrl::fromLocalFile(dir)}, m_recursive);
0051     });
0052 
0053     connect(m_watcher, &QFileSystemWatcher::fileChanged, [](QString file)
0054     {
0055         qDebug()<< "File changed" << file;
0056     });
0057 
0058     connect(this, &VideosModel::urlsChanged, [this]()
0059     {
0060          this->scan(m_urls, m_recursive, m_limit);
0061     });
0062 }
0063 
0064 const FMH::MODEL_LIST &VideosModel::items() const
0065 {
0066     return this->list;
0067 }
0068 
0069 void VideosModel::setUrls(const QStringList &urls)
0070 {
0071     qDebug()<< "setting urls"<< this->m_urls << urls;    
0072 
0073     if(this->m_urls == urls)
0074         return;
0075 
0076     this->m_urls = urls;
0077     this->clear();
0078     Q_EMIT this->urlsChanged();
0079 }
0080 
0081 QStringList VideosModel::urls() const
0082 {
0083     return m_urls;
0084 }
0085 
0086 void VideosModel::resetUrls()
0087 {
0088     setUrls({"collection:///"});
0089 }
0090 
0091 void VideosModel::setAutoScan(const bool &value)
0092 {
0093     if(m_autoScan == value)
0094         return;
0095 
0096     m_autoScan = value;
0097     Q_EMIT autoScanChanged();
0098 }
0099 
0100 bool VideosModel::autoScan() const
0101 {
0102     return m_autoScan;
0103 }
0104 
0105 void VideosModel::setAutoReload(const bool &value)
0106 {
0107     if(m_autoReload == value)
0108         return;
0109 
0110     m_autoReload = value;
0111     Q_EMIT autoReloadChanged();
0112 }
0113 
0114 bool VideosModel::autoReload() const
0115 {
0116     return m_autoReload;
0117 }
0118 
0119 QList<QUrl> VideosModel::folders() const
0120 {
0121     return m_folders;
0122 }
0123 
0124 bool VideosModel::recursive() const
0125 {
0126     return m_recursive;
0127 }
0128 
0129 int VideosModel::limit() const
0130 {
0131     return m_limit;
0132 }
0133 
0134 QStringList VideosModel::files() const
0135 {
0136     return FMH::modelToList(this->list, FMH::MODEL_KEY::URL);
0137 }
0138 
0139 void VideosModel::scan(const QStringList &urls, const bool &recursive, const int &limit)
0140 {
0141     this->clear();
0142     auto paths = urls.count() == 1 && urls.first() == "collection:///" ? Clip::instance()->sources() : urls;
0143 
0144     m_fileLoader->requestPath(QUrl::fromStringList(paths), recursive, FMStatic::FILTER_LIST[FMStatic::FILTER_TYPE::VIDEO]);
0145 }
0146 
0147 void VideosModel::insertFolder(const QUrl &path)
0148 {
0149     if(!m_folders.contains(path))
0150     {
0151         m_folders << path;
0152 
0153         if(m_autoReload)
0154         {
0155             this->m_watcher->addPath(path.toLocalFile());
0156         }
0157 
0158         Q_EMIT foldersChanged();
0159     }
0160 }
0161 
0162 bool VideosModel::remove(const int &index)
0163 {
0164     if (index >= this->list.size() || index < 0)
0165         return false;
0166 
0167     Q_EMIT this->preItemRemoved(index);
0168     this->list.removeAt(index);
0169     Q_EMIT this->postItemRemoved();
0170     Q_EMIT this->countChanged();
0171 
0172     return true;
0173 }
0174 
0175 bool VideosModel::deleteAt(const int &index)
0176 {
0177     if(index >= this->list.size() || index < 0)
0178         return false;
0179 
0180     Q_EMIT this->preItemRemoved(index);
0181     auto item = this->list.takeAt(index);
0182     FMStatic::removeFiles ({item[FMH::MODEL_KEY::URL]});
0183     Q_EMIT this->postItemRemoved();
0184     Q_EMIT this->countChanged();
0185 
0186     return true;
0187 }
0188 
0189 void VideosModel::append(const QVariantMap &item)
0190 {
0191     Q_EMIT this->preItemAppended();
0192     this->list << FMH::toModel (item);
0193     Q_EMIT this->postItemAppended();
0194     Q_EMIT this->countChanged();
0195 }
0196 
0197 void VideosModel::appendUrl(const QString &url)
0198 {
0199     Q_EMIT this->preItemAppended();
0200     this->list << FMStatic::getFileInfoModel(QUrl::fromUserInput (url));
0201     Q_EMIT this->postItemAppended();
0202     Q_EMIT this->countChanged();
0203 }
0204 
0205 void VideosModel::clear()
0206 {
0207     Q_EMIT this->preListChanged();
0208     this->list.clear ();
0209     Q_EMIT this->postListChanged();
0210     Q_EMIT this->countChanged();
0211 
0212     this->m_watcher->removePaths(m_watcher->directories());
0213 
0214     this->m_folders.clear ();
0215     Q_EMIT foldersChanged();
0216 }
0217 
0218 void VideosModel::rescan()
0219 {
0220     this->scan(m_urls, m_recursive, m_limit);
0221 }
0222 
0223 void VideosModel::setRecursive(bool recursive)
0224 {
0225     if (m_recursive == recursive)
0226         return;
0227 
0228     m_recursive = recursive;
0229     Q_EMIT recursiveChanged(m_recursive);
0230 }
0231 
0232 void VideosModel::setlimit(int limit)
0233 {
0234     if (m_limit == limit)
0235         return;
0236 
0237     m_limit = limit;
0238     Q_EMIT limitChanged(m_limit);
0239 }
0240 
0241 
0242 void VideosModel::classBegin()
0243 {
0244 }
0245 
0246 void VideosModel::componentComplete()
0247 {
0248      this->scan(m_urls, m_recursive, m_limit);
0249 }