2010-05-21 03:13:36 +02:00
|
|
|
// Copyright (c) 2010, Amar Takhar <verm@aegisub.org>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice appear in all copies.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
|
2012-10-11 18:19:16 +02:00
|
|
|
#include "libaegisub/mru.h"
|
|
|
|
|
2010-06-11 04:25:07 +02:00
|
|
|
#include "libaegisub/cajun/writer.h"
|
2010-05-21 03:13:36 +02:00
|
|
|
|
2011-07-26 21:51:56 +02:00
|
|
|
#include "libaegisub/io.h"
|
2011-07-26 21:51:38 +02:00
|
|
|
#include "libaegisub/json.h"
|
2010-06-03 20:09:00 +02:00
|
|
|
#include "libaegisub/log.h"
|
2012-10-11 18:19:16 +02:00
|
|
|
#include "libaegisub/option.h"
|
|
|
|
#include "libaegisub/option_value.h"
|
2010-05-21 03:13:36 +02:00
|
|
|
|
2014-07-05 06:21:35 +02:00
|
|
|
namespace {
|
|
|
|
const char *mru_names[] = {
|
|
|
|
"Audio",
|
|
|
|
"Find",
|
|
|
|
"Keyframes",
|
|
|
|
"Replace",
|
|
|
|
"Subtitle",
|
|
|
|
"Timecodes",
|
|
|
|
"Video",
|
|
|
|
};
|
|
|
|
|
|
|
|
const char *option_names[] = {
|
|
|
|
"Limits/MRU",
|
|
|
|
"Limits/Find Replace",
|
|
|
|
"Limits/MRU",
|
|
|
|
"Limits/Find Replace",
|
|
|
|
"Limits/MRU",
|
|
|
|
"Limits/MRU",
|
|
|
|
"Limits/MRU",
|
|
|
|
};
|
|
|
|
|
|
|
|
int mru_index(const char *key) {
|
|
|
|
int i;
|
|
|
|
switch (*key) {
|
|
|
|
case 'A': i = 0; break;
|
|
|
|
case 'F': i = 1; break;
|
|
|
|
case 'K': i = 2; break;
|
|
|
|
case 'R': i = 3; break;
|
|
|
|
case 'S': i = 4; break;
|
|
|
|
case 'T': i = 5; break;
|
|
|
|
case 'V': i = 6; break;
|
|
|
|
default: return -1;
|
|
|
|
}
|
|
|
|
return strcmp(key, mru_names[i]) == 0 ? i : -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-21 03:13:36 +02:00
|
|
|
namespace agi {
|
2014-03-21 22:02:49 +01:00
|
|
|
MRUManager::MRUManager(agi::fs::path const& config, std::pair<const char *, size_t> default_config, agi::Options *options)
|
2012-10-11 18:19:16 +02:00
|
|
|
: config_name(config)
|
|
|
|
, options(options)
|
|
|
|
{
|
2010-06-03 20:09:00 +02:00
|
|
|
LOG_D("agi/mru") << "Loading MRU List";
|
2010-05-21 03:13:36 +02:00
|
|
|
|
2014-06-28 05:44:47 +02:00
|
|
|
auto root = json_util::file(config, default_config);
|
|
|
|
for (auto const& it : static_cast<json::Object const&>(root))
|
2014-07-05 06:21:35 +02:00
|
|
|
Load(it.first.c_str(), it.second);
|
2010-05-21 03:13:36 +02:00
|
|
|
}
|
|
|
|
|
2014-07-05 06:21:35 +02:00
|
|
|
MRUManager::MRUListMap &MRUManager::Find(const char *key) {
|
|
|
|
auto index = mru_index(key);
|
|
|
|
if (index == -1)
|
|
|
|
throw MRUError("Invalid key value");
|
|
|
|
return mru[index];
|
2010-05-21 03:13:36 +02:00
|
|
|
}
|
|
|
|
|
2014-07-05 06:21:35 +02:00
|
|
|
void MRUManager::Add(const char *key, agi::fs::path const& entry) {
|
2011-07-26 21:51:56 +02:00
|
|
|
MRUListMap &map = Find(key);
|
2013-01-04 16:01:50 +01:00
|
|
|
auto it = find(begin(map), end(map), entry);
|
|
|
|
if (it == begin(map) && it != end(map))
|
|
|
|
return;
|
|
|
|
if (it != end(map))
|
2014-05-29 16:28:46 +02:00
|
|
|
rotate(begin(map), it, it + 1);
|
2013-01-04 16:01:50 +01:00
|
|
|
else {
|
2014-05-25 18:02:44 +02:00
|
|
|
map.insert(begin(map), entry);
|
2013-01-04 16:01:50 +01:00
|
|
|
Prune(key, map);
|
|
|
|
}
|
2011-09-28 21:52:46 +02:00
|
|
|
|
|
|
|
Flush();
|
2010-05-21 03:13:36 +02:00
|
|
|
}
|
|
|
|
|
2014-07-05 06:21:35 +02:00
|
|
|
void MRUManager::Remove(const char *key, agi::fs::path const& entry) {
|
2014-05-25 18:02:44 +02:00
|
|
|
auto& map = Find(key);
|
|
|
|
map.erase(remove(begin(map), end(map), entry), end(map));
|
2011-09-28 21:52:46 +02:00
|
|
|
Flush();
|
2010-05-21 03:13:36 +02:00
|
|
|
}
|
|
|
|
|
2014-07-05 06:21:35 +02:00
|
|
|
const MRUManager::MRUListMap* MRUManager::Get(const char *key) {
|
2011-07-26 21:51:56 +02:00
|
|
|
return &Find(key);
|
2010-05-21 03:13:36 +02:00
|
|
|
}
|
|
|
|
|
2014-07-05 06:21:35 +02:00
|
|
|
agi::fs::path const& MRUManager::GetEntry(const char *key, const size_t entry) {
|
2014-06-28 05:44:47 +02:00
|
|
|
const auto map = Get(key);
|
2011-12-22 22:11:01 +01:00
|
|
|
if (entry >= map->size())
|
2014-07-05 06:21:35 +02:00
|
|
|
throw MRUError("Requested element index is out of range.");
|
2010-05-21 03:13:36 +02:00
|
|
|
|
2012-11-29 05:36:03 +01:00
|
|
|
return *next(map->begin(), entry);
|
2010-05-21 03:13:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void MRUManager::Flush() {
|
|
|
|
json::Object out;
|
|
|
|
|
2014-07-05 06:21:35 +02:00
|
|
|
for (size_t i = 0; i < mru.size(); ++i) {
|
|
|
|
json::Array &array = out[mru_names[i]];
|
|
|
|
for (auto const& p : mru[i])
|
2014-06-28 04:38:53 +02:00
|
|
|
array.push_back(p.string());
|
2010-05-21 03:13:36 +02:00
|
|
|
}
|
|
|
|
|
2014-06-28 04:38:53 +02:00
|
|
|
agi::JsonWriter::Write(out, io::Save(config_name).Get());
|
2010-05-21 03:13:36 +02:00
|
|
|
}
|
|
|
|
|
2014-07-05 06:21:35 +02:00
|
|
|
void MRUManager::Prune(const char *key, MRUListMap& map) const {
|
2012-10-11 18:19:16 +02:00
|
|
|
size_t limit = 16u;
|
|
|
|
if (options) {
|
2014-07-05 06:21:35 +02:00
|
|
|
int idx = mru_index(key);
|
|
|
|
if (idx != -1)
|
|
|
|
limit = (size_t)options->Get(option_names[idx])->GetInt();
|
2012-10-11 18:19:16 +02:00
|
|
|
}
|
|
|
|
map.resize(std::min(limit, map.size()));
|
2011-07-26 21:51:56 +02:00
|
|
|
}
|
2010-05-21 03:13:36 +02:00
|
|
|
|
2014-07-05 06:21:35 +02:00
|
|
|
void MRUManager::Load(const char *key, const json::Array& array) {
|
|
|
|
int idx = mru_index(key);
|
|
|
|
if (idx == -1) return;
|
|
|
|
|
2011-09-29 02:37:18 +02:00
|
|
|
try {
|
2014-07-05 06:21:35 +02:00
|
|
|
mru[idx].reserve(array.size());
|
|
|
|
for (std::string const& str : array)
|
|
|
|
mru[idx].push_back(str);
|
2011-09-29 02:37:18 +02:00
|
|
|
}
|
|
|
|
catch (json::Exception const&) {
|
|
|
|
// Out of date MRU file; just discard the data and skip it
|
|
|
|
}
|
2014-07-05 06:21:35 +02:00
|
|
|
Prune(key, mru[idx]);
|
2010-05-21 03:13:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|