forked from GetiPlayerAutomator/get-iplayer-automator
-
Notifications
You must be signed in to change notification settings - Fork 26
/
DownloadHistoryController.m
171 lines (157 loc) · 5.91 KB
/
DownloadHistoryController.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
//
// DownloadHistoryController.m
// Get_iPlayer GUI
//
// Created by Thomas Willson on 10/15/09.
// Copyright 2009 __MyCompanyName__. All rights reserved.
//
#import "DownloadHistoryController.h"
#import "NSFileManager+DirectoryLocations.h"
#import "Get_iPlayer_Automator-Swift.h"
@implementation DownloadHistoryController
- (void)awakeFromNib
{
[self readHistory];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addToHistory:) name:@"AddProgToHistory" object:nil];
}
- (void)readHistory
{
DDLogVerbose(@"Read History");
if ([historyArrayController.arrangedObjects count] > 0) {
[historyArrayController removeObjectsAtArrangedObjectIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, [historyArrayController.arrangedObjects count])]];
}
NSString *historyFilePath = [[[NSFileManager defaultManager] applicationSupportDirectory] stringByAppendingPathComponent:@"download_history"];
NSFileHandle *historyFile = [NSFileHandle fileHandleForReadingAtPath:historyFilePath];
NSData *historyFileData = [historyFile readDataToEndOfFile];
NSString *historyFileInfo = [[NSString alloc] initWithData:historyFileData encoding:NSUTF8StringEncoding];
if (historyFileInfo.length > 0)
{
NSArray *historyEntries = [historyFileInfo componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
for (NSString *entry in historyEntries) {
if (entry.length == 0) {
continue;
}
DownloadHistoryEntry *historyEntry = [DownloadHistoryEntry new];
NSArray *components = [entry componentsSeparatedByString:@"|"];
historyEntry.pid = components[0];
historyEntry.showName = components[1];
historyEntry.episodeName = components[2];
historyEntry.type = components[3];
historyEntry.someNumber = components[4];
historyEntry.downloadFormat = components[5];
historyEntry.downloadPath = components[6];
[historyArrayController addObject:historyEntry];
}
}
DDLogVerbose(@"End read history");
}
- (IBAction)writeHistory:(id)sender
{
if (!runDownloads || [sender isEqualTo:self])
{
DDLogVerbose(@"Write History to File");
NSArray *currentHistory = historyArrayController.arrangedObjects;
NSMutableString *historyString = [[NSMutableString alloc] init];
for (DownloadHistoryEntry *entry in currentHistory)
{
[historyString appendFormat:@"%@\n", [entry entryString]];
}
NSString *historyPath = [[[NSFileManager defaultManager] applicationSupportDirectory] stringByAppendingPathComponent:@"download_history"];
NSData *historyData = [historyString dataUsingEncoding:NSUTF8StringEncoding];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:historyPath])
{
if (![fileManager createFileAtPath:historyPath contents:historyData attributes:nil])
{
NSAlert *alert = [[NSAlert alloc] init];
alert.informativeText = @"Please submit a bug report saying that the history file could not be created.";
alert.messageText = @"Could not create history file!";
[alert addButtonWithTitle:@"OK"];
[alert runModal];
DDLogWarn(@"%@: Could not create history file!", self.description);
}
}
else
{
NSError *writeToFileError;
if (![historyData writeToFile:historyPath options:NSDataWritingAtomic error:&writeToFileError])
{
NSAlert *alert = [[NSAlert alloc] init];
alert.informativeText = @"Please submit a bug report saying that the history file could not be written to.";
alert.messageText = @"Could not write to history file!";
[alert addButtonWithTitle:@"OK"];
[alert runModal];
}
}
}
else
{
NSAlert *alert = [[NSAlert alloc] init];
alert.informativeText = @"Your changes have been discarded.";
alert.messageText = @"Download History cannot be edited while downloads are running.";
[alert addButtonWithTitle:@"OK"];
[alert runModal];
[historyWindow close];
}
[saveButton setEnabled:NO];
[historyWindow setDocumentEdited:NO];
}
-(IBAction)showHistoryWindow:(id)sender
{
if (!runDownloads)
{
if (!historyWindow.documentEdited) {
[self readHistory];
}
[historyWindow makeKeyAndOrderFront:self];
saveButton.enabled = historyWindow.documentEdited;
}
else
{
NSAlert *alert = [NSAlert new];
alert.messageText = @"Download History cannot be edited while downloads are running.";
[alert runModal];
}
}
-(IBAction)removeSelectedFromHistory:(id)sender;
{
if (!runDownloads)
{
[saveButton setEnabled:YES];
[historyWindow setDocumentEdited:YES];
[historyArrayController remove:self];
}
else
{
NSAlert *alert = [NSAlert new];
alert.messageText = @"Download History cannot be edited while downloads are running.";
[alert runModal];
[historyWindow close];
}
}
- (IBAction)cancelChanges:(id)sender
{
[historyWindow setDocumentEdited:NO];
[saveButton setEnabled:NO];
[historyWindow close];
}
- (void)addToHistory:(NSNotification *)note
{
[self readHistory];
NSDictionary *userInfo = note.userInfo;
NSArray<Programme *> *programs = [userInfo valueForKey:@"Programmes"];
for (Programme *prog in programs) {
NSInteger now = [[NSDate new] timeIntervalSince1970];
DownloadHistoryEntry *entry = [DownloadHistoryEntry new];
entry.pid = prog.pid;
entry.showName = prog.seriesName;
entry.episodeName = prog.episodeName;
entry.someNumber = [NSString stringWithFormat:@"%ld", now];
entry.type = prog.typeDescription;
entry.downloadFormat = @"flashhigh";
entry.downloadPath = prog.path;
[historyArrayController addObject:entry];
}
[self writeHistory:self];
}
@end