Blame client/iOS/Controllers/BookmarkListController.m

Packit 1fb8d4
/*
Packit 1fb8d4
 bookmarks and active session view controller
Packit 1fb8d4
 
Packit 1fb8d4
 Copyright 2013 Thincast Technologies GmbH, Author: Martin Fleisz
Packit 1fb8d4
 
Packit 1fb8d4
 This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. 
Packit 1fb8d4
 If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
Packit 1fb8d4
 */
Packit 1fb8d4
Packit 1fb8d4
#import "BookmarkListController.h"
Packit 1fb8d4
#import "Utils.h"
Packit 1fb8d4
#import "BookmarkEditorController.h"
Packit 1fb8d4
#import "RDPSessionViewController.h"
Packit 1fb8d4
#import "Toast+UIView.h"
Packit 1fb8d4
#import "Reachability.h"
Packit 1fb8d4
#import "GlobalDefaults.h"
Packit 1fb8d4
#import "BlockAlertView.h"
Packit 1fb8d4
Packit 1fb8d4
#define SECTION_SESSIONS    0
Packit 1fb8d4
#define SECTION_BOOKMARKS   1
Packit 1fb8d4
#define NUM_SECTIONS        2
Packit 1fb8d4
Packit 1fb8d4
@interface BookmarkListController (Private)
Packit 1fb8d4
#pragma mark misc functions
Packit 1fb8d4
- (UIButton*)disclosureButtonWithImage:(UIImage*)image;
Packit 1fb8d4
- (void)performSearch:(NSString*)searchText;
Packit 1fb8d4
#pragma mark Persisting bookmarks
Packit 1fb8d4
- (void)scheduleWriteBookmarksToDataStore;
Packit 1fb8d4
- (void)writeBookmarksToDataStore;
Packit 1fb8d4
- (void)scheduleWriteManualBookmarksToDataStore;
Packit 1fb8d4
- (void)writeManualBookmarksToDataStore;
Packit 1fb8d4
- (void)readManualBookmarksFromDataStore;
Packit 1fb8d4
- (void)writeArray:(NSArray*)bookmarks toDataStoreURL:(NSURL*)url;
Packit 1fb8d4
- (NSMutableArray*)arrayFromDataStoreURL:(NSURL*)url;
Packit 1fb8d4
- (NSURL*)manualBookmarksDataStoreURL;
Packit 1fb8d4
- (NSURL*)connectionHistoryDataStoreURL;
Packit 1fb8d4
@end
Packit 1fb8d4
Packit 1fb8d4
Packit 1fb8d4
@implementation BookmarkListController
Packit 1fb8d4
Packit 1fb8d4
@synthesize searchBar = _searchBar, tableView = _tableView, bmTableCell = _bmTableCell, sessTableCell = _sessTableCell;
Packit 1fb8d4
Packit 1fb8d4
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
Packit 1fb8d4
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
Packit 1fb8d4
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) 
Packit 1fb8d4
	{   
Packit 1fb8d4
        // load bookmarks
Packit 1fb8d4
        [self readManualBookmarksFromDataStore];
Packit 1fb8d4
Packit 1fb8d4
        // load connection history
Packit 1fb8d4
        [self readConnectionHistoryFromDataStore];
Packit 1fb8d4
        
Packit 1fb8d4
		// init search result array
Packit 1fb8d4
		_manual_search_result = nil;		
Packit 1fb8d4
        
Packit 1fb8d4
        // register for session notifications
Packit 1fb8d4
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sessionDisconnected:) name:TSXSessionDidDisconnectNotification object:nil];
Packit 1fb8d4
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sessionFailedToConnect:) name:TSXSessionDidFailToConnectNotification object:nil];
Packit 1fb8d4
                
Packit 1fb8d4
        // set title and tabbar controller image
Packit 1fb8d4
        [self setTitle:NSLocalizedString(@"Connections", @"'Connections': bookmark controller title")];
Packit 1fb8d4
        [self setTabBarItem:[[[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemBookmarks tag:0] autorelease]];
Packit 1fb8d4
Packit 1fb8d4
        // load images
Packit 1fb8d4
        _star_on_img = [[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"icon_accessory_star_on" ofType:@"png"]] retain];
Packit 1fb8d4
        _star_off_img = [[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"icon_accessory_star_off" ofType:@"png"]] retain];
Packit 1fb8d4
Packit 1fb8d4
        // init reachability detection
Packit 1fb8d4
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
Packit 1fb8d4
        
Packit 1fb8d4
        // init other properties
Packit 1fb8d4
        _active_sessions = [[NSMutableArray alloc] init];
Packit 1fb8d4
        _temporary_bookmark = nil;
Packit 1fb8d4
    }
Packit 1fb8d4
    return self;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
