"Fossies" - the Fresh Open Source Software Archive

Member "jitsi-meet-7555/ios/sdk/src/dropbox/Dropbox.m" (28 Sep 2023, 6534 Bytes) of package /linux/misc/jitsi-meet-7555.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Matlab source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    1 /*
    2  * Copyright @ 2017-present Atlassian Pty Ltd
    3  *
    4  * Licensed under the Apache License, Version 2.0 (the "License");
    5  * you may not use this file except in compliance with the License.
    6  * You may obtain a copy of the License at
    7  *
    8  *     http://www.apache.org/licenses/LICENSE-2.0
    9  *
   10  * Unless required by applicable law or agreed to in writing, software
   11  * distributed under the License is distributed on an "AS IS" BASIS,
   12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   13  * See the License for the specific language governing permissions and
   14  * limitations under the License.
   15  */
   16 
   17 #import <React/RCTBridgeModule.h>
   18 #import <ObjectiveDropboxOfficial/ObjectiveDropboxOfficial.h>
   19 
   20 #import "Dropbox.h"
   21 
   22 RCTPromiseResolveBlock currentResolve = nil;
   23 RCTPromiseRejectBlock currentReject = nil;
   24 
   25 @implementation Dropbox
   26 
   27 + (NSString *)getAppKey{
   28     NSArray *urlTypes
   29         = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleURLTypes"];
   30 
   31     for (NSDictionary<NSString *, NSArray *> *urlType in urlTypes) {
   32         NSArray *urlSchemes = urlType[@"CFBundleURLSchemes"];
   33 
   34         if (urlSchemes) {
   35             for (NSString *urlScheme in urlSchemes) {
   36                 if (urlScheme && [urlScheme hasPrefix:@"db-"]) {
   37                     return [urlScheme substringFromIndex:3];
   38                 }
   39             }
   40         }
   41     }
   42    
   43     return nil;
   44 }
   45 
   46 RCT_EXPORT_MODULE();
   47 
   48 + (BOOL)requiresMainQueueSetup {
   49     return NO;
   50 }
   51 
   52 - (NSDictionary *)constantsToExport {
   53     BOOL enabled = [Dropbox getAppKey] != nil;
   54     
   55     return @{
   56         @"ENABLED": [NSNumber numberWithBool:enabled]
   57     };
   58 };
   59 
   60 RCT_EXPORT_METHOD(authorize:(RCTPromiseResolveBlock)resolve
   61                   reject:(__unused RCTPromiseRejectBlock)reject) {
   62     currentResolve = resolve;
   63     currentReject = reject;
   64     
   65     dispatch_async(dispatch_get_main_queue(), ^{
   66         DBScopeRequest *scopeRequest = [[DBScopeRequest alloc] initWithScopeType:DBScopeTypeUser
   67                                                                           scopes:@[]
   68                                                             includeGrantedScopes:NO];
   69         [DBClientsManager authorizeFromControllerV2:[UIApplication sharedApplication]
   70                                          controller:[[self class] topMostController]
   71                               loadingStatusDelegate:nil
   72                                             openURL:^(NSURL *url) { [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil]; }
   73                                        scopeRequest:scopeRequest];
   74     });
   75 }
   76 
   77 RCT_EXPORT_METHOD(getDisplayName: (NSString *)token
   78                   resolve: (RCTPromiseResolveBlock)resolve
   79                   reject:(RCTPromiseRejectBlock)reject) {
   80     DBUserClient *client = [[DBUserClient alloc] initWithAccessToken:token];
   81     [[client.usersRoutes getCurrentAccount] setResponseBlock:^(DBUSERSFullAccount *result, DBNilObject *routeError, DBRequestError *networkError) {
   82         if (result) {
   83             resolve(result.name.displayName);
   84         } else {
   85             NSString *msg = @"Failed!";
   86             if (networkError) {
   87                 msg = [NSString stringWithFormat:@"Failed! Error: %@", networkError];
   88             }
   89             reject(@"getDisplayName", @"Failed", nil);
   90         }
   91     }];
   92 
   93 }
   94 
   95 RCT_EXPORT_METHOD(getSpaceUsage: (NSString *)token
   96                   resolve: (RCTPromiseResolveBlock)resolve
   97                   reject:(RCTPromiseRejectBlock)reject) {
   98     DBUserClient *client = [[DBUserClient alloc] initWithAccessToken:token];
   99     [[client.usersRoutes getSpaceUsage] setResponseBlock:^(DBUSERSSpaceUsage *result, DBNilObject *routeError, DBRequestError *networkError) {
  100         if (result) {
  101             DBUSERSSpaceAllocation *allocation = result.allocation;
  102             NSNumber *allocated = 0;
  103             NSNumber *used = 0;
  104             if ([allocation isIndividual]) {
  105                 allocated = allocation.individual.allocated;
  106                 used = result.used;
  107             } else if ([allocation isTeam]) {
  108                 allocated = allocation.team.allocated;
  109                 used = allocation.team.used;
  110             }
  111             id objects[] = { used, allocated };
  112             id keys[] = { @"used", @"allocated" };
  113             NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:objects
  114                                             forKeys:keys
  115                                             count:2];
  116             resolve(dictionary);
  117         } else {
  118             NSString *msg = @"Failed!";
  119             if (networkError) {
  120                 msg = [NSString stringWithFormat:@"Failed! Error: %@", networkError];
  121             }
  122             reject(@"getSpaceUsage", msg, nil);
  123         }
  124     }];
  125 
  126 }
  127 
  128 + (BOOL)application:(UIApplication *)app
  129             openURL:(NSURL *)url
  130             options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {
  131     if (currentReject == nil || currentResolve == nil) {
  132         return NO;
  133     }
  134     
  135     BOOL canHandle = [DBClientsManager handleRedirectURL:url completion:^(DBOAuthResult *authResult) {
  136         if (authResult) {
  137             if ([authResult isSuccess]) {
  138                 NSInteger msTimestamp = authResult.accessToken.tokenExpirationTimestamp * 1000;
  139                 NSDictionary *authInfo = @{@"token": authResult.accessToken.accessToken,
  140                                            @"rToken": authResult.accessToken.refreshToken,
  141                                            @"expireDate": @(msTimestamp)
  142                 };
  143                 currentResolve(authInfo);
  144             } else {
  145                 NSString *msg;
  146                 if ([authResult isError]) {
  147                     msg = [NSString stringWithFormat:@"%@, error type: %zd", [authResult errorDescription], [authResult errorType]];
  148                 } else {
  149                     msg = @"OAuth canceled!";
  150                 }
  151                 currentReject(@"authorize", msg, nil);
  152             }
  153             currentResolve = nil;
  154             currentReject = nil;
  155         }
  156     }];
  157     
  158     return canHandle;
  159 }
  160 
  161 + (UIViewController *)topMostController {
  162     UIViewController *topController
  163         = [UIApplication sharedApplication].keyWindow.rootViewController;
  164     
  165     while (topController.presentedViewController) {
  166         topController = topController.presentedViewController;
  167     }
  168     
  169     return topController;
  170 }
  171 
  172 + (void)setAppKey {
  173     NSString *appKey = [self getAppKey];
  174 
  175     if (appKey) {
  176         [DBClientsManager setupWithAppKey:appKey];
  177     }
  178 }
  179 
  180 @end