Source/AIInterfaceController.m
changeset 3277 21ab21e877e0
parent 3092 ffb42621b742
child 3310 72ccf4b32d4d
     1.1 --- a/Source/AIInterfaceController.m	Tue Dec 29 13:44:29 2009 -0500
     1.2 +++ b/Source/AIInterfaceController.m	Sat Aug 28 22:01:12 2010 +0200
     1.3 @@ -138,6 +138,8 @@
     1.4  		
     1.5  		windowMenuArray = nil;
     1.6  		
     1.7 +		recentlyClosedChats = [[NSMutableArray alloc] init];
     1.8 +		
     1.9  #ifdef LOG_RESPONDER_CHAIN
    1.10  		[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(reportResponderChain:) userInfo:nil repeats:YES];
    1.11  #endif
    1.12 @@ -274,6 +276,8 @@
    1.13  	[[NSNotificationCenter defaultCenter] removeObserver:self];
    1.14  	[adium.preferenceController unregisterPreferenceObserver:self];
    1.15  	
    1.16 +	[recentlyClosedChats release]; recentlyClosedChats = nil;
    1.17 +	
    1.18      [super dealloc];
    1.19  }
    1.20  
    1.21 @@ -663,6 +667,31 @@
    1.22  {
    1.23  	if (inChat) {
    1.24  		if ([adium.chatController closeChat:inChat]) {
    1.25 +			
    1.26 +			NSMutableDictionary *newRecentlyClosedChat = [NSMutableDictionary dictionary];
    1.27 +			
    1.28 +			[newRecentlyClosedChat setObject:inChat.account.internalObjectID forKey:@"AccountID"];
    1.29 +			
    1.30 +			if (inChat.isGroupChat) {
    1.31 +				// -chatCreationDictionary may be nil, so put it last.
    1.32 +				[newRecentlyClosedChat addEntriesFromDictionary:[NSDictionary dictionaryWithObjectsAndKeys:
    1.33 +																 [NSNumber numberWithBool:YES], @"IsGroupChat",
    1.34 +																 inChat.name, @"Name",
    1.35 +																 [inChat chatCreationDictionary], @"ChatCreationInfo",nil]];
    1.36 +			} else {
    1.37 +				[newRecentlyClosedChat addEntriesFromDictionary:[NSDictionary dictionaryWithObjectsAndKeys:
    1.38 +																 inChat.listObject.UID, @"UID",
    1.39 +																 inChat.account.service.serviceID, @"serviceID",
    1.40 +																 inChat.account.internalObjectID, @"AccountID",nil]];
    1.41 +			}
    1.42 +			
    1.43 +			[recentlyClosedChats insertObject:newRecentlyClosedChat atIndex:0];
    1.44 +			
    1.45 +			// this sounds like a sensible limit: no-one will remember what chat they had in the closed tab beyond these
    1.46 +			while (recentlyClosedChats.count > 16) {
    1.47 +				[recentlyClosedChats removeLastObject];
    1.48 +			}
    1.49 +			
    1.50  			[interfacePlugin closeChat:inChat];
    1.51  		}
    1.52  	}
    1.53 @@ -783,6 +812,50 @@
    1.54  	[_cachedOpenChats release]; _cachedOpenChats = nil;
    1.55  }
    1.56  
    1.57 +- (IBAction)reopenChat:(id)sender
    1.58 +{
    1.59 +	if (recentlyClosedChats.count == 0) {
    1.60 +		AILogWithSignature(@"Can't open recently closed tab: no recently closed tabs!");
    1.61 +		return;
    1.62 +	}
    1.63 +	
    1.64 +	NSDictionary *chatDict = [[[recentlyClosedChats objectAtIndex:0] retain] autorelease];
    1.65 +	[recentlyClosedChats removeObjectAtIndex:0];
    1.66 +	
    1.67 +	AIChat			*chat = nil;
    1.68 +	AIService		*service = [adium.accountController firstServiceWithServiceID:[chatDict objectForKey:@"serviceID"]];
    1.69 +	AIAccount		*account = [adium.accountController accountWithInternalObjectID:[chatDict objectForKey:@"AccountID"]];
    1.70 +	
    1.71 +	if ([[chatDict objectForKey:@"IsGroupChat"] boolValue]) {
    1.72 +		chat = [adium.chatController chatWithName:[chatDict objectForKey:@"Name"]
    1.73 +									   identifier:nil
    1.74 +										onAccount:account
    1.75 +								 chatCreationInfo:[chatDict objectForKey:@"ChatCreationInfo"]];
    1.76 +	} else {
    1.77 +		AIListContact *contact = [adium.contactController contactWithService:service
    1.78 +																	 account:account
    1.79 +																		 UID:[chatDict objectForKey:@"UID"]];
    1.80 +		
    1.81 +		if (contact) chat = [adium.chatController chatWithContact:contact];
    1.82 +	}
    1.83 +	
    1.84 +	if (!chat) {
    1.85 +		NSRunAlertPanel(AILocalizedString(@"Restoring chat failed", nil),
    1.86 +						AILocalizedString(@"Restoring the last closed tab failed. Perhaps the account not exist anymore?", nil),
    1.87 +						AILocalizedString(@"OK", nil),
    1.88 +						nil,
    1.89 +						nil);
    1.90 +		return;
    1.91 +	}
    1.92 +	
    1.93 +	// Tag the chat as restored.
    1.94 +	[chat setValue:[NSNumber numberWithBool:YES]
    1.95 +	   forProperty:@"Restored Chat"
    1.96 +			notify:NotifyNow];
    1.97 +	
    1.98 +	[self openChat:chat inContainerWithID:nil atIndex:-1];
    1.99 +	[self setActiveChat:chat];
   1.100 +}
   1.101  
   1.102  
   1.103  //Interface plugin callbacks -------------------------------------------------------------------------------------------
   1.104 @@ -1931,7 +2004,9 @@
   1.105  									  AILocalizedString(@"Show Fonts",nil))];
   1.106  		return YES;
   1.107  	} else if (menuItem == menuItem_toggleUserlist || menuItem == menuItem_toggleUserlistSide) {
   1.108 -			return self.activeChat.isGroupChat;
   1.109 +		return self.activeChat.isGroupChat;
   1.110 +	} else if (menuItem == menuItem_reopenTab) {
   1.111 +		return recentlyClosedChats.count > 0;
   1.112  	} else {
   1.113  		return YES;
   1.114  	}