- (void)loadView
Packit 1fb8d4
{
Packit 1fb8d4
    [super loadView];    
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
Packit 1fb8d4
- (void)viewDidLoad {
Packit 1fb8d4
    [super viewDidLoad];        
Packit 1fb8d4
    
Packit 1fb8d4
    // set edit button to allow bookmark list editing
Packit 1fb8d4
    [[self navigationItem] setRightBarButtonItem:[self editButtonItem]];
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
Packit 1fb8d4
- (void)viewWillAppear:(BOOL)animated 
Packit 1fb8d4
{
Packit 1fb8d4
	[super viewWillAppear:animated];
Packit 1fb8d4
    
Packit 1fb8d4
    // in case we had a search - search again cause the bookmark searchable items could have changed
Packit 1fb8d4
    if ([[_searchBar text] length] > 0)
Packit 1fb8d4
        [self performSearch:[_searchBar text]];
Packit 1fb8d4
Packit 1fb8d4
    // to reflect any bookmark changes - reload table
Packit 1fb8d4
    [_tableView reloadData];
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
- (void)viewWillDisappear:(BOOL)animated
Packit 1fb8d4
{
Packit 1fb8d4
    [super viewWillDisappear:animated];
Packit 1fb8d4
    
Packit 1fb8d4
    // clear any search
Packit 1fb8d4
    [_searchBar setText:@""];
Packit 1fb8d4
    [_searchBar resignFirstResponder];
Packit 1fb8d4
    [self performSearch:@""];
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
// Override to allow orientations other than the default portrait orientation.
Packit 1fb8d4
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
Packit 1fb8d4
    // Return YES for supported orientations
Packit 1fb8d4
    return YES;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
Packit 1fb8d4
- (void)didReceiveMemoryWarning {
Packit 1fb8d4
    // Releases the view if it doesn't have a superview.
Packit 1fb8d4
    [super didReceiveMemoryWarning];
Packit 1fb8d4
    
Packit 1fb8d4
    // Release any cached data, images, etc that aren't in use.
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
- (void)viewDidUnload {
Packit 1fb8d4
    [super viewDidUnload];
Packit 1fb8d4
    // Release any retained subviews of the main view.
Packit 1fb8d4
    // e.g. self.myOutlet = nil;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
Packit 1fb8d4
- (void)dealloc 
Packit 1fb8d4
{
Packit 1fb8d4
    [[NSNotificationCenter defaultCenter] removeObserver:self];
Packit 1fb8d4
Packit 1fb8d4
    [_temporary_bookmark release];
Packit 1fb8d4
    [_connection_history release];
Packit 1fb8d4
    [_active_sessions release];
Packit 1fb8d4
	[_manual_search_result release];
Packit 1fb8d4
    [_manual_bookmarks release];
Packit 1fb8d4
    
Packit 1fb8d4
    [_star_on_img release];
Packit 1fb8d4
    [_star_off_img release];
Packit 1fb8d4
Packit 1fb8d4
    [super dealloc];
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
Packit 1fb8d4
#pragma mark -
Packit 1fb8d4
#pragma mark Table view data source
Packit 1fb8d4
Packit 1fb8d4
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
Packit 1fb8d4
    // Return the number of sections.
Packit 1fb8d4
	return NUM_SECTIONS;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
Packit 1fb8d4
	
Packit 1fb8d4
	switch(section)
Packit 1fb8d4
	{
Packit 1fb8d4
        case SECTION_SESSIONS:
Packit 1fb8d4
            return 0;
Packit 1fb8d4
            break;
Packit 1fb8d4
            
Packit 1fb8d4
		case SECTION_BOOKMARKS:
Packit 1fb8d4
			{
Packit 1fb8d4
				// (+1 for Add Bookmark entry)
Packit 1fb8d4
				if(_manual_search_result != nil)
Packit 1fb8d4
					return ([_manual_search_result count] + [_history_search_result count] + 1);
Packit 1fb8d4
				return ([_manual_bookmarks count] + 1);
Packit 1fb8d4
			}
Packit 1fb8d4
			break;	
Packit 1fb8d4
			
Packit 1fb8d4
		default:
Packit 1fb8d4
			break;
Packit 1fb8d4
	}
Packit 1fb8d4
	return 0;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
- (UITableViewCell*)cellForGenericListEntry
Packit 1fb8d4
{
Packit 1fb8d4
    static NSString *CellIdentifier = @"BookmarkListCell";
Packit 1fb8d4
    UITableViewCell *cell = [[self tableView] dequeueReusableCellWithIdentifier:CellIdentifier];
Packit 1fb8d4
    if(cell == nil)
Packit 1fb8d4
    {
Packit 1fb8d4
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
Packit 1fb8d4
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
Packit 1fb8d4
        [cell setAccessoryView:[self disclosureButtonWithImage:_star_off_img]];                        
Packit 1fb8d4
    }
Packit 1fb8d4
Packit 1fb8d4
    return cell;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
- (BookmarkTableCell*)cellForBookmark
Packit 1fb8d4
{
Packit 1fb8d4
    static NSString *BookmarkCellIdentifier = @"BookmarkCell";   
Packit 1fb8d4
    BookmarkTableCell *cell = (BookmarkTableCell*)[[self tableView] dequeueReusableCellWithIdentifier:BookmarkCellIdentifier];
Packit 1fb8d4
    if(cell == nil) 
Packit 1fb8d4
    {
Packit 1fb8d4
        [[NSBundle mainBundle] loadNibNamed:@"BookmarkTableViewCell" owner:self options:nil];
Packit 1fb8d4
        [_bmTableCell setAccessoryView:[self disclosureButtonWithImage:_star_on_img]];                        
Packit 1fb8d4
        cell = _bmTableCell;
Packit 1fb8d4
        _bmTableCell = nil;
Packit 1fb8d4
    }
Packit 1fb8d4
    
Packit 1fb8d4
    return cell;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
// Customize the appearance of table view cells.
Packit 1fb8d4
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
Packit 1fb8d4
Packit 1fb8d4
Packit 1fb8d4
    switch ([indexPath section])
Packit 1fb8d4
    {
Packit 1fb8d4
        case SECTION_SESSIONS:
Packit 1fb8d4
        {
Packit 1fb8d4
            // get custom session cell
Packit 1fb8d4
            static NSString *SessionCellIdentifier = @"SessionCell";
Packit 1fb8d4
            SessionTableCell *cell = (SessionTableCell*)[tableView dequeueReusableCellWithIdentifier:SessionCellIdentifier];
Packit 1fb8d4
            if(cell == nil) 
Packit 1fb8d4
            {
Packit 1fb8d4
                [[NSBundle mainBundle] loadNibNamed:@"SessionTableViewCell" owner:self options:nil];
Packit 1fb8d4
                cell = _sessTableCell;
Packit 1fb8d4
                _sessTableCell = nil;
Packit 1fb8d4
            }
Packit 1fb8d4
            
Packit 1fb8d4
            // set cell data
Packit 1fb8d4
            RDPSession* session = [_active_sessions objectAtIndex:[indexPath row]];        
Packit 1fb8d4
            [[cell title] setText:[session sessionName]];
Packit 1fb8d4
            [[cell server] setText:[[session params] StringForKey:@"hostname"]];
Packit 1fb8d4
            [[cell username] setText:[[session params] StringForKey:@"username"]];
Packit 1fb8d4
            [[cell screenshot] setImage:[session getScreenshotWithSize:[[cell screenshot] bounds].size]];
Packit 1fb8d4
            [[cell disconnectButton] setTag:[indexPath row]];        
Packit 1fb8d4
            return cell;
Packit 1fb8d4
        }
Packit 1fb8d4
            
Packit 1fb8d4
        case SECTION_BOOKMARKS:
Packit 1fb8d4
        {
Packit 1fb8d4
            // special handling for first cell - quick connect/quick create Bookmark cell
Packit 1fb8d4
            if([indexPath row] == 0)
Packit 1fb8d4
            {                
Packit 1fb8d4
                // if a search text is entered the cell becomes a quick connect/quick create bookmark cell - otherwise it's just an add bookmark cell
Packit 1fb8d4
                UITableViewCell* cell = [self cellForGenericListEntry];
Packit 1fb8d4
                if ([[_searchBar text] length] == 0)
Packit 1fb8d4
                {
Packit 1fb8d4
                    [[cell textLabel] setText:[@"  " stringByAppendingString:NSLocalizedString(@"Add Connection", @"'Add Connection': button label")]];
Packit 1fb8d4
                    [((UIButton*)[cell accessoryView]) setHidden:YES];
Packit 1fb8d4
                }
Packit 1fb8d4
                else
Packit 1fb8d4
                {
Packit 1fb8d4
                    [[cell textLabel] setText:[@"  " stringByAppendingString:[_searchBar text]]];            
Packit 1fb8d4
                    [((UIButton*)[cell accessoryView]) setHidden:NO];
Packit 1fb8d4
                }
Packit 1fb8d4
                
Packit 1fb8d4
                return cell;
Packit 1fb8d4
            }
Packit 1fb8d4
            else            
Packit 1fb8d4
            {
Packit 1fb8d4
                // do we have a history cell or bookmark cell?
Packit 1fb8d4
                if ([self isIndexPathToHistoryItem:indexPath])
Packit 1fb8d4
                {
Packit 1fb8d4
                    UITableViewCell* cell = [self cellForGenericListEntry];
Packit 1fb8d4
                    [[cell textLabel] setText:[@"  " stringByAppendingString:[_history_search_result objectAtIndex:[self historyIndexFromIndexPath:indexPath]]]];
Packit 1fb8d4
                    [((UIButton*)[cell accessoryView]) setHidden:NO];                    
Packit 1fb8d4
                    return cell;
Packit 1fb8d4
                }
Packit 1fb8d4
                else
Packit 1fb8d4
                {
Packit 1fb8d4
                    // set cell properties
Packit 1fb8d4
                    ComputerBookmark* entry;
Packit 1fb8d4
                    BookmarkTableCell* cell = [self cellForBookmark];
Packit 1fb8d4
                    if(_manual_search_result == nil)
Packit 1fb8d4
                        entry = [_manual_bookmarks objectAtIndex:[self bookmarkIndexFromIndexPath:indexPath]];
Packit 1fb8d4
                    else
Packit 1fb8d4
                        entry = [[_manual_search_result objectAtIndex:[self bookmarkIndexFromIndexPath:indexPath]] valueForKey:@"bookmark"];
Packit 1fb8d4
                    
Packit 1fb8d4
                    [[cell title] setText:[entry label]];
Packit 1fb8d4
                    [[cell subTitle] setText:[[entry params] StringForKey:@"hostname"]];
Packit 1fb8d4
                    return cell;                
Packit 1fb8d4
                }                
Packit 1fb8d4
            }            
Packit 1fb8d4
        }
Packit 1fb8d4
            
Packit 1fb8d4
        default:
Packit 1fb8d4
            break;
Packit 1fb8d4
    }
Packit 1fb8d4
    
Packit 1fb8d4
    NSAssert(0, @"Failed to create cell");
Packit 1fb8d4
    return nil;    	
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
Packit 1fb8d4
Packit 1fb8d4
// Override to support conditional editing of the table view.
Packit 1fb8d4
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
Packit 1fb8d4
	// dont allow to edit Add Bookmark item
Packit 1fb8d4
    if([indexPath section] == SECTION_SESSIONS)
Packit 1fb8d4
        return NO;
Packit 1fb8d4
    if([indexPath section] == SECTION_BOOKMARKS && [indexPath row] == 0)
Packit 1fb8d4
		return NO;
Packit 1fb8d4
    return YES;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
Packit 1fb8d4
Packit 1fb8d4
 // Override to support editing the table view.
Packit 1fb8d4
 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
Packit 1fb8d4
{
Packit 1fb8d4
	if(editingStyle == UITableViewCellEditingStyleDelete) 
Packit 1fb8d4
	{
Packit 1fb8d4
		// Delete the row from the data source
Packit 1fb8d4
		switch([indexPath section])
Packit 1fb8d4
		{
Packit 1fb8d4
            case SECTION_BOOKMARKS:
Packit 1fb8d4
            {
Packit 1fb8d4
                if (_manual_search_result == nil)
Packit 1fb8d4
                    [_manual_bookmarks removeObjectAtIndex:[self bookmarkIndexFromIndexPath:indexPath]];
Packit 1fb8d4
                else
Packit 1fb8d4
                {
Packit 1fb8d4
                    // history item or bookmark?
Packit 1fb8d4
                    if ([self isIndexPathToHistoryItem:indexPath])
Packit 1fb8d4
                    {
Packit 1fb8d4
                        [_connection_history removeObject:[_history_search_result objectAtIndex:[self historyIndexFromIndexPath:indexPath]]];
Packit 1fb8d4
                        [_history_search_result removeObjectAtIndex:[self historyIndexFromIndexPath:indexPath]];
Packit 1fb8d4
                    }                    
Packit 1fb8d4
                    else
Packit 1fb8d4
                    {
Packit 1fb8d4
                        [_manual_bookmarks removeObject:[[_manual_search_result objectAtIndex:[self bookmarkIndexFromIndexPath:indexPath]] valueForKey:@"bookmark"]];
Packit 1fb8d4
                        [_manual_search_result removeObjectAtIndex:[self bookmarkIndexFromIndexPath:indexPath]];
Packit 1fb8d4
                    }                    
Packit 1fb8d4
                }
Packit 1fb8d4
                [self scheduleWriteManualBookmarksToDataStore];
Packit 1fb8d4
                break;
Packit 1fb8d4
            }                
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		[tableView reloadSections:[NSIndexSet indexSetWithIndex:[indexPath section]] withRowAnimation:UITableViewRowAnimationNone];
Packit 1fb8d4
	}   
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
Packit 1fb8d4
// Override to support rearranging the table view.
Packit 1fb8d4
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath 
Packit 1fb8d4
{
Packit 1fb8d4
	if([fromIndexPath compare:toIndexPath] != NSOrderedSame)
Packit 1fb8d4
	{
Packit 1fb8d4
		switch([fromIndexPath section])
Packit 1fb8d4
		{
Packit 1fb8d4
            case SECTION_BOOKMARKS:
Packit 1fb8d4
            {
Packit 1fb8d4
                int fromIdx = [self bookmarkIndexFromIndexPath:fromIndexPath];
Packit 1fb8d4
                int toIdx = [self bookmarkIndexFromIndexPath:toIndexPath];
Packit 1fb8d4
                ComputerBookmark* temp_bookmark = [[_manual_bookmarks objectAtIndex:fromIdx] retain];
Packit 1fb8d4
                [_manual_bookmarks removeObjectAtIndex:fromIdx];
Packit 1fb8d4
                if (toIdx >= [_manual_bookmarks count])
Packit 1fb8d4
                    [_manual_bookmarks addObject:temp_bookmark];
Packit 1fb8d4
                else
Packit 1fb8d4
                    [_manual_bookmarks insertObject:temp_bookmark atIndex:toIdx];
Packit 1fb8d4
                [temp_bookmark release];
Packit 1fb8d4
                
Packit 1fb8d4
                [self scheduleWriteManualBookmarksToDataStore];
Packit 1fb8d4
                break;
Packit 1fb8d4
            }
Packit 1fb8d4
		}
Packit 1fb8d4
	}
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
Packit 1fb8d4
// prevent that an item is moved befoer the Add Bookmark item
Packit 1fb8d4
-(NSIndexPath*)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
Packit 1fb8d4
{
Packit 1fb8d4
    // don't allow to move:
Packit 1fb8d4
    //  - items between sections
Packit 1fb8d4
    //  - the quick connect/quick create bookmark cell
Packit 1fb8d4
    //  - any item while a search is applied
Packit 1fb8d4
    if([proposedDestinationIndexPath row] == 0 || ([sourceIndexPath section] != [proposedDestinationIndexPath section]) || 
Packit 1fb8d4
       _manual_search_result != nil)
Packit 1fb8d4
    {
Packit 1fb8d4
        return sourceIndexPath;
Packit 1fb8d4
    }
Packit 1fb8d4
    else
Packit 1fb8d4
    {
Packit 1fb8d4
        return proposedDestinationIndexPath;
Packit 1fb8d4
    }
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
Packit 1fb8d4
// Override to support conditional rearranging of the table view.
Packit 1fb8d4
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
Packit 1fb8d4
	// dont allow to reorder Add Bookmark item
Packit 1fb8d4
    if([indexPath section] == SECTION_BOOKMARKS && [indexPath row] == 0)
Packit 1fb8d4
		return NO;
Packit 1fb8d4
    return YES;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
- (NSString*)tableView:(UITableView*)tableView titleForHeaderInSection:(NSInteger)section
Packit 1fb8d4
{
Packit 1fb8d4
	if(section == SECTION_SESSIONS && [_active_sessions count] > 0)
Packit 1fb8d4
		return NSLocalizedString(@"My Sessions", @"'My Session': section sessions header");
Packit 1fb8d4
	if(section == SECTION_BOOKMARKS)
Packit 1fb8d4
		return NSLocalizedString(@"Manual Connections", @"'Manual Connections': section manual bookmarks header");
Packit 1fb8d4
	return nil;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
- (NSString*)tableView:(UITableView*)tableView titleForFooterInSection:(NSInteger)section
Packit 1fb8d4
{
Packit 1fb8d4
	return nil;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
Packit 1fb8d4
{
Packit 1fb8d4
    if([indexPath section] == SECTION_SESSIONS)
Packit 1fb8d4
        return 72;
Packit 1fb8d4
    return [tableView rowHeight];
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
#pragma mark -
Packit 1fb8d4
#pragma mark Table view delegate
Packit 1fb8d4
Packit 1fb8d4
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
Packit 1fb8d4
{
Packit 1fb8d4
    [super setEditing:editing animated:animated];
Packit 1fb8d4
    [[self tableView] setEditing:editing animated:animated];
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
- (void)accessoryButtonTapped:(UIControl*)button withEvent:(UIEvent*)event
Packit 1fb8d4
{
Packit 1fb8d4
    // forward a tap on our custom accessory button to the real accessory button handler
Packit 1fb8d4
    NSIndexPath* indexPath = [[self tableView] indexPathForRowAtPoint:[[[event touchesForView:button] anyObject] locationInView:[self tableView]]];
Packit 1fb8d4
    if (indexPath == nil)
Packit 1fb8d4
        return;
Packit 1fb8d4
    
Packit 1fb8d4
    [[[self tableView] delegate] tableView:[self tableView] accessoryButtonTappedForRowWithIndexPath:indexPath];
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
Packit 1fb8d4
{    
Packit 1fb8d4
    if([indexPath section] == SECTION_SESSIONS)
Packit 1fb8d4
    {
Packit 1fb8d4
        // resume session
Packit 1fb8d4
        RDPSession* session = [_active_sessions objectAtIndex:[indexPath row]];
Packit 1fb8d4
        UIViewController* ctrl = [[[RDPSessionViewController alloc] initWithNibName:@"RDPSessionView" bundle:nil session:session] autorelease];
Packit 1fb8d4
        [ctrl setHidesBottomBarWhenPushed:YES];
Packit 1fb8d4
        [[self navigationController] pushViewController:ctrl animated:YES];
Packit 1fb8d4
    }
Packit 1fb8d4
	else
Packit 1fb8d4
	{
Packit 1fb8d4
		ComputerBookmark* bookmark = nil;
Packit 1fb8d4
		if([indexPath section] == SECTION_BOOKMARKS)
Packit 1fb8d4
		{
Packit 1fb8d4
            // first row has either quick connect or add bookmark item
Packit 1fb8d4
            if([indexPath row] == 0)
Packit 1fb8d4
            {
Packit 1fb8d4
                if ([[_searchBar text] length] == 0)
Packit 1fb8d4
                {                    
Packit 1fb8d4
                    // show add bookmark controller
Packit 1fb8d4
                    ComputerBookmark *bookmark = [[[ComputerBookmark alloc] initWithBaseDefaultParameters] autorelease];
Packit 1fb8d4
                    BookmarkEditorController* bookmarkEditorController = [[[BookmarkEditorController alloc] initWithBookmark:bookmark] autorelease];
Packit 1fb8d4
                    [bookmarkEditorController setTitle:NSLocalizedString(@"Add Connection", @"Add Connection title")];
Packit 1fb8d4
                    [bookmarkEditorController setDelegate:self];
Packit 1fb8d4
                    [bookmarkEditorController setHidesBottomBarWhenPushed:YES];
Packit 1fb8d4
                    [[self navigationController] pushViewController:bookmarkEditorController animated:YES];
Packit 1fb8d4
                }
Packit 1fb8d4
                else
Packit 1fb8d4
                {
Packit 1fb8d4
                    // create a quick connect bookmark and add an entry to the quick connect history (if not already in the history)
Packit 1fb8d4
                    bookmark = [self bookmarkForQuickConnectTo:[_searchBar text]];     
Packit 1fb8d4
                    if (![_connection_history containsObject:[_searchBar text]])
Packit 1fb8d4
                    {
Packit 1fb8d4
                        [_connection_history addObject:[_searchBar text]];
Packit 1fb8d4
                        [self scheduleWriteConnectionHistoryToDataStore];                        
Packit 1fb8d4
                    }
Packit 1fb8d4
                }
Packit 1fb8d4
            }
Packit 1fb8d4
            else
Packit 1fb8d4
            {
Packit 1fb8d4
                if(_manual_search_result != nil)
Packit 1fb8d4
                {
Packit 1fb8d4
                    if ([self isIndexPathToHistoryItem:indexPath])
Packit 1fb8d4
                    {
Packit 1fb8d4
                        // create a quick connect bookmark for a history item
Packit 1fb8d4
                        NSString* item = [_history_search_result objectAtIndex:[self historyIndexFromIndexPath:indexPath]];
Packit 1fb8d4
                        bookmark = [self bookmarkForQuickConnectTo:item];
Packit 1fb8d4
                    }
Packit 1fb8d4
                    else
Packit 1fb8d4
                        bookmark = [[_manual_search_result objectAtIndex:[self bookmarkIndexFromIndexPath:indexPath]] valueForKey:@"bookmark"];                    
Packit 1fb8d4
                }
Packit 1fb8d4
                else
Packit 1fb8d4
                    bookmark = [_manual_bookmarks objectAtIndex:[self bookmarkIndexFromIndexPath:indexPath]]; // -1 because of ADD BOOKMARK entry
Packit 1fb8d4
            }
Packit 1fb8d4
            
Packit 1fb8d4
            // set reachability status
Packit 1fb8d4
            WakeUpWWAN();
Packit 1fb8d4
            [bookmark setConntectedViaWLAN:[[Reachability reachabilityWithHostName:[[bookmark params] StringForKey:@"hostname"]] currentReachabilityStatus] == ReachableViaWiFi];            
Packit 1fb8d4
		}
Packit 1fb8d4
		
Packit 1fb8d4
		if(bookmark != nil)
Packit 1fb8d4
		{	            
Packit 1fb8d4
			// create rdp session
Packit 1fb8d4
            RDPSession* session = [[[RDPSession alloc] initWithBookmark:bookmark] autorelease];
Packit 1fb8d4
            UIViewController* ctrl = [[[RDPSessionViewController alloc] initWithNibName:@"RDPSessionView" bundle:nil session:session] autorelease];
Packit 1fb8d4
            [ctrl setHidesBottomBarWhenPushed:YES];
Packit 1fb8d4
            [[self navigationController] pushViewController:ctrl animated:YES];
Packit 1fb8d4
            [_active_sessions addObject:session];
Packit 1fb8d4
        }
Packit 1fb8d4
	}
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
Packit 1fb8d4
- (void)tableView:(UITableView*)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath*)indexPath
Packit 1fb8d4
{    
Packit 1fb8d4
	// get the bookmark
Packit 1fb8d4
    NSString* bookmark_editor_title = NSLocalizedString(@"Edit Connection", @"Edit Connection title");
Packit 1fb8d4
	ComputerBookmark* bookmark = nil;
Packit 1fb8d4
	if ([indexPath section] == SECTION_BOOKMARKS)
Packit 1fb8d4
	{
Packit 1fb8d4
        if ([indexPath row] == 0)
Packit 1fb8d4
        {
Packit 1fb8d4
            // create a new bookmark and init hostname and label
Packit 1fb8d4
            bookmark = [self bookmarkForQuickConnectTo:[_searchBar text]];     
Packit 1fb8d4
            bookmark_editor_title = NSLocalizedString(@"Add Connection", @"Add Connection title");
Packit 1fb8d4
        }
Packit 1fb8d4
        else
Packit 1fb8d4
        {
Packit 1fb8d4
            if (_manual_search_result != nil)
Packit 1fb8d4
            {
Packit 1fb8d4
                if ([self isIndexPathToHistoryItem:indexPath])
Packit 1fb8d4
                {
Packit 1fb8d4
                    // create a new bookmark and init hostname and label
Packit 1fb8d4
                    NSString* item = [_history_search_result objectAtIndex:[self historyIndexFromIndexPath:indexPath]];
Packit 1fb8d4
                    bookmark = [self bookmarkForQuickConnectTo:item];     
Packit 1fb8d4
                    bookmark_editor_title = NSLocalizedString(@"Add Connection", @"Add Connection title");
Packit 1fb8d4
                }
Packit 1fb8d4
                else
Packit 1fb8d4
                    bookmark = [[_manual_search_result objectAtIndex:[self bookmarkIndexFromIndexPath:indexPath]] valueForKey:@"bookmark"];
Packit 1fb8d4
            }
Packit 1fb8d4
            else
Packit 1fb8d4
                bookmark = [_manual_bookmarks objectAtIndex:[self bookmarkIndexFromIndexPath:indexPath]];	// -1 because of ADD BOOKMARK entry
Packit 1fb8d4
        }
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
    // bookmark found? - start the editor
Packit 1fb8d4
	if (bookmark != nil)
Packit 1fb8d4
	{
Packit 1fb8d4
		BookmarkEditorController* editBookmarkController = [[[BookmarkEditorController alloc] initWithBookmark:bookmark] autorelease];
Packit 1fb8d4
        [editBookmarkController setHidesBottomBarWhenPushed:YES];
Packit 1fb8d4
        [editBookmarkController setTitle:bookmark_editor_title];
Packit 1fb8d4
        [editBookmarkController setDelegate:self];
Packit 1fb8d4
		[[self navigationController] pushViewController:editBookmarkController animated:YES];	
Packit 1fb8d4
	}
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
#pragma mark -
Packit 1fb8d4
#pragma mark Search Bar Delegates
Packit 1fb8d4
Packit 1fb8d4
- (BOOL)searchBarShouldBeginEditing:(UISearchBar*)searchBar
Packit 1fb8d4
{
Packit 1fb8d4
	// show cancel button
Packit 1fb8d4
	[searchBar setShowsCancelButton:YES animated:YES];
Packit 1fb8d4
	return YES;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
Packit 1fb8d4
{
Packit 1fb8d4
	// clear search result
Packit 1fb8d4
	[_manual_search_result release];
Packit 1fb8d4
	_manual_search_result = nil;
Packit 1fb8d4
	
Packit 1fb8d4
	// clear text and remove cancel button
Packit 1fb8d4
	[searchBar setText:@""];
Packit 1fb8d4
	[searchBar resignFirstResponder];
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
- (BOOL)searchBarShouldEndEditing:(UISearchBar*)searchBar
Packit 1fb8d4
{	
Packit 1fb8d4
	[searchBar setShowsCancelButton:NO animated:YES];
Packit 1fb8d4
	
Packit 1fb8d4
	// re-enable table selection
Packit 1fb8d4
	[_tableView setAllowsSelection:YES];
Packit 1fb8d4
	
Packit 1fb8d4
	return YES;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
Packit 1fb8d4
{
Packit 1fb8d4
	[_searchBar resignFirstResponder];
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
- (void)searchBar:(UISearchBar*)searchBar textDidChange:(NSString*)searchText
Packit 1fb8d4
{
Packit 1fb8d4
    [self performSearch:searchText];
Packit 1fb8d4
	[_tableView reloadData];
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
#pragma mark - Session handling
Packit 1fb8d4
Packit 1fb8d4
// session was added
Packit 1fb8d4
- (void)sessionDisconnected:(NSNotification*)notification
Packit 1fb8d4
{
Packit 1fb8d4
    // remove session from active sessions
Packit 1fb8d4
    RDPSession* session = (RDPSession*)[notification object];
Packit 1fb8d4
    [_active_sessions removeObject:session];
Packit 1fb8d4
Packit 1fb8d4
    // if this view is currently active refresh entries
Packit 1fb8d4
    if([[self navigationController] visibleViewController] == self)
Packit 1fb8d4
        [_tableView reloadSections:[NSIndexSet indexSetWithIndex:SECTION_SESSIONS] withRowAnimation:UITableViewRowAnimationNone];	
Packit 1fb8d4
    
Packit 1fb8d4
    // if session's bookmark is not in the bookmark list ask the user if he wants to add it
Packit 1fb8d4
    // (this happens if the session is created using the quick connect feature)
Packit 1fb8d4
    if (![_manual_bookmarks containsObject:[session bookmark]])
Packit 1fb8d4
    {
Packit 1fb8d4
        // retain the bookmark in case we want to save it later
Packit 1fb8d4
        _temporary_bookmark = [[session bookmark] retain];
Packit 1fb8d4
        
Packit 1fb8d4
        // ask the user if he wants to save the bookmark
Packit 1fb8d4
        NSString* title = NSLocalizedString(@"Save Connection Settings?", @"Save connection settings title");
Packit 1fb8d4
        NSString* message = NSLocalizedString(@"Your Connection Settings have not been saved. Do you want to save them?", @"Save connection settings message");
Packit 1fb8d4
        BlockAlertView* alert = [BlockAlertView alertWithTitle:title message:message];
Packit 1fb8d4
        [alert setCancelButtonWithTitle:NSLocalizedString(@"No", @"No Button") block:nil];
Packit 1fb8d4
        [alert addButtonWithTitle:NSLocalizedString(@"Yes", @"Yes Button") block:^{
Packit 1fb8d4
            if (_temporary_bookmark)
Packit 1fb8d4
            {
Packit 1fb8d4
                [_manual_bookmarks addObject:_temporary_bookmark];
Packit 1fb8d4
                [_tableView reloadSections:[NSIndexSet indexSetWithIndex:SECTION_BOOKMARKS] withRowAnimation:UITableViewRowAnimationNone];
Packit 1fb8d4
                [_temporary_bookmark autorelease];
Packit 1fb8d4
                _temporary_bookmark = nil;
Packit 1fb8d4
            }
Packit 1fb8d4
        }];
Packit 1fb8d4
        [alert show];
Packit 1fb8d4
    }
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
- (void)sessionFailedToConnect:(NSNotification*)notification
Packit 1fb8d4
{
Packit 1fb8d4
    // remove session from active sessions
Packit 1fb8d4
    RDPSession* session = (RDPSession*)[notification object];
Packit 1fb8d4
    [_active_sessions removeObject:session];    
Packit 1fb8d4
    
Packit 1fb8d4
    // display error toast
Packit 1fb8d4
    [[self view] makeToast:NSLocalizedString(@"Failed to connect to session!", @"Failed to connect error message") duration:ToastDurationNormal position:@"center"];
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
#pragma mark - Reachability notification
Packit 1fb8d4
- (void)reachabilityChanged:(NSNotification*)notification
Packit 1fb8d4
{
Packit 1fb8d4
    // no matter how the network changed - we will disconnect
Packit 1fb8d4
    // disconnect session (if there is any)
Packit 1fb8d4
    if ([_active_sessions count] > 0)
Packit 1fb8d4
    {
Packit 1fb8d4
        RDPSession* session = [_active_sessions objectAtIndex:0];
Packit 1fb8d4
        [session disconnect];
Packit 1fb8d4
    }
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
#pragma mark - BookmarkEditorController delegate
Packit 1fb8d4
Packit 1fb8d4
- (void)commitBookmark:(ComputerBookmark *)bookmark
Packit 1fb8d4
{
Packit 1fb8d4
    // if we got a manual bookmark that is not in the list yet - add it otherwise replace it
Packit 1fb8d4
    BOOL found = NO;
Packit 1fb8d4
    for (int idx = 0; idx < [_manual_bookmarks count]; ++idx)
Packit 1fb8d4
    {
Packit 1fb8d4
        if ([[bookmark uuid] isEqualToString:[[_manual_bookmarks objectAtIndex:idx] uuid]])
Packit 1fb8d4
        {
Packit 1fb8d4
            [_manual_bookmarks replaceObjectAtIndex:idx withObject:bookmark];
Packit 1fb8d4
            found = YES;
Packit 1fb8d4
            break;
Packit 1fb8d4
        }
Packit 1fb8d4
    }
Packit 1fb8d4
    if (!found)
Packit 1fb8d4
        [_manual_bookmarks addObject:bookmark];
Packit 1fb8d4
    
Packit 1fb8d4
    // remove any quick connect history entry with the same hostname
Packit 1fb8d4
    NSString* hostname = [[bookmark params] StringForKey:@"hostname"];
Packit 1fb8d4
    if ([_connection_history containsObject:hostname])
Packit 1fb8d4
    {
Packit 1fb8d4
        [_connection_history removeObject:hostname];
Packit 1fb8d4
        [self scheduleWriteConnectionHistoryToDataStore];
Packit 1fb8d4
    }
Packit 1fb8d4
        
Packit 1fb8d4
    [self scheduleWriteManualBookmarksToDataStore];
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
- (IBAction)disconnectButtonPressed:(id)sender
Packit 1fb8d4
{
Packit 1fb8d4
    // disconnect session and refresh table view
Packit 1fb8d4
    RDPSession* session = [_active_sessions objectAtIndex:[sender tag]];
Packit 1fb8d4
    [session disconnect];
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
#pragma mark - Misc functions
Packit 1fb8d4
Packit 1fb8d4
- (BOOL)hasNoBookmarks
Packit 1fb8d4
{
Packit 1fb8d4
    return ([_manual_bookmarks count] == 0);
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
- (UIButton*)disclosureButtonWithImage:(UIImage*)image
Packit 1fb8d4
{
Packit 1fb8d4
    // we make the button a little bit bigger (image widht * 2, height + 10) so that the user doesn't accidentally connect to the bookmark ...
Packit 1fb8d4
    UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];  
Packit 1fb8d4
    [button setFrame:CGRectMake(0, 0, [image size].width * 2, [image size].height + 10)];
Packit 1fb8d4
    [button setImage:image forState:UIControlStateNormal];
Packit 1fb8d4
    [button addTarget:self action:@selector(accessoryButtonTapped:withEvent:) forControlEvents:UIControlEventTouchUpInside];    
Packit 1fb8d4
    [button setUserInteractionEnabled:YES];
Packit 1fb8d4
    return button;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
- (void)performSearch:(NSString*)searchText
Packit 1fb8d4
{
Packit 1fb8d4
    [_manual_search_result autorelease];
Packit 1fb8d4
    
Packit 1fb8d4
	if([searchText length] > 0)
Packit 1fb8d4
	{
Packit 1fb8d4
		_manual_search_result = [FilterBookmarks(_manual_bookmarks, [searchText componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]) retain];
Packit 1fb8d4
        _history_search_result = [FilterHistory(_connection_history, searchText) retain];
Packit 1fb8d4
    }
Packit 1fb8d4
	else
Packit 1fb8d4
	{
Packit 1fb8d4
        _history_search_result = nil;
Packit 1fb8d4
		_manual_search_result = nil;
Packit 1fb8d4
	}    
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
- (int)bookmarkIndexFromIndexPath:(NSIndexPath*)indexPath
Packit 1fb8d4
{
Packit 1fb8d4
    return [indexPath row] - ((_history_search_result != nil) ? [_history_search_result count] : 0) - 1;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
- (int)historyIndexFromIndexPath:(NSIndexPath*)indexPath
Packit 1fb8d4
{
Packit 1fb8d4
    return [indexPath row] - 1;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
- (BOOL)isIndexPathToHistoryItem:(NSIndexPath*)indexPath
Packit 1fb8d4
{
Packit 1fb8d4
    return (([indexPath row] - 1) < [_history_search_result count]);
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
- (ComputerBookmark*)bookmarkForQuickConnectTo:(NSString*)host
Packit 1fb8d4
{
Packit 1fb8d4
    ComputerBookmark* bookmark = [[[ComputerBookmark alloc] initWithBaseDefaultParameters] autorelease];
Packit 1fb8d4
    [bookmark setLabel:host]; 
Packit 1fb8d4
    [[bookmark params] setValue:host forKey:@"hostname"];                
Packit 1fb8d4
    return bookmark;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
#pragma mark - Persisting bookmarks
Packit 1fb8d4
Packit 1fb8d4
- (void)scheduleWriteBookmarksToDataStore
Packit 1fb8d4
{
Packit 1fb8d4
	[[NSOperationQueue mainQueue] addOperationWithBlock:^{
Packit 1fb8d4
		[self writeBookmarksToDataStore];
Packit 1fb8d4
	}];
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
- (void)writeBookmarksToDataStore
Packit 1fb8d4
{    
Packit 1fb8d4
    [self writeManualBookmarksToDataStore];
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
- (void)scheduleWriteManualBookmarksToDataStore
Packit 1fb8d4
{
Packit 1fb8d4
	[[NSOperationQueue mainQueue] addOperation:[[[NSInvocationOperation alloc] initWithTarget:self selector:@selector(writeManualBookmarksToDataStore) object:nil] autorelease]];
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
- (void)writeManualBookmarksToDataStore
Packit 1fb8d4
{    
Packit 1fb8d4
    [self writeArray:_manual_bookmarks toDataStoreURL:[self manualBookmarksDataStoreURL]];
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
- (void)scheduleWriteConnectionHistoryToDataStore
Packit 1fb8d4
{
Packit 1fb8d4
	[[NSOperationQueue mainQueue] addOperation:[[[NSInvocationOperation alloc] initWithTarget:self selector:@selector(writeConnectionHistoryToDataStore) object:nil] autorelease]];
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
- (void)writeConnectionHistoryToDataStore
Packit 1fb8d4
{    
Packit 1fb8d4
    [self writeArray:_connection_history toDataStoreURL:[self connectionHistoryDataStoreURL]];
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
- (void)writeArray:(NSArray*)bookmarks toDataStoreURL:(NSURL*)url
Packit 1fb8d4
{
Packit 1fb8d4
	NSData* archived_data = [NSKeyedArchiver archivedDataWithRootObject:bookmarks];
Packit 1fb8d4
	[archived_data writeToURL:url atomically:YES];    
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
- (void)readManualBookmarksFromDataStore
Packit 1fb8d4
{
Packit 1fb8d4
    [_manual_bookmarks autorelease];
Packit 1fb8d4
    _manual_bookmarks = [self arrayFromDataStoreURL:[self manualBookmarksDataStoreURL]];
Packit 1fb8d4
Packit 1fb8d4
    if(_manual_bookmarks == nil)
Packit 1fb8d4
    {
Packit 1fb8d4
        _manual_bookmarks = [[NSMutableArray alloc] init];
Packit 1fb8d4
        [_manual_bookmarks addObject:[[[GlobalDefaults sharedGlobalDefaults] newTestServerBookmark] autorelease]];
Packit 1fb8d4
    }
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
- (void)readConnectionHistoryFromDataStore
Packit 1fb8d4
{
Packit 1fb8d4
    [_connection_history autorelease];
Packit 1fb8d4
    _connection_history = [self arrayFromDataStoreURL:[self connectionHistoryDataStoreURL]];
Packit 1fb8d4
    
Packit 1fb8d4
    if(_connection_history == nil)
Packit 1fb8d4
        _connection_history = [[NSMutableArray alloc] init];
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
- (NSMutableArray*)arrayFromDataStoreURL:(NSURL*)url
Packit 1fb8d4
{
Packit 1fb8d4
	NSData* archived_data = [NSData dataWithContentsOfURL:url];
Packit 1fb8d4
	
Packit 1fb8d4
	if (!archived_data)
Packit 1fb8d4
		return nil;
Packit 1fb8d4
    
Packit 1fb8d4
	return [[NSKeyedUnarchiver unarchiveObjectWithData:archived_data] retain];
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
- (NSURL*)manualBookmarksDataStoreURL
Packit 1fb8d4
{
Packit 1fb8d4
	return [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%@", [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject], @"com.freerdp.ifreerdp.bookmarks.plist"]];
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
- (NSURL*)connectionHistoryDataStoreURL
Packit 1fb8d4
{
Packit 1fb8d4
	return [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%@", [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject], @"com.freerdp.ifreerdp.connection_history.plist"]];
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
@end
Packit 1fb8d4