summaryrefslogtreecommitdiff
path: root/lib-apps-common/inc/Model/DataItem.h
blob: fb9684d3dbbb7b3546f10bbd172b6ff2174d4161 (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
/*
 * Copyright 2017 Samsung Electronics Co., Ltd
 *
 * Licensed under the Flora License, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://floralicense.org/license/
 *
 * 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.
 */

#ifndef MODEL_DATA_ITEM_H
#define MODEL_DATA_ITEM_H

#include <tizen.h>
#include "Utils/CallbackManager.h"

namespace Model
{
	/**
	 * @brief Change types.
	 */
	enum ChangeType
	{
		ChangeNone,
		ChangeUpdate,
		ChangeInsert,
		ChangeDelete
	};

	class EXPORT_API DataItem
	{
	public:
		/**
		 * @brief Called after item was updated.
		 * @param[in]   Which item data was updated (depends on the specific item)
		 * @param[in]   Next item according to sort order
		 */
		typedef Utils::CallbackManager<int, DataItem *> UpdateCallback;

		/**
		 * @brief Called before item is deleted.
		 */
		typedef Utils::CallbackManager<> DeleteCallback;

		DataItem();
		DataItem(const DataItem &that);
		DataItem &operator=(const DataItem &that);
		virtual ~DataItem() { }

		/**
		 * @return ID.
		 */
		int getId() const;

		/**
		 * @return Custom data associated with the item.
		 */
		void *getUserData() const;

		/**
		 * @brief Set custom data associated with the item.
		 * @param[in]   data    Data to set
		 */
		void setUserData(void *data);

		/**
		 * @return Whether the item was changed during update.
		 */
		bool isChanged() const;

		/**
		 * @return Whether item is standalone (not managed by DataProvider).
		 */
		bool isStandalone() const;

		/**
		 * @brief Set whether item is standalone or managed by DataProvider.
		 * @param[in]   isStandalone    Whether item is standalone
		 */
		void setStandalone(bool isStandalone);

		/**
		 * @brief Add/remove update callback.
		 */
		UpdateCallback &onUpdated();

		/**
		 * @brief Add/remove delete callback.
		 */
		DeleteCallback &onDeleted();

	protected:
		/**
		 * @brief Update item with new data.
		 * @param[in]   data    New item data
		 * @return Mask describing detected changes or 0 if none.
		 */
		int update(void *data);

		/**
		 * @brief Set ID.
		 * @param[in]   id   Data item id
		 */
		void setId(int id);

		/**
		 * @brief Mark item as changed with specified change type.
		 * @param[in]   changeType  Item change type
		 * @param[in]   changes     Which item data was updated if type is ChangeUpdate
		 */
		void setChanged(ChangeType changeType, int changes = 0);

		/**
		 * @brief Called after setStandalone() to change item standalone state.
		 * @param[in]   isStandalone    Whether item is standalone
		 */
		virtual void onStandalone(bool isStandalone) { }

		/**
		 * @brief Called after update() to update item data.
		 * @param[in]   data    New item data
		 * @return Mask specifying which item data was updated.
		 */
		virtual int onUpdate(void *data) { return 0; }

	private:
		friend class DataProvider;
		void finishUpdate(DataItem *nextItem = nullptr);

		int m_Id;
		bool m_IsStandalone;
		int m_Changes;
		ChangeType m_ChangeType;
		void *m_UserData;

		UpdateCallback m_OnUpdated;
		DeleteCallback m_OnDeleted;
	};
}

#endif /* MODEL_DATA_ITEM_H */