1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
|
/*
* Copyright (c) 2012-2019 Isode Limited.
* All rights reserved.
* See the COPYING file for more information.
*/
#include <Swift/Controllers/StatusCache.h>
#include <boost/algorithm/string.hpp>
#include <boost/filesystem/fstream.hpp>
#include <boost/lexical_cast.hpp>
#include <Swiften/Base/ByteArray.h>
#include <Swiften/Base/Log.h>
#include <SwifTools/Application/ApplicationPathProvider.h>
namespace Swift {
static const size_t MAX_ENTRIES = 200;
StatusCache::StatusCache(ApplicationPathProvider* paths) {
paths_ = paths;
path_ = paths_->getDataDir() / "StatusCache";
loadRecents();
}
StatusCache::~StatusCache() {
}
std::vector<StatusCache::PreviousStatus> StatusCache::getMatches(const std::string& substring, size_t maxCount) const {
std::vector<PreviousStatus> matches;
for (const auto& status : previousStatuses_) {
if (substring.empty() || (boost::algorithm::ifind_first(status.first, substring) && substring != status.first)) {
matches.push_back(status);
if (matches.size() == maxCount) {
break;
}
}
}
return matches;
}
void StatusCache::addRecent(const std::string& text, StatusShow::Type type) {
if (text.empty()) {
return;
}
previousStatuses_.remove_if([&](const PreviousStatus& previousStatus) {
return previousStatus.first == text && previousStatus.second == type;
});
previousStatuses_.push_front(PreviousStatus(text, type));
for (size_t i = previousStatuses_.size(); i > MAX_ENTRIES; i--) {
previousStatuses_.pop_back();
}
saveRecents();
}
void StatusCache::loadRecents() {
try {
if (boost::filesystem::exists(path_)) {
ByteArray data;
readByteArrayFromFile(data, path_);
std::string stringData = byteArrayToString(data);
std::vector<std::string> lines;
boost::split(lines, stringData, boost::is_any_of("\n"));
for (const auto& line : lines) {
std::vector<std::string> bits;
boost::split(bits, line, boost::is_any_of("\t"));
if (bits.size() < 2) {
continue;
}
StatusShow::Type type;
try {
type = static_cast<StatusShow::Type>(boost::lexical_cast<size_t>(bits[0]));
previousStatuses_.push_back(PreviousStatus(boost::trim_copy(bits[1]), type));
}
catch (const boost::bad_lexical_cast& e) {
SWIFT_LOG(error) << "Failed to load recent status cache entry: " << e.what();
}
}
}
}
catch (const boost::filesystem::filesystem_error& e) {
SWIFT_LOG(error) << "Failed to load recents: " << e.what();
}
}
void StatusCache::saveRecents() {
try {
if (!boost::filesystem::exists(path_.parent_path())) {
boost::filesystem::create_directories(path_.parent_path());
}
boost::filesystem::ofstream file(path_);
for (const auto& recent : previousStatuses_) {
std::string message = recent.first;
boost::replace_all(message, "\t", " ");
file << recent.second << "\t" << message << std::endl;
}
file.close();
}
catch (const boost::filesystem::filesystem_error& e) {
SWIFT_LOG(error) << "Failed to save recents: " << e.what();
}
}
}
|