| 1 | /* |
| 2 | * Copyright (C) 2021 Damir Porobic <[email protected]> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 17 | * Boston, MA 02110-1301, USA. |
| 18 | */ |
| 19 | |
| 20 | #include "RecentImagesPathStore.h" |
| 21 | |
| 22 | RecentImagesPathStore::RecentImagesPathStore(const QSharedPointer<IImagePathStorage> &imagePathStorage) : |
| 23 | mImagePathStorage(imagePathStorage), |
| 24 | mMaxRecentItems(10) |
| 25 | { |
| 26 | Q_ASSERT(mImagePathStorage != nullptr); |
| 27 | |
| 28 | loadRecentImagesPath(); |
| 29 | } |
| 30 | |
| 31 | void RecentImagesPathStore::loadRecentImagesPath() |
| 32 | { |
| 33 | const auto storedImageCount = mImagePathStorage->count(); |
| 34 | for (auto i = 0; i < storedImageCount; ++i) { |
| 35 | mRecentImagesPathCache.enqueue(mImagePathStorage->load(i)); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | void RecentImagesPathStore::storeImagePath(const QString &imagePath) |
| 40 | { |
| 41 | if (mRecentImagesPathCache.contains(imagePath)) { |
| 42 | return; |
| 43 | } |
| 44 | |
| 45 | if (mRecentImagesPathCache.size() == mMaxRecentItems) { |
| 46 | mRecentImagesPathCache.dequeue(); |
| 47 | } |
| 48 | |
| 49 | mRecentImagesPathCache.enqueue(imagePath); |
| 50 | saveRecentImagesPath(); |
| 51 | } |
| 52 | |
| 53 | void RecentImagesPathStore::saveRecentImagesPath() |
| 54 | { |
| 55 | for (auto i = 0 ; i < mRecentImagesPathCache.size(); ++i) { |
| 56 | mImagePathStorage->store(mRecentImagesPathCache.at(i), i); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | QStringList RecentImagesPathStore::getRecentImagesPath() const |
| 61 | { |
| 62 | QStringList reversedList = mRecentImagesPathCache; |
| 63 | std::reverse(reversedList.begin(), reversedList.end()); |
| 64 | |
| 65 | return reversedList; |
| 66 | } |