pEpObjCAdapter/PEPObjCAdapter.m
author buff <andreas@pep-project.org>
Fri, 30 Aug 2019 22:39:03 +0200
branchIOS-1784
changeset 1173 d766b32be20d
parent 1172 31cdf35e0b22
child 1174 0700139e399c
permissions -rw-r--r--
IOS-1784 adapts Engines directory cahnges for building with iOS
dirk@187
     1
//
edouard@191
     2
//  pEpObjCAdapter.m
edouard@191
     3
//  pEpObjCAdapter
dirk@187
     4
//
dirk@187
     5
//  Created by Volker Birk on 28.04.15.
dirk@187
     6
//  Copyright (c) 2015 p≡p. All rights reserved.
dirk@187
     7
//
dirk@187
     8
dirk@187
     9
@import Foundation;
dirk@187
    10
dirk@187
    11
#import "PEPObjCAdapter.h"
dirk@187
    12
#import "PEPObjCAdapter+Internal.h"
dirk@367
    13
#import "PEPMessageUtil.h"
dirk@484
    14
#import "NSError+PEP.h"
dirk@484
    15
dirk@484
    16
#import "keymanagement.h"
dirk@484
    17
#import "mime.h"
dirk@484
    18
#import "message.h"
dirk@187
    19
andreas@635
    20
const PEP_decrypt_flags PEP_decrypt_flag_none = 0x0;
andreas@635
    21
andreas@1173
    22
#if TARGET_OS_IPHONE
andreas@1173
    23
// macOS must use Engine's default directoties to potentially share the data with other apps.
andreas@1173
    24
const char* _Nullable perMachineDirectory = NULL;
andreas@1173
    25
const char* _Nullable perUserDirectory = NULL;
andreas@1173
    26
#endif
andreas@1173
    27
dirk@187
    28
NSURL *s_homeURL;
dirk@625
    29
dirk@601
    30
static BOOL s_unEncryptedSubjectEnabled = NO;
dirk@625
    31
static BOOL s_passiveModeEnabled = NO;
dirk@187
    32
dirk@187
    33
@implementation PEPObjCAdapter
dirk@187
    34
andreas@416
    35
#pragma mark - SUBJECT PROTECTION
andreas@416
    36
dirk@601
    37
+ (BOOL)unEncryptedSubjectEnabled;
andreas@416
    38
{
dirk@601
    39
    return s_unEncryptedSubjectEnabled;
andreas@416
    40
}
andreas@416
    41
dirk@601
    42
+ (void)setUnEncryptedSubjectEnabled:(BOOL)enabled;
andreas@416
    43
{
dirk@601
    44
    s_unEncryptedSubjectEnabled = enabled;
andreas@416
    45
}
andreas@416
    46
dirk@625
    47
#pragma mark - Passive Mode
dirk@625
    48
dirk@625
    49
+ (BOOL)passiveModeEnabled
dirk@625
    50
{
dirk@625
    51
    return s_passiveModeEnabled;
dirk@625
    52
}
dirk@625
    53
dirk@625
    54
+ (void)setPassiveModeEnabled:(BOOL)enabled
dirk@625
    55
{
dirk@625
    56
    s_passiveModeEnabled = enabled;
dirk@625
    57
}
dirk@625
    58
andreas@416
    59
#pragma mark - DB PATHS
andreas@416
    60
dirk@187
    61
+ (void)initialize
dirk@187
    62
{
dirk@187
    63
    s_homeURL = [self createApplicationDirectory];
dirk@442
    64
    [self setHomeDirectory:s_homeURL]; // Important, defines $HOME and $TEMP for the engine
dirk@187
    65
}
dirk@187
    66
dirk@187
    67
+ (NSURL *)homeURL
dirk@187
    68
{
dirk@187
    69
    return s_homeURL;
dirk@187
    70
}
dirk@187
    71
dirk@187
    72
+ (NSURL *)createApplicationDirectory
dirk@187
    73
{
dirk@187
    74
    NSString *bundleID = [[NSBundle mainBundle] bundleIdentifier];
dirk@187
    75
    if (!bundleID) {
dirk@187
    76
        // This can happen in unit tests
dirk@187
    77
        bundleID = @"test";
dirk@187
    78
    }
dirk@187
    79
    NSFileManager *fm = [NSFileManager defaultManager];
dirk@187
    80
    NSURL *dirPath = nil;
andreas@268
    81
    
dirk@187
    82
    // Find the application support directory in the home directory.
dirk@187
    83
    NSArray *appSupportDir = [fm URLsForDirectory:NSApplicationSupportDirectory
dirk@187
    84
                                        inDomains:NSUserDomainMask];
dirk@187
    85
    if ([appSupportDir count] > 0)
dirk@187
    86
    {
dirk@187
    87
        // Append the bundle ID to the URL for the
dirk@187
    88
        // Application Support directory.
dirk@187
    89
        // Mainly needed for OS X, but doesn't do any harm on iOS
dirk@187
    90
        dirPath = [[appSupportDir objectAtIndex:0] URLByAppendingPathComponent:bundleID];
andreas@268
    91
        
dirk@187
    92
        // If the directory does not exist, this method creates it.
dirk@187
    93
        // This method is only available in OS X v10.7 and iOS 5.0 or later.
dirk@187
    94
        NSError *theError = nil;
dirk@187
    95
        if (![fm createDirectoryAtURL:dirPath withIntermediateDirectories:YES
dirk@187
    96
                           attributes:nil error:&theError])
dirk@187
    97
        {
dirk@187
    98
            // Handle the error.
dirk@187
    99
            return nil;
dirk@187
   100
        }
dirk@187
   101
    }
andreas@268
   102
    
dirk@187
   103
    return dirPath;
dirk@187
   104
}
dirk@187
   105
