2
// MimeTypePolicyOptionsPanel.cs
5
// Lluis Sanchez Gual <lluis@novell.com>
7
// Copyright (c) 2009 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
28
using System.Collections.Generic;
32
using MonoDevelop.Core;
33
using MonoDevelop.Ide.Gui.Dialogs;
34
using MonoDevelop.Projects;
35
using MonoDevelop.Projects.Policies;
37
namespace MonoDevelop.Ide.Gui.Dialogs
39
internal interface IMimeTypePolicyOptionsPanel: IOptionsPanel
41
void InitializePolicy (PolicyContainer policyContainer, string mimeType, bool isExactMimeType);
42
void SetParentSection (MimeTypePolicyOptionsSection section);
44
string Label { get; set; }
45
Widget CreateMimePanelWidget ();
47
void LoadCurrentPolicy ();
48
void LoadParentPolicy ();
49
void LoadSetPolicy (PolicySet pset);
52
bool HasCustomPolicy { get; }
53
void RemovePolicy (PolicyContainer bag);
54
IEnumerable<PolicySet> GetPolicySets ();
55
PolicySet GetMatchingSet ();
58
public abstract class MimeTypePolicyOptionsPanel<T>: ItemOptionsPanel, IMimeTypePolicyOptionsPanel where T : class, IEquatable<T>, new ()
60
MimeTypePolicyOptionsSection section;
63
IEnumerable<string> mimeTypeScopes;
64
PolicyContainer policyContainer;
68
CheckButton defaultSettingsButton;
72
void IMimeTypePolicyOptionsPanel.InitializePolicy (PolicyContainer policyContainer, string mimeType, bool isExactMimeType)
74
this.mimeType = mimeType;
75
this.policyContainer = policyContainer;
76
this.isExactMimeType = isExactMimeType;
77
mimeTypeScopes = DesktopService.GetMimeTypeInheritanceChain (mimeType);
80
void IMimeTypePolicyOptionsPanel.SetParentSection (MimeTypePolicyOptionsSection section)
82
this.section = section;
85
void IMimeTypePolicyOptionsPanel.StorePolicy ()
88
if (defaultSettingsButton != null && defaultSettingsButton.Active)
89
policyContainer.Set<T> (null, mimeType);
91
policyContainer.Set<T> (GetPolicy (), mimeType);
92
} else if (hasCachedPolicy) {
93
policyContainer.Set<T> ((T) cachedPolicy, mimeType);
97
void IMimeTypePolicyOptionsPanel.LoadParentPolicy ()
99
T policy = GetInheritedPolicy (mimeTypeScopes);
102
UpdateDefaultSettingsButton (policyContainer.ParentPolicies);
103
LoadFrom ((T)policy);
105
cachedPolicy = policy;
106
if (GetDirectInherited (policyContainer.ParentPolicies) == null)
108
hasCachedPolicy = true;
112
T GetInheritedPolicy (IEnumerable<string> scopes)
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);
121
// Default settings requested for this scope. Start looking from the original
122
// bag now using the next scope in the chain
125
currentBag = currentBag.ParentPolicies;
128
return PolicyService.GetDefaultPolicy<T>(scopes);
131
void IMimeTypePolicyOptionsPanel.LoadSetPolicy (PolicySet pset)
133
object selected = pset.Get<T> (mimeTypeScopes);
134
if (selected == null)
135
selected = PolicyService.GetDefaultPolicy<T> (mimeTypeScopes);
138
if (defaultSettingsButton != null) {
139
defaultSettingsButton.Active = false;
140
panelWidget.Sensitive = true;
142
LoadFrom ((T)selected);
144
cachedPolicy = selected;
145
hasCachedPolicy = true;
149
bool IMimeTypePolicyOptionsPanel.HasCustomPolicy {
151
return policyContainer.DirectHas<T> (mimeType);
155
IEnumerable<PolicySet> IMimeTypePolicyOptionsPanel.GetPolicySets ()
157
return PolicyService.GetPolicySets<T> (mimeTypeScopes);
160
PolicySet IMimeTypePolicyOptionsPanel.GetMatchingSet ()
162
T pol = GetCurrentPolicy ();
163
return PolicyService.GetMatchingSet (pol, mimeTypeScopes);
166
void IMimeTypePolicyOptionsPanel.RemovePolicy (PolicyContainer bag)
168
bag.Remove<T> (mimeType);
171
T GetDirectInherited (PolicyContainer initialContainer)
173
if (initialContainer == policyContainer && !loaded && hasCachedPolicy)
174
return (T)cachedPolicy;
175
PolicyContainer pc = initialContainer;
177
if (pc.DirectHas<T> (mimeType))
178
return pc.DirectGet<T> (mimeType);
179
pc = pc.ParentPolicies;
181
return PolicyService.GetUserDefaultPolicySet ().Get<T> (mimeType);
184
void UpdateDefaultSettingsButton (PolicyContainer initialContainer)
186
if (defaultSettingsButton != null) {
187
T pol = GetDirectInherited (initialContainer);
189
panelWidget.Sensitive = true;
190
defaultSettingsButton.Active = false;
192
panelWidget.Sensitive = false;
193
defaultSettingsButton.Active = true;
198
void IMimeTypePolicyOptionsPanel.LoadCurrentPolicy ()
200
T policy = GetCurrentPolicy ();
201
UpdateDefaultSettingsButton (policyContainer);
203
hasCachedPolicy = false;
207
T GetCurrentPolicy ()
212
else if (hasCachedPolicy)
216
pol = policyContainer.Get<T> (mimeTypeScopes) ?? PolicyService.GetDefaultPolicy<T> (mimeTypeScopes);
220
string IMimeTypePolicyOptionsPanel.Label {
229
Widget IMimeTypePolicyOptionsPanel.CreateMimePanelWidget ()
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", "_");
238
VBox box = new VBox ();
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));
251
panelWidget.Sensitive = true;
255
box.PackStart (defaultSettingsButton, false, false, 0);
256
box.PackStart (new HSeparator (), false, false, 0);
258
box.PackStart (panelWidget, true, true, 0);
264
protected abstract void LoadFrom (T policy);
266
protected abstract T GetPolicy ();
268
public override void ApplyChanges ()
272
public void UpdateSelectedNamedPolicy ()
274
section.UpdateSelectedNamedPolicy ();