aa66bdf697fe0fa120b198fdc057fffc164296fb
[apps/home/wrt-setting.git] / webapp-common / ug.cpp
1 /*
2   * Copyright 2012  Samsung Electronics Co., Ltd
3   *
4   * Licensed under the Flora License, Version 1.0 (the "License");
5   * you may not use this file except in compliance with the License.
6   * You may obtain a copy of the License at
7   *
8   *     http://floralicense.org/license/
9   *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16
17 #include <ui-gadget.h>
18 #include <ui-gadget-module.h>
19 #include <bundle.h>
20
21 #include <stdexcept>
22 #undef None
23 #include <dpl/wrt-dao-ro/WrtDatabase.h>
24 #include <dpl/assert.h>
25
26 #include "mainview.h"
27
28 #define EXPORT_SYMBOL extern "C" __attribute__((visibility("default")))
29
30 struct priv {
31     WebAppCommonSetting::MainView *mainView;
32 };
33
34 static void *onCreate(ui_gadget_h ug,
35                       enum ug_mode /*mode*/,
36                       service_h /*service*/,
37                       void *user_data)
38 {
39     struct priv *priv;
40
41     Assert(ug && user_data);
42     priv = static_cast<struct priv *>(user_data);
43
44     try {
45         priv->mainView = new WebAppCommonSetting::MainView(ug);
46         if (priv->mainView->loadView())
47             return priv->mainView->getBase();
48         return NULL;
49     } catch (const std::bad_alloc &) {
50         return NULL;
51     }
52 }
53
54 static void onDestroy(ui_gadget_h /*ug*/,
55                       service_h /*service*/,
56                       void *user_data)
57 {
58     struct priv *priv;
59
60     Assert(user_data);
61     priv = static_cast<struct priv *>(user_data);
62     delete priv->mainView;
63     priv->mainView = NULL;
64 }
65
66 EXPORT_SYMBOL int UG_MODULE_INIT(struct ug_module_ops *ops)
67 {
68     struct priv *priv;
69
70     Assert(ops);
71     priv = static_cast<struct priv *>(calloc(1, sizeof(*priv)));
72     if (!priv)
73         return -1;
74
75     WrtDB::WrtDatabase::attachToThreadRW();
76
77     memset(ops, 0x00, sizeof(*ops));
78     ops->priv = priv;
79     ops->create = onCreate;
80     ops->destroy = onDestroy;
81     ops->opt = UG_OPT_INDICATOR_ENABLE;
82
83     return 0;
84 }
85
86 EXPORT_SYMBOL void UG_MODULE_EXIT(struct ug_module_ops *ops)
87 {
88     struct priv *priv;
89
90     Assert(ops);
91     priv = static_cast<struct priv *>(ops->priv);
92     if (priv->mainView)
93         delete priv->mainView;
94     free(priv);
95
96     WrtDB::WrtDatabase::detachFromThread();
97 }