1 /*
2   Original code: http://farmanager.googlecode.com/svn/trunk/plugins/common/unicode/PluginSettings.hpp
3   License: derived from original one without extra restriction
4 */
5 
6 module pluginsettings;
7 
8 import farplugin;
9 import wcharutil;
10 
11 import std.traits;
12 import core.stdc.string;
13 import core.sys.windows.windows;
14 
15 enum bool isIntegralType(T) = isBoolean!T || is(T == long) || is(T == ulong) || is(T == int) || is(T == uint);
16 
17 class PluginSettings
18 {
19 private:
20     HANDLE handle;
21     FARAPISETTINGSCONTROL SettingsControl;
22 
23 public:
24 
25     this(ref const GUID guid, FARAPISETTINGSCONTROL SettingsControl)
26     {
27         this.SettingsControl = SettingsControl;
28         handle = INVALID_HANDLE_VALUE;
29 
30         FarSettingsCreate settings = {FarSettingsCreate.sizeof, guid, handle};
31         if (SettingsControl(INVALID_HANDLE_VALUE, FAR_SETTINGS_CONTROL_COMMANDS.SCTL_CREATE, 0, &settings))
32             handle = settings.Handle;
33     }
34 
35     ~this()
36     {
37         SettingsControl(handle, FAR_SETTINGS_CONTROL_COMMANDS.SCTL_FREE, 0, null);
38     }
39 
40     auto CreateSubKey(size_t Root, wstring Name)
41     {
42         FarSettingsValue value = {FarSettingsValue.sizeof, Root, Name.toWStringz};
43         return SettingsControl(handle, FAR_SETTINGS_CONTROL_COMMANDS.SCTL_CREATESUBKEY, 0, &value);
44     }
45 
46     auto OpenSubKey(size_t Root, wstring Name)
47     {
48         FarSettingsValue value = {FarSettingsValue.sizeof, Root, Name.toWStringz};
49         return SettingsControl(handle, FAR_SETTINGS_CONTROL_COMMANDS.SCTL_OPENSUBKEY, 0, &value);
50     }
51 
52     bool DeleteSubKey(size_t Root)
53     {
54         FarSettingsValue value = {FarSettingsValue.sizeof, Root, null};
55         return SettingsControl(handle, FAR_SETTINGS_CONTROL_COMMANDS.SCTL_DELETE, 0, &value) ? true : false;
56     }
57 
58     bool DeleteValue(size_t Root, wstring Name)
59     {
60         FarSettingsValue value = {FarSettingsValue.sizeof, Root, Name.toWStringz};
61         return SettingsControl(handle, FAR_SETTINGS_CONTROL_COMMANDS.SCTL_DELETE, 0, &value) ? true : false;
62     }
63 
64     wchar[] Get(size_t Root, wstring Name, wchar[] Default)
65     {
66         FarSettingsItem item = {FarSettingsItem.sizeof, Root, Name.toWStringz, FARSETTINGSTYPES.FST_STRING};
67         if (SettingsControl(handle, FAR_SETTINGS_CONTROL_COMMANDS.SCTL_GET, 0, &item))
68         {
69             return item.String.toWString;
70         }
71         return Default;
72     }
73 
74     void Get(size_t Root, wstring Name, wchar[] Value, wchar[] Default)
75     {
76         Get(Root, Name, Value.ptr, Value.length, Default);
77     }
78 
79     void Get(size_t Root, wstring Name, wchar* Value, size_t Size, wchar[] Default)
80     {
81         lstrcpynW(Value, Get(Root, Name, Default).toWStringz, cast(int)Size);
82     }
83 
84     T Get(T)(size_t Root, wstring Name, T Default) if (isIntegralType!T)
85     {
86         FarSettingsItem item = {FarSettingsItem.sizeof, Root, Name.toWStringz, FARSETTINGSTYPES.FST_QWORD};
87         if (SettingsControl(handle, FAR_SETTINGS_CONTROL_COMMANDS.SCTL_GET, 0, &item))
88         {
89             return cast(T)item.Number;
90         }
91         return Default;
92     }
93 
94     size_t Get(size_t Root, wstring Name, void[] Value)
95     {
96         return Get(Root, Name, Value.ptr, Value.length);
97     }
98 
99     size_t Get(size_t Root, wstring Name, void* Value, size_t Size)
100     {
101         FarSettingsItem item = {FarSettingsItem.sizeof, Root, Name.toWStringz, FARSETTINGSTYPES.FST_DATA};
102         if (SettingsControl(handle, FAR_SETTINGS_CONTROL_COMMANDS.SCTL_GET, 0, &item))
103         {
104             Size = (item.Data.Size > Size) ? Size : item.Data.Size;
105             memcpy(Value, item.Data.Data, Size);
106             return Size;
107         }
108         return 0;
109     }
110 
111     bool Set(size_t Root, wstring Name, in wchar[] Value)
112     {
113         return Set(Root, Name, Value.toWStringz);
114     }
115 
116     bool Set(size_t Root, wstring Name, in wchar* Value)
117     {
118         FarSettingsItem item = {FarSettingsItem.sizeof, Root, Name.toWStringz, FARSETTINGSTYPES.FST_STRING};
119         item.String = Value;
120         return SettingsControl(handle, FAR_SETTINGS_CONTROL_COMMANDS.SCTL_SET, 0, &item) != FALSE;
121     }
122 
123     bool Set(T)(size_t Root, wstring Name, T Value) if (isIntegralType!T)
124     {
125         FarSettingsItem item = {FarSettingsItem.sizeof, Root, Name.toWStringz, FARSETTINGSTYPES.FST_QWORD};
126         item.Number = Value;
127         return SettingsControl(handle, FAR_SETTINGS_CONTROL_COMMANDS.SCTL_SET, 0, &item) != FALSE;
128     }
129 
130     bool Set(size_t Root, wstring Name, in void[] Value)
131     {
132         return Set(Root, Name, Value.ptr, Value.length);
133     }
134 
135     bool Set(size_t Root, wstring Name, in void* Value, size_t Size)
136     {
137         FarSettingsItem item = {FarSettingsItem.sizeof, Root, Name.toWStringz, FARSETTINGSTYPES.FST_DATA};
138         item.Data.Size = Size;
139         item.Data.Data = Value;
140         return SettingsControl(handle, FAR_SETTINGS_CONTROL_COMMANDS.SCTL_SET, 0, &item) != FALSE;
141     }
142 }