1
/* AppArmor Profile Editor (C) 2006 Novell, Inc.
3
* This program is free software; you can redistribute it and/or modify
4
* it under the terms of the GNU General Public License as published by
5
* the Free Software Foundation; either version 2 of the License, or
6
* (at your option) any later version.
10
#include "wx/wxprec.h"
16
#include <wx/config.h>
17
#include "Configuration.h"
19
// Initialize all of the static variables
20
wxString Configuration::mProfileDirectory = wxEmptyString;
21
wxString Configuration::mProfileEditorExecutable = wxEmptyString;
22
wxString Configuration::mParserCommand = wxEmptyString;
23
wxString Configuration::mTemplateText = wxEmptyString;
24
wxColour Configuration::mCommentColour = wxColour(DEFAULT_COMMENT_COLOUR);
25
wxColour Configuration::mIncludeColour = wxColour(DEFAULT_INCLUDE_COLOUR);
26
wxColour Configuration::mCapColour = wxColour(DEFAULT_CAP_COLOUR);
27
wxColour Configuration::mPathColour = wxColour(DEFAULT_PATH_COLOUR);
28
wxColour Configuration::mPermColour = wxColour(DEFAULT_PERM_COLOUR);
29
wxFont Configuration::mCapabilityFont(10, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL);
30
wxFont Configuration::mCommentFont(10, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_ITALIC, wxFONTWEIGHT_NORMAL);
31
wxFont Configuration::mIncludeFont(10, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL);
32
wxFont Configuration::mPathFont(10, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_BOLD);
33
wxFont Configuration::mPermsFont(10, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL,wxFONTWEIGHT_NORMAL);
34
int Configuration::mWindowX;
35
int Configuration::mWindowY;
36
int Configuration::mWindowHeight;
37
int Configuration::mWindowWidth;
39
* Reads in the initial variables
41
void Configuration::Initialize()
43
// Read in all of the values
44
mProfileEditorExecutable = wxTheApp->argv[0];
45
mWindowX = wxConfig::Get()->Read(_("WindowX"), 50);
46
mWindowY = wxConfig::Get()->Read(_("WindowY"), 50);
47
mWindowWidth = wxConfig::Get()->Read(_("WindowWidth"), 800);
48
mWindowHeight = wxConfig::Get()->Read(_("WindowHeight"), 600);
49
mProfileDirectory = wxConfig::Get()->Read(_("ProfileDirectory"), Configuration::BestGuessProfileDirectory());
50
mParserCommand = wxConfig::Get()->Read(_("Parser"), BestGuessParserCommand());
51
mTemplateText = wxConfig::Get()->Read(_("ProfileTemplate"), wxEmptyString);
52
mCommentColour = _ReadColour(_("CommentColour"), mCommentColour);
53
mIncludeColour = _ReadColour(_("IncludeColour"), mIncludeColour);
54
mCapColour = _ReadColour(_("CapabilityColour"), mCapColour);
55
mPathColour = _ReadColour(_("PathColour"), mPathColour);
56
mPermColour = _ReadColour(_("PermissionColour"), mPermColour);
57
_ReadFont(_("CommentFont"), mCommentFont);
58
_ReadFont(_("IncludeFont"), mIncludeFont);
59
_ReadFont(_("CapabilityFont"), mCapabilityFont);
60
_ReadFont(_("PathFont"), mPathFont);
61
_ReadFont(_("PermsFont"), mPermsFont);
66
* Profiles are most likely stored in either /etc/subdomain.d or
67
* /etc/apparmor.d. Stat each to see which.
69
* @return profile directory
71
wxString Configuration::BestGuessProfileDirectory(void)
73
if (wxDirExists(_("/etc/apparmor.d")))
74
return (_("/etc/apparmor.d"));
75
else if (wxDirExists(_("/etc/subdomain.d")))
76
return (_("/etc/subdomain.d"));
82
* The parser is probably apparmor_parser or
83
* subdomain_parser. If it's neither, the user
84
* will need to set it manually, so return /bin/false
85
* @return the path to the parser
87
wxString Configuration::BestGuessParserCommand()
89
if (wxFileExists(_("/sbin/apparmor_parser")))
90
return _("/sbin/apparmor_parser");
91
else if (wxFileExists(_("/sbin/subdomain_parser")))
92
return _("/sbin/subdomain_parser");
94
return _("/bin/false");
98
* Writes all of the values to disk
99
* @return only true for now
101
bool Configuration::CommitChanges()
103
wxConfig::Get()->Write(_("ProfileDirectory"), mProfileDirectory);
104
wxConfig::Get()->Write(_("Parser"), mParserCommand);
105
wxConfig::Get()->Write(_("ProfileTemplate"), mTemplateText);
106
_WriteColour(_("CommentColour"), mCommentColour);
107
_WriteColour(_("IncludeColour"), mIncludeColour);
108
_WriteColour(_("CapabilityColour"), mCapColour);
109
_WriteColour(_("PathColour"), mPathColour);
110
_WriteColour(_("PermissionColour"), mPermColour);
111
wxConfig::Get()->Write(_("CommentFont"), mCommentFont.GetNativeFontInfoDesc());
112
wxConfig::Get()->Write(_("IncludeFont"), mIncludeFont.GetNativeFontInfoDesc());
113
wxConfig::Get()->Write(_("CapabilityFont"), mCapabilityFont.GetNativeFontInfoDesc());
114
wxConfig::Get()->Write(_("PathFont"), mPathFont.GetNativeFontInfoDesc());
115
wxConfig::Get()->Write(_("PermsFont"), mPermsFont.GetNativeFontInfoDesc());
116
wxConfig::Get()->Flush();
121
* Writes the given window settings to the configuration file.
122
* This is kept separately from CommitChanges() because:
123
* a) There's no reason to re-write all configuration changes on exit and
124
* b) There's no reason to update the window position and size on every OnSize() event
126
void Configuration::WriteWindowSettings(const wxPoint &pos, const wxSize& size)
128
wxConfig::Get()->Write(_("WindowX"), pos.x);
129
wxConfig::Get()->Write(_("WindowY"), pos.y);
130
wxConfig::Get()->Write(_("WindowWidth"), size.GetWidth());
131
wxConfig::Get()->Write(_("WindowHeight"), size.GetHeight());
132
wxConfig::Get()->Flush();
135
* Reads a colour setting from the config file and translates it into
136
* a wxColour. If it can't convert the stored value, or the stored value
137
* does not exist, it will return whatever is passed as defaultColour.
138
* @param key the configuration key
139
* @param defaultColour a colour to return
142
wxColour Configuration::_ReadColour(const wxString& key, const wxColour& defaultColour)
145
wxString tmpStr = wxConfig::Get()->Read(key, wxEmptyString);
147
if (tmpStr.Length() == 6)
149
ret.Set(wxHexToDec(tmpStr.Mid(0,2)), // Red
150
wxHexToDec(tmpStr.Mid(2,2)), // Green
151
wxHexToDec(tmpStr.Mid(4,2)) // Blue
158
return defaultColour;
162
* Reads a font from the configuration file, and sets it as the 'font'
163
* @param key configuration key
164
* @param font the font to set
166
void Configuration::_ReadFont(const wxString& key, wxFont& font)
169
if (wxConfig::Get()->Read(key, &tmpStr))
170
font.SetNativeFontInfo(tmpStr);
174
* Takes a wxColour and converts it to a hex string for writing to disk.
175
* @param key configuration key
176
* @param colour the colour to convert
178
void Configuration::_WriteColour(const wxString& key, const wxColour& colour)
180
wxConfig::Get()->Write(key, wxString::Format(_T("%02x%02x%02x"),