summaryrefslogtreecommitdiff
path: root/lib-phone/ph-speeddial/src/PhSpeedDialItem.cpp
blob: da6338cb88606a2fc9f7301f82f70dcb8d3958e8 (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
/*
 * 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 "PhSpeedDialItem.h"
#include "PhSpeedDialLayout.h"
#include "PhCommon.h"
#include "PhStrings.h"

#include "ContactsAppControl.h"
#include "ContactsCommon.h"
#include "ContactsDebug.h"
#include "ContactsThumbnail.h"

#include <notification.h>

#define TEXT_BUFFER_SIZE 256

namespace
{
	const std::string layoutPath = ContactsCommon::getAppEdjePath("phone/" SPEED_DIAL_LAYOUT_EDJ);
}

PhSpeedDialItem::PhSpeedDialItem(int number)
	: m_Number(number)
{
}

void PhSpeedDialItem::setData(contacts_record_h record)
{
	if (record) {
		setLayoutData(getEvasObj(), record);
	} else {
		setEmptyLayout(getEvasObj());
	}
}

Evas_Object *PhSpeedDialItem::onCreate(Evas_Object *parent, void *param)
{
	Evas_Object *layout = elm_layout_add(parent);
	elm_layout_file_set(layout, layoutPath.c_str(), GROUP_ITEM_LAYOUT);
	return layout;
}

Evas_Object *PhSpeedDialItem::createAddButton(Evas_Object *parent)
{
	Evas_Object *button = elm_image_add(parent);
	elm_image_file_set(button, layoutPath.c_str(), GROUP_ITEM_ICON);
	evas_object_smart_callback_add(button, "clicked",
			&PhSpeedDialItem::onItemPressed, this);

	return button;
}

Evas_Object *PhSpeedDialItem::createDeleteButton(Evas_Object *parent)
{
	Evas_Object *button = elm_image_add(parent);
	elm_image_file_set(button, layoutPath.c_str(), GROUP_DELETE_BUTTON);
	evas_object_smart_callback_add(button, "clicked",
			&PhSpeedDialItem::onDeletePressed, this);

	return button;
}

void PhSpeedDialItem::setEmptyLayout(Evas_Object *layout)
{
	elm_object_part_content_set(layout, PART_ICON, createAddButton(layout));
	elm_object_part_content_set(layout, PART_DELETE_BUTTON, nullptr);

	char buffer[TEXT_BUFFER_SIZE];
	snprintf(buffer, sizeof(buffer), "%d", m_Number);
	elm_object_part_text_set(layout, PART_NAME, buffer);
}

void PhSpeedDialItem::setLayoutData(Evas_Object *layout, contacts_record_h record)
{
	char *name = nullptr;
	char *imagePath = nullptr;

	contacts_record_get_str_p(record, _contacts_speeddial.display_name, &name);
	contacts_record_get_str_p(record, _contacts_speeddial.image_thumbnail_path, &imagePath);

	elm_object_part_content_set(layout, PART_ICON, createThumbnail(layout, THUMBNAIL_240, imagePath));
	elm_object_part_content_set(layout, PART_DELETE_BUTTON, createDeleteButton(layout));

	char buffer[TEXT_BUFFER_SIZE];
	snprintf(buffer, sizeof(buffer), "%d. %s", m_Number, name);
	elm_object_part_text_set(layout, PART_NAME, buffer);
}

void PhSpeedDialItem::fetchData()
{
	contacts_record_h record = nullptr;
	contacts_db_get_record(_contacts_speeddial._uri, m_Number, &record);

	setData(record);
	contacts_record_destroy(record, true);
}

void PhSpeedDialItem::onPickResult(app_control_h request, app_control_h reply,
		app_control_result_e result, void *data)
{
	PhSpeedDialItem *item = (PhSpeedDialItem*) data;

	char *value = nullptr;
	int err = app_control_get_extra_data(reply, APP_CONTROL_DATA_SELECTED, &value);
	WPRET_M(err != APP_CONTROL_ERROR_NONE, "app_control_get_extra_data() failed(%d)", err);

	if (value) {
		int numberId = atoi(value);
		free(value);

		if (PhCommon::addSpeedDialNumber(item->m_Number, numberId)) {
			item->fetchData();
		} else {
			notification_status_message_post(PAT_("IDS_PB_POP_ALREADY_EXISTS_LC"));
		}
	}
}

void PhSpeedDialItem::onItemPressed(void *data, Evas_Object *obj, void *event_info)
{
	int err = launchContactPick(APP_CONTROL_DATA_SELECTION_MODE_SINGLE, APP_CONTROL_DATA_TYPE_PHONE,
			&PhSpeedDialItem::onPickResult, data);
	WPWARN(err != APP_CONTROL_ERROR_NONE, "launchContactPick() failed(%d)", err);
}

void PhSpeedDialItem::onDeletePressed(void *data, Evas_Object *obj, void *event_info)
{
	PhSpeedDialItem *item = (PhSpeedDialItem*) data;

	int err = contacts_db_delete_record(_contacts_speeddial._uri, item->m_Number);
	WPRET_M(err != CONTACTS_ERROR_NONE, "contacts_db_delete_record() failed(%d)", err);
	item->setEmptyLayout(item->getEvasObj());

	char buffer[TEXT_BUFFER_SIZE];
	snprintf(buffer, sizeof(buffer), PAT_(PH_KPD_TPOP_SPEED_DIAL_NUMBER_PD_REMOVED), item->m_Number);
	notification_status_message_post(buffer);
}