-
Notifications
You must be signed in to change notification settings - Fork 0
/
RememberDelegate.m
140 lines (110 loc) · 3.84 KB
/
RememberDelegate.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
140
//
// RememberDelegate.m
// Remember
//
// Created by Christoph on 25.05.2009.
// Copyright 2009-2010 Useless Coding. All rights reserved.
//
#import "RememberDelegate.h"
#import "UCCalendarList.h"
@implementation RememberDelegate
- (void)awakeFromNib
{
[tableView setDoubleAction:@selector(openEvent:)];
[tableView setTarget:self];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(eventsChanged:) name:CalEventsChangedExternallyNotification object:[CalCalendarStore defaultCalendarStore]];
timer = [[NSTimer scheduledTimerWithTimeInterval:30 target:self selector:@selector(timerFired:) userInfo:nil repeats:YES] retain];
statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength];
[statusItem retain];
[statusItem setImage:[NSImage imageNamed:@"TodayTemplate"]];
[statusItem setHighlightMode:YES];
[statusItem setMenu:statusMenu];
[self refresh:self];
}
- (void)dealloc
{
[timer invalidate];
[timer release];
[calendarList release];
[statusItem release];
[super dealloc];
}
#pragma mark -
- (BOOL)applicationOpenUntitledFile:(NSApplication *)theApplication
{
[window makeKeyAndOrderFront:self];
return YES;
}
#pragma mark Timer and Notification
- (void)timerFired:(NSTimer *)theTimer
{
[self refresh:self];
}
- (void)eventsChanged:(id)aNotification
{
[self refresh:self];
}
#pragma mark Actions
- (IBAction)refresh:(id)sender
{
[calendarList release];
NSPredicate * eventRange = [CalCalendarStore eventPredicateWithStartDate:
[[NSCalendarDate date] dateByAddingYears:0 months:0 days:-1 hours:0 minutes:0 seconds:0] endDate:
[[NSCalendarDate date] dateByAddingYears:0 months:0 days:7 hours:0 minutes:0 seconds:0] calendars:
[[CalCalendarStore defaultCalendarStore] calendars]];
calendarList = [[UCCalendarList alloc] initWithPredicate:eventRange];
switch(calendarList.activeCount) {
case 0: [[statusMenu itemAtIndex:0] setTitle:@"No Events"];
[[NSApp dockTile] setBadgeLabel:nil];
break;
case 1: [[statusMenu itemAtIndex:0] setTitle:@"One Event"];
[[NSApp dockTile] setBadgeLabel:@"1"];
break;
default: [[statusMenu itemAtIndex:0] setTitle:[NSString stringWithFormat:@"%d Events", calendarList.activeCount]];
[[NSApp dockTile] setBadgeLabel:[NSString stringWithFormat:@"%d", calendarList.activeCount]];
}
[tableView reloadData];
}
- (IBAction)openEvent:(id)sender
{
NSInteger row = [tableView clickedRow];
if([sender isKindOfClass:[NSTableView class]] && row!=-1 || [sender isKindOfClass:[NSMenuItem class]]) {
if(row==-1) { row=[tableView selectedRow]; }
NSLog(@"Öffnen: %@", [calendarList dictionaryAtIndex:row]);
}
}
#pragma mark Data Source
- (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView
{
return calendarList.count;
}
- (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex
{
return [[calendarList dictionaryAtIndex:rowIndex] objectForKey:[aTableColumn identifier]];
}
- (BOOL)tableView:(NSTableView *)tableView isGroupRow:(NSInteger)row
{
return [[calendarList dictionaryAtIndex:row] objectForKey:UCIsGroup]!=nil;
}
- (BOOL)tableView:(NSTableView *)aTableView shouldSelectRow:(NSInteger)rowIndex
{
return ![self tableView:aTableView isGroupRow:rowIndex];
}
#pragma mark Validation
- (BOOL)validateMenuItem:(NSMenuItem *)menuItem
{
if([menuItem action]==@selector(openEvent:)) {
return [tableView selectedRow]!=-1;
}
return YES;
}
#pragma mark Window Delegate
- (NSRect)windowWillUseStandardFrame:(NSWindow *)aWindow defaultFrame:(NSRect)defaultFrame
{
NSRect currentFrame = [aWindow frame];
CGFloat oldHeight=currentFrame.size.height;
currentFrame.size.height = currentFrame.size.height-[scrollView frame].size.height+[tableView frame].size.height;
currentFrame.origin.y = currentFrame.origin.y+oldHeight - currentFrame.size.height;
return currentFrame;
}
@end