mirror of
https://github.com/kiwix/kiwix-apple.git
synced 2025-09-28 06:25:04 -04:00
additional alerts when import zim file (#162)
* getMetaDataWithFileURL * Alerts
This commit is contained in:
parent
6ccba2d19b
commit
c2da61c617
@ -34,4 +34,8 @@
|
|||||||
- (void)stopIndexSearch;
|
- (void)stopIndexSearch;
|
||||||
- (NSArray *_Nonnull)getTitleSearchResults:(NSString *_Nonnull)searchText zimFileID:(NSString *_Nullable)zimFileID count:(unsigned int)count NS_REFINED_FOR_SWIFT;
|
- (NSArray *_Nonnull)getTitleSearchResults:(NSString *_Nonnull)searchText zimFileID:(NSString *_Nullable)zimFileID count:(unsigned int)count NS_REFINED_FOR_SWIFT;
|
||||||
|
|
||||||
|
NS_ASSUME_NONNULL_BEGIN
|
||||||
|
+ (NSDictionary *_Nullable)getMetaDataWithFileURL:(NSURL *)url NS_REFINED_FOR_SWIFT;
|
||||||
|
NS_ASSUME_NONNULL_END
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
@ -154,37 +154,13 @@ NSMutableDictionary *fileURLs = [[NSMutableDictionary alloc] init]; // [ID: File
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
- (NSDictionary *_Nullable)getMetaData:(NSString *_Nonnull)zimFileID {
|
- (NSDictionary *)getMetaData:(NSString *_Nonnull)zimFileID {
|
||||||
auto found = readers.find([zimFileID cStringUsingEncoding:NSUTF8StringEncoding]);
|
auto found = readers.find([zimFileID cStringUsingEncoding:NSUTF8StringEncoding]);
|
||||||
if (found == readers.end()) {
|
if (found == readers.end()) {
|
||||||
return nil;
|
return nil;
|
||||||
} else {
|
} else {
|
||||||
std::shared_ptr<kiwix::Reader> reader = found->second;
|
std::shared_ptr<kiwix::Reader> reader = found->second;
|
||||||
|
return [ZimMultiReader getMetaDataWithReader:reader];
|
||||||
NSMutableDictionary *meta = [[NSMutableDictionary alloc] init];
|
|
||||||
|
|
||||||
meta[@"id"] = [NSString stringWithCString:reader->getId().c_str() encoding:NSUTF8StringEncoding];
|
|
||||||
meta[@"name"] = [NSString stringWithCString:reader->getName().c_str() encoding:NSUTF8StringEncoding];
|
|
||||||
|
|
||||||
meta[@"title"] = [NSString stringWithCString:reader->getTitle().c_str() encoding:NSUTF8StringEncoding];
|
|
||||||
meta[@"description"] = [NSString stringWithCString:reader->getDescription().c_str() encoding:NSUTF8StringEncoding];
|
|
||||||
meta[@"language"] = [NSString stringWithCString:reader->getLanguage().c_str() encoding:NSUTF8StringEncoding];
|
|
||||||
|
|
||||||
meta[@"tags"] = [NSString stringWithCString:reader->getTags().c_str() encoding:NSUTF8StringEncoding];
|
|
||||||
meta[@"date"] = [NSString stringWithCString:reader->getDate().c_str() encoding:NSUTF8StringEncoding];
|
|
||||||
meta[@"creator"] = [NSString stringWithCString:reader->getCreator().c_str() encoding:NSUTF8StringEncoding];
|
|
||||||
meta[@"publisher"] = [NSString stringWithCString:reader->getPublisher().c_str() encoding:NSUTF8StringEncoding];
|
|
||||||
meta[@"fileSize"] = [[NSNumber alloc] initWithLongLong:(long long)reader->getFileSize() * 1024];
|
|
||||||
meta[@"articleCount"] = [[NSNumber alloc] initWithLongLong:reader->getArticleCount()];
|
|
||||||
meta[@"mediaCount"] = [[NSNumber alloc] initWithLongLong:reader->getMediaCount()];
|
|
||||||
meta[@"globalCount"] = [[NSNumber alloc] initWithLongLong:reader->getGlobalCount()];
|
|
||||||
|
|
||||||
string faviconEncoded;
|
|
||||||
string mimeType;
|
|
||||||
if (reader->getFavicon(faviconEncoded, mimeType)) {
|
|
||||||
meta[@"icon"] = [NSData dataWithBytes:faviconEncoded.c_str() length:faviconEncoded.length()];
|
|
||||||
}
|
|
||||||
return meta;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -271,7 +247,46 @@ NSMutableDictionary *fileURLs = [[NSMutableDictionary alloc] init]; // [ID: File
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
# pragma mark - Geo Search
|
# pragma mark - class methods
|
||||||
|
|
||||||
|
+ (NSDictionary *)getMetaDataWithFileURL:(NSURL *)url {
|
||||||
|
[url startAccessingSecurityScopedResource];
|
||||||
|
try {
|
||||||
|
std::shared_ptr<kiwix::Reader> reader = std::make_shared<kiwix::Reader>([url fileSystemRepresentation]);
|
||||||
|
[url stopAccessingSecurityScopedResource];
|
||||||
|
return [ZimMultiReader getMetaDataWithReader:reader];
|
||||||
|
} catch (std::exception e) {
|
||||||
|
[url stopAccessingSecurityScopedResource];
|
||||||
|
return nil;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
+ (NSDictionary *)getMetaDataWithReader:(std::shared_ptr<kiwix::Reader>)reader {
|
||||||
|
NSMutableDictionary *meta = [[NSMutableDictionary alloc] init];
|
||||||
|
|
||||||
|
meta[@"id"] = [NSString stringWithCString:reader->getId().c_str() encoding:NSUTF8StringEncoding];
|
||||||
|
meta[@"name"] = [NSString stringWithCString:reader->getName().c_str() encoding:NSUTF8StringEncoding];
|
||||||
|
|
||||||
|
meta[@"title"] = [NSString stringWithCString:reader->getTitle().c_str() encoding:NSUTF8StringEncoding];
|
||||||
|
meta[@"description"] = [NSString stringWithCString:reader->getDescription().c_str() encoding:NSUTF8StringEncoding];
|
||||||
|
meta[@"language"] = [NSString stringWithCString:reader->getLanguage().c_str() encoding:NSUTF8StringEncoding];
|
||||||
|
|
||||||
|
meta[@"tags"] = [NSString stringWithCString:reader->getTags().c_str() encoding:NSUTF8StringEncoding];
|
||||||
|
meta[@"date"] = [NSString stringWithCString:reader->getDate().c_str() encoding:NSUTF8StringEncoding];
|
||||||
|
meta[@"creator"] = [NSString stringWithCString:reader->getCreator().c_str() encoding:NSUTF8StringEncoding];
|
||||||
|
meta[@"publisher"] = [NSString stringWithCString:reader->getPublisher().c_str() encoding:NSUTF8StringEncoding];
|
||||||
|
meta[@"fileSize"] = [[NSNumber alloc] initWithLongLong:(long long)reader->getFileSize() * 1024];
|
||||||
|
meta[@"articleCount"] = [[NSNumber alloc] initWithLongLong:reader->getArticleCount()];
|
||||||
|
meta[@"mediaCount"] = [[NSNumber alloc] initWithLongLong:reader->getMediaCount()];
|
||||||
|
meta[@"globalCount"] = [[NSNumber alloc] initWithLongLong:reader->getGlobalCount()];
|
||||||
|
|
||||||
|
string faviconEncoded;
|
||||||
|
string mimeType;
|
||||||
|
if (reader->getFavicon(faviconEncoded, mimeType)) {
|
||||||
|
meta[@"icon"] = [NSData dataWithBytes:faviconEncoded.c_str() length:faviconEncoded.length()];
|
||||||
|
}
|
||||||
|
|
||||||
|
return meta;
|
||||||
|
}
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
@ -62,4 +62,8 @@ extension ZimMultiReader {
|
|||||||
return SearchResult(zimFileID: id, path: path, title: title)
|
return SearchResult(zimFileID: id, path: path, title: title)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static func getMetaData(url: URL) -> [String: Any]? {
|
||||||
|
return __getMetaData(withFileURL: url) as? [String: Any]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -68,12 +68,15 @@ class AppDelegate: UIResponder, UIApplicationDelegate, DirectoryMonitorDelegate
|
|||||||
mainController.load(url: url)
|
mainController.load(url: url)
|
||||||
return true
|
return true
|
||||||
} else if url.scheme == "file" {
|
} else if url.scheme == "file" {
|
||||||
let canOpenInPlace = options[.openInPlace] as? Bool ?? false
|
if let _ = ZimMultiReader.getMetaData(url: url) {
|
||||||
let fileImportController = FileImportController(fileURL: url, canOpenInPlace: canOpenInPlace)
|
let canOpenInPlace = options[.openInPlace] as? Bool ?? false
|
||||||
mainController.present(fileImportController, animated: true)
|
let fileImportController = FileImportController(fileURL: url, canOpenInPlace: canOpenInPlace)
|
||||||
|
mainController.present(fileImportController, animated: true)
|
||||||
|
} else {
|
||||||
|
mainController.present(FileImportAlertController(fileName: url.lastPathComponent), animated: true)
|
||||||
|
}
|
||||||
return true
|
return true
|
||||||
} else {
|
} else {
|
||||||
print(url)
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -100,7 +100,6 @@ fileprivate class ContentController: UIViewController, UITableViewDelegate, UITa
|
|||||||
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
||||||
defer {
|
defer {
|
||||||
tableView.deselectRow(at: indexPath, animated: true)
|
tableView.deselectRow(at: indexPath, animated: true)
|
||||||
dismiss(animated: true)
|
|
||||||
}
|
}
|
||||||
guard indexPath.section == items.count - 1,
|
guard indexPath.section == items.count - 1,
|
||||||
let action = items[indexPath.section][indexPath.row] as? Action else {return}
|
let action = items[indexPath.section][indexPath.row] as? Action else {return}
|
||||||
@ -116,13 +115,38 @@ fileprivate class ContentController: UIViewController, UITableViewDelegate, UITa
|
|||||||
case .openInPlace:
|
case .openInPlace:
|
||||||
LibraryOperationQueue.shared.addOperation(LibraryScanOperation(url: url))
|
LibraryOperationQueue.shared.addOperation(LibraryScanOperation(url: url))
|
||||||
}
|
}
|
||||||
|
dismiss(animated: true)
|
||||||
} catch {
|
} catch {
|
||||||
print(error)
|
present(FileImportAlertController(error: error), animated: true)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class FileImportAlertController: UIAlertController {
|
||||||
|
convenience init(message: String) {
|
||||||
|
self.init(title: NSLocalizedString("File Import Error", comment: "File Import Error"),
|
||||||
|
message: message,
|
||||||
|
preferredStyle: .alert)
|
||||||
|
let ok = NSLocalizedString("OK", comment: "File Import Error")
|
||||||
|
addAction(UIAlertAction(title: ok, style: .default, handler: { action in
|
||||||
|
self.dismiss(animated: true)
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
|
convenience init(fileName: String) {
|
||||||
|
let message = NSLocalizedString(
|
||||||
|
"The file \"\(fileName)\" seems to be corrupt and Kiwix was unable to open it.",
|
||||||
|
comment: "File Import Error")
|
||||||
|
self.init(message: message)
|
||||||
|
}
|
||||||
|
|
||||||
|
convenience init(error: Error) {
|
||||||
|
self.init(message: error.localizedDescription)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
fileprivate protocol Item: CustomStringConvertible {}
|
fileprivate protocol Item: CustomStringConvertible {}
|
||||||
|
|
||||||
|
|
||||||
|
@ -218,7 +218,11 @@ class LibraryMasterController: UIViewController, UIDocumentPickerDelegate, UITab
|
|||||||
// MARK: - UIDocumentPickerDelegate
|
// MARK: - UIDocumentPickerDelegate
|
||||||
|
|
||||||
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) {
|
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) {
|
||||||
present(FileImportController(fileURL: url), animated: true)
|
if let _ = ZimMultiReader.getMetaData(url: url) {
|
||||||
|
present(FileImportController(fileURL: url), animated: true)
|
||||||
|
} else {
|
||||||
|
present(FileImportAlertController(fileName: url.lastPathComponent), animated: true)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: - UITableViewDataSource & Delegates
|
// MARK: - UITableViewDataSource & Delegates
|
||||||
|
Loading…
x
Reference in New Issue
Block a user