summaryrefslogtreecommitdiff
path: root/lib-phone/ph-dialer/src/predictive-number/PhDialerPredictiveNumberUtils.cpp
blob: 699b87df90e7afee26fafb176771363899ca156b (plain)
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
 * 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 "ContactsThumbnail.h"
#include "ContactsUtils.h"
#include "PhDialerPredictiveNumberUtils.h"
#include "PhDialerLayout.h"
#include "ContactsDebug.h"

#include <Elementary.h>

using namespace Ph::Dialer;
using namespace Ph::Dialer::Utils;

namespace
{
	const char *mask[] = {
						"+",
						"",
						"abc",
						"def",
						"ghi",
						"jkl",
						"mno",
						"pqrs",
						"tuv",
						"wxyz"
					};

	char getDigit(char c)
	{
		static const char hash[] = "22233344455566677778889999";

		if(c == '+') {
			return '0';
		}

		size_t index = tolower(c) - 'a';

		if(index >= sizeof(hash) - 1) {
			return '\0';
		}

		return hash[index];
	}

	std::string getDigitMask(char digit)
	{
		if(digit > '9' || digit < '0') {
			WERROR("getDigitMask expected digit(0-9) argument, but %c is provided", digit);
			return "";
		}

		int index = digit - '0';
		return mask[index];
	}

	contacts_filter_h createContactNameFilters(char digit)
	{
		std::string mask = getDigitMask(digit);
		if (mask.empty()) {
			return NULL;
		}

		contacts_filter_h filter = NULL;
		contacts_filter_create(_contacts_contact_number._uri, &filter);
		bool isFirst = true;
		for(auto &x : mask) {
			if(!isFirst) {
				contacts_filter_add_operator(filter, CONTACTS_FILTER_OPERATOR_OR);
			}

			char str[] = { x, '\0' };
			contacts_filter_add_str(filter, _contacts_contact_number.display_name, CONTACTS_MATCH_CONTAINS, str);
			isFirst = false;
		}
		return filter;
	}

	contacts_list_h runQuery(const char *tableUri, contacts_filter_h filter)
	{
		contacts_list_h list = NULL;
		contacts_query_h query = NULL;

		contacts_query_create(tableUri, &query);
		contacts_query_set_filter(query, filter);

		contacts_db_get_records_with_query(query, 0, 0, &list);

		contacts_query_destroy(query);
		contacts_filter_destroy(filter);

		return list;
	}

	std::string getThumbnail(int id)
	{
		std::string path;
		contacts_record_h record = NULL;
		if(contacts_db_get_record(_contacts_contact._uri, id, &record) == CONTACTS_ERROR_NONE) {
			char *imgPath = NULL;
			contacts_record_get_str_p(record, _contacts_contact.image_thumbnail_path, &imgPath);
			if(imgPath) {
				path = imgPath;
			}
			contacts_record_destroy(record, true);
		}
		return path;
	}
}

contacts_list_h Ph::Dialer::Utils::getSpeedDial(char number)
{
	contacts_filter_h filter = NULL;
	contacts_filter_create(_contacts_speeddial._uri, &filter);
	contacts_filter_add_int(filter, _contacts_speeddial.speeddial_number, CONTACTS_MATCH_EQUAL, number - '0');

	contacts_list_h list = runQuery(_contacts_speeddial._uri, filter);

	return list;
}

contacts_list_h Ph::Dialer::Utils::getLogList(char digit)
{
	char number [] = { digit, '\0' };
	contacts_filter_h filter = NULL;
	contacts_filter_create(_contacts_phone_log._uri, &filter);

	contacts_filter_add_str(filter, _contacts_phone_log.address, CONTACTS_MATCH_CONTAINS, number);
	contacts_filter_add_operator(filter, CONTACTS_FILTER_OPERATOR_AND);
	contacts_filter_add_int(filter, _contacts_phone_log.log_type,
			CONTACTS_MATCH_GREATER_THAN_OR_EQUAL, CONTACTS_PLOG_TYPE_VOICE_INCOMMING);
	contacts_filter_add_operator(filter, CONTACTS_FILTER_OPERATOR_AND);
	contacts_filter_add_int(filter, _contacts_phone_log.log_type,
			CONTACTS_MATCH_LESS_THAN_OR_EQUAL, CONTACTS_PLOG_TYPE_VIDEO_BLOCKED);

	contacts_list_h list = runQuery(_contacts_phone_log._uri, filter);

	contacts_list_h retList = NULL;
	contacts_list_create(&retList);
	contacts_record_h record = NULL;
	CONTACTS_LIST_FOREACH(list, record) {
		int personId = 0;
		contacts_record_get_int(record, _contacts_phone_log.person_id, &personId);
		if(personId == 0) {
			contacts_list_add(retList, record);
		} else {
			contacts_record_destroy(record, true);
		}
	}
	contacts_list_destroy(list, false);

	return retList;
}

contacts_list_h Ph::Dialer::Utils::getContactListByName(char digit)
{
	contacts_query_h query = NULL;
	contacts_list_h list = NULL;
	contacts_query_create(_contacts_contact_number._uri, &query);
	contacts_filter_h filter = createContactNameFilters(digit);
	if(filter) {
		contacts_query_set_filter(query, filter);
		contacts_db_get_records_with_query(query, 0, 0, &list);
	}

	contacts_filter_destroy(filter);
	contacts_query_destroy(query);
	return list;
}

contacts_list_h Ph::Dialer::Utils::getContactListByNumber(char digit)
{
	char number [] = { digit, '\0' };
	contacts_filter_h filter = NULL;
	contacts_filter_create(_contacts_contact_number._uri, &filter);
	contacts_filter_add_str(filter, _contacts_contact_number.number, CONTACTS_MATCH_CONTAINS, number);

	contacts_list_h list = runQuery(_contacts_contact_number._uri, filter);

	return list;
}

std::string Ph::Dialer::Utils::contactNameToMask(const std::string &name)
{
	std::string number;
	number.reserve(name.size());

	for(auto &x : name) {
		char digit = getDigit(x);
		if(digit == '\0') {
			break;
		}
		number.push_back(digit);
	}
	return number;
}

Evas_Object *Ph::Dialer::Utils::createThumbnail(Evas_Object *parent, int contactId)
{
	std::string thumbnailPath = getThumbnail(contactId);
	Evas_Object *thumbnailImage = createThumbnail(parent, THUMBNAIL_98, thumbnailPath.c_str(), true);

	return thumbnailImage;
}