2018-08-06 18:37:16 +00:00
|
|
|
#include <boost/filesystem.hpp>
|
|
|
|
#include <QStandardPaths>
|
|
|
|
#include <pugixml.hpp>
|
|
|
|
|
|
|
|
#include <properties/config.h>
|
|
|
|
#include "appdatastorage.h"
|
|
|
|
|
|
|
|
namespace Ember
|
|
|
|
{
|
|
|
|
|
2018-08-07 14:51:29 +00:00
|
|
|
boost::filesystem::path AppDataStorage::s_appData;
|
|
|
|
boost::filesystem::path AppDataStorage::s_recentProjects;
|
|
|
|
|
|
|
|
void AppDataStorage::initialize()
|
2018-08-06 18:37:16 +00:00
|
|
|
{
|
|
|
|
QString appData = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
|
2018-08-07 14:51:29 +00:00
|
|
|
s_appData = appData.toStdString();
|
|
|
|
s_recentProjects = s_appData;
|
|
|
|
s_recentProjects += RECENT_PROJECTS_FILENAME;
|
2018-08-06 18:37:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AppDataStorage::readRecentProjects(std::vector<RecentProject> &projects)
|
|
|
|
{
|
2018-08-07 14:51:29 +00:00
|
|
|
if (boost::filesystem::exists(s_recentProjects))
|
2018-08-06 18:37:16 +00:00
|
|
|
{
|
|
|
|
pugi::xml_document doc;
|
2018-08-07 14:51:29 +00:00
|
|
|
doc.load_file(s_recentProjects.c_str());
|
2018-08-06 18:37:16 +00:00
|
|
|
|
|
|
|
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
|
2018-08-07 14:51:29 +00:00
|
|
|
boost::filesystem::create_directories(s_appData);
|
|
|
|
doc.save_file(s_recentProjects.string().c_str());
|
2018-08-06 18:37:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|