mirror of
https://github.com/MacPass/MacPass.git
synced 2025-12-13 21:42:32 +00:00
Moved favIconFetching to MPIconHelper
This commit is contained in:
@@ -144,4 +144,6 @@ typedef NS_ENUM(NSUInteger, MPIconType) {
|
||||
*/
|
||||
+ (NSArray *)databaseIconTypes;
|
||||
|
||||
+ (void)fetchIconDataForURL:(NSURL *)url completionHandler:(void (^)(NSData *iconData))handler;
|
||||
|
||||
@end
|
||||
|
||||
@@ -167,13 +167,45 @@
|
||||
@(MPIconExpiredEntry): NSImageNameCaution,
|
||||
@(MPIconExpiredGroup): NSImageNameCaution
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
});
|
||||
return imageNames;
|
||||
}
|
||||
|
||||
+ (void)fetchIconDataForURL:(NSURL *)url completionHandler:(void (^)(NSData *iconData))handler {
|
||||
|
||||
if(!url || !handler) {
|
||||
return; // no url, no handler so no need to do anything
|
||||
}
|
||||
|
||||
NSString *urlString = [NSString stringWithFormat:@"%@://%@/favicon.ico", url.scheme, url.host ? url.host : @""];
|
||||
NSURL *favIconURL = [NSURL URLWithString:urlString];
|
||||
if(!favIconURL) {
|
||||
/* call the handler with nil data */
|
||||
handler(nil);
|
||||
return;
|
||||
}
|
||||
|
||||
NSURLSessionTask *task = [[NSURLSession sharedSession] dataTaskWithURL:favIconURL completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
|
||||
if(error) {
|
||||
//dispatch_async(dispatch_get_main_queue(), ^{
|
||||
handler(nil);
|
||||
//});
|
||||
}
|
||||
else if([response respondsToSelector:@selector(statusCode)]
|
||||
&& (200 == [(id)response statusCode])
|
||||
&& data.length > 0) {
|
||||
//dispatch_async(dispatch_get_main_queue(), ^{
|
||||
handler(data);
|
||||
//});
|
||||
}
|
||||
else {
|
||||
//dispatch_async(dispatch_get_main_queue(), ^{
|
||||
handler(nil);
|
||||
//});
|
||||
}
|
||||
}];
|
||||
[task resume];
|
||||
}
|
||||
|
||||
|
||||
@end
|
||||
|
||||
@@ -117,48 +117,27 @@ typedef NS_ENUM(NSInteger, MPIconDownloadStatus) {
|
||||
}
|
||||
|
||||
NSURL *url = [NSURL URLWithString:URLString];
|
||||
if(!url) {
|
||||
self.downloadStatus = MPIconDownloadStatusError;
|
||||
return;
|
||||
}
|
||||
|
||||
NSString *urlString = [NSString stringWithFormat:@"%@://%@/favicon.ico", url.scheme, url.host ? url.host : @""];
|
||||
NSURL *favIconURL = [NSURL URLWithString:urlString];
|
||||
if(!favIconURL) {
|
||||
self.downloadStatus = MPIconDownloadStatusError;
|
||||
return;
|
||||
}
|
||||
|
||||
KPKMetaData *metaData = ((MPDocument *)[NSDocumentController sharedDocumentController].currentDocument).tree.metaData;
|
||||
NSURLSessionTask *task = [[NSURLSession sharedSession] dataTaskWithURL:favIconURL completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
|
||||
if(error) {
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
self.downloadStatus = MPIconDownloadStatusError;
|
||||
//[NSApp presentError:error];
|
||||
});
|
||||
}
|
||||
if([response respondsToSelector:@selector(statusCode)] &&
|
||||
(200 == [(id)response statusCode])
|
||||
&& data.length > 0) {
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
KPKIcon *newIcon = [[KPKIcon alloc] initWithImageData:data];
|
||||
if(newIcon && newIcon.image) {
|
||||
self.downloadStatus = MPIconDownloadStatusNone;
|
||||
[metaData addCustomIcon:newIcon];
|
||||
[self _updateCollectionViewContent];
|
||||
}
|
||||
else {
|
||||
self.downloadStatus = MPIconDownloadStatusError;
|
||||
}
|
||||
});
|
||||
}
|
||||
else {
|
||||
|
||||
[MPIconHelper fetchIconDataForURL:url completionHandler:^(NSData *iconData) {
|
||||
if(!iconData || iconData.length == 0) {
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
self.downloadStatus = MPIconDownloadStatusError;
|
||||
});
|
||||
return;
|
||||
}
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
KPKIcon *newIcon = [[KPKIcon alloc] initWithImageData:iconData];
|
||||
if(newIcon && newIcon.image) {
|
||||
self.downloadStatus = MPIconDownloadStatusNone;
|
||||
[metaData addCustomIcon:newIcon];
|
||||
[self _updateCollectionViewContent];
|
||||
}
|
||||
else {
|
||||
self.downloadStatus = MPIconDownloadStatusError;
|
||||
}
|
||||
});
|
||||
}];
|
||||
[task resume];
|
||||
}
|
||||
|
||||
- (void)deleteIcon:(id)sender {
|
||||
|
||||
Reference in New Issue
Block a user