summaryrefslogtreecommitdiff
path: root/lib-contact/ct-list/src/CtListModelGroup.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'lib-contact/ct-list/src/CtListModelGroup.cpp')
-rwxr-xr-xlib-contact/ct-list/src/CtListModelGroup.cpp419
1 files changed, 419 insertions, 0 deletions
diff --git a/lib-contact/ct-list/src/CtListModelGroup.cpp b/lib-contact/ct-list/src/CtListModelGroup.cpp
new file mode 100755
index 0000000..217d98c
--- /dev/null
+++ b/lib-contact/ct-list/src/CtListModelGroup.cpp
@@ -0,0 +1,419 @@
+/*
+ * Copyright (c) 2009-2015 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#include <contacts.h>
+
+#include "CtString.h"
+#include "ContactsDebug.h"
+#include "CtListModelGroup.h"
+
+CtListModelGroup::CtListModelGroup()
+{
+ WENTER();
+
+ __initialize();
+}
+
+CtListModelGroup::~CtListModelGroup()
+{
+ WENTER();
+
+ __groupDataList.clear();
+ __groupMemberDataList.clear();
+}
+
+
+std::shared_ptr< CtListDataPerson > CtListModelGroup::getAtPersonData(int index)
+{
+ if (index < (int)__groupMemberDataList.size()) {
+ return __groupMemberDataList[index];
+ }
+ else {
+ return NULL;
+ }
+}
+
+std::shared_ptr< CtListDataGroup > CtListModelGroup::getAtGroupData(int index)
+{
+ if (index < (int)__groupDataList.size()) {
+ return __groupDataList[index];
+ }
+ else {
+ return NULL;
+ }
+}
+
+int CtListModelGroup::getAvailableGroupCount(void)
+{
+ WENTER();
+
+ int count = 0;
+ int err = CONTACTS_ERROR_NONE;
+
+ contacts_query_h query = NULL;
+
+ unsigned int group_projection[] = {
+ _contacts_group_relation.group_id,
+ };
+
+ err = contacts_query_create(_contacts_group_relation._uri, &query);
+ WPWARN(CONTACTS_ERROR_NONE != err, "contacts_query_create() Failed(%d)", err);
+ err = contacts_query_set_projection(query, group_projection, sizeof(group_projection)/sizeof(int));
+ WPWARN(CONTACTS_ERROR_NONE != err, "contacts_query_set_projection() Failed(%d)", err);
+ err = contacts_query_set_distinct(query, true);
+ WPWARN(CONTACTS_ERROR_NONE != err, "contacts_query_set_distinct() Failed(%d)", err);
+
+ err = contacts_db_get_count_with_query(query, &count);
+ WPWARN(CONTACTS_ERROR_NONE != err, "contacts_db_get_records_with_query() Failed(%d)", err);
+
+ err = contacts_query_destroy(query);
+ WPWARN(CONTACTS_ERROR_NONE != err, "contacts_query_destroy() Failed(%d)", err);
+
+ WDEBUG("count : %d", count);
+
+ return count;
+
+}
+
+int CtListModelGroup::getAvailableGroups(void)
+{
+ WENTER();
+
+ int offset = 0;
+ int limit = 0;
+ int err = CONTACTS_ERROR_NONE;
+
+ contacts_list_h list = NULL;
+ contacts_query_h query = NULL;
+
+ __groupDataList.clear();
+
+ err = contacts_query_create(_contacts_group_relation._uri, &query);
+ WPWARN(CONTACTS_ERROR_NONE != err, "contacts_query_create() Failed(%d)", err);
+ err = contacts_query_set_sort(query, _contacts_group_relation.name, true);
+ WPWARN(CONTACTS_ERROR_NONE != err, "contacts_query_set_sort() Failed(%d)", err);
+ err = contacts_query_set_distinct(query, true);
+ WPWARN(CONTACTS_ERROR_NONE != err, "contacts_query_set_distinct() Failed(%d)", err);
+
+ err = contacts_db_get_records_with_query(query, offset, limit, &list);
+ WPWARN(CONTACTS_ERROR_NONE != err, "contacts_db_get_records_with_query() Failed(%d)", err);
+
+ err = contacts_query_destroy(query);
+ WPWARN(CONTACTS_ERROR_NONE != err, "contacts_query_destroy() Failed(%d)", err);
+
+ int count = 0;
+ err = contacts_list_get_count(list, &count);
+ WPWARN(CONTACTS_ERROR_NONE != err, "contacts_list_get_count() Failed(%d)", err);
+
+ if(count > 0) {
+ count = __makeGroupListUnifiedByName(list, false);
+ }
+
+ WDEBUG("count : %d", count);
+
+ err = contacts_list_destroy(list, true);
+ WPWARN(CONTACTS_ERROR_NONE != err, "contacts_list_destroy() Failed(%d)", err);
+
+ return count;
+}
+
+int CtListModelGroup::getAllMembersByGroupName(char* groupName)
+{
+ __groupMemberDataList.clear();
+
+ int err = CONTACTS_ERROR_NONE;
+ int offset = 0;
+ int limit = 0;
+
+ contacts_list_h list = NULL;
+ contacts_query_h query = NULL;
+
+ query = __getQuery(groupName);
+
+ err = contacts_db_get_records_with_query(query, offset, limit, &list);
+ WPWARN(CONTACTS_ERROR_NONE != err, "contacts_db_get_records_with_query() Failed(%d)", err);
+
+ err = contacts_query_destroy(query);
+ WPWARN(CONTACTS_ERROR_NONE != err, "contacts_query_destroy() Failed(%d)", err);
+
+ int count = 0;
+ err = contacts_list_get_count(list, &count);
+ WPWARN(CONTACTS_ERROR_NONE != err, "contacts_list_get_count() Failed(%d)", err);
+
+ if (count > 0) {
+ __makeMemberList(list);
+ }
+
+ err = contacts_list_destroy(list, true);
+ WPWARN(CONTACTS_ERROR_NONE != err, "contacts_list_destroy() Failed(%d)", err);
+
+ return count;
+}
+
+void CtListModelGroup::__initialize(void)
+{
+ __groupDataList.clear();
+ __groupMemberDataList.clear();
+}
+
+contacts_query_h CtListModelGroup::__getQuery(char* groupName)
+{
+ unsigned int person_group_projection[] = {
+ _contacts_person_grouprel.person_id,
+ _contacts_person_grouprel.status,
+ _contacts_person_grouprel.display_name,
+ _contacts_person_grouprel.display_name_index,
+ _contacts_person_grouprel.addressbook_ids,
+ _contacts_person_grouprel.has_phonenumber,
+ _contacts_person_grouprel.image_thumbnail_path,
+ _contacts_person_grouprel.is_favorite,
+ };
+
+ contacts_query_h query = NULL;
+ contacts_filter_h filter = NULL;
+
+ int err = CONTACTS_ERROR_NONE;
+
+ err = contacts_query_create(_contacts_person_grouprel._uri, &query);
+ WPWARN(CONTACTS_ERROR_NONE != err, "contacts_query_create() Failed(%d)", err);
+ err = contacts_filter_create(_contacts_person_grouprel._uri, &filter);
+ WPWARN(CONTACTS_ERROR_NONE != err, "contacts_filter_create() Failed(%d)", err);
+
+ std::vector<int> groupIds = __getGroupIdsByName(groupName);
+
+ bool isFirst = true;
+ for (auto iter = groupIds.begin(); iter != groupIds.end(); iter++) {
+ if (isFirst) {
+ isFirst = false;
+ }
+ else {
+ err = contacts_filter_add_operator(filter, CONTACTS_FILTER_OPERATOR_OR);
+ WPWARN(CONTACTS_ERROR_NONE != err, "contacts_filter_add_operator() Failed(%d)", err);
+ }
+ err = contacts_filter_add_int(filter, _contacts_person_grouprel.group_id, CONTACTS_MATCH_EQUAL, *iter);
+ WPWARN(CONTACTS_ERROR_NONE != err, "contacts_filter_add_int() Failed(%d)", err);
+ }
+
+ err = contacts_query_set_projection(query, person_group_projection, sizeof(person_group_projection)/sizeof(int));
+ WPWARN(CONTACTS_ERROR_NONE != err, "contacts_query_set_projection() Failed(%d)", err);
+ err = contacts_query_set_distinct(query, true);
+ WPWARN(CONTACTS_ERROR_NONE != err, "contacts_query_set_distinct() Failed(%d)", err);
+ err = contacts_query_set_filter(query, filter);
+ WPWARN(CONTACTS_ERROR_NONE != err, "contacts_query_set_filter() Failed(%d)", err);
+
+ err = contacts_filter_destroy(filter);
+ WPWARN(CONTACTS_ERROR_NONE != err, "contacts_filter_destroy() Failed(%d)", err);
+
+ groupIds.clear();
+
+ return query;
+}
+
+int CtListModelGroup::__makeGroupListUnifiedByName(contacts_list_h list, bool needNotList)
+{
+ WENTER();
+
+ WPRET_VM(list == NULL, 0, "list is NULL");
+
+ int err = CONTACTS_ERROR_NONE;
+ int groupCount = 0;
+
+ char* groupName = strdup("");
+
+ contacts_record_h recordGroup = NULL;
+ while (CONTACTS_ERROR_NONE == contacts_list_get_current_record_p(list, &recordGroup)) {
+ if (recordGroup){
+ char* currentName = NULL;
+
+ err = contacts_record_get_str_p(recordGroup, _contacts_group_relation.name, &currentName);
+ WPWARN(CONTACTS_ERROR_NONE != err, "contacts_record_get_str() Failed(%d)", err);
+
+ if ((SAFE_STRCMP(currentName, "") != 0 && SAFE_STRCMP(currentName, groupName)) == 0) {
+ err = contacts_list_next(list);
+ WPWARN(CONTACTS_ERROR_NONE != err, "contacts_list_next() Failed(%d)", err);
+
+ continue;
+ }
+
+ if (needNotList == false) {
+ std::shared_ptr < CtListDataGroup > dataGroup (std::make_shared < CtListDataGroup > (recordGroup));
+ __groupDataList.push_back(dataGroup);
+ }
+
+ groupCount++;
+
+ if (groupName) {
+ free(groupName);
+ groupName = NULL;
+ }
+ groupName = SAFE_STRDUP(currentName);
+ }
+ err = contacts_list_next(list);
+ WPWARN(CONTACTS_ERROR_NONE != err, "contacts_list_next() Failed(%d)", err);
+ }
+ if (groupName) {
+ free(groupName);
+ }
+
+ return groupCount;
+}
+
+int CtListModelGroup::__makeMemberList(contacts_list_h list)
+{
+ int memberCount = 0;
+ int err = CONTACTS_ERROR_NONE;
+
+ contacts_record_h recordMember = NULL;
+ while (CONTACTS_ERROR_NONE == contacts_list_get_current_record_p(list, &recordMember)) {
+ if (recordMember){
+ std::shared_ptr < CtListDataPerson > dataPerson (std::make_shared < CtListDataPerson > (recordMember));
+ __groupMemberDataList.push_back(dataPerson);
+ memberCount++;
+ }
+ err = contacts_list_next(list);
+ WPWARN(CONTACTS_ERROR_NONE != err, "contacts_list_next() Failed(%d)", err);
+ }
+
+ return memberCount;
+}
+
+std::vector<int> CtListModelGroup::__getGroupIdsByName(char* groupName)
+{
+ std::vector<int> groupIds;
+ groupIds.clear();
+
+ int err = CONTACTS_ERROR_NONE;
+
+ contacts_filter_h filter = NULL;
+ contacts_query_h query = NULL;
+ contacts_list_h list = NULL;
+ contacts_record_h record = NULL;
+
+ unsigned int group_projection[] = {
+ _contacts_group.id,
+ _contacts_group.name,
+ };
+
+ err = contacts_filter_create(_contacts_group._uri, &filter);
+ WPRET_VM(CONTACTS_ERROR_NONE != err, groupIds, "contactcs_filter_create() Failed(%d)", err);
+
+ // search records which have same name
+ err = contacts_filter_add_str(filter, _contacts_group.name, CONTACTS_MATCH_EXACTLY, groupName);
+ if (CONTACTS_ERROR_NONE != err) {
+ WERROR("contacts_filter_add_str() Failed(%d)", err);
+ contacts_filter_destroy(filter);
+
+ return groupIds;
+ }
+
+ // add filter for google default groups
+ if (SAFE_STRCMP(groupName, V_("IDS_PB_BODY_MY_CONTACTS")) == 0) {
+ err = contacts_filter_add_operator(filter, CONTACTS_FILTER_OPERATOR_OR);
+ WERROR("contacts_filter_add_operator() Failed(%d)", err);
+ err = contacts_filter_add_str(filter, _contacts_group.name, CONTACTS_MATCH_EXACTLY, "System Group: My Contacts");
+ WERROR("contacts_filter_add_str() Failed(%d)", err);
+ }
+ else if (SAFE_STRCMP(groupName, V_("IDS_PB_BODY_DEFAULT_GROUP_FRIENDS")) == 0) {
+ err = contacts_filter_add_operator(filter, CONTACTS_FILTER_OPERATOR_OR);
+ WERROR("contacts_filter_add_operator() Failed(%d)", err);
+ err = contacts_filter_add_str(filter, _contacts_group.name, CONTACTS_MATCH_EXACTLY, "System Group: Friends");
+ WERROR("contacts_filter_add_str() Failed(%d)", err);
+ }
+ else if (SAFE_STRCMP(groupName, V_("IDS_PB_BODY_DEFAULT_GROUP_FAMILY")) == 0) {
+ err = contacts_filter_add_operator(filter, CONTACTS_FILTER_OPERATOR_OR);
+ WERROR("contacts_filter_add_operator() Failed(%d)", err);
+ err = contacts_filter_add_str(filter, _contacts_group.name, CONTACTS_MATCH_EXACTLY, "System Group: Family");
+ WERROR("contacts_filter_add_str() Failed(%d)", err);
+ }
+ else if (SAFE_STRCMP(groupName, V_("IDS_PB_BODY_CO_WORKERS")) == 0) {
+ err = contacts_filter_add_operator(filter, CONTACTS_FILTER_OPERATOR_OR);
+ WERROR("contacts_filter_add_operator() Failed(%d)", err);
+ err = contacts_filter_add_str(filter, _contacts_group.name, CONTACTS_MATCH_EXACTLY, "System Group: Coworkers");
+ WERROR("contacts_filter_add_str() Failed(%d)", err);
+ }
+ if (CONTACTS_ERROR_NONE != err) {
+ contacts_filter_destroy(filter);
+
+ return groupIds;
+ }
+
+ err = contacts_query_create(_contacts_group._uri, &query);
+ if (CONTACTS_ERROR_NONE != err) {
+ WERROR("contacts_query_create() Failed(%d)", err);
+ contacts_filter_destroy(filter);
+
+ return groupIds;
+ }
+ err = contacts_query_set_filter(query, filter);
+ if (CONTACTS_ERROR_NONE != err) {
+ WERROR("contacts_query_set_filter() Failed(%d)", err);
+ contacts_filter_destroy(filter);
+ contacts_query_destroy(query);
+
+ return groupIds;
+ }
+ contacts_filter_destroy(filter);
+
+ err = contacts_query_set_projection(query, group_projection, sizeof(group_projection)/sizeof(int));
+ if (CONTACTS_ERROR_NONE != err) {
+ WERROR("contacts_query_set_projection() Failed(%d)", err);
+ contacts_query_destroy(query);
+
+ return groupIds;
+ }
+
+ if (query == NULL) {
+ WERROR("query is NULL !!");
+ return groupIds;
+ }
+
+ err = contacts_db_get_records_with_query(query, 0, 0, &list);
+ if (CONTACTS_ERROR_NONE != err) {
+ WERROR("contacts_db_get_records_with_query() Failed(%d)", err);
+ contacts_query_destroy(query);
+ if(list)
+ contacts_list_destroy(list, true);
+ return groupIds;
+ }
+ contacts_query_destroy(query);
+
+ int count = 0;
+
+ contacts_list_get_count(list, &count);
+ if (0 == count) {
+ WERROR("no contact records found");
+ if(list)
+ contacts_list_destroy(list, true);
+ return groupIds;
+ }
+
+ int groupId = 0;
+
+ while (CONTACTS_ERROR_NONE == contacts_list_get_current_record_p(list, &record)) {
+ err = contacts_record_get_int(record, _contacts_group.id, &groupId);
+ if (CONTACTS_ERROR_NONE != err) {
+ WERROR("contacts_record_get_int Failed(%d)", err);
+ break;
+ }
+
+ groupIds.push_back(groupId);
+ contacts_list_next(list);
+ }
+ contacts_list_destroy(list, true);
+
+ return groupIds;
+}