dropshell/src/templates.cpp

80 lines
2.3 KiB
C++

#include "init_user_directory.hpp"
#include "templates.hpp"
#include "config.hpp"
#include <filesystem>
#include <vector>
#include <string>
#include <algorithm>
namespace dropshell {
template_manager::template_manager() {
// Constructor implementation
}
template_manager::~template_manager() {
// Destructor implementation
}
bool template_manager::get_templates(std::vector<template_info>& templates) {
templates.clear();
// System templates directory
const std::string system_templates_dir = "/opt/dropshell/templates";
// User templates directory (from config)
std::string user_templates_dir;
if (!get_user_directory(user_templates_dir)) {
return false;
}
user_templates_dir += "/usertemplates";
// Helper function to add templates from a directory
auto add_templates_from_dir = [&templates](const std::string& dir_path) {
if (!std::filesystem::exists(dir_path)) {
return;
}
for (const auto& entry : std::filesystem::directory_iterator(dir_path)) {
if (entry.is_directory()) {
template_info info;
info.name = entry.path().filename().string();
info.path = entry.path().string();
// Check if template with same name already exists
auto it = std::find_if(templates.begin(), templates.end(),
[&info](const template_info& t) { return t.name == info.name; });
if (it == templates.end()) {
templates.push_back(info);
}
}
}
};
// First add system templates
add_templates_from_dir(system_templates_dir);
// Then add user templates (which will override system templates with same name)
add_templates_from_dir(user_templates_dir);
return true;
}
bool template_manager::get_template_info(const std::string& name, template_info& info) {
std::vector<template_info> templates;
if (!get_templates(templates)) {
return false;
}
auto it = std::find_if(templates.begin(), templates.end(),
[&name](const template_info& t) { return t.name == name; });
if (it != templates.end()) {
info = *it;
return true;
}
return false;
}
} // namespace dropshell