mirror of
https://github.com/MacPass/MacPass.git
synced 2025-12-14 05:52:58 +00:00
Moved favIconFetching to MPIconHelper
This commit is contained in:
@@ -144,4 +144,6 @@ typedef NS_ENUM(NSUInteger, MPIconType) {
|
|||||||
*/
|
*/
|
||||||
+ (NSArray *)databaseIconTypes;
|
+ (NSArray *)databaseIconTypes;
|
||||||
|
|
||||||
|
+ (void)fetchIconDataForURL:(NSURL *)url completionHandler:(void (^)(NSData *iconData))handler;
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|||||||
@@ -167,13 +167,45 @@
|
|||||||
@(MPIconExpiredEntry): NSImageNameCaution,
|
@(MPIconExpiredEntry): NSImageNameCaution,
|
||||||
@(MPIconExpiredGroup): NSImageNameCaution
|
@(MPIconExpiredGroup): NSImageNameCaution
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
});
|
});
|
||||||
return imageNames;
|
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
|
@end
|
||||||
|
|||||||
@@ -117,31 +117,17 @@ typedef NS_ENUM(NSInteger, MPIconDownloadStatus) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
NSURL *url = [NSURL URLWithString:URLString];
|
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;
|
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) {
|
[MPIconHelper fetchIconDataForURL:url completionHandler:^(NSData *iconData) {
|
||||||
|
if(!iconData || iconData.length == 0) {
|
||||||
dispatch_async(dispatch_get_main_queue(), ^{
|
dispatch_async(dispatch_get_main_queue(), ^{
|
||||||
self.downloadStatus = MPIconDownloadStatusError;
|
self.downloadStatus = MPIconDownloadStatusError;
|
||||||
//[NSApp presentError:error];
|
|
||||||
});
|
});
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
if([response respondsToSelector:@selector(statusCode)] &&
|
|
||||||
(200 == [(id)response statusCode])
|
|
||||||
&& data.length > 0) {
|
|
||||||
dispatch_async(dispatch_get_main_queue(), ^{
|
dispatch_async(dispatch_get_main_queue(), ^{
|
||||||
KPKIcon *newIcon = [[KPKIcon alloc] initWithImageData:data];
|
KPKIcon *newIcon = [[KPKIcon alloc] initWithImageData:iconData];
|
||||||
if(newIcon && newIcon.image) {
|
if(newIcon && newIcon.image) {
|
||||||
self.downloadStatus = MPIconDownloadStatusNone;
|
self.downloadStatus = MPIconDownloadStatusNone;
|
||||||
[metaData addCustomIcon:newIcon];
|
[metaData addCustomIcon:newIcon];
|
||||||
@@ -151,14 +137,7 @@ typedef NS_ENUM(NSInteger, MPIconDownloadStatus) {
|
|||||||
self.downloadStatus = MPIconDownloadStatusError;
|
self.downloadStatus = MPIconDownloadStatusError;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
|
||||||
else {
|
|
||||||
dispatch_async(dispatch_get_main_queue(), ^{
|
|
||||||
self.downloadStatus = MPIconDownloadStatusError;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}];
|
}];
|
||||||
[task resume];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)deleteIcon:(id)sender {
|
- (void)deleteIcon:(id)sender {
|
||||||
|
|||||||
Reference in New Issue
Block a user