ember/storage/appdatastorage.cpp

65 lines
2.0 KiB
C++

#include <boost/filesystem.hpp>
#include <QStandardPaths>
#include <pugixml.hpp>
#include <properties/config.h>
#include "appdatastorage.h"
namespace Ember
{
boost::filesystem::path AppDataStorage::s_appData;
boost::filesystem::path AppDataStorage::s_recentProjects;
void AppDataStorage::initialize()
{
QString appData = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
s_appData = appData.toStdString();
s_recentProjects = s_appData;
s_recentProjects += RECENT_PROJECTS_FILENAME;
}
void AppDataStorage::readRecentProjects(std::vector<RecentProject> &projects)
{
if (boost::filesystem::exists(s_recentProjects))
{
pugi::xml_document doc;
doc.load_file(s_recentProjects.c_str());
for (auto& node : doc.document_element())
{
if (strcmp(node.name(), "recentProject") == 0)
{
RecentProject recent;
recent.name = node.attribute("name").as_string();
recent.path = node.attribute("path").as_string();
recent.access = static_cast<time_t>(node.attribute("access").as_llong());
recent.pinned = node.attribute("pinned").as_bool();
projects.push_back(recent);
}
}
}
}
void AppDataStorage::storeRecentProjects(const std::vector<RecentProject> &projects)
{
pugi::xml_document doc;
doc.append_child(pugi::node_declaration);
auto root = doc.append_child("recentProjects");
for (RecentProject recent : projects)
{
auto node = root.append_child("recentProject");
node.append_attribute("name").set_value(recent.name.c_str());
node.append_attribute("path").set_value(recent.path.c_str());
node.append_attribute("access").set_value(static_cast<long long>(recent.access));
node.append_attribute("pinned").set_value(recent.pinned);
}
// Save file, ensure directory exists
boost::filesystem::create_directories(s_appData);
doc.save_file(s_recentProjects.string().c_str());
}
}