Changeset 3611

Show
Ignore:
Timestamp:
03/10/07 23:36:22 (2 years ago)
Author:
akempgen
Message:

Avatar Support: support for sending and receiving avatar urls complete, code cleanup

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/Plug-Ins/Avatar Support/AKAvatarSupport.h

    r3600 r3611  
    1818} 
    1919 
     20- (void) requestAvatarFromUser:(MVChatUser *)chatUser; 
     21- (void) offerAvatarToUser:(MVChatUser *)chatUser; 
     22 
    2023- (void) saveAvatar:(NSImage *)anImage forUser:(MVChatUser *)chatUser; 
    2124- (void) addAvatarToUser:(MVChatUser *)chatUser; 
    2225- (NSImage *) avatarForUser:(MVChatUser *)chatUser; 
    2326 
    24 //TODO: remove this when testing is done 
    25 - (void)alertDidEnd:(NSAlert *)alert returnCode:(int)returnCode contextInfo:(void *)contextInfo; 
    26  
    2727@end 
  • trunk/Plug-Ins/Avatar Support/AKAvatarSupport.m

    r3600 r3611  
    1717#import "MVChatConnection.h" 
    1818 
     19//For Growl output 
     20#import "/Users/Alex/dev/svn/colloquy/Controllers/JVNotificationController.h" 
     21 
    1922//some classes 
    2023#import "JVChatMessage.h" 
     
    3942{ 
    4043        self = [super init]; 
    41         //NSLog(@"Avatar Support Plugin loaded"); 
     44        //NSLog(@"Avatar Support: ** Plugin loaded"); 
     45        return self; 
     46
     47 
     48- (void) dealloc 
     49
     50        [super dealloc]; 
     51
     52 
     53#pragma mark - 
     54#pragma mark MVChatPluginReloadSupport 
     55 
     56- (void) load 
     57
     58        if ([[NSFileManager defaultManager] fileExistsAtPath: [cacheDir stringByExpandingTildeInPath]] == NO) 
     59        { 
     60                [[NSFileManager defaultManager] createDirectoryAtPath: [cacheDir stringByExpandingTildeInPath] attributes: nil]; 
     61                NSLog(@"Avatar Support: ** Cache directory created at %@.", cacheDir); 
     62        } 
     63
     64 
     65- (void) unload 
     66
    4267         
    43         NSLog(cacheDir); 
    44         if ([[NSFileManager defaultManager] fileExistsAtPath: [cacheDir stringByExpandingTildeInPath]] == NO) 
    45         { 
    46                 [[NSFileManager defaultManager] createDirectoryAtPath: [cacheDir stringByExpandingTildeInPath] attributes: nil]; 
    47                 NSLog(@"Avatar Cache Dir created"); 
    48         } 
    49 //      else 
    50 //      { 
    51 //              NSLog(@"Avatar Cache Dir exists"); 
    52 //      } 
    53          
    54         return self; 
    55 } 
    56  
    57 - (void) dealloc 
    58 { 
    59         [super dealloc]; 
    60 } 
    61  
    62 #pragma mark - 
    63 #pragma mark MVChatPluginReloadSupport 
    64  
    65 - (void) load 
    66 { 
    67          
    68 } 
    69  
    70 - (void) unload 
    71 { 
    72          
    7368} 
    7469 
     
    7873- (void) processIncomingMessage:(JVMutableChatMessage *)message inView:(id <JVChatViewController>)view 
    7974{ 
     75        //TODO: use this if to put the user object into a var, call addavatartouser after the if (#1) 
    8076        if([[message sender] isMemberOfClass:[JVChatRoomMember class]]) 
    8177        { 
    82                 //TODO: check first if the user already has an icon, no need to do this over and over again. also: check for buddies. 
    83                 [self addAvatarToUser:[(JVChatRoomMember *)[message sender] user]]; 
     78                //NSLog(@"Avatar Support: -- %@ (class), user: %@/%@: %@ (class)", [[[message sender] class] description], [[(JVChatRoomMember *)[message sender] user] serverAddress], [[(JVChatRoomMember *)[message sender] user] nickname], [[[[message sender] user] class] description]); 
     79                 
     80                //TODO: check for buddies, unless this works for them too (doesnt, *.quakenet.org bug) 
     81                if ([[[(JVChatRoomMember *)[message sender] user] attributes] objectForKey:@"MVChatUserPictureAttribute"] == nil) 
     82                { 
     83                        //NSLog(@"Avatar Support: -- nil: %@", [[[[(JVChatRoomMember *)[message sender] user] attributes] objectForKey:@"MVChatUserPictureAttribute"] description]); 
     84                        [self addAvatarToUser:[(JVChatRoomMember *)[message sender] user]]; 
     85                } 
     86                else 
     87                { 
     88                        //NSLog(@"Avatar Support: -- nicht nil: %@", [[[[(JVChatRoomMember *)[message sender] user] attributes] objectForKey:@"MVChatUserPictureAttribute"] description]); 
     89                        //TODO: remove this 
     90                        [[JVNotificationController defaultController] performNotification:@"JVPluginNotification" withContextInfo:[NSDictionary dictionaryWithObjectsAndKeys:[[[(JVChatRoomMember *)[message sender] user] attributes] objectForKey:@"MVChatUserPictureAttribute"],@"image",@"Avatar Support",@"title",[NSString stringWithFormat:@"%@ already has an Avatar.", [[(JVChatRoomMember *)[message sender] user] nickname]],@"description",nil]]; 
     91                } 
     92        } 
     93        else 
     94        { 
     95                //TODO: this is for direct chats, see comment #1 above 
     96                //NSLog(@"Avatar Support: -- %@/%@: %@ (class)", [(MVChatUser *)[message sender] serverAddress], [(MVChatUser *)[message sender] nickname], [[[message sender] class] description]); 
     97                 
     98                //TODO: check for buddies, unless this works for them too (doesnt) 
     99                if ([[(MVChatUser *)[message sender] attributes] objectForKey:@"MVChatUserPictureAttribute"] == nil) 
     100                { 
     101                        //NSLog(@"Avatar Support: -- nil: %@", [[[(MVChatUser *)[message sender] attributes] objectForKey:@"MVChatUserPictureAttribute"] description]); 
     102                        [self addAvatarToUser:(MVChatUser *)[message sender]]; 
     103                } 
     104                else 
     105                { 
     106                        //NSLog(@"Avatar Support: --nicht nil: %@", [[[(MVChatUser *)[message sender] attributes] objectForKey:@"MVChatUserPictureAttribute"] description]); 
     107                        //TODO: remove this 
     108                        [[JVNotificationController defaultController] performNotification:@"JVPluginNotification" withContextInfo:[NSDictionary dictionaryWithObjectsAndKeys:[[(MVChatUser *)[message sender] attributes] objectForKey:@"MVChatUserPictureAttribute"],@"image",@"Avatar Support",@"title",[NSString stringWithFormat:@"%@ already has an Avatar.", [(MVChatUser *)[message sender] nickname]],@"description",nil]]; 
     109                } 
    84110        } 
    85111} 
     
    93119#pragma mark MVChatPluginChatConnectionSupport 
    94120 
    95 - (BOOL) processSubcodeRequest:(NSString *)command withArguments:(NSData *)arguments fromUser:(MVChatUser *)user 
    96 
    97         //TODO: remove NSLog 
    98 //      NSLog([@"AVATAR: Subcode Request: " stringByAppendingString:command]); 
    99 //      NSLog([user nickname]); 
    100          
     121- (BOOL) processSubcodeRequest:(NSString *)command withArguments:(NSData *)arguments fromUser:(MVChatUser *)chatUser 
     122
     123        if ([[command uppercaseString] isEqualToString:@"AVATEST"]) 
     124        { 
     125                //TODO: remove NSLog 
     126                NSLog(@"Avatar Support: <- Bogus request event for %@.", [chatUser nickname]); 
     127                [self requestAvatarFromUser:chatUser]; 
     128                return YES; 
     129        } 
    101130        if ([[command uppercaseString] isEqualToString:AKAvatarSupportCTCPCommand]) 
    102131        { 
    103                 if (arguments) 
    104                 { 
    105                         NSLog(@"das war ein angebot von %@", [user nickname]); 
     132                //TODO: remove NSLog 
     133                NSLog(@"Avatar Support: -> request by %@.", [chatUser nickname]); 
     134//              if (weWantToSendAvatarToUser:chatUser) 
     135//              { 
     136                        [self offerAvatarToUser:chatUser]; 
     137                        return YES; 
     138//              } 
     139        } 
     140        return NO; 
     141
     142 
     143- (BOOL) processSubcodeReply:(NSString *)command withArguments:(NSData *)arguments fromUser:(MVChatUser *)chatUser 
     144
     145        if ([[command uppercaseString] isEqualToString:AKAvatarSupportCTCPCommand]) 
     146        { 
     147                NSArray *argumentArray = [[[[NSString alloc] initWithData:arguments encoding:[[chatUser connection] encoding]] autorelease] componentsSeparatedByString:@" "]; 
     148                 
     149                //TODO: remove NSLog 
     150                NSLog(@"Avatar Support: <- reply from %@ with arguments %@.", [chatUser nickname], [argumentArray description]); 
     151                 
     152//              if (weWantToReceiveAvatarFromUser:chatUser) 
     153//              { 
     154                        //NSLog(@"Avatar Support: <- accepting avatar from %@.", [chatUser nickname]); 
     155                        NSImage *receivedImage = [NSImage alloc]; 
    106156                         
    107                         NSArray *argumentArray = [[[[NSString alloc] initWithData:arguments encoding:[[user connection] encoding]] autorelease] componentsSeparatedByString:@" "]; 
    108                         NSLog(@"Arguments: %@",[argumentArray description]); 
    109 //                      if (weWantToReceiveAvatarFromUser:user) 
     157                        //TODO: do some checks first: filezise, evil filetypes... 
     158                        if ([receivedImage initWithContentsOfURL:[NSURL URLWithString:[argumentArray objectAtIndex:0]]]) 
     159                        { 
     160                                [self saveAvatar:receivedImage forUser:chatUser]; 
     161                                [self addAvatarToUser:chatUser]; 
     162                                [[JVNotificationController defaultController] performNotification:@"JVPluginNotification" withContextInfo:[NSDictionary dictionaryWithObjectsAndKeys:[[chatUser attributes] objectForKey:@"MVChatUserPictureAttribute"],@"image",@"Avatar Support",@"title",[NSString stringWithFormat:@"Received Avatar from user %@.", [chatUser nickname]],@"description",nil]]; 
     163                                return YES; 
     164                        } 
     165//                      else 
    110166//                      { 
    111                                 NSLog(@"wir wollen annehmen"); 
    112                                 NSImage *receivedImage = [NSImage alloc]; 
    113                                  
    114                                 //TODO: do some checks first: filezise, evil filetypes... 
    115                                 if ([receivedImage initWithContentsOfURL:[NSURL URLWithString:[argumentArray objectAtIndex:0]]]) 
    116                                 { 
    117                                         [self saveAvatar:receivedImage forUser:user]; 
    118                                         [self addAvatarToUser:user]; 
    119                                         return YES; 
    120                                 } 
    121 //                              else 
     167//                              if (filesizeisokay) 
    122168//                              { 
    123 //                                      if (filesizeisokay) 
    124 //                                      { 
    125                                                 //NSLog(@"DCC Filetransfer required"); 
    126                                                 //[18:39] <xenon> there is, just dont add it to the MVFileTransferManager 
    127                                                 //MVFileTransferController 
    128                                                 //MVFileTransfer 
    129                                                 //filetransfer <- requestAvatarFromUser:user 
    130                                                 //filetransferdelegate: [self addAvatarToUser:user]; 
    131 //                                      } 
     169                                        //NSLog(@"Avatar Support: <- dcc file transfer request for %@ required.", [chatUser nickname]); 
     170                                        //[18:39] <xenon> there is, just dont add it to the MVFileTransferManager 
     171                                        //MVFileTransferController 
     172                                        //MVFileTransfer 
     173                                        //filetransfer <- requestAvatarFromUser:chatUser 
     174                                        //filetransferdelegate: [self addAvatarToUser:chatUser]; 
     175                                        //return YES; 
    132176//                              } 
    133177//                      } 
    134                 } 
    135                 else 
    136                 { 
    137                         //TODO: remove this! 
    138                         NSLog(@"%@ has requested our avatar", [user nickname]); 
    139 //                      if (weWantToSendAvatarToUser:user) 
    140 //                      { 
    141                                 NSLog(@"wir schicken unseren avatar los!"); 
    142                                 return YES; 
    143 //                      } 
    144                 } 
    145         } 
    146          
     178//              } 
     179        } 
    147180        return NO; 
    148181} 
     
    150183#pragma mark - 
    151184#pragma mark Plugin Methods 
     185 
     186 
     187- (void) requestAvatarFromUser:(MVChatUser *)chatUser 
     188{ 
     189        //TODO: remove NSLog 
     190        NSLog(@"Avatar Support: <- request from %@.", [chatUser nickname]); 
     191        [chatUser sendSubcodeRequest:AKAvatarSupportCTCPCommand withArguments:@"?"]; 
     192} 
     193 
     194- (void) offerAvatarToUser:(MVChatUser *)chatUser 
     195{ 
     196        //TODO: remove NSLog 
     197        NSLog(@"Avatar Support: -> reply to %@.", [chatUser nickname]); 
     198        [chatUser sendSubcodeReply:AKAvatarSupportCTCPCommand withArguments:@"http://alex.speanet.info/stuff/AKAvatarSupportIcon.png"]; 
     199} 
    152200 
    153201- (void) saveAvatar:(NSImage *)theImage forUser:(MVChatUser *)chatUser 
     
    156204        if([[theImage TIFFRepresentation] writeToFile:[[[cacheDir stringByExpandingTildeInPath] stringByAppendingPathComponent:[chatUser serverAddress]] stringByAppendingPathComponent:[chatUser nickname]] atomically: NO]) 
    157205        { 
    158                 NSLog(@"Avatar written for user %@.", [chatUser nickname]); 
     206                NSLog(@"Avatar Support: <- image written for %@/%@.", [chatUser serverAddress], [chatUser nickname]); 
    159207        } 
    160208} 
     
    164212        if([self avatarForUser:chatUser]) 
    165213        { 
     214        //TODO: handle buddies differnetly (ask: "add as buddy icon?") 
     215         
    166216        //TODO This has no actual effect in the app yet 
    167217        [chatUser setAttribute:[self avatarForUser:chatUser] forKey:@"MVChatUserPictureAttribute"]; 
    168218         
    169         //TODO: Remove this test output of the image 
    170         NSAlert *alert = [[[NSAlert alloc] init] autorelease]; 
    171         [alert addButtonWithTitle:@"OK"]; 
    172         [alert setMessageText:@"Added avatar to user"]; 
    173         [alert setInformativeText:[chatUser nickname]]; 
    174         [alert setIcon:[self avatarForUser:chatUser]]; 
    175         [alert setAlertStyle:NSInformationalAlertStyle]; 
    176         [alert beginSheetModalForWindow:nil modalDelegate:self didEndSelector:@selector(alertDidEnd:returnCode:contextInfo:) contextInfo:nil]; 
    177         } 
    178 
    179  
    180 - (void)alertDidEnd:(NSAlert *)alert returnCode:(int)returnCode contextInfo:(void *)contextInfo 
    181 
    182         //TODO: remove this here and in the header, just for testing 
     219        //TODO: remove this later, its enough to growl on "received avatar" 
     220        [[JVNotificationController defaultController] performNotification:@"JVPluginNotification" withContextInfo:[NSDictionary dictionaryWithObjectsAndKeys:[[chatUser attributes] objectForKey:@"MVChatUserPictureAttribute"],@"image",@"Avatar Support",@"title",[NSString stringWithFormat:@"Avatar added for user %@.", [chatUser nickname]],@"description",nil]]; 
     221        } 
    183222} 
    184223 
     
    187226        /*if ([[NSFileManager defaultManager] fileExistsAtPath: [[[[cacheDir stringByExpandingTildeInPath] stringByAppendingPathComponent:[chatUser serverAddress]] stringByAppendingPathComponent:[chatUser nickname]] stringByAppendingPathExtension:@"png"]]) 
    188227        { 
    189                 NSLog(@"Avatar exists for user %@", [chatUser nickname]); 
     228                NSLog(@"Avatar Support: -- Avatar exists for user %@", [chatUser nickname]); 
    190229        }*/ 
    191          
     230                
    192231        //TODO: what about file type extenstions? 
    193232        return [[NSImage alloc]initWithContentsOfFile: [[[cacheDir stringByExpandingTildeInPath] stringByAppendingPathComponent:[chatUser serverAddress]] stringByAppendingPathComponent:[chatUser nickname]]]; 
    194233} 
    195234 
     235/* 
     236TODO: 
     237context menu, command line interface 
     238file transfer support (send/receive) 
     239local user defineable avatar 
     240custom avatars for users 
     241actual file management (with file extensions -> nsdictionary, lazy nsimages) 
     242... 
     243(fix colloquys buddies/ bubbles style) 
     244*/ 
    196245 
    197246/* 
     
    199248Avatar > 
    200249Request Avatar 
     250Offer Avatar 
    201251Manually Select Avatar 
    202252-