~ubuntu-branches/ubuntu/saucy/monodevelop/saucy-proposed

« back to all changes in this revision

Viewing changes to src/core/MonoDevelop.Ide/MonoDevelop.Ide.Gui.Dialogs/MimeTypePolicyOptionsPanel.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2010-09-10 16:54:48 UTC
  • mfrom: (19.1.1 experimental)
  • Revision ID: james.westby@ubuntu.com-20100910165448-0rybfk25zd4o9431
Tags: 2.4+dfsg-2
* debian/patches/inject_Mono.Debugger.Soft_source.patch,
  debian/patches/use_system_Mono.Debugger.Soft.patch,
  debian/control:
  + Build against system Soft Debugger, since we now have a new
    enough Mono to match MonoDevelop's required API

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// 
 
2
// MimeTypePolicyOptionsPanel.cs
 
3
//  
 
4
// Author:
 
5
//       Lluis Sanchez Gual <lluis@novell.com>
 
6
// 
 
7
// Copyright (c) 2009 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
 
 
27
using System;
 
28
using System.Collections.Generic;
 
29
using Gtk;
 
30
using System.Linq;
 
31
 
 
32
using MonoDevelop.Core;
 
33
using MonoDevelop.Ide.Gui.Dialogs;
 
34
using MonoDevelop.Projects;
 
35
using MonoDevelop.Projects.Policies;
 
36
 
 
37
namespace MonoDevelop.Ide.Gui.Dialogs
 
