StropheCappuccino API 1.0.0
/tmp/tempDoc.doc/MUC/TNStropheMUCRoster.j
Go to the documentation of this file.
00001 /*
00002  * TNStropheMUCRoster.j
00003  *
00004  * Copyright (C) 2010 Ben Langfeld <ben@langfeld.me>
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 @class TNStropheMUCRoom
00023 
00024 TNStropheMUCContactJoinedNotification   = @"TNStropheMUCContactJoinedNotification";
00025 TNStropheMUCContactLeftNotification     = @"TNStropheMUCContactLeftNotification";
00026 
00027 
00031 @implementation TNStropheMUCRoster : TNStropheRosterBase
00032 {
00033     TNStropheGroup          _admins;
00034     TNStropheGroup          _moderators;
00035     TNStropheGroup          _owners;
00036     TNStropheGroup          _participants;
00037     TNStropheGroup          _visitors;
00038     TNStropheMUCRoom        _room;
00039 }
00040 
00041 #pragma mark -
00042 #pragma mark Class methods
00043 
00044 + (id)rosterWithConnection:(TNStropheConnection)aConnection forRoom:(TNStropheMUCRoom)aRoom
00045 {
00046     return [[TNStropheMUCRoster alloc] initWithConnection:aConnection forRoom:aRoom];
00047 }
00048 
00049 #pragma mark -
00050 #pragma mark Initialization
00051 
00056 - (id)initWithConnection:(TNStropheConnection)aConnection forRoom:(TNStropheMUCRoom)aRoom
00057 {
00058     if (self = [super initWithConnection:aConnection])
00059     {
00060         _room           = aRoom;
00061 
00062         _visitors       = [TNStropheGroup stropheGroupWithName:@"Visitors"];
00063         _participants   = [TNStropheGroup stropheGroupWithName:@"Participants"];
00064         _moderators     = [TNStropheGroup stropheGroupWithName:@"Moderators"];
00065         _admins         = [TNStropheGroup stropheGroupWithName:@"Admins"];
00066         _owners         = [TNStropheGroup stropheGroupWithName:@"Owners"];
00067 
00068         var params      = [CPDictionary dictionaryWithObjectsAndKeys:@"presence", @"name",
00069                                                                      [[_room roomJID] full], @"from",
00070                                                                      {matchBare: true}, @"options"];
00071         [_connection registerSelector:@selector(_didReceivePresence:) ofObject:self withDict:params];
00072     }
00073 
00074     return self;
00075 }
00076 
00077 
00078 #pragma mark -
00079 #pragma mark Presence
00080 
00084 - (BOOL)_didReceivePresence:(id)aStanza
00085 {
00086     var contact = [self contactWithFullJID:[aStanza from]],
00087         data    = [aStanza firstChildWithName:@"x"],
00088         group;
00089 
00090     if (data && [data namespace] == @"http://jabber.org/protocol/muc#user")
00091     {
00092         switch ([[data firstChildWithName:@"item"] valueForAttribute:@"role"])
00093         {
00094             case @"visitor":
00095                 group = _visitors;
00096                 break;
00097             case @"participant":
00098                 group = _participants;
00099                 break;
00100             case @"moderator":
00101                 group = _moderators;
00102                 break;
00103         }
00104     }
00105 
00106     if (contact)
00107         [contact _didReceivePresence:aStanza];
00108     else
00109         contact = [self addContact:[aStanza from] withName:[[aStanza from] resource] inGroup:group];
00110 
00111     if ([aStanza type] === @"unavailable")
00112     {
00113         var statusCode;
00114         if ([data containsChildrenWithName:@"status"])
00115             statusCode = [[data firstChildWithName:@"status"] valueForAttribute:@"code"];
00116 
00117         [self removeContact:contact withStatusCode:statusCode];
00118     }
00119 
00120     return YES;
00121 }
00122 
00123 
00124 #pragma mark -
00125 #pragma mark Contacts
00126 
00133 - (TNStropheContact)addContact:(TNStropheJID)aJID withName:(CPString)aName inGroup:(TNStropheGroup)aGroup
00134 {
00135     if ([self containsFullJID:aJID])
00136         return;
00137 
00138     if (!aGroup)
00139         aGroup = _visitors;
00140 
00141     var contact = [TNStropheContact contactWithConnection:_connection JID:aJID group:aGroup];
00142     [contact setNickname:aName];
00143 
00144     [[aGroup contacts] addObject:contact];
00145     [self cacheContact:contact];
00146 
00147     var userInfo = [CPDictionary dictionaryWithObject:contact forKey:@"contact"];
00148 
00149     [[CPNotificationCenter defaultCenter] postNotificationName:TNStropheMUCContactJoinedNotification object:self userInfo:userInfo];
00150 
00151     return contact;
00152 }
00153 
00158 - (void)removeContact:(TNStropheContact)aContact withStatusCode:(CPString)aStatusCode
00159 {
00160     [super uncacheContact:aContact];
00161 
00162     var userInfo = [CPDictionary dictionaryWithObjectsAndKeys:aStatusCode, @"statusCode",
00163                                                              aContact, @"contact"];
00164 
00165     [[CPNotificationCenter defaultCenter] postNotificationName:TNStropheMUCContactLeftNotification object:self userInfo:userInfo];
00166 }
00167 
00172 - (TNStropheGroup)groupOfContact:(TNStropheContact)aContact
00173 {
00174     var groups = [CPArray arrayWithObjects:_visitors, _participants, _moderators, _admins, _owners];
00175     for (var i = 0; i < [groups count]; i++)
00176     {
00177         var group = [groups objectAtIndex:i];
00178         if ([[group contacts] containsObject:aContact])
00179             return group;
00180     }
00181 
00182     return;
00183 }
00184 
00185 @end
00186 
00187 @implementation TNStropheMUCRoster (CPSynthesizedAccessors)
00188 
00192 - (TNStropheGroup)admins
00193 {
00194     return _admins;
00195 }
00196 
00200 - (TNStropheGroup)moderators
00201 {
00202     return _moderators;
00203 }
00204 
00208 - (TNStropheGroup)owners
00209 {
00210     return _owners;
00211 }
00212 
00216 - (TNStropheGroup)participants
00217 {
00218     return _participants;
00219 }
00220 
00224 - (TNStropheGroup)visitors
00225 {
00226     return _visitors;
00227 }
00228 
00232 - (TNStropheMUCRoom)room
00233 {
00234     return _room;
00235 }
00236 
00237 @end
 All Classes Namespaces Files Functions Variables