~ifolder-dev/simias/trunk-packaging

« back to all changes in this revision

Viewing changes to src/core/Policy/.svn/text-base/SyncInterval.cs.svn-base

  • Committer: Jorge O. Castro
  • Date: 2007-12-03 06:56:46 UTC
  • Revision ID: jorge@ubuntu.com-20071203065646-mupcnjcwgm5mnhyt
* Remove a bunch of .svn directories we no longer need.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/****************************************************************************
2
 
 |
3
 
 | Copyright (c) 2007 Novell, Inc.
4
 
 | All Rights Reserved.
5
 
 |
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.
9
 
 |
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.
14
 
 |
15
 
 | You should have received a copy of the GNU General Public License
16
 
 | along with this program; if not, contact Novell, Inc.
17
 
 |
18
 
 | To contact Novell about this file by physical or electronic mail,
19
 
 | you may find current contact information at www.novell.com 
20
 
 |
21
 
 | Author: Mike Lasky <mlasky@novell.com>
22
 
 |***************************************************************************/
23
 
 
24
 
using System;
25
 
 
26
 
using Simias;
27
 
using Simias.Storage;
28
 
 
29
 
namespace Simias.Policy
30
 
{
31
 
        /// <summary>
32
 
        /// Implements the synchronization interval policy.
33
 
        /// </summary>
34
 
        public class SyncInterval
35
 
        {
36
 
                #region Class Members
37
 
                /// <summary>
38
 
                /// Well known name for the sync interval policy.
39
 
                /// </summary>
40
 
                static public readonly string SyncIntervalPolicyID = "bee14cb3-f323-40cb-a948-44a0c3275f2f";
41
 
 
42
 
                /// <summary>
43
 
                /// Well known name for the sync interval policy description.
44
 
                /// </summary>
45
 
                static public readonly string SyncIntervalShortDescription = "Sync Interval Setting";
46
 
 
47
 
                /// <summary>
48
 
                /// Tag used to lookup and store the interval value on the policy.
49
 
                /// </summary>
50
 
                static private readonly string IntervalTag = "Interval";
51
 
 
52
 
                /// <summary>
53
 
                /// Implies to never synchronize.
54
 
                /// </summary>
55
 
                static public readonly int InfiniteSyncInterval = -1;
56
 
 
57
 
                /// <summary>
58
 
                /// Used to hold the aggregate policy.
59
 
                /// </summary>
60
 
                private Policy policy;
61
 
                #endregion
62
 
 
63
 
                #region Properties
64
 
                /// <summary>
65
 
                /// Gets the sync interval in seconds. If the policy is aggregated, the largest
66
 
                /// sync interval will be returned.
67
 
                /// </summary>
68
 
                public int Interval
69
 
                {
70
 
                        get
71
 
                        {
72
 
                                // Set to the default interval.
73
 
                                int interval = 0;
74
 
 
75
 
                                // If there is a policy find the greatest interval.
76
 
                                if ( policy != null )
77
 
                                {
78
 
                                        // Get the initial interval.
79
 
                                        object objValue = policy.GetValue( IntervalTag );
80
 
                                        if ( objValue != null )
81
 
                                        {
82
 
                                                interval = ( int )objValue;
83
 
                                                foreach ( PolicyValue pv in policy.Values )
84
 
                                                {
85
 
                                                        // Make sure that it is the right tag.
86
 
                                                        if ( pv.Name == IntervalTag )
87
 
                                                        {
88
 
                                                                int policyInterval = ( int )pv.Value;
89
 
                                                                if ( policyInterval != InfiniteSyncInterval )
90
 
                                                                {
91
 
                                                                        if ( policyInterval > interval )
92
 
                                                                        {
93
 
                                                                                interval = policyInterval;
94
 
                                                                        }
95
 
                                                                }
96
 
                                                                else
97
 
                                                                {
98
 
                                                                        interval = policyInterval;
99
 
                                                                        break;
100
 
                                                                }
101
 
                                                        }
102
 
                                                }
103
 
                                        }
104
 
                                }
105
 
 
106
 
                                return interval;
107
 
                        }
108
 
                }
109
 
                #endregion
110
 
 
111
 
                #region Constructor
112
 
                /// <summary>
113
 
                /// Initializes a new instance of the object.
114
 
                /// </summary>
115
 
                /// <param name="policy">The aggregate policy. This may be null if no policy exists.</param>
116
 
                private SyncInterval( Policy policy )
117
 
                {
118
 
                        this.policy = policy;
119
 
                }
120
 
                #endregion
121
 
 
122
 
                #region Public Methods
123
 
                /// <summary>
124
 
                /// Creates a system wide sync interval policy.
125
 
                /// </summary>
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 )
129
 
                {
130
 
                        // Need a policy manager.
131
 
                        PolicyManager pm = new PolicyManager();
132
 
                        
133
 
                        // See if the policy already exists.
134
 
                        Policy policy = pm.GetPolicy( SyncIntervalPolicyID, domainID );
135
 
                        if ( ( interval == InfiniteSyncInterval ) || ( interval > 0 ) )
136
 
                        {
137
 
                                if ( policy == null )
138
 
                                {
139
 
                                        // The policy does not exist, create a new one and add the rules.
140
 
                                        policy = new Policy( SyncIntervalPolicyID, SyncIntervalShortDescription );
141
 
                                }
142
 
 
143
 
                                // Add the new value and save the policy.
144
 
                                policy.AddValue( IntervalTag, interval );
145
 
                                pm.CommitPolicy( policy, domainID );
146
 
                        }
147
 
                        else if ( policy != null )
148
 
                        {
149
 
                                // Setting the interval to zero is the same as deleting the policy.
150
 
                                pm.DeletePolicy( policy );
151
 
                        }
152
 
                }
153
 
 
154
 
                /// <summary>
155
 
                /// Creates a sync interval policy for the specified member.
156
 
                /// </summary>
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 )
160
 
                {
161
 
                        // Need a policy manager.
162
 
                        PolicyManager pm = new PolicyManager();
163
 
                        
164
 
                        // See if the policy already exists.
165
 
                        Policy policy = pm.GetPolicy( SyncIntervalPolicyID, member );
166
 
                        if ( ( interval == InfiniteSyncInterval ) || ( interval > 0 ) )
167
 
                        {
168
 
                                if ( policy == null )
169
 
                                {
170
 
                                        // The policy does not exist, create a new one and add the rules.
171
 
                                        policy = new Policy( SyncIntervalPolicyID, SyncIntervalShortDescription );
172
 
                                }
173
 
 
174
 
                                // Add the new value and save the policy.
175
 
                                policy.AddValue( IntervalTag, interval );
176
 
                                pm.CommitPolicy( policy, member );
177
 
                        }
178
 
                        else if ( policy != null )
179
 
                        {
180
 
                                // Setting the interval to zero is the same as deleting the policy.
181
 
                                pm.DeletePolicy( policy );
182
 
                        }
183
 
                }