dirk@442
   106
+ (void)setHomeDirectory:(NSURL *)homeDir
dirk@187
   107
{
andreas@1173
   108
#if TARGET_OS_IPHONE
andreas@1173
   109
    if (perUserDirectory) {
andreas@1173
   110
        free((void *) perUserDirectory);
andreas@1173
   111
    }
andreas@1173
   112
    perUserDirectory = strdup([homeDir path].UTF8String);
andreas@1173
   113
#endif
dirk@187
   114
}
dirk@187
   115
andreas@276
   116
+ (NSString *)getBundlePathFor: (NSString *) filename
dirk@187
   117
{
dirk@187
   118
    return nil;
dirk@187
   119
}
dirk@187
   120
dirk@442
   121
+ (NSString *)copyAssetsIntoDocumentsDirectory:(NSBundle *)rootBundle
dirk@442
   122
                                    bundleName:(NSString *)bundleName
dirk@442
   123
                                      fileName:(NSString *)fileName {
andreas@268
   124
    
dirk@442
   125
    NSURL *homeUrl = s_homeURL;
dirk@187
   126
    NSString *documentsDirectory = [homeUrl path];
dirk@187
   127
    
dirk@187
   128
    if(!(documentsDirectory && bundleName && fileName))
dirk@187
   129
        return nil;
dirk@187
   130
    
dirk@187
   131
    // Check if the database file exists in the documents directory.
dirk@187
   132
    NSString *destinationPath = [documentsDirectory stringByAppendingPathComponent:fileName];
dirk@187
   133
    if (![[NSFileManager defaultManager] fileExistsAtPath:destinationPath]) {
dirk@187
   134
        // The file does not exist in the documents directory, so copy it from bundle now.
dirk@187
   135
        NSBundle *bundleObj = [NSBundle bundleWithPath:
dirk@187
   136
                               [[rootBundle resourcePath]
dirk@187
   137
                                stringByAppendingPathComponent: bundleName]];
dirk@187
   138
        if (!bundleObj)
dirk@187
   139
            return nil;
dirk@187
   140
        
dirk@187
   141
        NSString *sourcePath =[[bundleObj resourcePath] stringByAppendingPathComponent: fileName];
dirk@187
   142
        
dirk@187
   143
        NSError *error;
dirk@187
   144
        [[NSFileManager defaultManager]
dirk@187
   145
         copyItemAtPath:sourcePath toPath:destinationPath error:&error];
dirk@187
   146
        
dirk@187
   147
        // Check if any error occurred during copying and display it.
dirk@187
   148
        if (error != nil) {
dirk@187
   149
            NSLog(@"%@", [error localizedDescription]);
dirk@187
   150
            return nil;
dirk@187
   151
        }
dirk@187
   152
    }
dirk@187
   153
    return destinationPath;
dirk@187
   154
}
dirk@187
   155
andreas@1173
   156
+ (void)setupTrustWordsDB:(NSBundle *)rootBundle {
andreas@1173
   157
#if TARGET_OS_IPHONE
dirk@442
   158
    NSString *systemDBPath = [PEPObjCAdapter
dirk@442
   159
                              copyAssetsIntoDocumentsDirectory:rootBundle
dirk@442
   160
                              bundleName:@"pEpTrustWords.bundle"
dirk@442
   161
                              fileName:@"system.db"];
andreas@1173
   162
    if (perMachineDirectory) {
andreas@1173
   163
        free((void *) perMachineDirectory);
dirk@187
   164
    }
andreas@1173
   165
    perMachineDirectory = strdup(systemDBPath.UTF8String);
andreas@1173
   166
#endif
dirk@187
   167
}
dirk@187
   168
dirk@187
   169
+ (void)setupTrustWordsDB
dirk@187
   170
{
dirk@187
   171
    [PEPObjCAdapter setupTrustWordsDB:[NSBundle mainBundle]];
dirk@187
   172
}
dirk@187
   173
dirk@187
   174
@end