added iOS source code
[wl-app.git] / iOS / Pods / Realm / Realm / RLMRealmConfiguration+Sync.mm
1 ////////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright 2016 Realm Inc.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 ////////////////////////////////////////////////////////////////////////////
18
19 #import "RLMRealmConfiguration+Sync.h"
20
21 #import "RLMRealmConfiguration_Private.hpp"
22 #import "RLMSyncConfiguration_Private.hpp"
23 #import "RLMSyncUser_Private.hpp"
24 #import "RLMSyncManager_Private.h"
25 #import "RLMSyncUtil_Private.hpp"
26 #import "RLMUtil.hpp"
27
28 #import "sync/sync_config.hpp"
29 #import "sync/sync_manager.hpp"
30
31 @implementation RLMRealmConfiguration (Sync)
32
33 #pragma mark - API
34
35 - (void)setSyncConfiguration:(RLMSyncConfiguration *)syncConfiguration {
36     if (self.config.should_compact_on_launch_function) {
37         @throw RLMException(@"Cannot set `syncConfiguration` when `shouldCompactOnLaunch` is set.");
38     }
39     RLMSyncUser *user = syncConfiguration.user;
40     if (user.state == RLMSyncUserStateError) {
41         @throw RLMException(@"Cannot set a sync configuration which has an errored-out user.");
42     }
43
44     NSURL *realmURL = syncConfiguration.realmURL;
45     // Ensure sync manager is initialized, if it hasn't already been.
46     [RLMSyncManager sharedManager];
47     NSAssert(user.identity, @"Cannot call this method on a user that doesn't have an identity.");
48     if (syncConfiguration.customFileURL) {
49         self.config.path = syncConfiguration.customFileURL.path.UTF8String;
50     } else {
51         self.config.path = SyncManager::shared().path_for_realm(*[user _syncUser],
52                                                                 [realmURL.absoluteString UTF8String]);
53     }
54     self.config.in_memory = false;
55     self.config.sync_config = std::make_shared<realm::SyncConfig>([syncConfiguration rawConfiguration]);
56     self.config.schema_mode = realm::SchemaMode::Additive;
57     if (!self.config.encryption_key.empty()) {
58         auto& sync_encryption_key = self.config.sync_config->realm_encryption_key;
59         sync_encryption_key = std::array<char, 64>();
60         std::copy_n(self.config.encryption_key.begin(), 64, sync_encryption_key->begin());
61     }
62 }
63
64 - (RLMSyncConfiguration *)syncConfiguration {
65     if (!self.config.sync_config) {
66         return nil;
67     }
68     realm::SyncConfig& sync_config = *self.config.sync_config;
69     return [[RLMSyncConfiguration alloc] initWithRawConfig:sync_config];
70 }
71
72 @end