184
 
 
185
 
                /// <summary>
186
 
                /// Creates a sync interval policy for the specified collection.
187
 
                /// </summary>
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 )
191
 
                {
192
 
                        // Need a policy manager.
193
 
                        PolicyManager pm = new PolicyManager();
194
 
                        
195
 
                        // See if the policy already exists.
196
 
                        Policy policy = pm.GetPolicy( SyncIntervalPolicyID, collection );
197
 
                        if ( ( interval == InfiniteSyncInterval ) || ( interval > 0 ) )
198
 
                        {
199
 
                                if ( policy == null )
200
 
                                {
201
 
                                        // The policy does not exist, create a new one and add the rules.
202
 
                                        policy = new Policy( SyncIntervalPolicyID, SyncIntervalShortDescription );
203
 
                                }
204
 
 
205
 
                                // Add the new value and save the policy.
206
 
                                policy.AddValue( IntervalTag, interval );
207
 
                                pm.CommitPolicy( policy, collection );
208
 
                        }
209
 
                        else if ( policy != null )
210
 
                        {
211
 
                                // Setting the interval to zero is the same as deleting the policy.
212
 
                                pm.DeletePolicy( policy );
213
 
                        }
214
 
                }
215
 
 
216
 
                /// <summary>
217
 
                /// Creates a sync interval policy for the current user on the current machine.
