summaryrefslogtreecommitdiffstats
path: root/src/config.hh
blob: 326a1e0d2fb47c9882d7eef8ab837d3f6361d3f6 (plain)
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
# pragma once

# include <functional>

# include "astroid.hh"

# include <boost/filesystem.hpp>
# include <boost/property_tree/ptree.hpp>
# include <boost/property_tree/json_parser.hpp>

namespace bfs = boost::filesystem;
using boost::property_tree::ptree;

namespace Astroid {
  struct StandardPaths {
    bfs::path home;
    bfs::path config_dir;
    bfs::path data_dir;
    bfs::path cache_dir;
    bfs::path runtime_dir;
    bfs::path config_file;
    bfs::path searches_file;
    bfs::path plugin_dir;
    bfs::path save_dir;
    bfs::path attach_dir;
  };

  struct RuntimePaths {
    bfs::path save_dir;   // last used save to folder
    bfs::path attach_dir; // last used attach from folder
  };

  class Config {
    public:
      Config (bool _test = false, bool no_load = false);
      Config (const char *, bool no_load = false);

      /* dir env vars from XDG with defaults:
       * XDG_CONFIG_HOME  : $HOME/.config/
       * XDG_DATA_HOME    : $HOME/.local/share/
       * XDG_CACHE_HOME   : $HOME/.cache/
       * XDG_RUNTIME_HOME : none
       */

      bool test;

      StandardPaths std_paths;
      RuntimePaths  run_paths;


      void load_config (bool initial = false);
      void load_dirs ();
      bool check_config (ptree);
      void write_back_config ();

      ptree config;
      ptree notmuch_config;
      bool has_notmuch_config;

      const int CONFIG_VERSION = 11;

    private:
      ptree setup_default_config (bool);
      void  setup_default_initial_config (ptree &conf, bool accounts = true, bool startup = true);

      /* merge of property trees */

      // from http://stackoverflow.com/questions/8154107/how-do-i-merge-update-a-boostproperty-treeptree
      template<typename T>
        void traverse_recursive(
            const boost::property_tree::ptree &parent,
            const boost::property_tree::ptree::path_type &childPath,
            const boost::property_tree::ptree &child, T &method);

      void traverse(
          const boost::property_tree::ptree &parent,
          std::function<void(const ptree &,
            const ptree::path_type &,
            const ptree&)> method);

      void merge(const ptree &parent,
                 const ptree::path_type &childPath,
                 const ptree &child);


      void merge_ptree (const ptree &pt);
  };
}