/
Source.m
117 lines (90 loc) · 3.45 KB
/
Source.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
//
// Source.m
// Noise
//
// Created by Joshua Bassett on 1/12/09.
// Copyright 2009 CLEAR Interactive. All rights reserved.
//
#import "Source.h"
#import "Message.h"
@interface Source (Private)
- (void)contextDidSave:(NSNotification *)notification;
- (BOOL)messageExistsWithID:(NSString *)id;
- (void)saveAction;
@end
@implementation Source
@synthesize managedObjectContext, delegate, enabled;
- (id)initWithDelegate:(id <SourceDelegate>)aDelegate {
if (self = [super init]) {
delegate = aDelegate;
enabled = YES;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(contextDidSave:)
name:NSManagedObjectContextDidSaveNotification
object:managedObjectContext];
}
return self;
}
- (NSManagedObjectContext *)managedObjectContext {
if (managedObjectContext) return managedObjectContext;
NSPersistentStoreCoordinator *coordinator = [delegate persistentStoreCoordinator];
managedObjectContext = [[NSManagedObjectContext alloc] init];
[managedObjectContext setPersistentStoreCoordinator:coordinator];
return managedObjectContext;
}
- (NSString *)name {
return [[self class] description];
}
- (NSString *)fullName {
Class sourceClass = [self class];
NSBundle *sourceBundle = [NSBundle bundleForClass:sourceClass];
return [sourceBundle bundleIdentifier];
}
- (NSImage *)icon {
return [NSImage imageNamed:@"NSApplicationIcon"];
}
- (void)update {
NSLog(@"Updating %@ source...", [[self class] description]);
}
- (void)messageReceivedWithID:(NSString *)id title:(NSString *)title content:(NSString *)content received:(NSDate *)received priority:(int)priority sticky:(BOOL)sticky {
if ([self messageExistsWithID:id]) return;
Message *message = [NSEntityDescription insertNewObjectForEntityForName:MESSAGE_ENTITY_NAME inManagedObjectContext:[self managedObjectContext]];
[message setSource:[self fullName]];
[message setId:id];
[message setTitle:title];
[message setContent:content];
[message setReceived:received];
[message setPriority:priority];
[message setSticky:sticky];
[self saveAction];
}
#pragma mark -
#pragma mark Private methods
- (void)contextDidSave:(NSNotification *)notification {
[[delegate managedObjectContext] performSelectorOnMainThread:@selector(mergeChangesFromContextDidSaveNotification:)
withObject:notification
waitUntilDone:YES];
}
- (BOOL)messageExistsWithID:(NSString *)id {
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:MESSAGE_ENTITY_NAME inManagedObjectContext:[self managedObjectContext]]];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"source == %@ AND id == %@", [self fullName], id];
[request setPredicate:predicate];
NSError *error = nil;
NSArray *results = [[self managedObjectContext] executeFetchRequest:request error:&error];
if (error) {
[NSApp presentError:error];
return NO;
}
return [results count] > 0;
}
- (void)saveAction {
NSError *error = nil;
if (![[self managedObjectContext] commitEditing]) {
NSLog(@"%@:%s unable to commit editing before saving", [self class], _cmd);
}
if (![[self managedObjectContext] save:&error]) {
[[NSApplication sharedApplication] presentError:error];
}
}
@end