38
{
 
39
        internal interface IMimeTypePolicyOptionsPanel: IOptionsPanel
 
40
        {
 
41
                void InitializePolicy (PolicyContainer policyContainer, string mimeType, bool isExactMimeType);
 
42
                void SetParentSection (MimeTypePolicyOptionsSection section);
 
43
                
 
44
                string Label { get; set; }
 
45
                Widget CreateMimePanelWidget ();
 
46
                
 
47
                void LoadCurrentPolicy ();
 
48
                void LoadParentPolicy ();
 
49
                void LoadSetPolicy (PolicySet pset);
 
50
                void StorePolicy ();
 
51
                
 
52
                bool HasCustomPolicy { get; }
 
53
                void RemovePolicy (PolicyContainer bag);
 
54
                IEnumerable<PolicySet> GetPolicySets ();
 
55
                PolicySet GetMatchingSet ();
 
56
        }
 
57
        
 
58
        public abstract class MimeTypePolicyOptionsPanel<T>: ItemOptionsPanel, IMimeTypePolicyOptionsPanel where T : class, IEquatable<T>, new ()
 
59
        {
 
60
                MimeTypePolicyOptionsSection section;
 
61
                string label;
 
62
                string mimeType;
 
63
                IEnumerable<string> mimeTypeScopes;
 
64
                PolicyContainer policyContainer;
 
65
                bool loaded;
 
66
                object cachedPolicy;
 
67
                bool hasCachedPolicy;
 
68
                CheckButton defaultSettingsButton;
 
69
                Widget panelWidget;
 
70
                bool isExactMimeType;
 
71
                
 
72
                void IMimeTypePolicyOptionsPanel.InitializePolicy (PolicyContainer policyContainer, string mimeType, bool isExactMimeType)
 
73
                {
 
74
                        this.mimeType = mimeType;
 
75
                        this.policyContainer = policyContainer;
 
76
                        this.isExactMimeType = isExactMimeType;
 
77
                        mimeTypeScopes = DesktopService.GetMimeTypeInheritanceChain (mimeType);
 
78
                }
 
79
                
 
80
                void IMimeTypePolicyOptionsPanel.SetParentSection (MimeTypePolicyOptionsSection section)
 
81
                {
 
82
                        this.section = section;
 
83
                }
 
84
                
 
85
                void IMimeTypePolicyOptionsPanel.StorePolicy ()
 
86
                {
 
87
                        if (loaded) {
 
88
                                if (defaultSettingsButton != null && defaultSettingsButton.Active)
 
89
                                        policyContainer.Set<T> (null, mimeType);
 
90
                                else
 
91
                                        policyContainer.Set<T> (GetPolicy (), mimeType);
 
92
                        } else if (hasCachedPolicy) {
 
93
                                policyContainer.Set<T> ((T) cachedPolicy, mimeType);
 
94
                        }
 
95
                }
 
96
                
 
97
                void IMimeTypePolicyOptionsPanel.LoadParentPolicy ()
 
98
                {
 
99
                        T policy = GetInheritedPolicy (mimeTypeScopes);
 
100
                        
 
101
                        if (loaded) {
 
102
                                UpdateDefaultSettingsButton (policyContainer.ParentPolicies);
 
103
                                LoadFrom ((T)policy);
 
104
                        } else {
 
105
                                cachedPolicy = policy;
 
106
                                if (GetDirectInherited (policyContainer.ParentPolicies) == null)
 
107
                                        cachedPolicy = null;
 
108
                                hasCachedPolicy = true;
 
109
                        }
 
110
                }
 
111
                
 
112
                T GetInheritedPolicy (IEnumerable<string> scopes)
 
113
                {
 
114
                        foreach (string scope in scopes) {
 
115
                                PolicyContainer currentBag = scope == mimeType ? policyContainer.ParentPolicies : policyContainer;
 
116
                                while (currentBag != null) {
 
117
                                        if (currentBag.DirectHas<T> (scope)) {
 
118
                                                T pol = currentBag.DirectGet<T> (scope);
 
119
                                                if (pol != null)
 
120
                                                        return pol;
 
121
                                                // Default settings requested for this scope. Start looking from the original
 
122
                                                // bag now using the next scope in the chain
 
123
                                                break;
 
124
                                        } else
 
125
                                                currentBag = currentBag.ParentPolicies;
 
126
                                }
 
127
                        }
 
128
                        return PolicyService.GetDefaultPolicy<T>(scopes);
 
129
                }
 
130
                
 
131
                void IMimeTypePolicyOptionsPanel.LoadSetPolicy (PolicySet pset)
 
132
                {
 
133
                        object selected = pset.Get<T> (mimeTypeScopes);
 
134
                        if (selected == null)
 
135
                                selected = PolicyService.GetDefaultPolicy<T> (mimeTypeScopes);
 
136
 
 
137
                        if (loaded) {
 
138
                                if (defaultSettingsButton != null) {
 
139
                                        defaultSettingsButton.Active = false;
 
140
                                        panelWidget.Sensitive = true;
 
141
                                }
 
142
                                LoadFrom ((T)selected);
 
143
                        } else {
 
144
                                cachedPolicy = selected;
 
145
                                hasCachedPolicy = true;
 
146
                        }
 
147
                }
 
148
                
 
149
                bool IMimeTypePolicyOptionsPanel.HasCustomPolicy {
 
150
                        get {
 
151
                                return policyContainer.DirectHas<T> (mimeType);
 
152
                        }
 
153
                }
 
154
                
 
155
                IEnumerable<PolicySet> IMimeTypePolicyOptionsPanel.GetPolicySets ()
 
156
                {
 
157
                        return PolicyService.GetPolicySets<T> (mimeTypeScopes);
 
158
                }
 
159
                
 
160
                PolicySet IMimeTypePolicyOptionsPanel.GetMatchingSet ()
 
161
                {
 
162
                        T pol = GetCurrentPolicy ();
 
163
                        return PolicyService.GetMatchingSet (pol, mimeTypeScopes);
 
164
                }
 
165
                
 
166
                void IMimeTypePolicyOptionsPanel.RemovePolicy (PolicyContainer bag)
 
167
                {
 
168
                        bag.Remove<T> (mimeType);
 
169
                }
 
170
                
 
171
                T GetDirectInherited (PolicyContainer initialContainer)
 
172
                {
 
173
                        if (initialContainer == policyContainer && !loaded && hasCachedPolicy)
 
174
                                return (T)cachedPolicy;
 
175
                        PolicyContainer pc = initialContainer;
 
176
                        while (pc != null) {
 
177
                                if (pc.DirectHas<T> (mimeType))
 
178
                                        return pc.DirectGet<T> (mimeType);
 
179
                                pc = pc.ParentPolicies;
 
180
                        }
 
181
                        return PolicyService.GetUserDefaultPolicySet ().Get<T> (mimeType);
 
182
                }
 
183
                
 
184
                void UpdateDefaultSettingsButton (PolicyContainer initialContainer)
 
185
                {
 
186
                        if (defaultSettingsButton != null) {
 
187
                                T pol = GetDirectInherited (initialContainer);
 
188
                                if (pol != null) {
 
189
                                        panelWidget.Sensitive = true;
 
190
                                        defaultSettingsButton.Active = false;
 
191
                                } else {
 
192
                                        panelWidget.Sensitive = false;
 
193
                                        defaultSettingsButton.Active = true;
 
194
                                }
 
195
                        }
 
196
                }
 
197
                
 
198
                void IMimeTypePolicyOptionsPanel.LoadCurrentPolicy ()
 
199
                {
 
200
                        T policy = GetCurrentPolicy ();
 
201
                        UpdateDefaultSettingsButton (policyContainer);
 
202
                        loaded = true;
 
203
                        hasCachedPolicy = false;
 
204
                        LoadFrom (policy);
 
205
                }
 
206
                
 
207
                T GetCurrentPolicy ()
 
208
                {
 
209
                        object pol = null;
 
210
                        if (loaded)
 
211
                                pol = GetPolicy ();
 
212
                        else if (hasCachedPolicy)
 
213
                                pol = cachedPolicy;
 
214
 
 
215
                        if (pol == null)
 
216
                                pol = policyContainer.Get<T> (mimeTypeScopes) ?? PolicyService.GetDefaultPolicy<T> (mimeTypeScopes);
 
217
                        return (T) pol;
 
218
                }
 
219
                
 
220
                string IMimeTypePolicyOptionsPanel.Label {
 
221
                        get {
 
222
                                return label;
 
223
                        }
 
224
                        set {
 
225
                                label = value;
 
226
                        }
 
227
                }
 
228
 
 
229
                Widget IMimeTypePolicyOptionsPanel.CreateMimePanelWidget ()
 
230
                {
 
231
                        panelWidget = CreatePanelWidget ();
 
232
                        //HACK: work around bug 469427 - broken themes match on widget names
 
233
                        if (panelWidget.Name.IndexOf ("Panel") > 0)
 
234
                                panelWidget.Name = panelWidget.Name.Replace ("Panel", "_");
 
235
                        if (isExactMimeType)
 
236
                                return panelWidget;
 
237
                        
 
238
                        VBox box = new VBox ();
 
239
                        box.Spacing = 6;
 
240
                        
 
241
                        string baseType = mimeTypeScopes.ElementAt (1);
 
242
                        baseType = DesktopService.GetMimeTypeDescription (baseType);
 
243
                        defaultSettingsButton = new CheckButton (GettextCatalog.GetString ("Use default settings from '{0}'", baseType));
 
244
                        defaultSettingsButton.Clicked += delegate {
 
245
                                if (defaultSettingsButton.Active) {
 
246
                                        panelWidget.Sensitive = false;
 
247
                                        List<string> baseTypes = new List<string> (mimeTypeScopes);
 
248
                                        baseTypes.RemoveAt (0);
 
249
                                        LoadFrom (GetInheritedPolicy (baseTypes));
 
250
                                } else {
 
251
                                        panelWidget.Sensitive = true;
 
252
                                }
 
253
                        };
 
254
                        
 
255
                        box.PackStart (defaultSettingsButton, false, false, 0);
 
256
                        box.PackStart (new HSeparator (), false, false, 0);
 
257
                        box.ShowAll ();
 
258
                        box.PackStart (panelWidget, true, true, 0);
 
259
                        panelWidget.Show ();
 
260
                        return box;
 
261
                }
 
262
 
 
263
                
 
264
                protected abstract void LoadFrom (T policy);
 
265
                
 
266
                protected abstract T GetPolicy ();
 
267
                
 
268
                public override void ApplyChanges ()
 
269
                {
 
270
                }
 
271
                
 
272
                public void UpdateSelectedNamedPolicy ()
 
273
                {
 
274
                        section.UpdateSelectedNamedPolicy ();
 
275
                }
 
276
        }
 
277
}