1 /* 2 * Copyright (c) 2021-2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef CONTACT_NAPI_COMMON_H 17 #define CONTACT_NAPI_COMMON_H 18 19 #include "datashare_predicates.h" 20 #include "datashare_helper.h" 21 #include "napi/native_api.h" 22 #include "napi/native_common.h" 23 #include "napi/native_node_api.h" 24 #include "rdb_helper.h" 25 #include "rdb_open_callback.h" 26 #include "rdb_predicates.h" 27 #include "rdb_store.h" 28 29 namespace OHOS { 30 namespace ContactsApi { 31 constexpr int MAX_PARAMS = 10; 32 constexpr int ERROR = -1; 33 constexpr int SUCCESS = 0; 34 constexpr int RESULT_DATA_SIZE = 2; 35 constexpr int RDB_PERMISSION_ERROR = -2; 36 constexpr int RDB_PARAMETER_ERROR = -3; 37 constexpr int VERIFICATION_PERMISSION_ERROR = -4; 38 constexpr int PERMISSION_ERROR = 201; 39 constexpr int PARAMETER_ERROR = 401; 40 41 constexpr int NAPI_GET_STRING_SIZE = 256; 42 constexpr int REQUEST_PARAMS_COUNT_ONE = 1; 43 44 // NAPI callback type 45 constexpr int NAPI_CALL_TYPE_CALLBACK = 0; 46 constexpr int NAPI_CALL_TYPE_PROMISE = 1; 47 48 // NAPI type 49 constexpr int TYPE_NAPI_ERROR = -1; 50 constexpr int TYPE_NAPI_NUMBER = 0; 51 constexpr int TYPE_NAPI_STRING = 1; 52 constexpr int TYPE_HOLDER = 2; 53 constexpr int TYPE_ATTR = 3; 54 constexpr int TYPE_CONTACT = 4; 55 56 // Execute action code 57 constexpr int ADD_CONTACT = 1001; 58 constexpr int DELETE_CONTACT = 2001; 59 constexpr int UPDATE_CONTACT = 3001; 60 61 constexpr int QUERY_CONTACTS = 4001; 62 constexpr int QUERY_CONTACT = 4008; 63 64 constexpr int QUERY_CONTACTS_BY_EMAIL = 4002; 65 constexpr int QUERY_CONTACTS_BY_PHONE_NUMBER = 4003; 66 constexpr int QUERY_GROUPS = 4004; 67 constexpr int QUERY_HOLDERS = 4005; 68 constexpr int QUERY_KEY = 4006; 69 constexpr int QUERY_MY_CARD = 4007; 70 constexpr int IS_LOCAL_CONTACT = 5008; 71 constexpr int IS_MY_CARD = 5009; 72 73 constexpr int SELECT_CONTACT = 6001; 74 75 // contactsData type 76 constexpr int EMAIL = 1; 77 constexpr int IM = 2; 78 constexpr int NICKNAME = 3; 79 constexpr int ORGANIZATION = 4; 80 constexpr int PHONE = 5; 81 constexpr int NAME = 6; 82 constexpr int POSTAL_ADDRESS = 7; 83 constexpr int PHOTO = 8; 84 constexpr int GROUP_MEMBERSHIP = 9; 85 constexpr int NOTE = 10; 86 constexpr int CONTACT_EVENT = 11; 87 constexpr int WEBSITE = 12; 88 constexpr int RELATION = 13; 89 constexpr int CONTACT_MISC = 14; 90 constexpr int HICALL_DEVICE = 15; 91 constexpr int CAMCARD = 16; 92 constexpr int SIP_ADDRESS = 17; 93 94 constexpr int ARGS_ZERO = 0; 95 constexpr int ARGS_ONE = 1; 96 constexpr int ARGS_TWO = 2; 97 constexpr int ARGS_THREE = 3; 98 constexpr int ARGS_FOUR = 4; 99 constexpr int ARGS_FIVE = 5; 100 const std::string CONTACTS_DATA_URI = "datashare:///com.ohos.contactsdataability"; 101 102 struct ExecuteHelper { ExecuteHelperOHOS::ContactsApi::ExecuteHelper103 ExecuteHelper() 104 : work(nullptr), deferred(nullptr), sync(NAPI_CALL_TYPE_PROMISE), 105 argc(0), abilityContext(nullptr), actionCode(-1), callBack(nullptr), 106 childActionCode(0), promise(nullptr), resultData(-1), resultSet(nullptr) {} 107 napi_async_work work; 108 napi_deferred deferred; 109 int sync; 110 unsigned int argc; 111 napi_value argv[MAX_PARAMS] = {0}; 112 napi_value abilityContext; 113 int actionCode; 114 napi_ref callBack; 115 int childActionCode; 116 napi_value promise; 117 // query 118 std::vector<std::string> columns; 119 // condition 120 DataShare::DataSharePredicates predicates; 121 // delete contact predicates for update contact 122 DataShare::DataSharePredicates deletePredicates; 123 // update 124 std::vector<DataShare::DataShareValuesBucket> valueUpdateContact; 125 // insert 126 std::vector<DataShare::DataShareValuesBucket> valueContact; 127 // insertContactData 128 std::vector<DataShare::DataShareValuesBucket> valueContactData; 129 // dataShareHelper 130 std::shared_ptr<DataShare::DataShareHelper> dataShareHelper; 131 132 // operation result 133 int resultData; 134 std::shared_ptr<DataShare::DataShareResultSet> resultSet; 135 }; 136 } // namespace ContactsApi 137 } // namespace OHOS 138 #endif // CONTACT_NAPI_COMMON_H