forked from avh4/time-tracker-for-mac
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTTDocument.m
123 lines (100 loc) · 3.4 KB
/
TTDocument.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
//
// TTDocument.m
// Time Tracker
//
// Created by Aaron VonderHaar on 7/5/08.
// Copyright 2008 __MyCompanyName__. All rights reserved.
//
#import "TTDocument.h"
@implementation TTDocument
- (id) init
{
_projects = [[NSMutableArray alloc] init];
return self;
}
- (NSArray *) projects
{
return _projects;
}
- (void) setProjects:(NSArray *)projs
{
_projects = [[NSMutableArray alloc] initWithArray:projs];
}
- (void) addProject:(TProject *)proj
{
[_projects addObject:proj];
}
- (void) removeProject:(TProject *)proj
{
[_projects removeObject:proj];
}
- (id)objectInProjectsAtIndex:(int)index
{
return [_projects objectAtIndex:index];
}
- (void)moveProject:(TProject *)proj toIndex:(int)index
{
int oldIndex = [_projects indexOfObject:proj];
if (oldIndex == NSNotFound)
{
NSLog(@"TTDocument moveProject:toIndex: project was not found in the projects lists");
return;
}
[_projects insertObject:proj atIndex:index];
if (oldIndex >= index) oldIndex++;
[_projects removeObjectAtIndex:oldIndex];
}
- (NSData *)dataOfType:(NSString *)aType error:(NSError **)outError
{
NSMutableData* data = [[[NSMutableData alloc] init] autorelease];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc]
initWithDateFormat:@"%Y-%m-%d" allowNaturalLanguage:NO];
NSDateFormatter *timeFormat = [[NSDateFormatter alloc]
initWithDateFormat:@"%H:%M" allowNaturalLanguage:NO];
// Write the CSV column headers
[data appendData:[@"Date,Start time,End time,Duration (seconds),Project,Task\n" dataUsingEncoding:NSASCIIStringEncoding]];
NSEnumerator *proje = [_projects objectEnumerator];
TProject *proj;
while ( proj = [proje nextObject] )
{
NSEnumerator *taske = [[proj tasks] objectEnumerator];
TTask *task;
while (task = [taske nextObject])
{
NSEnumerator *wpe = [[task workPeriods] objectEnumerator];
TWorkPeriod *wp;
while (wp = [wpe nextObject])
{
// Date
[data appendData:[[dateFormat stringFromDate:[wp startTime]] dataUsingEncoding:NSASCIIStringEncoding]];
[data appendData:[@"," dataUsingEncoding:NSASCIIStringEncoding]];
// Start time
[data appendData:[[timeFormat stringFromDate:[wp startTime]] dataUsingEncoding:NSASCIIStringEncoding]];
[data appendData:[@"," dataUsingEncoding:NSASCIIStringEncoding]];
// End time
[data appendData:[[timeFormat stringFromDate:[wp endTime]] dataUsingEncoding:NSASCIIStringEncoding]];
[data appendData:[@"," dataUsingEncoding:NSASCIIStringEncoding]];
// Duration (seconds)
[data appendData:[[NSString stringWithFormat:@"%d", [wp totalTime]] dataUsingEncoding:NSASCIIStringEncoding]];
[data appendData:[@"," dataUsingEncoding:NSASCIIStringEncoding]];
// Project
[data appendData:[[proj name] dataUsingEncoding:NSASCIIStringEncoding]];
[data appendData:[@"," dataUsingEncoding:NSASCIIStringEncoding]];
// Task
[data appendData:[[task name] dataUsingEncoding:NSASCIIStringEncoding]];
[data appendData:[@"\n" dataUsingEncoding:NSASCIIStringEncoding]];
}
}
}
[dateFormat release];
[timeFormat release];
return data;
}
@end
// This initialization function gets called when we import the Ruby module.
// It doesn't need to do anything because the RubyCocoa bridge will do
// all the initialization work.
// The rbiphonetest test framework automatically generates bundles for
// each objective-c class containing the following line. These
// can be used by your tests.
void Init_TTDocument() { }