~ubuntu-branches/ubuntu/oneiric/monodevelop/oneiric

« back to all changes in this revision

Viewing changes to src/core/MonoDevelop.Ide/MonoDevelop.Ide.Projects/ApplyPolicyDialog.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2011-06-27 17:03:13 UTC
  • mto: (1.8.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 54.
  • Revision ID: james.westby@ubuntu.com-20110627170313-6cvz3s19x6e9hqe9
ImportĀ upstreamĀ versionĀ 2.5.92+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// 
 
2
// ApplyPolicyDialog.cs
 
3
//  
 
4
// Author:
 
5
//       Lluis Sanchez Gual <lluis@novell.com>
 
6
// 
 
7
// Copyright (c) 2011 Novell, Inc (http://www.novell.com)
 
8
// 
 
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:
 
15
// 
 
16
// The above copyright notice and this permission notice shall be included in
 
17
// all copies or substantial portions of the Software.
 
18
// 
 
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
 
25
// THE SOFTWARE.
 
26
using System;
 
27
using MonoDevelop.Projects.Policies;
 
28
using System.Linq;
 
29
using MonoDevelop.Ide;
 
30
using MonoDevelop.Core;
 
31
using System.Collections.Generic;
 
32
using System.Text;
 
33
 
 
34
namespace MonoDevelop.Ide.Projects
 
35
{
 
36
        public partial class ApplyPolicyDialog : Gtk.Dialog
 
37
        {
 
38
                IPolicyProvider policyProvider;
 
39
                
 
40
                public ApplyPolicyDialog (IPolicyProvider policyProvider)
 
41
                {
 
42
                        this.Build ();
 
43
                        this.policyProvider = policyProvider;
 
44
                        
 
45
                        foreach (PolicySet pset in PolicyService.GetPolicySets ())
 
46
                                if (pset.Visible)
 
47
                                        combPolicies.AppendText (pset.Name);
 
48
                        
 
49
                        fileEntry.DefaultPath = ExportProjectPolicyDialog.DefaultFileDialogPolicyDir;
 
50
                        combPolicies.Active = 0;
 
51
                        OnRadioCustomToggled (null, null);
 
52
                        UpdateContentLabels ();
 
53
                }
 
54
 
 
55
                protected void OnRadioCustomToggled (object sender, System.EventArgs e)
 
56
                {
 
57
                        boxCustom.Sensitive = radioCustom.Active;
 
58
                        boxFile.Sensitive = !radioCustom.Active;
 
59
                        UpdateContentLabels ();
 
60
                }
 
61
                 
 
62
                protected void OnButtonOkClicked (object sender, System.EventArgs e)
 
63
                {
 
64
                        PolicySet pset = GetPolicySet (true);
 
65
                        if (pset != null) {
 
66
                                policyProvider.Policies.Import (pset, false);
 
67
                                Respond (Gtk.ResponseType.Ok);
 
68
                        }
 
69
                }
 
70
                
 
71
                PolicySet GetPolicySet (bool notifyErrors)
 
72
                {
 
73
                        if (radioCustom.Active) {
 
74
                                return PolicyService.GetPolicySet (combPolicies.ActiveText);
 
75
                        }
 
76
                        else {
 
77
                                if (string.IsNullOrEmpty (fileEntry.Path)) {
 
78
                                        if (notifyErrors)
 
79
                                                MessageService.ShowError (GettextCatalog.GetString ("File name not specified"));
 
80
                                        return null;
 
81
                                }
 
82
                                try {
 
83
                                        PolicySet pset = new PolicySet ();
 
84
                                        pset.LoadFromFile (fileEntry.Path);
 
85
                                        ExportProjectPolicyDialog.DefaultFileDialogPolicyDir = System.IO.Path.GetDirectoryName (fileEntry.Path);
 
86
                                        return pset;
 
87
                                } catch (Exception ex) {
 
88
                                        if (notifyErrors)
 
89
                                                MessageService.ShowException (ex, GettextCatalog.GetString ("The policy set could not be loaded"));
 
90
                                        return null;
 
91
                                }
 
92
                        }
 
93
                }
 
94
                
 
95
                void UpdateContentLabels ()
 
96
                {
 
97
                        PolicySet pset = GetPolicySet (false);
 
98
                        if (pset == null) {
 
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");
 
103
                                        }
 
104
                                        else {
 
105
                                                labelChanges.Text = GettextCatalog.GetString ("The selected file is not a valid policies file");
 
106
                                        }
 
107
                                }
 
108
                                else
 
109
                                        labelChanges.Text = string.Empty;
 
110
                                return;
 
111
                        }
 
112
                        labelChangesTitle.Show ();
 
113
                        labelChanges.Text = GetPoliciesDescription (pset);
 
114
                }
 
115
                
 
116
                public static string GetPoliciesDescription (PolicyContainer pset)
 
117
                {
 
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);
 
121
                                List<string> scopes;
 
122
                                if (!content.TryGetValue (name, out scopes))
 
123
                                        scopes = content [name] = new List<string> ();
 
124
                                scopes.Add (p.Scope ?? "");
 
125
                        }
 
126
                        
 
127
                        var sorted = content.ToList ();
 
128
                        sorted.Sort ((x, y) => x.Key.CompareTo(y.Key));
 
129
                        StringBuilder sb = new StringBuilder ();
 
130
                        
 
131
                        foreach (var pol in sorted) {
 
132
                                if (sb.Length > 0)
 
133
                                        sb.Append ('\n');
 
134
                                sb.Append (pol.Key);
 
135
                                if (pol.Value.Count != 1 || pol.Value[0].Length != 0) {
 
136
                                        sb.Append (" (");
 
137
                                        bool first = true;
 
138
                                        if (pol.Value.Remove ("")) {
 
139
                                                sb.Append (GettextCatalog.GetString ("default settings"));
 
140
                                                first = false;
 
141
                                        }
 
142
                                        foreach (var s in pol.Value) {
 
143
                                                if (!first)
 
144
                                                        sb.Append (", ");
 
145
                                                sb.Append (s);
 
146
                                                first = false;
 
147
                                        }
 
148
                                        sb.Append (")");
 
149
                                }
 
150
                        }
 
151
                        return sb.ToString ();
 
152
                }
 
153
 
 
154
                protected void OnCombPoliciesChanged (object sender, System.EventArgs e)
 
155
                {
 
156
                        UpdateContentLabels ();
 
157
                }
 
158
 
 
159
                protected void OnFileEntryPathChanged (object sender, System.EventArgs e)
 
160
                {
 
161
                        UpdateContentLabels ();
 
162
                }
 
163
        }
 
164
}
 
165