Files
lcd-image-converter/classes/settings/recentlist.cpp
2025-01-13 01:25:57 +05:00

92 lines
2.2 KiB
C++

/*
* LCD Image Converter. Converts images and fonts for embedded applications.
* Copyright (C) 2012 riuson
* mailto: riuson@gmail.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/
*/
#include "recentlist.h"
#include <QFileInfo>
#include <QStringList>
#include <QStringListIterator>
#include "appsettings.h"
namespace Settings
{
RecentList::RecentList(QObject* parent) : QObject(parent)
{
this->mFiles = new QStringList();
// load from settings
AppSettings appsett;
QSettings& sett = appsett.get();
int size = sett.beginReadArray("recent");
for (int i = 0; i < size; i++) {
sett.setArrayIndex(i);
QString filename = sett.value("filename").toString();
QFileInfo info(filename);
if (info.exists()) {
this->mFiles->append(filename);
}
}
sett.endArray();
}
RecentList::~RecentList()
{
// save to settings
AppSettings appsett;
QSettings& sett = appsett.get();
sett.beginWriteArray("recent");
QStringListIterator recentFilesIterator(*this->mFiles);
int i = 0;
while (recentFilesIterator.hasNext()) {
sett.setArrayIndex(i++);
sett.setValue("filename", recentFilesIterator.next());
}
sett.endArray();
delete this->mFiles;
}
void RecentList::add(const QString& filename)
{
if (this->mFiles->contains(filename)) {
this->mFiles->removeOne(filename);
}
this->mFiles->insert(0, filename);
if (this->mFiles->count() > MaxRecentFiles) {
for (int i = this->mFiles->count() - 1; i >= 10; i--) {
this->mFiles->removeAt(i);
}
}
emit this->listChanged();
}
const QStringList* RecentList::files() const { return this->mFiles; }
} // namespace Settings