JLChen
2021-08-02 38f4fb064df09f344fc3237409c76a9fba2a8a9e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
 * Copyright (c) 2010-2020 Belledonne Communications SARL.
 *
 * This file is part of linphone-iphone
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
 
#import <Foundation/Foundation.h>
 
#import "LinphoneManager.h"
#import "XMLRPCHelper.h"
#import "Utils.h"
#import "PhoneMainView.h"
 
/* This subclass allows use to store the block to execute on success */
@interface XMLRPCRequestObject : NSObject
@property(copy, nonatomic) void (^XMLRPCHelperBlockSuccess)(NSString *something);
@property(copy, nonatomic) void (^XMLRPCHelperBlockError)(NSString *something);
@property LinphoneXmlRpcRequest *xmlRpcRequest;
@end
 
@implementation XMLRPCRequestObject
@end
 
@implementation XMLRPCHelper
 
#pragma mark - API
 
// typedef void (^XMLRPCHelperBlock)(NSString *something);
 
// XMLRPCHelperBlock successBlock = nil;
// XMLRPCHelperBlock errorBlock = nil;
 
NSMutableArray *personsArray;
 
/*****************************************************************************************/
+ (void)initArray {
    personsArray = [[NSMutableArray alloc] init];
}
 
+ (void)sendXMLRPCRequest:(NSString *)method {
    [self sendXMLRPCRequestWithParams:method withParams:nil onSuccess:nil onError:nil];
}
 
+ (void)sendXMLRPCRequestWithParams:(NSString *)method withParams:(NSArray *)params {
    [self sendXMLRPCRequestWithParams:method withParams:params onSuccess:nil onError:nil];
}
 
+ (void)sendXMLRPCRequestWithParams:(NSString *)method
                         withParams:(NSArray *)params
                          onSuccess:(void (^)(NSString *))successBk {
    [self sendXMLRPCRequestWithParams:method withParams:params onSuccess:successBk onError:nil];
}
 
// change block by callback and implement callback with different behavior if success (: call InAppManager) or error (:
// manage error here)
+ (void)sendXMLRPCRequestWithParams:(NSString *)method
                         withParams:(NSArray *)params
                          onSuccess:(void (^)(NSString *))successBk
                            onError:(void (^)(NSString *req))errorBk {
    LOGI(@"XMLRPC %@ - %@", method, params);
    XMLRPCRequestObject *requestObject = [XMLRPCRequestObject alloc];
    const char *URL =
        [LinphoneManager.instance lpConfigStringForKey:@"receipt_validation_url" inSection:@"in_app_purchase"]
            .UTF8String;
 
    requestObject.XMLRPCHelperBlockSuccess = successBk;
    requestObject.XMLRPCHelperBlockError = errorBk;
 
    // Create LinphoneXMLRPCRequest
    LinphoneXmlRpcSession *requestSession = linphone_xml_rpc_session_new(LC, URL);
    // LinphoneXmlRpcRequest *request = linphone_xml_rpc_request_new(method.UTF8String, LinphoneXmlRpcArgString);
    requestObject.xmlRpcRequest = linphone_xml_rpc_request_new(LinphoneXmlRpcArgString, method.UTF8String);
 
    [personsArray addObject:requestObject];
    // Set argument to this LinphoneXMLRPCRequest
    for (NSString *item in params) {
        NSLog(@"Linphone XMLRPC Request with argument: %@", item);
        linphone_xml_rpc_request_add_string_arg(requestObject.xmlRpcRequest, item.UTF8String);
    }
 
    // Ref and send the LinphoneXMLRPCRequest
    requestSession = linphone_xml_rpc_session_ref(requestSession);
    linphone_xml_rpc_session_send_request(requestSession, requestObject.xmlRpcRequest);
 
    // Set the callbacks to this LinphoneXMLRPCRequest
    LinphoneXmlRpcRequestCbs *cbs = linphone_xml_rpc_request_get_callbacks(requestObject.xmlRpcRequest);
 
    // Register XMLRPCHelper in user data to get it back on Callback rised
    XMLRPCHelper *xMLRPCHelper = [[XMLRPCHelper alloc] init];
    linphone_xml_rpc_request_set_user_data(requestObject.xmlRpcRequest, ((void *)CFBridgingRetain(xMLRPCHelper)));
 
    // Set the response Callback
    linphone_xml_rpc_request_cbs_set_response(cbs, linphone_xmlrpc_call_back_received);
}
 
static void linphone_xmlrpc_call_back_received(LinphoneXmlRpcRequest *request) {
    [(__bridge XMLRPCHelper *)linphone_xml_rpc_request_get_user_data(request) dealWithXmlRpcResponse:request];
}
 
- (void)dealWithXmlRpcResponse:(LinphoneXmlRpcRequest *)request {
    XMLRPCRequestObject *xmlrpcObject;
    NSInteger index = 0;
    for (int i = 0; i < [personsArray count]; i++) {
        xmlrpcObject = [personsArray objectAtIndex:i];
        if (xmlrpcObject.xmlRpcRequest == request)
            break;
        index++;
    }
 
    NSString *responseString =
        [NSString stringWithFormat:@"%s", (linphone_xml_rpc_request_get_string_response(request))];
    LOGI(@"XMLRPC query: %@", responseString);
    if (linphone_xml_rpc_request_get_status(request) == LinphoneXmlRpcStatusOk) {
        // Call success block
        xmlrpcObject.XMLRPCHelperBlockSuccess(responseString);
    } else if (linphone_xml_rpc_request_get_status(request) == LinphoneXmlRpcStatusFailed) {
        if (xmlrpcObject.XMLRPCHelperBlockError != nil) {
            LOGI(@"XMLRPC query ErrorBlock rised");
            xmlrpcObject.XMLRPCHelperBlockError(responseString);
        }
        // Display Error alert
        [self displayErrorPopup:@"LinphoneXMLRPC Request Failed"];
    }
    linphone_xml_rpc_request_unref(request);
    [personsArray removeObjectAtIndex:index];
}
 
#pragma mark - Error alerts
 
- (void)displayErrorPopup:(NSString *)error {
    UIAlertController *errView = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Server request error", nil)
                                                                     message:error
                                                              preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", nil)
                                                            style:UIAlertActionStyleDefault
                                                          handler:^(UIAlertAction * action) {}];
    
    [errView addAction:defaultAction];
    [PhoneMainView.instance presentViewController:errView animated:YES completion:nil];
}
 
@end
 
/*****************************************************************************************/