File size: 6,583 Bytes
e1191aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/* Copyright 2013 David Axmark

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.
*/

/**
 * @file AppMoblet.cpp
 * @author Bogdan Iusco
 *
 * AppMoblet class shows how to:
 * - open and close contacts list.
 * - create contact item.
 * - remove contact item from the list.
 */

// Minimum number of values for drag events.
#define MIN_DRAG_VALUES 5

#include <conprint.h>
#include <IX_PIM.h>
#include <maapi.h>

#include "AppMoblet.h"
#include "Util.h"
#include "PIMContact.h"

/**
 * Constructor.
 */
AppMoblet::AppMoblet() :
	Moblet(),
	mHandlePointerEvent(false)
{
	this->startPIM();
}

/**
 * Destructor.
 */
AppMoblet::~AppMoblet()
{

}

/**
 *  This function is called with a coordinate when a pointer is moved.
 */
void AppMoblet::pointerMoveEvent(MAPoint2d point)
{
	if (mHandlePointerEvent)
	{
		mDragValues.add(point.x);
	}
}

/**
 *  This function is called with a coordinate when a pointer is released.
 */
void AppMoblet::pointerReleaseEvent(MAPoint2d point)
{
	// Handle this event only if no PIM action is on progress.
	if (mHandlePointerEvent)
	{
		// Check if there are enough drag events for analysis.
		if (MIN_DRAG_VALUES > mDragValues.size())
		{
			return;
		}

		// Check if drag is from left to right of from right to left.
		bool dragFromLeftToRight = true;
		bool dragFromRightToLeft = true;
		int previousValue = mDragValues[0];
		for (int i = 1; (i < mDragValues.size()) && (dragFromLeftToRight
			|| dragFromRightToLeft); i++)
		{
			int currentValue = mDragValues[i];
			if (previousValue > currentValue)
			{
				dragFromLeftToRight = false;
			}
			if (previousValue < currentValue)
			{
				dragFromRightToLeft = false;
			}

			previousValue = currentValue;
		}

		// If drag is from left to right print the next contact, otherwise
		// close the PIM contacts list.
		if (dragFromLeftToRight)
		{
			this->printNextContact();
		}
		else if (dragFromRightToLeft)
		{
			this->closePIMContactsList();
		}
	}
}

/**
 * Called when the screen is touched.
 */
void AppMoblet::pointerPressEvent(MAPoint2d point)
{
	if (mHandlePointerEvent)
	{
		mDragValues.clear();
	}
}

/**
 * Method called when a key is pressed.
 */
void AppMoblet::keyPressEvent(int keyCode, int nativeCode)
{
    // Close the application if the back key is pressed.
    if(MAK_BACK == keyCode)
    {
        close();
    }
}

/**
 * Start working with PIM.
 * Add, modify and remove a PIM contact.
 */
void AppMoblet::startPIM()
{
	// Open PIM contacts list.
	this->openPIMContactsList();

	// Create new contact and add data into fields.
	PIMContact* newContact = this->createContact();
	if (NULL == newContact)
	{
		return;
	}
	newContact->addDataToContact();

	// Modify the new contact.
	this->modifyContact(newContact);

	// Remove the new contact from the list and delete the object.
	this->removeContact(newContact);
	delete newContact;

	// Print options on the screen and wait for drag events.
	this->printInfo();
}

/**
 * Open PIM contacts list.
 */
void AppMoblet::openPIMContactsList()
{
	printf("\n==============Open contacts list===============");

	// Get a handle to the list of contacts.
	mContactsListHandle = maPimListOpen(MA_PIM_CONTACTS);
	checkResultCode(mContactsListHandle);
}

/**
 * Print the next contact from the PIM contacts list.
 */
void AppMoblet::printNextContact()
{
	// Ignore all drag events handled by this moblet.
	mHandlePointerEvent = false;

	printf("\n=============Printing next contact=============\n");

	// Get a hold on a contact item.
	MAHandle pimItemHandle = maPimListNext(mContactsListHandle);

	// Check if the new contact handle is valid.
	if (0 < pimItemHandle)
	{
		// Create new contact item and print it on the screen.
		PIMContact* item = new PIMContact(pimItemHandle);
		item->printContact();
		delete item;
	}
	else if (pimItemHandle == 0)
	{
		// Reached at the end of the list.
		printf("The contacts list does not contain more items.");
	}
	else
	{
		// Print the error on the screen.
		printResultCode(pimItemHandle);
	}

	// Print options on the screen and wait for drag events.
	this->printInfo();
}

/**
 * Create a new contact.
 * @return The new created contact, or NULL in case of error.
 * The ownership of the result is passed to the caller.
 */
PIMContact* AppMoblet::createContact()
{
	// Get a handle to the new created contact.
	printf("\n==============Create new contact==============\n");
	MAHandle newContantHandle = maPimItemCreate(mContactsListHandle);

	// Check for errors.
	if (0 > newContantHandle)
	{
		printf("Cann't create new contact.");
		printResultCode(newContantHandle);
		return NULL;
	}

	// Create and return the new contact.
	PIMContact* newContact = new PIMContact(newContantHandle);
	return newContact;
}

/**
 * Modify the address field of a specified contact.
 * Remove the first value from the phone field.
 * @param contact The given contact item.
 */
void AppMoblet::modifyContact(PIMContact* contact)
{
	contact->modifyAddressField();

	printf("\n====Remove field value from the new contact====\n");
	contact->removeFieldValue(MA_PIM_FIELD_CONTACT_TEL, 0);
}

/**
 * Remove a specified contact from the list.
 * It also closes the item.
 * @param contact The given contact item.
 */
void AppMoblet::removeContact(PIMContact* contact)
{
	printf("\n===========Remove the new contact=============\n");
	int resultCode = maPimItemRemove(mContactsListHandle, contact->getHandle());
	printResultCode(resultCode);
}

/**
 * Close the PIM contacts list.
 */
void AppMoblet::closePIMContactsList()
{
	printf("\n ==============Close the contacts list=========\n");
	printResultCode(maPimListClose(mContactsListHandle));

	// Start again working with PIM>
	this->startPIM();
}

/**
 * Print show next contact / close pim list options on the screen.
 */
void AppMoblet::printInfo()
{
	printf("\n==============================================");
	printf("Drag from left to right to show the next \n contact");
	printf("Drag from right to left to close the contacts \n list");
	printf("==============================================\n\n");

	// Allow this moblet to handle pointer events.
	mHandlePointerEvent = true;
}