218
 
                /// </summary>
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 )
221
 
                {
222
 
                        // Need a policy manager.
223
 
                        PolicyManager pm = new PolicyManager();
224
 
                        
225
 
                        // See if the policy already exists.
226
 
                        Policy policy = pm.GetPolicy( SyncIntervalPolicyID );
227
 
                        if ( ( interval == InfiniteSyncInterval ) || ( interval > 0 ) )
228
 
                        {
229
 
                                if ( policy == null )
230
 
                                {
231
 
                                        // The policy does not exist, create a new one and add the rules.
232
 
                                        policy = new Policy( SyncIntervalPolicyID, SyncIntervalShortDescription );
233
 
                                }
234
 
 
235
 
                                // Add the new value and save the policy.
236
 
                                policy.AddValue( IntervalTag, interval );
237
 
                                pm.CommitLocalMachinePolicy( policy );
238
 
                        }
239
 
                        else if ( policy != null )
240
 
                        {
241
 
                                // Setting the interval to zero is the same as deleting the policy.
242
 
                                pm.DeletePolicy( policy );
243
 
                        }
244
 
                }
245
 
 
246
 
                /// <summary>
247
 
                /// Deletes a system wide sync interval policy.
248
 
                /// </summary>
249
 
                /// <param name="domainID">Domain that the interval will be associated with.</param>
250
 
                static public void Delete( string domainID )
251
 
                {
252
 
                        // Need a policy manager.
253
 
                        PolicyManager pm = new PolicyManager();
254
 
                        
255
 
                        // See if the policy already exists.
256
 
                        Policy policy = pm.GetPolicy( SyncIntervalPolicyID, domainID );
257
 
                        if ( policy != null )
258
 
                        {
259
 
                                // Delete the policy.
260
 
                                pm.DeletePolicy( policy );
261
 
                        }
262
 
                }
263
 
 
264
 
                /// <summary>
265
 
                /// Deletes a sync interval policy for the specified member.
266
 
                /// </summary>
267
 
                /// <param name="member">Member that the filter will be associated with.</param>
268
 
                static public void Delete( Member member )
269
 
                {
270
 
                        // Need a policy manager.
271
 
                        PolicyManager pm = new PolicyManager();
272
 
                        
273
 
                        // See if the policy already exists.
274
 
                        Policy policy = pm.GetPolicy( SyncIntervalPolicyID, member );
275
 
                        if ( policy != null )
276
 
                        {
277
 
                                // Delete the policy.
278
 
                                pm.DeletePolicy( policy );
279
 
                        }
280
 
                }
281
 
 
282
 
                /// <summary>
283
 
                /// Deletes a sync interval policy for the specified collection.
284
 
                /// </summary>
285
 
                /// <param name="collection">Collection that the filter will be associated with.</param>
286
 
                static public void Delete( Collection collection )
287
 
                {
288
 
                        // Need a policy manager.
289
 
                        PolicyManager pm = new PolicyManager();
290
 
                        
291
 
                        // See if the policy already exists.
292
 
                        Policy policy = pm.GetPolicy( SyncIntervalPolicyID, collection );
293
 
                        if ( policy != null )
294
 
                        {
295
 
                                // Delete the policy.
296
 
                                pm.DeletePolicy( policy );
297
 
                        }
298
 
                }
299
 
 
300
 
                /// <summary>
301
 
                /// Deletes a sync interval policy for the current user on the current machine.
302
 
                /// </summary>
303
 
                static public void Delete()
304
 
                {
305
 
                        // Need a policy manager.
306
 
                        PolicyManager pm = new PolicyManager();
307
 
                        
308
 
                        // See if the policy already exists.
309
 
                        Policy policy = pm.GetPolicy( SyncIntervalPolicyID );
310
 
                        if ( policy != null )
311
 
                        {
312
 
                                // Delete the policy.
313
 
                                pm.DeletePolicy( policy );
314
 
                        }
315
 
                }
