StropheCappuccino API 1.0.0
/tmp/tempDoc.doc/TNStropheGroup.j
Go to the documentation of this file.
00001 /*
00002  * TNStropheGroup.j
00003  *
00004  * Copyright (C) 2010  Antoine Mercadal <antoine.mercadal@inframonde.eu>
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 3.0 of the License, or (at your option) any later version.
00010  *
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with this library; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00019  */
00020 
00021 
00022 @global TNStropheRosterRosterDelimiter
00023 
00029 TNStropheGroupRemovedNotification = @"TNStropheGroupRemoved";
00030 
00031 
00035 @implementation TNStropheGroup : CPObject
00036 {
00037     CPArray         _subGroups;
00038     CPArray         _contacts;
00039     CPString        _name;
00040     TNStropheGroup  _parentGroup;
00041 }
00042 
00043 #pragma mark -
00044 #pragma mark Initialization
00045 
00050 + (TNStropheGroup)stropheGroupWithName:(CPString)aName
00051 {
00052     return [[TNStropheGroup alloc] initWithName:aName];
00053 }
00054 
00059 - (TNStropheGroup)initWithName:(CPString)aName
00060 {
00061     if (self = [super init])
00062     {
00063         _contacts       = [CPArray array];
00064         _subGroups      = [CPArray array];
00065         _name           = aName;
00066         _parentGroup    = nil;
00067     }
00068 
00069     return self;
00070 }
00071 
00072 
00073 #pragma mark -
00074 #pragma mark Overides
00075 
00078 - (CPString)description
00079 {
00080     return _name;
00081 }
00082 
00083 
00084 #pragma mark -
00085 #pragma mark Contacts
00086 
00091 - (TNStropheContact)contactWithJID:(TNStropheJID)aJID matchBare:(BOOL)matchBare
00092 {
00093     for (var i = 0; i < [_contacts count]; i++)
00094     {
00095         if (matchBare)
00096         {
00097             if ([[[_contacts objectAtIndex:i] JID] bareEquals:aJID])
00098                 return [_contacts objectAtIndex:i];
00099         }
00100         else
00101         {
00102             if ([[[_contacts objectAtIndex:i] JID] fullEquals:aJID])
00103                 return [_contacts objectAtIndex:i];
00104         }
00105     }
00106 
00107     return nil;
00108 }
00109 
00110 
00111 #pragma mark -
00112 #pragma mark Subgroups
00113 
00117 - (void)addSubGroup:(TNStropheGroup)aGroup
00118 {
00119     if (![aGroup isKindOfClass:TNStropheGroup])
00120         [CPException raise:"Invalid Object" reason:"addSubGroup only supports to add TNStropheGroups"];
00121 
00122     [aGroup setParentGroup:self];
00123     [_subGroups addObject:aGroup];
00124 }
00125 
00129 - (void)removeSubGroup:(TNStropheGroup)aGroup
00130 {
00131     if (![_subGroups containsObject:aGroup])
00132         return;
00133 
00134     [aGroup setParentGroup:nil];
00135     [_subGroups removeObject:aGroup];
00136 }
00137 
00140 - (void)removeSubGroups
00141 {
00142     for (var i = 0; i < [self subGroupsCount]; i++)
00143     {
00144         var subGroup = [_subGroups objectAtIndex:i];
00145         [self removeSubGroup:subGroup];
00146     }
00147 
00148     [_subGroups removeAllObjects];
00149 }
00150 
00154 - (TNStropheGroup)subGroupWithName:(CPString)aName
00155 {
00156     for (var i = 0; i < [self subGroupsCount]; i++)
00157         if ([[[_subGroups objectAtIndex:i] name] uppercaseString] == [aName uppercaseString])
00158             return [_subGroups objectAtIndex:i];
00159     return nil;
00160 }
00161 
00166 - (CPString)path
00167 {
00168     var path = [[self name]],
00169         currentGroup = self;
00170 
00171     while (currentGroup)
00172     {
00173         currentGroup = [currentGroup parentGroup];
00174         if (currentGroup)
00175             [path addObject:[[currentGroup name] uppercaseString]];
00176     }
00177     return path.reverse().join(TNStropheRosterRosterDelimiter);
00178 }
00179 
00180 
00181 #pragma mark -
00182 #pragma mark Counting
00183 
00186 - (void)flush
00187 {
00188     [self removeSubGroups];
00189     [_contacts removeAllObjects];
00190 }
00191 
00195 - (int)subGroupsCount
00196 {
00197     return [_subGroups count];
00198 }
00199 
00203 - (int)contactCount
00204 {
00205     return [_contacts count];
00206 }
00207 
00211 - (int)count
00212 {
00213     return [self subGroupsCount] + [self contactCount];
00214 }
00215 
00216 
00217 - (CPArray)content
00218 {
00219     return [_subGroups.sort() arrayByAddingObjectsFromArray:_contacts.sort()];
00220 }
00221 
00222 
00223 @end
00224 
00225 @implementation TNStropheGroup (CPCoding)
00226 
00227 - (id)initWithCoder:(CPCoder)aCoder
00228 {
00229     if (self = [super init])
00230     {
00231         _contacts       = [aCoder decodeObjectForKey:@"_contacts"];
00232         _name           = [aCoder decodeObjectForKey:@"_name"];
00233         _parentGroup    = [aCoder decodeObjectForKey:@"_parentGroup"];
00234         _subGroups      = [aCoder decodeObjectForKey:@"_subGroups"];
00235     }
00236     return self;
00237 }
00238 
00239 - (void)encodeWithCoder:(CPCoder)aCoder
00240 {
00241     [aCoder encodeObject:_contacts forKey:@"_contacts"];
00242     [aCoder encodeObject:_name forKey:@"_name"];
00243     [aCoder encodeObject:_parentGroup forKey:@"_parentGroup"];
00244     [aCoder encodeObject:_subGroups forKey:@"_subGroups"];
00245 }
00246 
00247 @end
00248 
00249 @implementation TNStropheGroup (CPSynthesizedAccessors)
00250 
00254 - (CPArray)subGroups
00255 {
00256     return _subGroups;
00257 }
00258 
00262 - (CPArray)contacts
00263 {
00264     return _contacts;
00265 }
00266 
00270 - (void)setContacts:(CPArray)aValue
00271 {
00272     _contacts = aValue;
00273 }
00274 
00278 - (CPString)name
00279 {
00280     return _name;
00281 }
00282 
00286 - (void)setName:(CPString)aValue
00287 {
00288     _name = aValue;
00289 }
00290 
00294 - (TNStropheGroup)parentGroup
00295 {
00296     return _parentGroup;
00297 }
00298 
00302 - (void)setParentGroup:(TNStropheGroup)aValue
00303 {
00304     _parentGroup = aValue;
00305 }
00306 
00307 @end
 All Classes Namespaces Files Functions Variables