~ubuntu-branches/ubuntu/wily/apparmor/wily

« back to all changes in this revision

Viewing changes to deprecated/management/profile-editor/src/Configuration.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Kees Cook
  • Date: 2011-08-10 18:12:34 UTC
  • mto: This revision was merged to the branch mainline in revision 9.
  • Revision ID: james.westby@ubuntu.com-20110810181234-b6obckg60cp99crg
Tags: upstream-2.7.0~beta1+bzr1774
ImportĀ upstreamĀ versionĀ 2.7.0~beta1+bzr1774

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*      AppArmor Profile Editor (C) 2006 Novell, Inc.
2
 
 *
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.
7
 
 * 
8
 
 */
9
 
 
10
 
#include "wx/wxprec.h"
11
 
 
12
 
#ifndef WX_PRECOMP
13
 
#include "wx/wx.h"
14
 
#endif
15
 
 
16
 
#include <wx/config.h>
17
 
#include "Configuration.h"
18
 
 
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;
38
 
/**
39
 
 * Reads in the initial variables
40
 
 */
41
 
void Configuration::Initialize()
42
 
{
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);
62
 
 
63
 
}
64
 
 
65
 
/**
66
 
 * Profiles are most likely stored in either /etc/subdomain.d or
67
 
 * /etc/apparmor.d.  Stat each to see which.
68
 
 * @param void
69
 
 * @return profile directory
70
 
 */
71
 
wxString Configuration::BestGuessProfileDirectory(void)
72
 
{
73
 
        if (wxDirExists(_("/etc/apparmor.d")))
74
 
                return (_("/etc/apparmor.d"));
75
 
        else if (wxDirExists(_("/etc/subdomain.d")))
76
 
                return (_("/etc/subdomain.d"));
77
 
        else
78
 
                return (_("/"));
79
 
}
80
 
 
81
 
/**
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
86
 
 */
87
 
wxString Configuration::BestGuessParserCommand()
88
 
{
89
 
        if (wxFileExists(_("/sbin/apparmor_parser")))
90
 
                return _("/sbin/apparmor_parser");
91
 
        else if (wxFileExists(_("/sbin/subdomain_parser")))
92
 
                return _("/sbin/subdomain_parser");
93
 
        else 
94
 
                return _("/bin/false");
95
 
}
96
 
 
97
 
/**
98
 
 * Writes all of the values to disk
99
 
 * @return only true for now
100
 
 */
101
 
bool Configuration::CommitChanges()
102
 
{
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();
117
 
        return true;
118
 
}
119
 
 
120
 
/**
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
125
 
 */
126
 
void Configuration::WriteWindowSettings(const wxPoint &pos, const wxSize& size)
127
 
{
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();
133
 
}
134
 
/**
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
140
 
 * @return a colour
141
 
 */
142
 
wxColour Configuration::_ReadColour(const wxString& key, const wxColour& defaultColour)
143
 
{
144
 
        wxColour ret;
145
 
        wxString tmpStr = wxConfig::Get()->Read(key, wxEmptyString);
146
 
 
147
 
        if (tmpStr.Length() == 6)
148
 
        {
149
 
                ret.Set(wxHexToDec(tmpStr.Mid(0,2)), // Red
150
 
                        wxHexToDec(tmpStr.Mid(2,2)), // Green
151
 
                        wxHexToDec(tmpStr.Mid(4,2)) // Blue
152
 
                );
153
 
        }
154
 
 
155
 
        if (ret.Ok())
156
 
                return ret;
157
 
        else
158
 
                return defaultColour;
159
 
}
160
 
 
161
 
/**
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
165
 
 */
166
 
void Configuration::_ReadFont(const wxString& key, wxFont& font)
167
 
{
168
 
        wxString tmpStr;
169
 
        if (wxConfig::Get()->Read(key, &tmpStr))
170
 
                font.SetNativeFontInfo(tmpStr);
171
 
}
172
 
 
173
 
/**
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
177
 
 */
178
 
void Configuration::_WriteColour(const wxString& key, const wxColour& colour)
179
 
{
180
 
        wxConfig::Get()->Write(key, wxString::Format(_T("%02x%02x%02x"),
181
 
                                                        colour.Red(),
182
 
                                                        colour.Green(),
183
 
                                                        colour.Blue()));
184
 
}
185