1
/****************************************************************************
3
| Copyright (c) 2007 Novell, Inc.
6
| This program is free software; you can redistribute it and/or
7
| modify it under the terms of version 2 of the GNU General Public License as
8
| published by the Free Software Foundation.
10
| This program is distributed in the hope that it will be useful,
11
| but WITHOUT ANY WARRANTY; without even the implied warranty of
12
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
| GNU General Public License for more details.
15
| You should have received a copy of the GNU General Public License
16
| along with this program; if not, contact Novell, Inc.
18
| To contact Novell about this file by physical or electronic mail,
19
| you may find current contact information at www.novell.com
21
| Author: Mike Lasky <mlasky@novell.com>
22
|***************************************************************************/
29
namespace Simias.Policy
32
/// Implements the synchronization interval policy.
34
public class SyncInterval
38
/// Well known name for the sync interval policy.
40
static public readonly string SyncIntervalPolicyID = "bee14cb3-f323-40cb-a948-44a0c3275f2f";
43
/// Well known name for the sync interval policy description.
45
static public readonly string SyncIntervalShortDescription = "Sync Interval Setting";
48
/// Tag used to lookup and store the interval value on the policy.
50
static private readonly string IntervalTag = "Interval";
53
/// Implies to never synchronize.
55
static public readonly int InfiniteSyncInterval = -1;
58
/// Used to hold the aggregate policy.
60
private Policy policy;
65
/// Gets the sync interval in seconds. If the policy is aggregated, the largest
66
/// sync interval will be returned.
72
// Set to the default interval.
75
// If there is a policy find the greatest interval.
78
// Get the initial interval.
79
object objValue = policy.GetValue( IntervalTag );
80
if ( objValue != null )
82
interval = ( int )objValue;
83
foreach ( PolicyValue pv in policy.Values )
85
// Make sure that it is the right tag.
86
if ( pv.Name == IntervalTag )
88
int policyInterval = ( int )pv.Value;
89
if ( policyInterval != InfiniteSyncInterval )
91
if ( policyInterval > interval )
93
interval = policyInterval;
98
interval = policyInterval;
113
/// Initializes a new instance of the object.
115
/// <param name="policy">The aggregate policy. This may be null if no policy exists.</param>
116
private SyncInterval( Policy policy )
118
this.policy = policy;
122
#region Public Methods
124
/// Creates a system wide sync interval policy.
126
/// <param name="domainID">Domain that the interval will be associated with.</param>
127
/// <param name="interval">Sync interval in seconds that all users in the domain will be set to.</param>
128
static public void Create( string domainID, int interval )
130
// Need a policy manager.
131
PolicyManager pm = new PolicyManager();
133
// See if the policy already exists.
134
Policy policy = pm.GetPolicy( SyncIntervalPolicyID, domainID );
135
if ( ( interval == InfiniteSyncInterval ) || ( interval > 0 ) )
137
if ( policy == null )
139
// The policy does not exist, create a new one and add the rules.
140
policy = new Policy( SyncIntervalPolicyID, SyncIntervalShortDescription );
143
// Add the new value and save the policy.
144
policy.AddValue( IntervalTag, interval );
145
pm.CommitPolicy( policy, domainID );
147
else if ( policy != null )
149
// Setting the interval to zero is the same as deleting the policy.
150
pm.DeletePolicy( policy );
155
/// Creates a sync interval policy for the specified member.
157
/// <param name="member">Member that the filter will be associated with.</param>
158
/// <param name="interval">Sync interval in seconds that all users in the domain will be set to.</param>
159
static public void Create( Member member, int interval )
161
// Need a policy manager.
162
PolicyManager pm = new PolicyManager();
164
// See if the policy already exists.
165
Policy policy = pm.GetPolicy( SyncIntervalPolicyID, member );
166
if ( ( interval == InfiniteSyncInterval ) || ( interval > 0 ) )
168
if ( policy == null )
170
// The policy does not exist, create a new one and add the rules.
171
policy = new Policy( SyncIntervalPolicyID, SyncIntervalShortDescription );
174
// Add the new value and save the policy.
175
policy.AddValue( IntervalTag, interval );
176
pm.CommitPolicy( policy, member );
178
else if ( policy != null )
180
// Setting the interval to zero is the same as deleting the policy.
181
pm.DeletePolicy( policy );
186
/// Creates a sync interval policy for the specified collection.
188
/// <param name="collection">Collection that the filter will be associated with.</param>
189
/// <param name="interval">Sync interval in seconds that all users in the domain will be set to.</param>
190
static public void Create( Collection collection, int interval )
192
// Need a policy manager.
193
PolicyManager pm = new PolicyManager();
195
// See if the policy already exists.
196
Policy policy = pm.GetPolicy( SyncIntervalPolicyID, collection );
197
if ( ( interval == InfiniteSyncInterval ) || ( interval > 0 ) )
199
if ( policy == null )
201
// The policy does not exist, create a new one and add the rules.
202
policy = new Policy( SyncIntervalPolicyID, SyncIntervalShortDescription );
205
// Add the new value and save the policy.
206
policy.AddValue( IntervalTag, interval );
207
pm.CommitPolicy( policy, collection );
209
else if ( policy != null )
211
// Setting the interval to zero is the same as deleting the policy.
212
pm.DeletePolicy( policy );
217
/// Creates a sync interval policy for the current user on the current machine.
219
/// <param name="interval">Sync interval in seconds that all users in the domain will be set to.</param>
220
static public void Create( int interval )
222
// Need a policy manager.
223
PolicyManager pm = new PolicyManager();
225
// See if the policy already exists.
226
Policy policy = pm.GetPolicy( SyncIntervalPolicyID );
227
if ( ( interval == InfiniteSyncInterval ) || ( interval > 0 ) )
229
if ( policy == null )
231
// The policy does not exist, create a new one and add the rules.
232
policy = new Policy( SyncIntervalPolicyID, SyncIntervalShortDescription );
235
// Add the new value and save the policy.
236
policy.AddValue( IntervalTag, interval );
237
pm.CommitLocalMachinePolicy( policy );
239
else if ( policy != null )
241
// Setting the interval to zero is the same as deleting the policy.
242
pm.DeletePolicy( policy );
247
/// Deletes a system wide sync interval policy.
249
/// <param name="domainID">Domain that the interval will be associated with.</param>
250
static public void Delete( string domainID )
252
// Need a policy manager.
253
PolicyManager pm = new PolicyManager();
255
// See if the policy already exists.
256
Policy policy = pm.GetPolicy( SyncIntervalPolicyID, domainID );
257
if ( policy != null )
259
// Delete the policy.
260
pm.DeletePolicy( policy );
265
/// Deletes a sync interval policy for the specified member.
267
/// <param name="member">Member that the filter will be associated with.</param>
268
static public void Delete( Member member )
270
// Need a policy manager.
271
PolicyManager pm = new PolicyManager();
273
// See if the policy already exists.
274
Policy policy = pm.GetPolicy( SyncIntervalPolicyID, member );
275
if ( policy != null )
277
// Delete the policy.
278
pm.DeletePolicy( policy );
283
/// Deletes a sync interval policy for the specified collection.
285
/// <param name="collection">Collection that the filter will be associated with.</param>
286
static public void Delete( Collection collection )
288
// Need a policy manager.
289
PolicyManager pm = new PolicyManager();
291
// See if the policy already exists.
292
Policy policy = pm.GetPolicy( SyncIntervalPolicyID, collection );
293
if ( policy != null )
295
// Delete the policy.
296
pm.DeletePolicy( policy );
301
/// Deletes a sync interval policy for the current user on the current machine.
303
static public void Delete()
305
// Need a policy manager.
306
PolicyManager pm = new PolicyManager();
308
// See if the policy already exists.
309
Policy policy = pm.GetPolicy( SyncIntervalPolicyID );
310
if ( policy != null )
312
// Delete the policy.
313
pm.DeletePolicy( policy );
318
/// Gets the aggregate sync interval for the specified member.
320
/// <param name="member">Member that policy is associated with.</param>
321
/// <returns>A SyncInterval object that contains the policy for the specified member.</returns>
322
static public SyncInterval Get( Member member )
324
// Get the aggregate policy.
325
PolicyManager pm = new PolicyManager();
326
Policy policy = pm.GetAggregatePolicy( SyncIntervalPolicyID, member );
327
return new SyncInterval( policy );
331
/// Gets the aggregate sync interval for the specified member and collection.
333
/// <param name="member">Member that policy is associated with.</param>
334
/// <param name="collection">Collection to add to the aggregate policy.</param>
335
/// <returns>A SyncInterval object that contains the policy for the specified member.</returns>
336
[ Obsolete( "This method is obsolete. Please use SyncInterval.Get( Collection collection ) instead.", false ) ]
337
static public SyncInterval Get( Member member, Collection collection )
339
// Get the aggregate policy.
340
return SyncInterval.Get( collection );
344
/// Gets the aggregate sync interval for the specified member and collection.
346
/// <param name="collection">Collection to add to the aggregate policy.</param>
347
/// <returns>A SyncInterval object that contains the policy for the specified member.</returns>
348
static public SyncInterval Get( Collection collection )
350
// Get the aggregate policy.
351
PolicyManager pm = new PolicyManager();
352
Member member = collection.Owner;
353
Policy policy = pm.GetAggregatePolicy( SyncIntervalPolicyID, member, collection );
354
return new SyncInterval( policy );
358
/// Gets the interval associated with the specified domain.
360
/// <param name="domainID">Domain that the interval is associated with.</param>
361
/// <returns>The sync interval that all users in the domain are limited to.</returns>
362
static public int GetInterval( string domainID )
364
PolicyManager pm = new PolicyManager();
365
Policy policy = pm.GetPolicy( SyncIntervalPolicyID, domainID );
366
return ( policy != null ) ? ( int )policy.GetValue( IntervalTag ) : 0;
370
/// Gets the sync interval associated with the specified member.
372
/// <param name="member">Member that the interval is associated with.</param>
373
/// <returns>The sync interval that the member is limited to.</returns>
374
static public int GetInterval( Member member )
376
PolicyManager pm = new PolicyManager();
377
Policy policy = pm.GetPolicy( SyncIntervalPolicyID, member );
378
return ( policy != null ) ? ( int )policy.GetValue( IntervalTag ) : 0;
382
/// Gets the sync interval associated with the specified collection.
384
/// <param name="collection">Collection that the interval is associated with.</param>
385
/// <returns>The sync interval that all users in the collection are limited to.</returns>
386
static public int GetInterval( Collection collection )
388
PolicyManager pm = new PolicyManager();
389
Policy policy = pm.GetPolicy( SyncIntervalPolicyID, collection );
390
return ( policy != null ) ? ( int )policy.GetValue( IntervalTag ) : 0;
394
/// Gets the sync interval associated with the current user on the current machine.
396
/// <returns>The sync interval that the current user is limited to.</returns>
397
static public int GetInterval()
399
PolicyManager pm = new PolicyManager();
400
Policy policy = pm.GetPolicy( SyncIntervalPolicyID );
401
return ( policy != null ) ? ( int )policy.GetValue( IntervalTag ) : 0;
405
/// Sets the sync interval associated with the specified domain.
407
/// <param name="domainID">Domain that the interval is associated with.</param>
408
/// <param name="interval">Sync interval in seconds that all users in the domain will be set to.</param>
409
static public void Set( string domainID, int interval )
411
Create( domainID, interval );
415
/// Sets the sync interval associated with the specified member.
417
/// <param name="member">Member that the filter is associated with.</param>
418
/// <param name="interval">Sync interval in seconds that the associated member will be set to.</param>
419
static public void Set( Member member, int interval )
421
Create( member, interval );
425
/// Sets the sync interval associated with the specified collection.
427
/// <param name="collection">Collection that the policy is associated with.</param>
428
/// <param name="interval">Sync interval in seconds that the collection will be set to.</param>
429
static public void Set( Collection collection, int interval )
431
Create( collection, interval );
435
/// Sets the sync interval associated with the current user on the current machine.
437
/// <param name="interval">Sync interval in seconds that the current user will be set to.</param>
438
static public void Set( int interval )