1/* 2 Project: Cartotheque 3 4 Copyright (C) 2005 Stefan Urbanek 5 6 Author : Stefan Urbanek 7 Created: 2005-01-27 8 License: GNU LGPL 2.1 9*/ 10 11#import "Cartotheque.h" 12 13#import <Foundation/NSAttributedString.h> 14#import <Foundation/NSException.h> 15#import <Foundation/NSNotification.h> 16#import <Foundation/NSString.h> 17 18#import "Card.h" 19#import "CardRepository.h" 20 21NSString *CardCreatedNotification = @"CardCreatedNotification"; 22NSString *CardEditedNotification = @"CardEditedNotification"; 23 24@interface Cartotheque(CartothequePrivate) 25- (Card *)_cardWithIdentifier:(id)identifier; 26- (Card *)_fetchCardWithIdentifier:(id)identifier; 27@end 28 29@implementation Cartotheque 30- initWithRepository:(CardRepository *)aRepository 31{ 32 self = [super init]; 33 34 repository = RETAIN(aRepository); 35 36 return self; 37} 38- (Card *)homeCard 39{ 40 return [self _cardWithIdentifier:[repository homeCard]]; 41} 42 43- (Card *)createCard 44{ 45 NSAttributedString *contents; 46 Card *card; 47 id ident; 48 49 //NSLog(@"1 create in repository"); 50 ident = [repository createCard]; 51 52 //NSLog(@"2 alloc card"); 53 card = [[Card alloc] initWithRepository:repository 54 identifier:ident]; 55 56 //NSLog(@"3 set contents"); 57 /* FIXME: use some default context */ 58 contents = [[NSAttributedString alloc] init]; 59 [card setContents:contents]; 60 AUTORELEASE(contents); 61 //NSLog(@"4 done"); 62 63 /* FIXME: post card that was created as user info */ 64 [[NSNotificationCenter defaultCenter] 65 postNotificationName:CardCreatedNotification 66 object:self]; 67 68 return AUTORELEASE(card); 69} 70- (void)deleteCards:(NSArray *)cards 71{ 72 NSEnumerator *enumerator; 73 Card *card; 74 75 /* FIXME: use card deleted notification, as this is misleading 76 we just reuse it here temporarily */ 77 enumerator = [cards objectEnumerator]; 78 79 while( (card = [enumerator nextObject]) ) 80 { 81 [self deleteCard:card]; 82 } 83 [[NSNotificationCenter defaultCenter] 84 postNotificationName:CardCreatedNotification 85 object:self]; 86} 87- (void)deleteCard:(Card *)card 88{ 89 [fetchedCards removeObjectForKey:[card identifier]]; 90 [changedCards removeObject:card]; 91 [repository deleteCard:[card identifier]]; 92 [card invalidate]; 93} 94- (NSArray *)allCards 95{ 96 NSMutableArray *array; 97 NSEnumerator *enumerator; 98 id identifier; 99 100 array = [NSMutableArray array]; 101 102 enumerator = [[repository allCards] objectEnumerator]; 103 104 while( (identifier = [enumerator nextObject]) ) 105 { 106 [array addObject:[self _cardWithIdentifier:identifier]]; 107 } 108 109 return [NSArray arrayWithArray:array]; 110} 111- (Card *)_cardWithIdentifier:(id)identifier 112{ 113 Card *card; 114 card = [fetchedCards objectForKey:identifier]; 115 if(!card) 116 { 117 card = [self _fetchCardWithIdentifier:identifier]; 118 } 119 return card; 120} 121- (Card *)_fetchCardWithIdentifier:(id)identifier 122{ 123 Card *card; 124 125 card = [[Card alloc] initWithRepository:repository 126 identifier:identifier]; 127 if(!fetchedCards) 128 { 129 fetchedCards = [[NSMutableDictionary alloc] init]; 130 } 131 132 [fetchedCards setObject:AUTORELEASE(card) forKey:identifier]; 133 134 return card; 135} 136- (void)commitChanges 137{ 138 NSEnumerator *enumerator; 139 Card *card; 140 id ident; 141 NSLog(@" Commiting changes to %i cards", [changedCards count]); 142 enumerator = [changedCards objectEnumerator]; 143 while( (card = [enumerator nextObject]) ) 144 { 145 ident = [card identifier]; 146 147 [repository setContents:[card contents] forCard:ident]; 148 [repository setInfo:[card info] forCard:ident]; 149 } 150 151 [changedCards removeAllObjects]; 152} 153 154- (void)touchCard:(Card *)card 155{ 156 if(card) 157 { 158 if(!changedCards) 159 { 160 changedCards = [[NSMutableSet alloc] init]; 161 } 162 [changedCards addObject:card]; 163 } 164 else 165 { 166 NSLog(@"Warning: nil card to touch. Ignoring."); 167 } 168} 169@end 170/* BEGIN Generated by DevelKit */ 171 172@implementation Cartotheque (DKGeneratedMethods) 173/* Accessor methods */ 174 175- (CardRepository *)repository 176{ 177 return repository; 178} 179- (void)dealloc 180{ 181 RELEASE(changedCards); 182 183 [super dealloc]; 184} 185 186/* Encoding methods */ 187- (void)encodeWithCoder:(NSCoder *)coder 188{ 189 // NSLog(@"Encoding class %@", [self className]); 190 if ( [coder allowsKeyedCoding] ) 191 { 192 [coder encodeObject:changedCards forKey:@"changedCards"]; 193 [coder encodeObject:repository forKey:@"repository"]; 194 } 195 else 196 { 197 [coder encodeValueOfObjCType: @encode(NSMutableSet *) at: &changedCards]; 198 [coder encodeValueOfObjCType: @encode(CardRepository *) at: &repository]; 199 } 200} 201- initWithCoder:(NSCoder *)decoder 202{ 203 self = [super init]; 204 if ( [decoder allowsKeyedCoding] ) 205 { 206 changedCards = [decoder decodeObjectForKey:@"changedCards"]; 207 repository = [decoder decodeObjectForKey:@"repository"]; 208 } 209 else 210 { 211 [decoder decodeValueOfObjCType: @encode(NSMutableSet *) at: &changedCards]; 212 [decoder decodeValueOfObjCType: @encode(CardRepository *) at: &repository]; 213 } 214 215 RETAIN(changedCards); 216 217 return self; 218} 219 220@end 221/* END Generated by DevelKit */ 222