#include "mologram/ui/PrefsStore.hpp"
#include <algorithm>
#include <cerrno>
#include <cstring>
#include <fcntl.h>
#include <filesystem>
#include <fstream>
#include <iterator>
#include <unistd.h>
namespace mologram::ui {
namespace fs = std::filesystem;
bool PrefsStore::load(std::string& err) {
std::ifstream in(path_);
if (!in) return true; // no file yet
std::string text((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>());
ttgui::json::ParseError pe;
auto v = ttgui::json::parse(text, &pe);
if (!v || !v->isObj()) {
err = path_ + ": rejected, " + (v ? std::string("not an object") : "JSON error at " + std::to_string(pe.line) + ":" + std::to_string(pe.col));
return false;
}
root_ = *v->asObj();
return true;
}
bool PrefsStore::save(std::string& err) const {
std::error_code ec;
fs::path dir = fs::path(path_).parent_path();
if (!dir.empty()) fs::create_directories(dir, ec);
std::string tmp = path_ + ".tmp";
std::string text = ttgui::json::dump(js::Value(root_));
int fd = ::open(tmp.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0600);
if (fd < 0) { err = std::string("open: ") + std::strerror(errno); return false; }
size_t off = 0;
while (off < text.size()) {
ssize_t n = ::write(fd, text.data() + off, text.size() - off);
if (n < 0) { if (errno == EINTR) continue; err = std::string("write: ") + std::strerror(errno); ::close(fd); fs::remove(tmp, ec); return false; }
off += (size_t)n;
}
if (::fsync(fd) != 0) { err = std::string("fsync: ") + std::strerror(errno); ::close(fd); fs::remove(tmp, ec); return false; }
::close(fd);
fs::rename(tmp, path_, ec);
if (ec) { err = "rename: " + ec.message(); return false; }
return true;
}
std::string PrefsStore::str(const std::string& key, const std::string& fallback) const {
auto it = root_.find(key);
return it == root_.end() ? fallback : it->second.asStr(fallback);
}
void PrefsStore::setStr(const std::string& key, const std::string& value) { root_[key] = js::Value(value); }
std::map<std::string, std::string> PrefsStore::keybindings() const {
std::map<std::string, std::string> m;
auto it = root_.find("keybindings");
if (it != root_.end() && it->second.isObj())
for (const auto& kv : *it->second.asObj()) if (kv.second.isStr()) m[kv.first] = kv.second.asStr();
return m;
}
void PrefsStore::setKeybinding(const std::string& action, const std::string& key) {
js::Object o;
auto it = root_.find("keybindings");
if (it != root_.end() && it->second.isObj()) o = *it->second.asObj();
o[action] = js::Value(key);
root_["keybindings"] = js::Value(std::move(o));
}
void PrefsStore::clearKeybindings() { root_.erase("keybindings"); }
std::vector<std::string> PrefsStore::recentEmoji() const {
std::vector<std::string> v;
auto it = root_.find("recentEmoji");
if (it != root_.end() && it->second.isArr()) for (const auto& e : *it->second.asArr()) if (e.isStr()) v.push_back(e.asStr());
return v;
}
void PrefsStore::addRecentEmoji(const std::string& e) {
auto v = recentEmoji();
v.erase(std::remove(v.begin(), v.end(), e), v.end());
v.insert(v.begin(), e);
js::Array a;
for (const auto& x : v) a.push_back(js::Value(x));
root_["recentEmoji"] = js::Value(std::move(a));
}
std::vector<PrefsStore::WindowRec> PrefsStore::windows() const {
std::vector<WindowRec> v;
auto it = root_.find("windows");
if (it == root_.end() || !it->second.isArr()) return v;
for (const auto& e : *it->second.asArr()) {
WindowRec r;
r.peer = js::getStr(e, "peer"); r.x = js::getInt(e, "x"); r.y = js::getInt(e, "y"); r.w = js::getInt(e, "w"); r.h = js::getInt(e, "h");
if (!r.peer.empty()) v.push_back(r);
}
return v;
}
void PrefsStore::setWindows(const std::vector<WindowRec>& w) {
js::Array a;
for (const auto& r : w) {
js::Object o;
o["peer"] = js::Value(r.peer); o["x"] = js::Value(r.x); o["y"] = js::Value(r.y); o["w"] = js::Value(r.w); o["h"] = js::Value(r.h);
a.push_back(js::Value(std::move(o)));
}
root_["windows"] = js::Value(std::move(a));
}
} // namespace mologram::ui