| 1 |
/* |
| 2 |
* Copyright (C) 2019-2021 Rerrah |
| 3 |
* |
| 4 |
* Permission is hereby granted, free of charge, to any person |
| 5 |
* obtaining a copy of this software and associated documentation |
| 6 |
* files (the "Software"), to deal in the Software without |
| 7 |
* restriction, including without limitation the rights to use, |
| 8 |
* copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 |
* copies of the Software, and to permit persons to whom the |
| 10 |
* Software is furnished to do so, subject to the following |
| 11 |
* conditions: |
| 12 |
* |
| 13 |
* The above copyright notice and this permission notice shall be |
| 14 |
* included in all copies or substantial portions of the Software. |
| 15 |
* |
| 16 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 17 |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
| 18 |
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 19 |
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 20 |
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 21 |
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 22 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 23 |
* OTHER DEALINGS IN THE SOFTWARE. |
| 24 |
*/ |
| 25 |
|
| 26 |
#include "file_history.hpp" |
| 27 |
#include <algorithm> |
| 28 |
#include <QSettings> |
| 29 |
#include "utils.hpp" |
| 30 |
#include "gui/gui_utils.hpp" |
| 31 |
|
| 32 |
namespace |
| 33 |
{ |
| 34 |
const int HISTORY_SIZE = 8; |
| 35 |
} |
| 36 |
|
| 37 |
void FileHistory::addFile(const QString& path) |
| 38 |
{ |
| 39 |
auto it = utils::find(list_, path); |
| 40 |
if (it != list_.end()) list_.erase(it); |
| 41 |
list_.push_front(path); |
| 42 |
if (list_.size() > HISTORY_SIZE) list_.pop_back(); |
| 43 |
} |
| 44 |
|
| 45 |
void FileHistory::clearHistory() |
| 46 |
{ |
| 47 |
list_.clear(); |
| 48 |
} |
| 49 |
|
| 50 |
QString FileHistory::at(size_t i) |
| 51 |
{ |
| 52 |
return list_.at(i); |
| 53 |
} |
| 54 |
|
| 55 |
size_t FileHistory::size() const |
| 56 |
{ |
| 57 |
return list_.size(); |
| 58 |
} |
| 59 |
|
| 60 |
bool FileHistory::empty() const |
| 61 |
{ |
| 62 |
return list_.empty(); |
| 63 |
} |
| 64 |
|
| 65 |
namespace io |
| 66 |
{ |
| 67 |
namespace |
| 68 |
{ |
| 69 |
// config path (*nix): ~/.config/<ORGANIZATION_>/<FILE_>.ini |
| 70 |
const QString FILE = "FileHistory"; |
| 71 |
} |
| 72 |
|
| 73 |
bool saveFileHistory(std::weak_ptr<FileHistory> history) |
| 74 |
{ |
| 75 |
try { |
| 76 |
QSettings settings(QSettings::IniFormat, QSettings::UserScope, io::ORGANIZATION_NAME, FILE); |
| 77 |
settings.beginWriteArray("fileHistory"); |
| 78 |
int n = 0; |
| 79 |
for (size_t i = 0; i < history.lock()->size(); ++i) { |
| 80 |
settings.setArrayIndex(n++); |
| 81 |
settings.setValue("path", history.lock()->at(i)); |
| 82 |
} |
| 83 |
settings.endArray(); |
| 84 |
return true; |
| 85 |
} catch (...) { |
| 86 |
return false; |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
bool loadFileHistory(std::weak_ptr<FileHistory> history) |
| 91 |
{ |
| 92 |
try { |
| 93 |
QSettings settings(QSettings::IniFormat, QSettings::UserScope, io::ORGANIZATION_NAME, FILE); |
| 94 |
int size = settings.beginReadArray("fileHistory"); |
| 95 |
history.lock()->clearHistory(); |
| 96 |
for (int i = size - 1; 0 <= i; --i) { |
| 97 |
settings.setArrayIndex(i); |
| 98 |
history.lock()->addFile(settings.value("path").toString()); |
| 99 |
} |
| 100 |
settings.endArray(); |
| 101 |
return true; |
| 102 |
} catch (...) { |
| 103 |
return false; |
| 104 |
} |
| 105 |
} |
| 106 |
} |