File indexing completed on 2024-11-17 04:21:18

0001 #include "librarymodel.h"
0002 
0003 #include <QDebug>
0004 
0005 #include <MauiKit3/FileBrowsing/fileloader.h>
0006 #include <MauiKit3/FileBrowsing/fmstatic.h>
0007 
0008 #include <MauiKit3/Core/fmh.h>
0009 
0010 #include "library.h"
0011 
0012 static FMH::MODEL fileData(const QUrl &url)
0013 {
0014     FMH::MODEL model;
0015     model = FMStatic::getFileInfoModel(url);
0016 
0017     const auto fileName = url.fileName();
0018     if(fileName.toLower().endsWith("cbr") || fileName.toLower().endsWith("cbz"))
0019     {
0020         model.insert(FMH::MODEL_KEY::PREVIEW, QString("image://comiccover/").append(url.toLocalFile()));
0021 
0022     }else
0023     {
0024         model.insert(FMH::MODEL_KEY::PREVIEW, "image://preview/"+url.toString());
0025 
0026     }
0027 
0028     return model;
0029 }
0030 
0031 LibraryModel::LibraryModel(QObject *parent) : MauiList(parent)
0032   , m_fileLoader(new FMH::FileLoader(parent))
0033   , m_sources({"collection:///"})
0034 {
0035     qRegisterMetaType<LibraryModel*>("const LibraryModel*");
0036 
0037     //    connect(Library::instance(), &Library::sourcesChanged, this, &LibraryModel::setList);
0038 
0039     connect(m_fileLoader, &FMH::FileLoader::itemsReady,[this](FMH::MODEL_LIST items)
0040     {
0041         Q_EMIT this->preItemsAppended(items.size());
0042         this->list << items;
0043         Q_EMIT this->postItemAppended();
0044         Q_EMIT this->countChanged();
0045     });
0046 
0047     connect(this, &LibraryModel::sourcesChanged, this, &LibraryModel::setList);
0048 }
0049 
0050 void LibraryModel::setList(const QStringList &sources)
0051 {
0052     this->clear();
0053     QStringList paths = sources;
0054     QStringList filters;
0055 
0056     if(sources.count() == 1 )
0057     {
0058         QString source = sources.first();
0059         paths = Library::instance()->sources();
0060 
0061         if(source == "collection:///")
0062         {
0063             filters = FMStatic::FILTER_LIST[FMStatic::FILTER_TYPE::DOCUMENT];
0064 
0065         }else if( source == "comics:///")
0066         {
0067             QMimeDatabase mimedb;
0068             QStringList types = mimedb.mimeTypeForName("application/vnd.comicbook+zip").suffixes();
0069             types << mimedb.mimeTypeForName("application/vnd.comicbook+rar").suffixes();
0070 
0071             for(const auto &type : types)
0072             {
0073                 filters << "*."+type;
0074             }
0075 
0076         }else if( source == "documents:///")
0077         {
0078             QMimeDatabase mimedb;
0079             QStringList types = mimedb.mimeTypeForName("application/pdf").suffixes();
0080 
0081             for(const auto &type : types)
0082             {
0083                 filters << "*."+type;
0084             }
0085         }
0086     }else
0087     {
0088         filters = FMStatic::FILTER_LIST[FMStatic::FILTER_TYPE::DOCUMENT];
0089     }
0090 
0091     this->m_fileLoader->informer = &fileData;
0092     this->m_fileLoader->requestPath(QUrl::fromStringList(paths), true, filters);
0093 }
0094 
0095 const FMH::MODEL_LIST &LibraryModel::items() const
0096 {
0097     return this->list;
0098 }
0099 
0100 bool LibraryModel::remove(const int &index)
0101 {
0102     if(index >= this->list.size() || index < 0)
0103         return false;
0104 
0105     Q_EMIT this->preItemRemoved(index);
0106     this->list.remove(index);
0107     Q_EMIT this->postItemRemoved();
0108 
0109     return true;
0110 }
0111 
0112 bool LibraryModel::deleteAt(const int &index)
0113 {
0114     if(index >= this->list.size() || index < 0)
0115         return false;
0116 
0117     auto url = this->list.at(index).value(FMH::MODEL_KEY::URL);
0118     if(remove(index))
0119     {
0120         if(FMStatic::removeFiles({url}))
0121         {
0122             return true;
0123         }
0124     }
0125 
0126     return false;
0127 }
0128 
0129 bool LibraryModel::bookmark(const int &index, const int &)
0130 {
0131     if(index >= this->list.size() || index < 0)
0132         return false;
0133 
0134     //    return this->dba->bookmarkDoc(this->list[index][FMH::MODEL_KEY::URL], value);
0135     return false;
0136 }
0137 
0138 void LibraryModel::clear()
0139 {
0140     if(this->list.isEmpty())
0141     {
0142         return;
0143     }
0144 
0145     Q_EMIT this->preListChanged();
0146     this->list.clear();
0147     Q_EMIT this->postListChanged();
0148     Q_EMIT this->countChanged();
0149 }
0150 
0151 void LibraryModel::rescan()
0152 {
0153     this->setList(m_sources);
0154 }
0155 
0156 void LibraryModel::setSources(QStringList sources)
0157 {
0158     if (m_sources == sources)
0159         return;
0160 
0161     m_sources = sources;
0162     Q_EMIT sourcesChanged(m_sources);
0163 }
0164 
0165 void LibraryModel::componentComplete()
0166 {
0167     this->setList(m_sources);
0168 }
0169 
0170 QStringList LibraryModel::sources() const
0171 {
0172     return m_sources;
0173 }
0174 
0175 void LibraryModel::resetSources()
0176 {
0177     setSources({"collection:///"});
0178 }