Skip to content

Commit

Permalink
Remember connected state, rooms and direct chats that are open when q…
Browse files Browse the repository at this point in the history
…uiting. Restore on launch.

git-svn-id: http://source.colloquy.info/svn/trunk@3905 cc480944-b4dd-0310-b5e3-89908df9b951
  • Loading branch information
kijiro committed Dec 17, 2008
1 parent 1c208ed commit b53caa0
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 8 deletions.
3 changes: 3 additions & 0 deletions Mobile/Controllers/CQChatController.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@

@property (nonatomic) NSInteger totalImportantUnreadCount;

- (NSDictionary *) persistentStateForConnection:(MVChatConnection *) connection;
- (void) restorePersistentState:(NSDictionary *) state forConnection:(MVChatConnection *) connection;

- (void) showNewChatActionSheet;
- (void) showChatControllerWhenAvailableForRoomNamed:(NSString *) room andConnection:(MVChatConnection *) connection;
- (void) showChatController:(id <CQChatViewController>) controller animated:(BOOL) animated;
Expand Down
40 changes: 40 additions & 0 deletions Mobile/Controllers/CQChatController.m
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,46 @@ - (void) _gotDirectChatMessage:(NSNotification *) notification {

#pragma mark -

- (NSDictionary *) persistentStateForConnection:(MVChatConnection *) connection {
NSArray *controllers = [self chatViewControllersForConnection:connection];
if (!controllers.count)
return nil;

NSMutableDictionary *state = [[NSMutableDictionary alloc] init];

NSMutableArray *rooms = [[NSMutableArray alloc] init];
NSMutableArray *users = [[NSMutableArray alloc] init];

for (CQChatRoomController *controller in controllers) {
if ([controller isMemberOfClass:[CQChatRoomController class]])
[rooms addObject:controller.room.uniqueIdentifier];
else if ([controller isMemberOfClass:[CQDirectChatController class]])
[users addObject:controller.user.nickname];
}

if (rooms.count) [state setObject:rooms forKey:@"rooms"];
if (users.count) [state setObject:users forKey:@"users"];

[rooms release];
[users release];

return [state autorelease];
}

- (void) restorePersistentState:(NSDictionary *) state forConnection:(MVChatConnection *) connection {
for (NSString *roomName in [state objectForKey:@"rooms"]) {
MVChatRoom *room = [connection chatRoomWithName:roomName];
if (room) [self chatViewControllerForRoom:room ifExists:NO];
}

for (NSString *nickname in [state objectForKey:@"users"]) {
MVChatUser *user = [[connection chatUsersWithNickname:nickname] anyObject];
if (user) [self chatViewControllerForUser:user ifExists:NO];
}
}

#pragma mark -

- (NSInteger) totalImportantUnreadCount {
return _totalImportantUnreadCount;
}
Expand Down
59 changes: 52 additions & 7 deletions Mobile/Controllers/CQConnectionsController.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#import "NSStringAdditions.h"

#import <ChatCore/MVChatConnection.h>
#import <ChatCore/MVChatRoom.h>

#if defined(TARGET_IPHONE_SIMULATOR) && TARGET_IPHONE_SIMULATOR
#import <Foundation/NSDebug.h>
Expand Down Expand Up @@ -108,10 +109,10 @@ - (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfa
#pragma mark -

- (void) applicationWillTerminate {
[self saveConnections];

for (MVChatConnection *connection in _connections)
[connection disconnectWithReason:[MVChatConnection defaultQuitMessage]];

[self saveConnections];
}

#pragma mark -
Expand Down Expand Up @@ -228,9 +229,24 @@ - (void) _willConnect:(NSNotification *) notification {
[connection sendCommand:command withArguments:arguments];
}

NSArray *rooms = connection.automaticJoinedRooms;
NSMutableArray *rooms = [connection.automaticJoinedRooms mutableCopy];

NSDictionary *persistentInformation = connection.persistentInformation;
NSArray *previousRooms = [persistentInformation objectForKey:@"previousRooms"];

if (previousRooms.count) {
[rooms addObjectsFromArray:previousRooms];

NSMutableDictionary *persistentInformation = [connection.persistentInformation mutableCopy];
[persistentInformation removeObjectForKey:@"previousRooms"];
connection.persistentInformation = persistentInformation;
[persistentInformation release];
}

if (rooms.count)
[connection joinChatRoomsNamed:rooms];

[rooms release];
}

- (void) _didConnectOrDidNotConnect:(NSNotification *) notification {
Expand Down Expand Up @@ -272,6 +288,8 @@ - (void) _loadConnectionList {
[persistentInformation setObject:[info objectForKey:@"automatic"] forKey:@"automatic"];
if ([info objectForKey:@"rooms"])
[persistentInformation setObject:[info objectForKey:@"rooms"] forKey:@"rooms"];
if ([info objectForKey:@"previousRooms"])
[persistentInformation setObject:[info objectForKey:@"previousRooms"] forKey:@"previousRooms"];
if ([info objectForKey:@"description"])
[persistentInformation setObject:[info objectForKey:@"description"] forKey:@"description"];
if ([info objectForKey:@"commands"] && ((NSString *)[info objectForKey:@"commands"]).length)
Expand Down Expand Up @@ -307,7 +325,10 @@ - (void) _loadConnectionList {

[_connections addObject:connection];

if ([[info objectForKey:@"automatic"] boolValue])
if ([info objectForKey:@"chatState"])
[[CQChatController defaultController] restorePersistentState:[info objectForKey:@"chatState"] forConnection:connection];

if ([[info objectForKey:@"automatic"] boolValue] || [[info objectForKey:@"wasConnected"] boolValue])
[connection connect];

[connection release];
Expand All @@ -326,23 +347,47 @@ - (void) saveConnections {
NSMutableDictionary *persistentInformation = [connection.persistentInformation mutableCopy];
if ([persistentInformation objectForKey:@"automatic"])
[info setObject:[persistentInformation objectForKey:@"automatic"] forKey:@"automatic"];
if ([persistentInformation objectForKey:@"rooms"])
if ([[persistentInformation objectForKey:@"rooms"] count])
[info setObject:[persistentInformation objectForKey:@"rooms"] forKey:@"rooms"];
if ([persistentInformation objectForKey:@"description"])
if ([[persistentInformation objectForKey:@"description"] length])
[info setObject:[persistentInformation objectForKey:@"description"] forKey:@"description"];
if ([persistentInformation objectForKey:@"commands"])
if ([[persistentInformation objectForKey:@"commands"] count])
[info setObject:[[persistentInformation objectForKey:@"commands"] componentsJoinedByString:@"\n"] forKey:@"commands"];

[persistentInformation removeObjectForKey:@"rooms"];
[persistentInformation removeObjectForKey:@"previousRooms"];
[persistentInformation removeObjectForKey:@"commands"];
[persistentInformation removeObjectForKey:@"description"];
[persistentInformation removeObjectForKey:@"automatic"];

NSDictionary *chatState = [[CQChatController defaultController] persistentStateForConnection:connection];
if (chatState.count)
[info setObject:chatState forKey:@"chatState"];

if (persistentInformation.count)
[info setObject:persistentInformation forKey:@"persistentInformation"];

[persistentInformation release];

[info setObject:[NSNumber numberWithBool:connection.connected] forKey:@"wasConnected"];

NSSet *joinedRooms = connection.joinedChatRooms;
if (connection.connected && joinedRooms.count) {
NSMutableArray *previousJoinedRooms = [[NSMutableArray alloc] init];

for (MVChatRoom *room in joinedRooms) {
if (room && room.name)
[previousJoinedRooms addObject:room.name];
}

[previousJoinedRooms removeObjectsInArray:[info objectForKey:@"rooms"]];

if (previousJoinedRooms.count)
[info setObject:previousJoinedRooms forKey:@"previousRooms"];

[previousJoinedRooms release];
}

[info setObject:connection.server forKey:@"server"];
[info setObject:connection.urlScheme forKey:@"type"];
[info setObject:[NSNumber numberWithBool:connection.secure] forKey:@"secure"];
Expand Down
4 changes: 3 additions & 1 deletion Mobile/Views/CQChatTableCell.m
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ - (id) initWithFrame:(CGRect) frame reuseIdentifier:(NSString *) reuseIdentifier
_nameLabel = [[UILabel alloc] initWithFrame:CGRectZero];
_unreadCountView = [[CQUnreadCountView alloc] initWithFrame:CGRectZero];

_iconImageView.alpha = _nameLabel.alpha = 0.5;

_maximumMessagePreviews = 2;
_showsUserInMessagePreviews = YES;
_available = YES;
_available = NO;

[self.contentView addSubview:_iconImageView];
[self.contentView addSubview:_nameLabel];
Expand Down

0 comments on commit b53caa0

Please sign in to comment.