1 /*
2  *  KCemu -- The emulator for the KC85 homecomputer series and much more.
3  *  Copyright (C) 1997-2010 Torsten Paul
4  *
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License along
16  *  with this program; if not, write to the Free Software Foundation, Inc.,
17  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 #ifndef __kc_prefs_prefs_h
21 #define __kc_prefs_prefs_h
22 
23 #include <map>
24 #include <string>
25 
26 #include "kc/prefs/types.h"
27 #include "kc/prefs/profile.h"
28 
29 using namespace std;
30 
31 struct ProfileVisitor {
32     virtual void handle_profile(Profile *profile) = 0;
33 };
34 
35 class Preferences {
36 private:
37     typedef map<string, Profile *> profile_map_t;
38 
39     static Preferences *_instance;
40 
41     static const char * PROFILE_NAME_ROOT;
42     static const char * PROFILE_NAME_DEFAULT;
43     static const char * PROFILE_KEY_NAME;
44     static const char * PROFILE_KEY_SYSTEM;
45     static const char * PROFILE_KEY_VARIANT;
46     static const char * CONFIG_FILE_EXTENSION;
47     static const char * USER_CONFIG_PREFIX;
48 
49     string _sys_dir;
50     string _usr_dir;
51     string _add_dir;
52 
53     Profile *_root_profile;
54     Profile *_default_profile;
55 
56     profile_map_t _sys_profiles;
57     profile_map_t _usr_profiles;
58     profile_map_t _add_profiles;
59 
60     map<kc_type_t, Profile *> _usr_profiles_by_type;
61 
62     Profile *_current_profile;
63     SystemType *_current_system_type;
64 
65     void load_default_profiles(void);
66     void load_system_profiles(void);
67     void load_user_profiles(void);
68 
69 protected:
70     Preferences(const char *sys_dir, const char *usr_dir, const char *add_dir);
71     virtual ~Preferences(void);
72 
73     void visit_changed(ProfileVisitor &visitor);
74 
75     string get_profile_path(string dir, string config_name);
76     Profile * get_profile(string path, profile_level_t level, string config_name, string name);
77 
78 public:
79     static void init(const char *system_dir, const char *user_dir, const char *add_dir);
80     static Preferences * instance(void);
81 
82     Profile * find_profile(const char *key);
83     Profile * find_profile_by_name(const char *name);
84     list<Profile *> find_child_profiles(const char *key);
85     Profile * copy_user_profile(Profile *profile);
86     Profile * create_user_profile(Profile *parent);
87 
88     void save(void);
89     void reject(void);
90     bool mkdirs(string dir);
91     bool has_changed_profiles(void);
92     bool save_profile(Profile *profile);
93     void reject_changes(Profile *profile);
94     void dump_profile(Profile *profile);
95 
96     virtual void set_current_profile(const char *name, int type);
97     virtual SystemType * find_system_type(kc_type_t kc_type, kc_variant_t kc_variant);
98 
99     virtual SystemType * get_system_type(void);
100     virtual kc_type_t get_kc_type(void);
101     virtual kc_variant_t get_kc_variant(void);
102     virtual const char * get_kc_type_name(void);
103     virtual const char * get_kc_variant_name(void);
104 
105     virtual int get_int_value(string key, int default_value);
106     virtual const char * get_string_value(string key, const char *default_value);
107 };
108 
109 #endif /* __kc_prefs_prefs_h */
110