summaryrefslogtreecommitdiff
path: root/app-control/ct-list-app/src/CtListApp.cpp
blob: 6ea57b00eff61892b3186e5cd30ec3be928434cc (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
/*
 * 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 "CtListApp.h"
#include "ContactsAppControl.h"

#include "WAppWindow.h"
#include "WDebugBase.h"

namespace
{
	const char *resultTypes[] = {
		APP_CONTROL_DATA_TYPE_ID,
		APP_CONTROL_DATA_TYPE_PHONE,
		APP_CONTROL_DATA_TYPE_PHONE_CALL,
		APP_CONTROL_DATA_TYPE_PHONE_MESSAGE,
		APP_CONTROL_DATA_TYPE_EMAIL,
		APP_CONTROL_DATA_TYPE_VCARD
	};
}

CtListApp::CtListApp()
	: m_Naviframe(NULL), m_Request(NULL),
	  m_SelectMode(LIST_SELECT_SINGLE), m_ResultType(LIST_RESULT_ID)
{
	contacts_connect();
}

CtListApp::~CtListApp()
{
	if (m_Request) {
		app_control_destroy(m_Request);
	}
	contacts_disconnect();
}

bool CtListApp::onCreate()
{
	ContactsCommon::loadLocalization();
	attachWindow(new WAppWindow("Contacts-List", ELM_WIN_BASIC));

	m_Naviframe = new WNaviframe();
	m_Naviframe->setOnLastItemPop([](bool *popOut) {
		ui_app_exit();
	});
	getWindow()->attachBaseUiObject(m_Naviframe);

	return true;
}

void CtListApp::onAppControl(app_control_h request, bool firstLaunch)
{
	WHIT();
	CtListView *view = NULL;
	char *operation = NULL;

	app_control_clone(&m_Request, request);
	app_control_get_operation(m_Request, &operation);

	if (operation) {
		if (strcmp(operation, APP_CONTROL_OPERATION_PICK) == 0) {
			m_SelectMode = getSelectMode(m_Request);
			m_ResultType = getResultType(m_Request);
			view = new CtListView(m_SelectMode, m_ResultType, getSelectLimit(m_Request));

			using namespace std::placeholders;
			if (m_SelectMode == LIST_SELECT_SINGLE) {
				view->setOnSingleResult(std::bind(&CtListApp::replySingleResult, this, _1, _2));
			} else {
				view->setOnMultiResult(std::bind(&CtListApp::replyMultiResult, this, _1, _2));
			}
		} else if (strcmp(operation, APP_CONTROL_OPERATION_UPDATE) == 0) {
			view = new CtListView(LIST_SELECT_SINGLE_OR_CREATE, LIST_RESULT_ID, 0);
			view->setOnSingleResult([this](const char *id, CtListResultType type) {
				if (id) {
					if (atoi(id) == 0) {
						app_control_set_operation(m_Request, APP_CONTROL_OPERATION_ADD);
					} else {
						app_control_set_operation(m_Request, APP_CONTROL_OPERATION_EDIT);
						app_control_add_extra_data(m_Request, APP_CONTROL_DATA_ID, id);
					}

					app_control_send_launch_request(m_Request, NULL, NULL);
				}
			});
		}

		free(operation);
	}

	if (view) {
		m_Naviframe->push(view);
	}  else {
		app_control_h reply;
		app_control_create(&reply);
		app_control_reply_to_launch_request(reply, m_Request, APP_CONTROL_RESULT_FAILED);
		app_control_destroy(reply);
		ui_app_exit();
	}
}

CtListSelectMode CtListApp::getSelectMode(app_control_h request)
{
	char *selectMode = NULL;
	app_control_get_extra_data(request, APP_CONTROL_DATA_SELECTION_MODE, &selectMode);

	if (selectMode) {
		if (strcmp(selectMode, APP_CONTROL_DATA_SELECTION_MODE_SINGLE) == 0) {
			return LIST_SELECT_SINGLE;
		} else if (strcmp(selectMode, APP_CONTROL_DATA_SELECTION_MODE_MULTIPLE) == 0) {
			return LIST_SELECT_MULTIPLE;
		}

		free(selectMode);
	}

	return LIST_SELECT_SINGLE;
}

CtListResultType CtListApp::getResultType(app_control_h request)
{
	char *resultType = NULL;
	app_control_get_extra_data(request, APP_CONTROL_DATA_TYPE, &resultType);

	if (resultType) {
		if (strcmp(resultType, APP_CONTROL_DATA_TYPE_ID) == 0) {
			return LIST_RESULT_ID;
		} else if (strcmp(resultType, APP_CONTROL_DATA_TYPE_PHONE) == 0) {
			return LIST_RESULT_NUMBER;
		} else if (strcmp(resultType, APP_CONTROL_DATA_TYPE_EMAIL) == 0) {
			return LIST_RESULT_EMAIL;
		} else if (strcmp(resultType, APP_CONTROL_DATA_TYPE_VCARD) == 0) {
			return LIST_RESULT_VCARD;
		} else if (strcmp(resultType, APP_CONTROL_DATA_TYPE_PHONE_OR_EMAIL) == 0) {
			return LIST_RESULT_NUMBER_OR_EMAIL;
		}

		free(resultType);
	}

	return LIST_RESULT_ID;
}

int CtListApp::getSelectLimit(app_control_h request)
{
	int limit = 0;
	char *limitStr = NULL;

	app_control_get_extra_data(request, APP_CONTROL_DATA_LIMIT, &limitStr);
	if (limitStr) {
		limit = atoi(limitStr);
		free(limitStr);
	}

	return limit;
}

void CtListApp::replySingleResult(const char *data, CtListResultType type)
{
	WHIT();
	app_control_h reply = NULL;
	app_control_create(&reply);

	if (data) {
		app_control_add_extra_data(reply, APP_CONTROL_DATA_TYPE, resultTypes[type]);
		app_control_add_extra_data(reply, APP_CONTROL_DATA_SELECTED, data);
	}

	app_control_reply_to_launch_request(reply, m_Request, APP_CONTROL_RESULT_SUCCEEDED);
	app_control_destroy(reply);
}

void CtListApp::replyMultiResult(const char *data[], size_t count)
{
	app_control_h reply = NULL;
	app_control_create(&reply);

	if (data) {
		app_control_add_extra_data(reply, APP_CONTROL_DATA_TYPE, resultTypes[m_ResultType]);
		app_control_add_extra_data_array(reply, APP_CONTROL_DATA_SELECTED, data, count);
	}

	app_control_reply_to_launch_request(reply, m_Request, APP_CONTROL_RESULT_SUCCEEDED);
	app_control_destroy(reply);
}