summaryrefslogtreecommitdiff
path: root/lib-apps-common/inc
diff options
context:
space:
mode:
Diffstat (limited to 'lib-apps-common/inc')
-rw-r--r--lib-apps-common/inc/Model/DataControlConsumer.h166
-rw-r--r--lib-apps-common/inc/Model/DataControlProvider.h59
-rw-r--r--lib-apps-common/inc/Model/DataItem.h17
-rw-r--r--lib-apps-common/inc/Model/DataProvider.h7
-rw-r--r--lib-apps-common/inc/Utils/Bundle.h88
-rw-r--r--lib-apps-common/inc/Utils/Logger.h2
6 files changed, 336 insertions, 3 deletions
diff --git a/lib-apps-common/inc/Model/DataControlConsumer.h b/lib-apps-common/inc/Model/DataControlConsumer.h
new file mode 100644
index 0000000..3cb0b24
--- /dev/null
+++ b/lib-apps-common/inc/Model/DataControlConsumer.h
@@ -0,0 +1,166 @@
+/*
+ * 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_CONTROL_CONSUMER_H
+#define MODEL_DATA_CONTROL_CONSUMER_H
+
+#include "Model/DataProvider.h"
+#include "Utils/Bundle.h"
+#include "Utils/CallbackManager.h"
+#include "Utils/Range.h"
+
+#include <data_control_sql.h>
+
+namespace Model
+{
+ /**
+ * @brief Represents the data consumer of the data control.
+ */
+ class EXPORT_API DataControlConsumer
+ {
+ public:
+ typedef DataProvider::DataList DataList;
+
+ /**
+ * @brief List of columns for select query.
+ * The first must be the index column.
+ */
+ typedef Utils::Range<const char **> ColumnList;
+
+ /**
+ * @brief Called after data item changed.
+ * @param[in] Which item data was changed
+ * @param[in] Change type
+ */
+ typedef Utils::CallbackManager<int, data_control_data_change_type_e> DataItemChangeCallback;
+
+ /**
+ * @brief Called when requested Data Item(s) is received.
+ * @param[in] dataList List of received Data Items
+ */
+ typedef std::function<void(DataList dataList)> GetCallback;
+
+ /**
+ * @brief Called when result of insert, update or delete is received.
+ * @param[in] isSuccess Whether operation was successful
+ * @param[in] id Data Item's id if it was inserted
+ */
+ typedef std::function<void(bool isSuccess, int id)> ResultCallback;
+
+ /**
+ * @brief Create data consumer.
+ * @param[in] providerId The data provider's id
+ * @param[in] tableId The table's id
+ * @param[in] columnList The list of columns for select query
+ */
+ DataControlConsumer(const char *providerId, const char *tableId, ColumnList columnList);
+ virtual ~DataControlConsumer();
+
+ DataControlConsumer(const DataControlConsumer &that) = delete;
+ DataControlConsumer &operator=(const DataControlConsumer &that) = delete;
+
+ /**
+ * @brief Retrieve item from database by ID.
+ * @param[in] id ID
+ * @param[in] callback Callback to be called when data item is received
+ */
+ virtual void getDataItem(int id, GetCallback callback);
+
+ /**
+ * @brief Retrieve all data items from database.
+ * @param[in] callback Callback to be called when data items are received
+ */
+ virtual void getDataItems(GetCallback callback);
+
+ /**
+ * @brief Convenience wrapper to insert new or update existing data item.
+ * @see insertDataItem()
+ * @see updateDataItem()
+ */
+ void saveDataItem(const DataItem &item, ResultCallback callback);
+
+ /**
+ * @brief Insert data item into database.
+ * @param[in] item Data item to insert
+ * @param[in] callback Callback to be called when operation is complete
+ */
+ void insertDataItem(const DataItem &item, ResultCallback callback = nullptr);
+
+ /**
+ * @brief Update existing data item in the database.
+ * @param[in] item Data item to update
+ * @param[in] callback Callback to be called when operation is complete
+ */
+ void updateDataItem(const DataItem &item, ResultCallback callback = nullptr);
+
+ /**
+ * @brief Delete data item from the database.
+ * @param[in] id Data item ID
+ * @param[in] callback Callback to be called when operation is complete
+ */
+ void deleteDataItem(int id, ResultCallback callback = nullptr);
+
+ /**
+ * @brief Add/remove data item change callback.
+ */
+ DataItemChangeCallback &onDataItemChanged();
+
+ protected:
+ /**
+ * @brief Create data item from the result cursor.
+ * @param[in] cursor The result cursor
+ * @return Data item or nullptr.
+ */
+ virtual DataItem *createDataItem(result_set_cursor cursor) = 0;
+
+ /**
+ * @brief Create bundle from data item.
+ * @param[in] item Data item
+ * @return Bundle.
+ */
+ virtual Utils::Bundle createBundle(const DataItem &item) = 0;
+
+ /**
+ * @brief Invoke select query to database.
+ * @param[in] where The select query condition
+ * @param[in] callback Callback to be called when operation is completed
+ */
+ void selectDataItems(const char *where, GetCallback callback);
+
+ private:
+ void onSelectResponse(int requestId, data_control_h provider,
+ result_set_cursor result, bool isSuccess, const char *error);
+ void onInsertResponse(int requestId, data_control_h provider,
+ long long id, bool isSuccess, const char *error);
+ void onUpdateResponse(int requestId, data_control_h provider,
+ bool isSuccess, const char *error);
+ void onDeleteResponse(int requestId, data_control_h provider,
+ bool isSuccess, const char *error);
+ void onDataChanged(data_control_h provider,
+ data_control_data_change_type_e type, bundle *data);
+ void onDataCallbackAdded(data_control_h provider,
+ data_control_error_e result, int callbackId);
+
+ data_control_h m_Provider;
+ ColumnList m_ColumnList;
+ int m_ChangeCallbackId;
+ DataItemChangeCallback m_OnDataItemChanged;
+ std::vector<std::pair<int, ResultCallback>> m_ResultCallbacks;
+ std::vector<std::pair<int, GetCallback>> m_GetCallbacks;
+ };
+}
+
+#endif /* MODEL_DATA_CONTROL_CONSUMER_H */
diff --git a/lib-apps-common/inc/Model/DataControlProvider.h b/lib-apps-common/inc/Model/DataControlProvider.h
new file mode 100644
index 0000000..387b3dd
--- /dev/null
+++ b/lib-apps-common/inc/Model/DataControlProvider.h
@@ -0,0 +1,59 @@
+/*
+ * 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_CONTROL_PROVIDER_H
+#define MODEL_DATA_CONTROL_PROVIDER_H
+
+#include "Model/DataProvider.h"
+#include <data_control_noti.h>
+
+namespace Model
+{
+ class DataControlConsumer;
+
+ /**
+ * @brief Represents data provider that works through DataControlConsumer.
+ */
+ class EXPORT_API DataControlProvider : public DataProvider
+ {
+ public:
+ /**
+ * @brief Create data control provider.
+ * @param[in] consumer The data control consumer
+ */
+ explicit DataControlProvider(DataControlConsumer &consumer);
+ virtual ~DataControlProvider() override;
+
+ private:
+ struct ChangeInfo
+ {
+ int id;
+ data_control_data_change_type_e type;
+ };
+
+ virtual void startInit() override;
+ virtual void startUpdate() override;
+
+ void applyChange(ChangeInfo change, ::Model::DataItem *newItem);
+ void onDataItemChanged(int id, data_control_data_change_type_e type);
+
+ DataControlConsumer &m_Consumer;
+ std::vector<ChangeInfo> m_Changes;
+ int m_ChangesPending;
+ };
+}
+
+#endif /* MODEL_DATA_CONTROL_PROVIDER_H */
diff --git a/lib-apps-common/inc/Model/DataItem.h b/lib-apps-common/inc/Model/DataItem.h
index e410d3e..ff70af6 100644
--- a/lib-apps-common/inc/Model/DataItem.h
+++ b/lib-apps-common/inc/Model/DataItem.h
@@ -38,7 +38,7 @@ namespace Model
public:
/**
* @brief Called after item was updated.
- * @param[in] Which item data was updated (depends on the specific item).
+ * @param[in] Which item data was updated (depends on the specific item)
*/
typedef Utils::CallbackManager<int> UpdateCallback;
@@ -49,11 +49,17 @@ namespace Model
DataItem();
DataItem(const DataItem &);
+ DataItem &operator=(const DataItem &);
virtual ~DataItem() { }
/**
+ * @return ID.
+ */
+ int getId() const;
+
+ /**
* @brief Update item with new data.
- * @param[in] data New item data.
+ * @param[in] data New item data
*/
void update(void *data);
@@ -80,6 +86,12 @@ namespace Model
protected:
/**
+ * @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
@@ -103,6 +115,7 @@ namespace Model
friend class DataProvider;
void finishUpdate();
+ int m_Id;
bool m_IsStandalone;
int m_Changes;
ChangeType m_ChangeType;
diff --git a/lib-apps-common/inc/Model/DataProvider.h b/lib-apps-common/inc/Model/DataProvider.h
index 6c4422f..101023b 100644
--- a/lib-apps-common/inc/Model/DataProvider.h
+++ b/lib-apps-common/inc/Model/DataProvider.h
@@ -74,6 +74,13 @@ namespace Model
void destroy();
/**
+ * @brief Find data item by id.
+ * @param[in] id Data item ID
+ * @return Data item or nullptr.
+ */
+ DataItem *findDataItem(int id);
+
+ /**
* @brief Set whether to start update immediately when update() is called.
* @param[in] isEnabled Whether data update is enabled
*/
diff --git a/lib-apps-common/inc/Utils/Bundle.h b/lib-apps-common/inc/Utils/Bundle.h
new file mode 100644
index 0000000..630f889
--- /dev/null
+++ b/lib-apps-common/inc/Utils/Bundle.h
@@ -0,0 +1,88 @@
+/*
+ * 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 UTILS_BUNDLE_H
+#define UTILS_BUNDLE_H
+
+#include <bundle.h>
+#include <tizen.h>
+
+namespace Utils
+{
+ /**
+ * @brief Bundle wrapper. Provides lifetime management and convenience
+ * methods for adding and accessing stored values.
+ */
+ class EXPORT_API Bundle
+ {
+ public:
+ Bundle();
+
+ /**
+ * @brief Wrap a pre-existing bundle object.
+ * @param[in] bundle A bundle to wrap
+ * @remark The bundle lifetime is not tied to the wrapper in this case.
+ */
+ explicit Bundle(bundle *bundle);
+ Bundle(const Bundle &that) = delete;
+ Bundle(Bundle &&that);
+ virtual ~Bundle();
+
+ Bundle &operator=(const Bundle &that) = delete;
+ Bundle &operator=(Bundle &&that);
+
+ /**
+ * @return Wrapped bundle object.
+ */
+ bundle *getBundle() const;
+
+ /**
+ * @brief Get integer value from a bundle.
+ * @param[in] key Value key
+ * @return Integer value.
+ */
+ int getInt(const char *key) const;
+
+ /**
+ * @brief Get string value from a bundle.
+ * @param[in] key Value key
+ * @return string value.
+ */
+ const char *getStr(const char *key) const;
+
+ /**
+ * @brief Add integer value to a bundle.
+ * @param[in] key Value key
+ * @param[in] value Integer value
+ * @return This object for method chaining.
+ */
+ Bundle &addInt(const char *key, int value);
+
+ /**
+ * @brief Add string value to a bundle.
+ * @param[in] key Value key
+ * @param[in] str String value
+ * @return This object for method chaining.
+ */
+ Bundle &addStr(const char *key, const char *str);
+
+ private:
+ bundle *m_Bundle;
+ bool m_IsOwner;
+ };
+}
+
+#endif /* UTILS_BUNDLE_H */
diff --git a/lib-apps-common/inc/Utils/Logger.h b/lib-apps-common/inc/Utils/Logger.h
index 97ebe42..8922e62 100644
--- a/lib-apps-common/inc/Utils/Logger.h
+++ b/lib-apps-common/inc/Utils/Logger.h
@@ -51,7 +51,7 @@ if (expr) { \
}
#define LOG_IF_ERR(code, action, fmt, arg...) \
-LOG_IF(code != TIZEN_ERROR_NONE, action, fmt " %s.", ##arg, get_error_message(code))
+LOG_IF(TIZEN_ERROR_IS_ERROR(code), action, fmt " %s.", ##arg, get_error_message(code))
#define WARN_IF(expr, fmt, arg...) \
LOG_IF(expr, , fmt, ##arg)