Merge MatrixKit develop with commit hash: b85b736313bec0592bd1cabc68035d97f5331137

This commit is contained in:
SBiOSoftWhare
2021-12-03 11:47:24 +01:00
parent 8d15fe55a2
commit e7d4cd7707
475 changed files with 87437 additions and 0 deletions
@@ -0,0 +1,69 @@
/*
Copyright 2017 Vector Creations Ltd
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#import <UIKit/UIKit.h>
@class MXSession, MXKViewController, MXKRoomDataSource;
/**
`MXKEncryptionKeysExportView` is a dialog to export encryption keys from
the user's crypto store.
*/
@interface MXKEncryptionKeysExportView : NSObject
/**
The UIAlertController instance which handles the dialog.
*/
@property (nonatomic, readonly) UIAlertController *alertController;
/**
The minimum length of the passphrase. 1 by default.
*/
@property (nonatomic) NSUInteger passphraseMinLength;
/**
Create the `MXKEncryptionKeysExportView` instance.
@param mxSession the mxSession to export keys from.
@return the newly created MXKEncryptionKeysExportView instance.
*/
- (instancetype)initWithMatrixSession:(MXSession*)mxSession;
/**
Show the dialog in a given view controller.
@param mxkViewController the mxkViewController where to show the dialog.
@param keyFile the path where to export keys to.
@param onComplete a block called when the operation is done.
*/
- (void)showInViewController:(MXKViewController*)mxkViewController toExportKeysToFile:(NSURL*)keyFile onComplete:(void(^)(BOOL success))onComplete;
/**
Show the dialog in a given view controller.
@param viewController the UIViewController where to show the dialog.
@param keyFile the path where to export keys to.
@param onLoading a block called when to show a spinner.
@param onComplete a block called when the operation is done.
*/
- (void)showInUIViewController:(UIViewController*)viewController
toExportKeysToFile:(NSURL*)keyFile
onLoading:(void(^)(BOOL onLoading))onLoading
onComplete:(void(^)(BOOL success))onComplete;
@end
@@ -0,0 +1,192 @@
/*
Copyright 2017 Vector Creations Ltd
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#import "MXKEncryptionKeysExportView.h"
#import "MXKViewController.h"
#import "MXKRoomDataSource.h"
#import "NSBundle+MatrixKit.h"
#import <MatrixSDK/MatrixSDK.h>
#import "MXKSwiftHeader.h"
@interface MXKEncryptionKeysExportView ()
{
MXSession *mxSession;
}
@end
@implementation MXKEncryptionKeysExportView
- (instancetype)initWithMatrixSession:(MXSession *)matrixSession
{
self = [super init];
if (self)
{
mxSession = matrixSession;
_passphraseMinLength = 1;
_alertController = [UIAlertController alertControllerWithTitle:[MatrixKitL10n e2eExportRoomKeys] message:[MatrixKitL10n e2eExportPrompt] preferredStyle:UIAlertControllerStyleAlert];
}
return self;
}
- (void)showInViewController:(MXKViewController *)mxkViewController toExportKeysToFile:(NSURL *)keyFile onComplete:(void (^)(BOOL success))onComplete
{
[self showInUIViewController:mxkViewController toExportKeysToFile:keyFile onLoading:^(BOOL onLoading) {
if (onLoading)
{
[mxkViewController startActivityIndicator];
}
else
{
[mxkViewController stopActivityIndicator];
}
} onComplete:onComplete];
}
- (void)showInUIViewController:(UIViewController*)viewController
toExportKeysToFile:(NSURL*)keyFile
onLoading:(void(^)(BOOL onLoading))onLoading
onComplete:(void(^)(BOOL success))onComplete
{
__weak typeof(self) weakSelf = self;
// Finalise the dialog
[_alertController addTextFieldWithConfigurationHandler:^(UITextField *textField)
{
textField.secureTextEntry = YES;
textField.placeholder = [MatrixKitL10n e2ePassphraseCreate];
[textField resignFirstResponder];
}];
[_alertController addTextFieldWithConfigurationHandler:^(UITextField *textField)
{
textField.secureTextEntry = YES;
textField.placeholder = [MatrixKitL10n e2ePassphraseConfirm];
[textField resignFirstResponder];
}];
[_alertController addAction:[UIAlertAction actionWithTitle:[MatrixKitL10n cancel]
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
if (weakSelf)
{
onComplete(NO);
}
}]];
[_alertController addAction:[UIAlertAction actionWithTitle:[MatrixKitL10n e2eExport]
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
if (weakSelf)
{
typeof(self) self = weakSelf;
// Retrieve the password and confirmation
UITextField *textField = [self.alertController textFields].firstObject;
NSString *password = textField.text;
textField = [self.alertController textFields][1];
NSString *confirmation = textField.text;
// Check they are valid
if (password.length < self.passphraseMinLength || ![password isEqualToString:confirmation])
{
NSString *error;
if (!password.length)
{
error = [MatrixKitL10n e2ePassphraseEmpty];
}
else if (password.length < self.passphraseMinLength)
{
error = [MatrixKitL10n e2ePassphraseTooShort:self.passphraseMinLength];
}
else
{
error = [MatrixKitL10n e2ePassphraseNotMatch];
}
UIAlertController *otherAlert = [UIAlertController alertControllerWithTitle:[MatrixKitL10n error] message:error preferredStyle:UIAlertControllerStyleAlert];
[otherAlert addAction:[UIAlertAction actionWithTitle:[MatrixKitL10n ok] style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
if (weakSelf)
{
onComplete(NO);
}
}]];
[viewController presentViewController:otherAlert animated:YES completion:nil];
}
else
{
// Start the export process
onLoading(YES);
[self->mxSession.crypto exportRoomKeysWithPassword:password success:^(NSData *keyFileData) {
if (weakSelf)
{
onLoading(NO);
// Write the result to the passed file
[keyFileData writeToURL:keyFile atomically:YES];
onComplete(YES);
}
} failure:^(NSError *error) {
if (weakSelf)
{
onLoading(NO);
// TODO: i18n the error
UIAlertController *otherAlert = [UIAlertController alertControllerWithTitle:[MatrixKitL10n error] message:error.localizedDescription preferredStyle:UIAlertControllerStyleAlert];
[otherAlert addAction:[UIAlertAction actionWithTitle:[MatrixKitL10n ok] style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
if (weakSelf)
{
onComplete(NO);
}
}]];
[viewController presentViewController:otherAlert animated:YES completion:nil];
}
}];
}
}
}]];
// And show it
[viewController presentViewController:_alertController animated:YES completion:nil];
}
@end
@@ -0,0 +1,49 @@
/*
Copyright 2017 Vector Creations Ltd
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#import <UIKit/UIKit.h>
@class MXSession, MXKViewController;
/**
`MXKEncryptionKeysImportView` is a dialog to import encryption keys into
the user's crypto store.
*/
@interface MXKEncryptionKeysImportView : NSObject
/**
The UIAlertController instance which handles the dialog.
*/
@property (nonatomic, readonly) UIAlertController *alertController;
/**
Create the `MXKEncryptionKeysImportView` instance.
@param mxSession the mxSession to import keys to.
@return the newly created MXKEncryptionKeysImportView instance.
*/
- (instancetype)initWithMatrixSession:(MXSession*)mxSession;
/**
Show the dialog in a given view controller.
@param mxkViewController the mxkViewController where to show the dialog.
@param fileURL the url of the keys file.
@param onComplete a block called when the operation is done (whatever it succeeded or failed).
*/
- (void)showInViewController:(MXKViewController*)mxkViewController toImportKeys:(NSURL*)fileURL onComplete:(void(^)(void))onComplete;
@end
@@ -0,0 +1,122 @@
/*
Copyright 2017 Vector Creations Ltd
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#import "MXKEncryptionKeysImportView.h"
#import "MXKViewController.h"
#import "NSBundle+MatrixKit.h"
#import <MatrixSDK/MatrixSDK.h>
#import "MXKSwiftHeader.h"
@interface MXKEncryptionKeysImportView ()
{
MXSession *mxSession;
}
@end
@implementation MXKEncryptionKeysImportView
- (instancetype)initWithMatrixSession:(MXSession *)matrixSession
{
self = [super init];
if (self)
{
mxSession = matrixSession;
_alertController = [UIAlertController alertControllerWithTitle:[MatrixKitL10n e2eImportRoomKeys] message:[MatrixKitL10n e2eImportPrompt] preferredStyle:UIAlertControllerStyleAlert];
}
return self;
}
- (void)showInViewController:(MXKViewController*)mxkViewController toImportKeys:(NSURL*)fileURL onComplete:(void(^)(void))onComplete
{
__weak typeof(self) weakSelf = self;
// Finalise the dialog
[_alertController addTextFieldWithConfigurationHandler:^(UITextField *textField)
{
textField.secureTextEntry = YES;
textField.placeholder = [MatrixKitL10n e2ePassphraseEnter];
[textField resignFirstResponder];
}];
[_alertController addAction:[UIAlertAction actionWithTitle:[MatrixKitL10n cancel]
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
if (weakSelf)
{
onComplete();
}
}]];
[_alertController addAction:[UIAlertAction actionWithTitle:[MatrixKitL10n e2eImport]
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
if (weakSelf)
{
typeof(self) self = weakSelf;
// Retrieve the password
UITextField *textField = [self.alertController textFields].firstObject;
NSString *password = textField.text;
// Start the import process
[mxkViewController startActivityIndicator];
[self->mxSession.crypto importRoomKeys:[NSData dataWithContentsOfURL:fileURL] withPassword:password success:^(NSUInteger total, NSUInteger imported) {
if (weakSelf)
{
[mxkViewController stopActivityIndicator];
onComplete();
}
} failure:^(NSError *error) {
if (weakSelf)
{
[mxkViewController stopActivityIndicator];
// TODO: i18n the error
UIAlertController *otherAlert = [UIAlertController alertControllerWithTitle:[MatrixKitL10n error] message:error.localizedDescription preferredStyle:UIAlertControllerStyleAlert];
[otherAlert addAction:[UIAlertAction actionWithTitle:[MatrixKitL10n ok] style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
if (weakSelf)
{
onComplete();
}
}]];
[mxkViewController presentViewController:otherAlert animated:YES completion:nil];
}
}];
}
}]];
// And show it
[mxkViewController presentViewController:_alertController animated:YES completion:nil];
}
@end