FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
DocSets-for-iOS/Classes/DocSetDownloadManager.m at master · fabgithub/DocSets-for-iOS · GitHub
fabgithub
/
DocSets-for-iOS
Public
forked from
aral/DocSets-for-iOS
Notifications
You must be signed in to change notification settings
Fork
0
Star
1
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
DocSets-for-iOS
/
Classes
/
DocSetDownloadManager.m
Copy path
More file actions
More file actions
Latest commit
History
History
History
398 lines (333 loc) · 13.5 KB
Breadcrumbs
DocSets-for-iOS
/
Classes
/
DocSetDownloadManager.m
Copy path
File metadata and controls
398 lines (333 loc) · 13.5 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
//
//
DocSetDownloadManager.m
//
DocSets
//
//
Created by Ole Zorn on 22.01.12.
//
Copyright (c) 2012 omz:software. All rights reserved.
//
#
import
"
DocSetDownloadManager.h
"
#
import
"
DocSet.h
"
#
import
"
xar.h
"
#
include
<
sys/xattr.h
>
@interface
DocSetDownloadManager
()
- (
void
)
startNextDownload
;
- (
void
)
reloadDownloadedDocSets
;
- (
void
)
downloadFinished
:
(DocSetDownload *)
download
;
- (
void
)
downloadFailed
:
(DocSetDownload *)
download
;
@end
@implementation
DocSetDownloadManager
@synthesize
downloadedDocSets=_downloadedDocSets, downloadedDocSetNames=_downloadedDocSetNames, availableDownloads=_availableDownloads, currentDownload=_currentDownload, lastUpdated=_lastUpdated, neverDisableIdleTimer = _neverDisableIdleTimer;
- (
id
)
init
{
self = [
super
init
];
if
(self) {
[
self
reloadAvailableDocSets
];
_downloadsByURL = [
NSMutableDictionary
new
];
_downloadQueue = [
NSMutableArray
new
];
[
self
reloadDownloadedDocSets
];
}
return
self;
}
- (
void
)
reloadAvailableDocSets
{
NSString
*cachesPath = [
NSSearchPathForDirectoriesInDomains
(NSCachesDirectory, NSUserDomainMask,
YES
)
lastObject
];
NSString
*cachedAvailableDownloadsPath = [cachesPath
stringByAppendingPathComponent:
@"
AvailableDocSets.plist
"
];
NSFileManager
*fm = [[
NSFileManager
alloc
]
init
];
if
(![fm
fileExistsAtPath:
cachedAvailableDownloadsPath]) {
NSString
*bundledAvailableDocSetsPlistPath = [[
NSBundle
mainBundle
]
pathForResource:
@"
AvailableDocSets
"
ofType:
@"
plist
"
];
[fm
copyItemAtPath:
bundledAvailableDocSetsPlistPath
toPath:
cachedAvailableDownloadsPath
error:
NULL
];
}
self.
lastUpdated
= [[fm
attributesOfItemAtPath:
cachedAvailableDownloadsPath
error:
NULL
]
fileModificationDate
];
_availableDownloads = [[
NSDictionary
dictionaryWithContentsOfFile:
cachedAvailableDownloadsPath]
objectForKey:
@"
DocSets
"
];
}
- (
void
)
updateAvailableDocSetsFromWeb
{
if
(_updatingAvailableDocSetsFromWeb)
return
;
_updatingAvailableDocSetsFromWeb =
YES
;
dispatch_async
(
dispatch_get_global_queue
(
DISPATCH_QUEUE_PRIORITY_DEFAULT
,
0
), ^{
NSURL
*availableDocSetsURL = [
NSURL
URLWithString:
@"
https://raw.github.com/omz/DocSets-for-iOS/master/Resources/AvailableDocSets.plist
"
];
NSHTTPURLResponse
*response =
nil
;
NSData
*updatedDocSetsData = [
NSURLConnection
sendSynchronousRequest:
[
NSURLRequest
requestWithURL:
availableDocSetsURL]
returningResponse:
&response
error:
NULL
];
if
(response.
statusCode
==
200
) {
NSDictionary
*plist = [
NSPropertyListSerialization
propertyListFromData:
updatedDocSetsData
mutabilityOption:
NSPropertyListImmutable
format:
NULL
errorDescription:
NULL
];
if
(plist && [plist
objectForKey:
@"
DocSets
"
]) {
NSString
*cachesPath = [
NSSearchPathForDirectoriesInDomains
(NSCachesDirectory, NSUserDomainMask,
YES
)
lastObject
];
NSString
*cachedAvailableDownloadsPath = [cachesPath
stringByAppendingPathComponent:
@"
AvailableDocSets.plist
"
];
[updatedDocSetsData
writeToFile:
cachedAvailableDownloadsPath
atomically:
YES
];
}
else
{
//
Downloaded file is somehow not a valid plist...
}
}
dispatch_async
(
dispatch_get_main_queue
(), ^{
_updatingAvailableDocSetsFromWeb =
NO
;
[
self
reloadAvailableDocSets
];
[[
NSNotificationCenter
defaultCenter
]
postNotificationName:
DocSetDownloadManagerAvailableDocSetsChangedNotification
object:
self
];
});
});
}
- (
void
)
reloadDownloadedDocSets
{
NSFileManager
*fm = [
NSFileManager
defaultManager
];
NSString
*docPath = [
NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask,
YES
)
objectAtIndex:
0
];
NSArray
*documents = [fm
contentsOfDirectoryAtPath:
docPath
error:
NULL
];
NSMutableArray
*loadedSets = [
NSMutableArray
array
];
for
(
NSString
*path in documents) {
if
([[[path
pathExtension
]
lowercaseString
]
isEqual:
@"
docset
"
]) {
NSString
*fullPath = [docPath
stringByAppendingPathComponent:
path];
u_int8_t
b =
1
;
setxattr
([fullPath
fileSystemRepresentation
],
"
com.apple.MobileBackup
"
, &b,
1
,
0
,
0
);
DocSet *docSet = [[DocSet
alloc
]
initWithPath:
fullPath];
if
(docSet) [loadedSets
addObject:
docSet];
}
}
self.
downloadedDocSets
= [
NSArray
arrayWithArray:
loadedSets];
self.
downloadedDocSetNames
= [
NSSet
setWithArray:
documents];
[[
NSNotificationCenter
defaultCenter
]
postNotificationName:
DocSetDownloadManagerUpdatedDocSetsNotification
object:
self
];
}
+ (
id
)
sharedDownloadManager
{
static
id
sharedDownloadManager =
nil
;
static
dispatch_once_t
onceToken;
dispatch_once
(&onceToken, ^{
sharedDownloadManager = [[
self
alloc
]
init
];
});
return
sharedDownloadManager;
}
- (DocSetDownload *)
downloadForURL
:
(
NSString
*)
URL
{
return
[_downloadsByURL
objectForKey:
URL
];
}
- (
void
)
stopDownload
:
(DocSetDownload *)
download
{
if
(download.
status
== DocSetDownloadStatusWaiting) {
[_downloadQueue
removeObject:
download];
[_downloadsByURL
removeObjectForKey:
[download.
URL
absoluteString
]];
[[
NSNotificationCenter
defaultCenter
]
postNotificationName:
DocSetDownloadFinishedNotification
object:
download];
}
else
if
(download.
status
== DocSetDownloadStatusDownloading) {
[download
cancel
];
self.
currentDownload
=
nil
;
[_downloadsByURL
removeObjectForKey:
[download.
URL
absoluteString
]];
[[
NSNotificationCenter
defaultCenter
]
postNotificationName:
DocSetDownloadFinishedNotification
object:
download];
[
self
startNextDownload
];
}
else
if
(download.
status
== DocSetDownloadStatusExtracting) {
download.
shouldCancelExtracting
=
YES
;
}
[
self
toggleIdleTimerIfNeeded
];
}
- (
void
)
downloadDocSetAtURL
:
(
NSString
*)
URL
{
if
([_downloadsByURL
objectForKey:
URL
]) {
//
already downloading
return
;
}
DocSetDownload *download = [[DocSetDownload
alloc
]
initWithURL:
[
NSURL
URLWithString:
URL
]];
[_downloadQueue
addObject:
download];
[_downloadsByURL
setObject:
download
forKey:
URL
];
[[
NSNotificationCenter
defaultCenter
]
postNotificationName:
DocSetDownloadManagerStartedDownloadNotification
object:
self
];
[
self
startNextDownload
];
[
self
toggleIdleTimerIfNeeded
];
}
- (
void
)
deleteDocSet
:
(DocSet *)
docSetToDelete
{
[[
NSNotificationCenter
defaultCenter
]
postNotificationName:
DocSetWillBeDeletedNotification
object:
docSetToDelete
userInfo:
nil
];
[[
NSFileManager
defaultManager
]
removeItemAtPath:
docSetToDelete.path
error:
NULL
];
[
self
reloadDownloadedDocSets
];
}
- (DocSet *)
downloadedDocSetWithName
:
(
NSString
*)
docSetName
{
for
(DocSet *docSet in _downloadedDocSets) {
if
([[docSet.path
lastPathComponent
]
isEqualToString:
docSetName]) {
return
docSet;
}
}
return
nil
;
}
- (
void
)
startNextDownload
{
if
([_downloadQueue
count
] ==
0
) {
[[UIApplication
sharedApplication
]
setNetworkActivityIndicatorVisible:
NO
];
return
;
}
if
(self.
currentDownload
!=
nil
)
return
;
self.
currentDownload
= [_downloadQueue
objectAtIndex:
0
];
[_downloadQueue
removeObjectAtIndex:
0
];
[[UIApplication
sharedApplication
]
setNetworkActivityIndicatorVisible:
YES
];
[
self
.currentDownload
start
];
}
- (
void
)
downloadFinished
:
(DocSetDownload *)
download
{
[[
NSNotificationCenter
defaultCenter
]
postNotificationName:
DocSetDownloadFinishedNotification
object:
download];
NSString
*docPath = [
NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask,
YES
)
objectAtIndex:
0
];
NSArray
*extractedItems = [[
NSFileManager
defaultManager
]
contentsOfDirectoryAtPath:
download.extractedPath
error:
NULL
];
for
(
NSString
*file in extractedItems) {
if
([[[file
pathExtension
]
lowercaseString
]
isEqualToString:
@"
docset
"
]) {
NSString
*fullPath = [download.extractedPath
stringByAppendingPathComponent:
file];
NSString
*targetPath = [docPath
stringByAppendingPathComponent:
file];
[[
NSFileManager
defaultManager
]
moveItemAtPath:
fullPath
toPath:
targetPath
error:
NULL
];
NSLog
(
@"
Moved downloaded docset to
%@
"
, targetPath);
}
}
[
self
reloadDownloadedDocSets
];
[_downloadsByURL
removeObjectForKey:
[download.
URL
absoluteString
]];
self.
currentDownload
=
nil
;
[
self
startNextDownload
];
[
self
toggleIdleTimerIfNeeded
];
}
- (
void
)
downloadFailed
:
(DocSetDownload *)
download
{
[[
NSNotificationCenter
defaultCenter
]
postNotificationName:
DocSetDownloadFinishedNotification
object:
download];
[_downloadsByURL
removeObjectForKey:
[download.
URL
absoluteString
]];
self.
currentDownload
=
nil
;
[
self
startNextDownload
];
[
self
toggleIdleTimerIfNeeded
];
[[[UIAlertView
alloc
]
initWithTitle:
NSLocalizedString(
@"
Download Failed
"
,
nil
)
message:
NSLocalizedString(
@"
An error occured while trying to download the DocSet.
"
,
nil
)
delegate:
nil
cancelButtonTitle:
NSLocalizedString(
@"
OK
"
,
nil
)
otherButtonTitles:
nil
]
show
];
}
- (
void
)
toggleIdleTimerIfNeeded
{
BOOL
shouldDisableIdleTimer =
NO
;
if
(self.
currentDownload
&& !self.
neverDisableIdleTimer
) {
shouldDisableIdleTimer =
YES
;
}
[[UIApplication
sharedApplication
]
setIdleTimerDisabled:
shouldDisableIdleTimer];
[[
NSNotificationCenter
defaultCenter
]
postNotificationName:
DocSetDownloadManagerIdleTimerToggledNotification
object:
self
];
}
- (
void
)
setNeverDisableIdleTimer
:
(
BOOL
)
neverDisableIdleTimer
{
_neverDisableIdleTimer = neverDisableIdleTimer;
[
self
toggleIdleTimerIfNeeded
];
}
@end
@implementation
DocSetDownload
@synthesize
connection=_connection,
URL
=_URL, fileHandle=_fileHandle, downloadTargetPath=_downloadTargetPath, extractedPath=_extractedPath, progress=_progress, status=_status, shouldCancelExtracting = _shouldCancelExtracting;
@synthesize
downloadSize, bytesDownloaded;
- (
id
)
initWithURL
:
(
NSURL
*)
URL
{
self = [
super
init
];
if
(self) {
_URL =
URL
;
self.
status
= DocSetDownloadStatusWaiting;
}
return
self;
}
- (
void
)
start
{
if
(self.
status
!= DocSetDownloadStatusWaiting) {
return
;
}
_backgroundTask = [[UIApplication
sharedApplication
]
beginBackgroundTaskWithExpirationHandler:
nil
];
self.
status
= DocSetDownloadStatusDownloading;
self.
downloadTargetPath
= [
NSTemporaryDirectory
()
stringByAppendingPathComponent:
@"
download.xar
"
];
[
@"
"
writeToFile:
self
.downloadTargetPath
atomically:
YES
encoding:
NSUTF8StringEncoding
error:
NULL
];
self.
fileHandle
= [
NSFileHandle
fileHandleForWritingAtPath:
self
.downloadTargetPath];
bytesDownloaded =
0
;
self.
connection
= [
NSURLConnection
connectionWithRequest:
[
NSURLRequest
requestWithURL:
self
.
URL
]
delegate:
self
];
}
- (
void
)
cancel
{
if
(self.
status
== DocSetDownloadStatusDownloading) {
[
self
.connection
cancel
];
self.
status
= DocSetDownloadStatusFinished;
if
(_backgroundTask != UIBackgroundTaskInvalid) {
[[UIApplication
sharedApplication
]
endBackgroundTask:
_backgroundTask];
}
}
}
- (
void
)
connection
:
(
NSURLConnection
*)
connection
didReceiveResponse
:
(
NSURLResponse
*)
response
{
NSDictionary
*headers = [(
NSHTTPURLResponse
*)response
allHeaderFields
];
downloadSize = [[headers
objectForKey:
@"
Content-Length
"
]
integerValue
];
}
- (
void
)
connection
:
(
NSURLConnection
*)
connection
didReceiveData
:
(
NSData
*)
data
{
bytesDownloaded += [data
length
];
if
(downloadSize !=
0
) {
self.
progress
= (
float
)bytesDownloaded / (
float
)downloadSize;
//
NSLog(@"Download progress: %f", self.progress);
}
[
self
.fileHandle
writeData:
data];
}
- (
void
)
connectionDidFinishLoading
:
(
NSURLConnection
*)
connection
{
[
self
.fileHandle
closeFile
];
self.
fileHandle
=
nil
;
self.
status
= DocSetDownloadStatusExtracting;
self.
progress
=
0.0
;
dispatch_async
(
dispatch_get_global_queue
(
DISPATCH_QUEUE_PRIORITY_DEFAULT
,
0
), ^{
NSFileManager
*fm = [[
NSFileManager
alloc
]
init
];
NSString
*extractionTargetPath = [[
self
.downloadTargetPath
stringByDeletingLastPathComponent
]
stringByAppendingPathComponent:
@"
xar_extract
"
];
self.
extractedPath
= extractionTargetPath;
[fm
createDirectoryAtPath:
extractionTargetPath
withIntermediateDirectories:
YES
attributes:
nil
error:
NULL
];
const
char
*xar_path = [
self
.downloadTargetPath
fileSystemRepresentation
];
xar_t
x =
xar_open
(xar_path,
READ
);
xar_iter_t
i =
xar_iter_new
();
xar_file_t
f =
xar_file_first
(x, i);
NSInteger
numberOfFiles =
1
;
do
{
f =
xar_file_next
(i);
if
(f !=
NULL
) {
numberOfFiles +=
1
;
}
}
while
(f !=
NULL
);
xar_iter_free
(i);
chdir
([extractionTargetPath
fileSystemRepresentation
]);
if
(x ==
NULL
) {
NSLog
(
@"
Could not open archive
"
);
[
self
fail
];
}
else
{
xar_iter_t
i =
xar_iter_new
();
xar_file_t
f =
xar_file_first
(x, i);
NSInteger
filesExtracted =
0
;
do
{
if
(self.
shouldCancelExtracting
) {
NSLog
(
@"
Extracting cancelled
"
);
break
;
}
if
(f) {
const
char
*name =
NULL
;
xar_prop_get
(f,
"
name
"
, &name);
int32_t
extractResult =
xar_extract
(x, f);
if
(extractResult !=
0
) {
NSLog
(
@"
Could not extract file:
%s
"
, name);
}
f =
xar_file_next
(i);
filesExtracted++;
float
extractionProgress = (
float
)filesExtracted / (
float
)numberOfFiles;
dispatch_async
(
dispatch_get_main_queue
(), ^{
self.
progress
= extractionProgress;
});
}
}
while
(f !=
NULL
);
xar_iter_free
(i);
if
(self.
shouldCancelExtracting
) {
//
Cleanup: delete all files that have already been extracted
NSFileManager
*fm = [[
NSFileManager
alloc
]
init
];
[fm
removeItemAtPath:
extractionTargetPath
error:
NULL
];
}
}
xar_close
(x);
[fm
removeItemAtPath:
self
.downloadTargetPath
error:
NULL
];
dispatch_async
(
dispatch_get_main_queue
(), ^{
self.
status
= DocSetDownloadStatusFinished;
[[DocSetDownloadManager
sharedDownloadManager
]
downloadFinished:
self
];
if
(_backgroundTask != UIBackgroundTaskInvalid) {
[[UIApplication
sharedApplication
]
endBackgroundTask:
_backgroundTask];
}
});
});
}
- (
void
)
connection
:
(
NSURLConnection
*)
connection
didFailWithError
:
(
NSError
*)
error
{
[
self
fail
];
}
- (
void
)
fail
{
[[DocSetDownloadManager
sharedDownloadManager
]
downloadFailed:
self
];
if
(_backgroundTask != UIBackgroundTaskInvalid) {
[[UIApplication
sharedApplication
]
endBackgroundTask:
_backgroundTask];
}
}
@end
Back
|
FazBrowse Home
|
New Git URL