2
// ApplyPolicyDialog.cs
5
// Lluis Sanchez Gual <lluis@novell.com>
7
// Copyright (c) 2011 Novell, Inc (http://www.novell.com)
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
10
// of this software and associated documentation files (the "Software"), to deal
11
// in the Software without restriction, including without limitation the rights
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
// copies of the Software, and to permit persons to whom the Software is
14
// furnished to do so, subject to the following conditions:
16
// The above copyright notice and this permission notice shall be included in
17
// all copies or substantial portions of the Software.
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27
using MonoDevelop.Projects.Policies;
29
using MonoDevelop.Ide;
30
using MonoDevelop.Core;
31
using System.Collections.Generic;
34
namespace MonoDevelop.Ide.Projects
36
public partial class ApplyPolicyDialog : Gtk.Dialog
38
IPolicyProvider policyProvider;
40
public ApplyPolicyDialog (IPolicyProvider policyProvider)
43
this.policyProvider = policyProvider;
45
foreach (PolicySet pset in PolicyService.GetPolicySets ())
47
combPolicies.AppendText (pset.Name);
49
fileEntry.DefaultPath = ExportProjectPolicyDialog.DefaultFileDialogPolicyDir;
50
combPolicies.Active = 0;
51
OnRadioCustomToggled (null, null);
52
UpdateContentLabels ();
55
protected void OnRadioCustomToggled (object sender, System.EventArgs e)
57
boxCustom.Sensitive = radioCustom.Active;
58
boxFile.Sensitive = !radioCustom.Active;
59
UpdateContentLabels ();
62
protected void OnButtonOkClicked (object sender, System.EventArgs e)
64
PolicySet pset = GetPolicySet (true);
66
policyProvider.Policies.Import (pset, false);
67
Respond (Gtk.ResponseType.Ok);
71
PolicySet GetPolicySet (bool notifyErrors)
73
if (radioCustom.Active) {
74
return PolicyService.GetPolicySet (combPolicies.ActiveText);
77
if (string.IsNullOrEmpty (fileEntry.Path)) {
79
MessageService.ShowError (GettextCatalog.GetString ("File name not specified"));
83
PolicySet pset = new PolicySet ();
84
pset.LoadFromFile (fileEntry.Path);
85
ExportProjectPolicyDialog.DefaultFileDialogPolicyDir = System.IO.Path.GetDirectoryName (fileEntry.Path);
87
} catch (Exception ex) {
89
MessageService.ShowException (ex, GettextCatalog.GetString ("The policy set could not be loaded"));
95
void UpdateContentLabels ()
97
PolicySet pset = GetPolicySet (false);
99
labelChangesTitle.Hide ();
100
if (radioFile.Active) {
101
if (string.IsNullOrEmpty (fileEntry.Path) || !System.IO.File.Exists (fileEntry.Path)) {
102
labelChanges.Text = GettextCatalog.GetString ("Please select a valid policy file");
105
labelChanges.Text = GettextCatalog.GetString ("The selected file is not a valid policies file");
109
labelChanges.Text = string.Empty;
112
labelChangesTitle.Show ();
113
labelChanges.Text = GetPoliciesDescription (pset);
116
public static string GetPoliciesDescription (PolicyContainer pset)
118
Dictionary<string,List<string>> content = new Dictionary<string, List<string>> ();
119
foreach (var p in pset.DirectGetAll ()) {
120
string name = PolicyService.GetPolicyTypeDescription (p.PolicyType);
122
if (!content.TryGetValue (name, out scopes))
123
scopes = content [name] = new List<string> ();
124
scopes.Add (p.Scope ?? "");
127
var sorted = content.ToList ();
128
sorted.Sort ((x, y) => x.Key.CompareTo(y.Key));
129
StringBuilder sb = new StringBuilder ();
131
foreach (var pol in sorted) {
135
if (pol.Value.Count != 1 || pol.Value[0].Length != 0) {
138
if (pol.Value.Remove ("")) {
139
sb.Append (GettextCatalog.GetString ("default settings"));
142
foreach (var s in pol.Value) {
151
return sb.ToString ();
154
protected void OnCombPoliciesChanged (object sender, System.EventArgs e)
156
UpdateContentLabels ();
159
protected void OnFileEntryPathChanged (object sender, System.EventArgs e)
161
UpdateContentLabels ();