-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLentEntry.m
139 lines (115 loc) · 4.06 KB
/
LentEntry.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*-
* Copyright 2011 os-cillation GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#import <AddressBook/AddressBook.h>
#import "Database.h"
#import "LentEntry.h"
#import "NSDateFormatter+DateFormatter.h"
@interface LentEntry ()
- (void)generateTextData;
@end
@implementation LentEntry
#pragma mark -
#pragma mark Synthesized properties
@synthesize entryId = _entryId;
@synthesize type = _type;
@synthesize description = _description;
@synthesize description2 = _description2;
@synthesize person = _person;
@synthesize date = _date;
@synthesize returnDate = _returnDate;
@synthesize firstLine = _firstLine;
@synthesize secondLine = _secondLine;
@synthesize personName = _personName;
@synthesize pushAlarm = _pushAlarm;
#pragma mark -
#pragma mark Constructors and destructors
- (void)dealloc
{
[_entryId release], _entryId = nil;
[_type release], _type = nil;
[_description release], _description = nil;
[_description2 release], _description2 = nil;
[_person release], _person = nil;
[_date release], _date = nil;
[_returnDate release], _returnDate = nil;
[_firstLine release], _firstLine = nil;
[_secondLine release], _secondLine = nil;
[_personName release], _personName = nil;
[_pushAlarm release], _pushAlarm = nil;
[super dealloc];
}
#pragma mark -
#pragma mark Derived properties
- (NSString *)dateString
{
return self.date ? [[NSDateFormatter dateFormatterForShortStyle] stringFromDate:self.date] : @"";
}
- (NSString *)returnDateString
{
return self.returnDate ? [[NSDateFormatter dateFormatterForShortStyle] stringFromDate:self.returnDate] : @"";
}
#pragma mark -
#pragma mark Database operations
- (void)generateOutgoingText
{
[self generateTextData];
[Database addOutgoingText:self.entryId withFirstLine:self.firstLine withSecondLine:self.secondLine withPerson:self.personName];
}
- (void)generateIncomingText
{
[self generateTextData];
[Database addIncomingText:self.entryId withFirstLine:self.firstLine withSecondLine:self.secondLine withPerson:self.personName];
}
#pragma mark -
#pragma mark Private methods
- (void)generateTextData
{
if ([self.description length] && [self.description2 length]) {
self.firstLine = [NSString stringWithFormat:@"%@ - %@", self.description, self.description2];
}
else if ([self.description length]) {
self.firstLine = self.description;
}
else {
self.firstLine = [NSString stringWithFormat:@"%@", self.description2];
}
ABAddressBookRef addressBook = ABAddressBookCreate();
if (addressBook) {
ABRecordRef person = ABAddressBookGetPersonWithRecordID(addressBook, [self.person intValue]);
NSString *fullName = @"";
if (person) {
self.personName = [(NSString *)ABRecordCopyCompositeName(person) autorelease];
fullName = [NSString stringWithFormat:@"%@ %@", NSLocalizedString(@"to", @""), self.personName];
}
else if ([self.person length]) {
self.personName = self.person;
fullName = [NSString stringWithFormat:@"%@ %@", NSLocalizedString(@"to", @""), self.personName];
}
if (self.date) {
if ([fullName length]) {
self.secondLine = [NSString stringWithFormat:@"%@ %@, %@", NSLocalizedString(@"at", @""), self.dateString, fullName];
}
else {
self.secondLine = [NSString stringWithFormat:@"%@ %@", NSLocalizedString(@"at", @""), self.dateString];
}
}
else {
self.secondLine = fullName;
}
CFRelease(addressBook);
}
}
@end