316
 
 
317
 
                /// <summary>
318
 
                /// Gets the aggregate sync interval for the specified member.
319
 
                /// </summary>
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 )
323
 
                {
324
 
                        // Get the aggregate policy.
325
 
                        PolicyManager pm = new PolicyManager();
326
 
                        Policy policy = pm.GetAggregatePolicy( SyncIntervalPolicyID, member );
327
 
                        return new SyncInterval( policy );
328
 
                }
329
 
 
330
 
                /// <summary>
331
 
                /// Gets the aggregate sync interval for the specified member and collection.
332
 
                /// </summary>
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 )
338
 
                {
339
 
                        // Get the aggregate policy.
340
 
                        return SyncInterval.Get( collection );
341
 
                }
342
 
 
343
 
                /// <summary>
344
 
                /// Gets the aggregate sync interval for the specified member and collection.
345
 
                /// </summary>
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 )
349
 
                {
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 );
355
 
                }
356
 
 
357
 
                /// <summary>
358
 
                /// Gets the interval associated with the specified domain.
359
 
                /// </summary>
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 )
363
 
                {
364
 
                        PolicyManager pm = new PolicyManager();
365
 
                        Policy policy = pm.GetPolicy( SyncIntervalPolicyID, domainID );
366
 
                        return ( policy != null ) ? ( int )policy.GetValue( IntervalTag ) : 0;
367
 
                }
368
 
 
369
 
                /// <summary>
370
 
                /// Gets the sync interval associated with the specified member.
371
 
                /// </summary>
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 )
375
 
                {
376
 
                        PolicyManager pm = new PolicyManager();
377
 
                        Policy policy = pm.GetPolicy( SyncIntervalPolicyID, member );
378
 
                        return ( policy != null ) ? ( int )policy.GetValue( IntervalTag ) : 0;
379
 
                }
380
 
 
381
 
                /// <summary>
382
 
                /// Gets the sync interval associated with the specified collection.
383
 
                /// </summary>
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 )
387
 
                {
388
 
                        PolicyManager pm = new PolicyManager();
389
 
                        Policy policy = pm.GetPolicy( SyncIntervalPolicyID, collection );
390
 
                        return ( policy != null ) ? ( int )policy.GetValue( IntervalTag ) : 0;
391
 
                }
392
 
 
393
 
                /// <summary>
394
 
                /// Gets the sync interval associated with the current user on the current machine.
395
 
                /// </summary>
396
 
                /// <returns>The sync interval that the current user is limited to.</returns>
397
 
                static public int GetInterval()
398
 
                {
399
 
                        PolicyManager pm = new PolicyManager();
400
 
                        Policy policy = pm.GetPolicy( SyncIntervalPolicyID );
401
 
                        return ( policy != null ) ? ( int )policy.GetValue( IntervalTag ) : 0;
402
 
                }
403
 
 
404
 
                /// <summary>
405
 
                /// Sets the sync interval associated with the specified domain.
406
 
                /// </summary>
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 )
410
 
                {
411
 
                        Create( domainID, interval );
412
 
                }
413
 
 
414
 
                /// <summary>
415
 
                /// Sets the sync interval associated with the specified member.
416
 
                /// </summary>
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 )
420
 
                {
421
 
                        Create( member, interval );
422
 
                }
423
 
 
424
 
                /// <summary>
425
 
                /// Sets the sync interval associated with the specified collection.
426
 
                /// </summary>
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 )
430
 
                {
431
 
                        Create( collection, interval );
432
 
                }
433
 
 
434
 
                /// <summary>
435
 
                /// Sets the sync interval associated with the current user on the current machine.
436
 
                /// </summary>
437
 
                /// <param name="interval">Sync interval in seconds that the current user will be set to.</param>
438
 
                static public void Set( int interval )
439
 
                {
440
 
                        Create( interval );
441
 
                }
442
 
                #endregion
443
 
        }
444
 
}