File indexing completed on 2024-05-12 05:53:57

0001 /*
0002  * Copyright (c) 2018 Sune Vuorela <sune@vuorela.dk>
0003  *
0004  * Permission is hereby granted, free of charge, to any person
0005  * obtaining a copy of this software and associated documentation
0006  * files (the "Software"), to deal in the Software without
0007  * restriction, including without limitation the rights to use,
0008  * copy, modify, merge, publish, distribute, sublicense, and/or sell
0009  * copies of the Software, and to permit persons to whom the
0010  * Software is furnished to do so, subject to the following
0011  * conditions:
0012  *
0013  * The above copyright notice and this permission notice shall be
0014  * included in all copies or substantial portions of the Software.
0015  *
0016  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
0017  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
0018  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
0019  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
0020  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
0021  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
0022  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
0023  * OTHER DEALINGS IN THE SOFTWARE.
0024  */
0025 
0026 #include "scanneradapter.h"
0027 #include "scanner.h"
0028 #include <QSortFilterProxyModel>
0029 
0030 #include <QFile>
0031 #include <QSettings>
0032 #include <QStandardPaths>
0033 
0034 ScannerAdapter::ScannerAdapter(QObject* parent) : QObject(parent), m_scanner(std::make_unique<Scanner>()), m_titleList(std::make_unique<QSortFilterProxyModel>())
0035 {
0036     m_titleList->setFilterCaseSensitivity(Qt::CaseInsensitive);
0037     connect(m_scanner.get(), &Scanner::dataUpdated, this, [&]() {
0038         auto titleList = m_scanner->parsedTitleList();
0039         m_titleList->setSourceModel(titleList.get());
0040         m_titleListSource = titleList;
0041     });
0042     QSettings s;
0043     s.beginGroup("General");
0044     m_scanner->setRootPath(s.value("location", QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation) + "/recipes/").toString());
0045 }
0046 
0047 ScannerAdapter::~ScannerAdapter()
0048 {
0049 }
0050 
0051 QObject * ScannerAdapter::titleList() const {
0052     return m_titleList.get();
0053 }
0054 
0055 void ScannerAdapter::setFilter(const QString& filter)
0056 {
0057     if (filter != m_filterFixedString) {
0058         m_titleList->setFilterFixedString(filter);
0059         m_filterFixedString = filter;
0060         Q_EMIT filterChanged();
0061     }
0062 }
0063 
0064 void ScannerAdapter::setRootPath(const QString& rootPath)
0065 {
0066     if (m_scanner->rootPath() != rootPath)
0067     {
0068         m_scanner->setRootPath(rootPath);
0069         Q_EMIT rootPathChanged();
0070         QSettings s;
0071         s.beginGroup("General");
0072         if (QFile::exists(rootPath)) {
0073             s.setValue("location", rootPath);
0074         }
0075     }
0076 }
0077 
0078 QString ScannerAdapter::filter() const
0079 {
0080     return m_filterFixedString;
0081 }
0082 
0083 QString ScannerAdapter::rootPath() const
0084 {
0085     return m_scanner->rootPath();
0086 }
0087 
0088 void ScannerAdapter::refresh()
0089 {
0090     m_scanner->doUpdate();
0091 }