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.
|
|
|
|
|
|
|
|
/// @file mru.cpp
|
|
|
|
/// @brief Most Recently Used (MRU) Lists
|
|
|
|
/// @ingroup libaegisub
|
|
|
|
|
2011-12-22 22:20:44 +01:00
|
|
|
#include "../config.h"
|
|
|
|
|
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 {
|
|
|
|
|
2012-10-11 18:19:16 +02:00
|
|
|
MRUManager::MRUManager(std::string const& config, std::string const& default_config, agi::Options *options)
|
|
|
|
: config_name(config)
|
|
|
|
, options(options)
|
|
|
|
{
|
|
|
|
option_names["Audio"] = "Limits/MRU";
|
|
|
|
option_names["Keyframes"] = "Limits/MRU";
|
|
|
|
option_names["Subtitles"] = "Limits/MRU";
|
|
|
|
option_names["Timecodes"] = "Limits/MRU";
|
|
|
|
option_names["Video"] = "Limits/MRU";
|
|
|
|
|
|
|
|
option_names["Find"] = "Limits/Find Replace";
|
|
|
|
option_names["Replace"] = "Limits/Find Replace";
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
MRUManager::~MRUManager() {
|
2011-07-26 21:51:56 +02:00
|
|
|
}
|
2010-05-23 08:58:06 +02:00
|
|
|
|
2011-07-26 21:51:56 +02:00
|
|
|
MRUManager::MRUListMap &MRUManager::Find(std::string const& key) {
|
|
|
|
MRUMap::iterator index = mru.find(key);
|
|
|
|
if (index == mru.end())
|
|
|
|
throw MRUErrorInvalidKey("Invalid key value");
|
|
|
|
return index->second;
|
2010-05-21 03:13:36 +02:00
|
|
|
}
|
|
|
|
|
2012-10-11 18:19:16 +02:00
|
|
|
void MRUManager::Add(std::string const& key, std::string const& entry) {
|
2011-07-26 21:51:56 +02:00
|
|
|
MRUListMap &map = Find(key);
|
|
|
|
map.remove(entry);
|
|
|
|
map.push_front(entry);
|
2012-10-11 18:19:16 +02:00
|
|
|
Prune(key, 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
|
|
|
void MRUManager::Remove(std::string const& key, std::string const& entry) {
|
2011-07-26 21:51:56 +02:00
|
|
|
Find(key).remove(entry);
|
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
|
|
|
}
|
|
|
|
|
2012-10-11 18:19:16 +02:00
|
|
|
std::string const& MRUManager::GetEntry(std::string const& key, const size_t entry) {
|
|
|
|
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];
|
|
|
|
array.insert(array.end(), mru_map.second.begin(), mru_map.second.end());
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/// @brief Prune MRUListMap to the desired length.
|
|
|
|
/// This uses the user-set values for MRU list length.
|
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
|
|
|
|
|
|
|
/// @brief Load MRU Lists.
|
|
|
|
/// @param key List name.
|
|
|
|
/// @param array json::Array of values.
|
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 {
|
2011-10-18 00:00:28 +02:00
|
|
|
copy(array.begin(), array.end(), back_inserter(mru[key]));
|
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
|
|
|
}
|
|
|
|
|
|
|
|
}
|