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
|
|
|
|
|
|
|
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)
|
2014-05-25 18:02:44 +02:00
|
|
|
, option_names({
|
|
|
|
{"Audio", "Limits/MRU"},
|
|
|
|
{"Find", "Limits/Find Replace"},
|
|
|
|
{"Keyframes", "Limits/MRU"},
|
|
|
|
{"Replace", "Limits/Find Replace"},
|
|
|
|
{"Subtitle", "Limits/MRU"},
|
|
|
|
{"Timecodes", "Limits/MRU"},
|
|
|
|
{"Video", "Limits/MRU"},
|
|
|
|
})
|
2012-10-11 18:19:16 +02:00
|
|
|
{
|
2010-06-03 20:09:00 +02:00
|
|
|
LOG_D("agi/mru") << "Loading MRU List";
|
2010-05-21 03:13:36 +02:00
|
|
|
|
2011-12-22 22:10:50 +01:00
|
|
|
json::Object root(json_util::file(config, default_config));
|
2012-11-13 15:07:39 +01:00
|
|
|
for (auto const& it : root)
|
|
|
|
Load(it.first, it.second);
|
2010-05-21 03:13:36 +02:00
|
|
|
}
|
|
|
|
|
2011-07-26 21:51:56 +02:00
|
|
|
MRUManager::MRUListMap &MRUManager::Find(std::string const& key) {
|
2013-11-21 18:13:36 +01:00
|
|
|
auto index = mru.find(key);
|
2011-07-26 21:51:56 +02:00
|
|
|
if (index == mru.end())
|
|
|
|
throw MRUErrorInvalidKey("Invalid key value");
|
|
|
|
return index->second;
|
2010-05-21 03:13:36 +02:00
|
|
|
}
|
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
void MRUManager::Add(std::string const& 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-25 18:02:44 +02:00
|
|
|
rotate(begin(map), it, it);
|
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
|
|
|
}
|
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
void MRUManager::Remove(std::string const& 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
|
|
|
}
|
|
|
|
|
2012-10-11 18:19:16 +02:00
|
|
|
const MRUManager::MRUListMap* MRUManager::Get(std::string const& key) {
|
2011-07-26 21:51:56 +02:00
|
|
|
return &Find(key);
|
2010-05-21 03:13:36 +02:00
|
|
|
}
|
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
agi::fs::path const& MRUManager::GetEntry(std::string const& key, const size_t entry) {
|
2012-10-11 18:19:16 +02:00
|
|
|
const MRUManager::MRUListMap *const map = Get(key);
|
2010-05-21 03:13:36 +02:00
|
|
|
|
2011-12-22 22:11:01 +01:00
|
|
|
if (entry >= map->size())
|
2010-05-21 03:13:36 +02:00
|
|
|
throw MRUErrorIndexOutOfRange("Requested element index is out of range.");
|
|
|
|
|
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;
|
|
|
|
|
2012-11-13 15:07:39 +01:00
|
|
|
for (auto const& mru_map : mru) {
|
|
|
|
json::Array &array = out[mru_map.first];
|
2013-01-04 16:01:50 +01:00
|
|
|
transform(begin(mru_map.second), end(mru_map.second),
|
|
|
|
back_inserter(array), [](agi::fs::path const& p) { return p.string(); });
|
2010-05-21 03:13:36 +02:00
|
|
|
}
|
|
|
|
|
2011-07-26 21:51:56 +02:00
|
|
|
json::Writer::Write(out, io::Save(config_name).Get());
|
2010-05-21 03:13:36 +02:00
|
|
|
}
|
|
|
|
|
2012-10-11 18:19:16 +02:00
|
|
|
void MRUManager::Prune(std::string const& key, MRUListMap& map) const {
|
|
|
|
size_t limit = 16u;
|
|
|
|
if (options) {
|
2012-11-13 15:07:39 +01:00
|
|
|
auto it = option_names.find(key);
|
2012-10-11 18:19:16 +02:00
|
|
|
if (it != option_names.end())
|
|
|
|
limit = (size_t)options->Get(it->second)->GetInt();
|
|
|
|
}
|
|
|
|
map.resize(std::min(limit, map.size()));
|
2011-07-26 21:51:56 +02:00
|
|
|
}
|
2010-05-21 03:13:36 +02:00
|
|
|
|
2012-10-11 18:19:16 +02:00
|
|
|
void MRUManager::Load(std::string const& key, const json::Array& array) {
|
2011-09-29 02:37:18 +02:00
|
|
|
try {
|
2013-01-04 16:01:50 +01:00
|
|
|
transform(begin(array), end(array),
|
|
|
|
back_inserter(mru[key]), [](std::string const& s) { return s; });
|
2011-09-29 02:37:18 +02:00
|
|
|
}
|
|
|
|
catch (json::Exception const&) {
|
|
|
|
// Out of date MRU file; just discard the data and skip it
|
|
|
|
}
|
2012-10-11 18:19:16 +02:00
|
|
|
Prune(key, mru[key]);
|
2010-05-21 03:13:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|