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

0001 #include "historymodel.h"
0002 
0003 #include <QSettings>
0004 #include <MauiKit3/FileBrowsing/fmstatic.h>
0005 
0006 static bool isTextDocument(const QUrl &url)
0007 {
0008     return FMStatic::checkFileType(FMStatic::FILTER_TYPE::TEXT, FMStatic::getMime(url));
0009 }
0010 
0011 HistoryModel::HistoryModel(QObject *parent)
0012     : MauiList(parent)
0013 {
0014 }
0015 
0016 const FMH::MODEL_LIST &HistoryModel::items() const
0017 {
0018     return this->m_list;
0019 }
0020 
0021 void HistoryModel::append(const QUrl &url)
0022 {
0023     auto urls = this->getHistory();
0024     if (urls.contains(url) || !isTextDocument(url))
0025         return;
0026 
0027     Q_EMIT this->preItemAppended();
0028     this->m_list << FMStatic::getFileInfoModel(url);
0029     Q_EMIT this->postItemAppended();
0030     Q_EMIT this->countChanged();
0031 
0032     urls << url;
0033 
0034     QSettings settings;
0035     settings.beginGroup("HISTORY");
0036     settings.setValue("URLS", QUrl::toStringList(urls));
0037     settings.endGroup();
0038 }
0039 
0040 int HistoryModel::indexOfName(const QString &query)
0041 {
0042     const auto it = std::find_if(this->items().constBegin(), this->items().constEnd(), [&](const FMH::MODEL &item) -> bool {
0043         return item[FMH::MODEL_KEY::LABEL].startsWith(query, Qt::CaseInsensitive);
0044     });
0045 
0046     if (it != this->items().constEnd())
0047         return (std::distance(this->items().constBegin(), it));
0048     else
0049         return -1;
0050 }
0051 
0052 QList<QUrl> HistoryModel::getHistory()
0053 {
0054     QSettings settings;
0055     settings.beginGroup("HISTORY");
0056      auto urls = settings.value("URLS", QStringList()).toStringList();
0057     settings.endGroup();
0058 
0059     urls.removeDuplicates();
0060     auto res = QUrl::fromStringList(urls);
0061     res.removeAll(QString(""));
0062     return res;
0063 }
0064 
0065 void HistoryModel::setList()
0066 {
0067     const auto urls = this->getHistory();
0068     Q_EMIT this->preListChanged();
0069 
0070     for (const auto &url : urls)
0071     {
0072         if (!url.isLocalFile() || !FMH::fileExists(url) || !isTextDocument(url))
0073         {
0074             continue;
0075         }
0076 
0077         this->m_list << FMStatic::getFileInfoModel(url);        
0078     }
0079     Q_EMIT this->postListChanged();
0080     Q_EMIT this->countChanged();
0081 }
0082 
0083 
0084 void HistoryModel::componentComplete()
0085 {
0086     this->setList();
0087 }