Revert "Revert "Add the appcmd protocol for product extended routine.""
[sdk/target/sdbd.git] / src / default_plugin_event.c
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.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://www.apache.org/licenses/LICENSE-2.0
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 <vconf.h>
18 #include <glib.h>
19
20 #define TRACE_TAG TRACE_SDB
21 #include "log.h"
22
23 #include "sysdeps.h"
24 #include "sdbd_plugin.h"
25 #include "default_plugin.h"
26
27 int get_lock_state ( parameters* in, parameters* out )
28 {
29     if ( in == NULL || in->number_of_parameter != 1 || in->array_of_parameter == NULL
30             || in->array_of_parameter[0].type != type_int32 ) {
31         D ( "Invalid argument\n" );
32         return PLUGIN_CMD_FAIL;
33     }
34
35     if ( out == NULL ) {
36         D ( "Invalid argument\n" );
37         return PLUGIN_CMD_FAIL;
38     }
39
40     D ( "lock type: %d\n", in->array_of_parameter[0].v_int32);
41
42     out->number_of_parameter = 1;
43     out->array_of_parameter = ( parameter* ) malloc ( sizeof ( parameter ) );
44     out->array_of_parameter[0].type = type_int32;
45     out->array_of_parameter[0].v_int32 = ( plugin_pwlocked() == 1 ) ? PLUGIN_RET_ON : PLUGIN_RET_OFF;
46
47     return PLUGIN_CMD_SUCCESS;
48 }
49
50 int plugin_pwlocked ( void )
51 {
52     int pwlock_status = 0;
53     int pwlock_type = 0;
54
55     if ( vconf_get_int ( VCONFKEY_IDLE_LOCK_STATE, &pwlock_status ) ) {
56         pwlock_status = 0;
57         PLUGIN_LOG ( "failed to get pw lock status\n" );
58     }
59 #ifdef _WEARABLE
60     PLUGIN_LOG ( "wearable lock applied\n" );
61     // for wearable which uses different VCONF key (lock type)
62     if ( vconf_get_int ( VCONFKEY_SETAPPL_PRIVACY_LOCK_TYPE_INT, &pwlock_type ) ) {
63         pwlock_type = 0;
64         PLUGIN_LOG ( "failed to get pw lock type\n" );
65     }
66     if ( ( pwlock_status == VCONFKEY_IDLE_LOCK ) && ( pwlock_type != SETTING_PRIVACY_LOCK_TYPE_NONE ) ) {
67         PLUGIN_LOG ( "device has been locked\n" );
68         return 1; // locked!
69     }
70 #else
71     PLUGIN_LOG ( "mobile lock applied\n" );
72     // for mobile
73     if ( vconf_get_int ( VCONFKEY_SETAPPL_SCREEN_LOCK_TYPE_INT, &pwlock_type ) ) {
74         pwlock_type = 0;
75         PLUGIN_LOG ( "failed to get pw lock type\n" );
76     }
77     if ( pwlock_status == VCONFKEY_IDLE_LOCK && ( ( pwlock_type != SETTING_SCREEN_LOCK_TYPE_NONE ) && ( pwlock_type != SETTING_SCREEN_LOCK_TYPE_SWIPE ) ) ) {
78         PLUGIN_LOG ( "device has been locked\n" );
79         return 1; // locked!
80     }
81 #endif
82     return 0; // unlocked!
83 }
84
85 static void pwlock_cb ( keynode_t *key, void* data )
86 {
87     PLUGIN_LOG ( "pwlock callback is issued\n" );
88     int pwlocked = plugin_pwlocked();
89
90     parameters* out = ( parameters* ) malloc ( sizeof ( parameters ) );
91     out->number_of_parameter = 1;
92     out->array_of_parameter = ( parameter* ) malloc ( sizeof ( parameter ) );
93     out->array_of_parameter[0].type = type_int32;
94     out->array_of_parameter[0].v_int32 = ( pwlocked == 1 ) ? PLUGIN_RET_ON : PLUGIN_RET_OFF;
95
96     event_handler ( PLUGIN_EVENT_PWLOCK, out );
97 }
98
99 static void register_pwlock_cb()
100 {
101     int ret = vconf_notify_key_changed ( VCONFKEY_IDLE_LOCK_STATE, pwlock_cb, NULL );
102     if ( ret != 0 ) {
103         PLUGIN_LOG ( "cannot register vconf callback.\n" );
104         return;
105     }
106     PLUGIN_LOG ( "registered vconf callback\n" );
107 }
108
109 static void unregister_pwlock_cb()
110 {
111     int ret = vconf_ignore_key_changed ( VCONFKEY_IDLE_LOCK_STATE, pwlock_cb );
112     if ( ret != 0 ) {
113         PLUGIN_LOG ( "cannot unregister vconf callback.\n" );
114         return;
115     }
116     PLUGIN_LOG ( "unregistered vconf callback\n" );
117 }
118
119 static void *pwlock_thread ( void *x )
120 {
121     GMainLoop *loop;
122     loop = g_main_loop_new ( NULL, FALSE );
123     register_pwlock_cb();
124     g_main_loop_run ( loop );
125     g_main_loop_unref ( loop );
126     unregister_pwlock_cb();
127     return NULL;
128 }
129
130 void create_pwlock_thread()
131 {
132     sdb_thread_t t;
133     if ( sdb_thread_create ( &t, pwlock_thread, NULL ) ) {
134         PLUGIN_LOG ( "cannot create_pwlock_thread.\n" );
135         return;
136     }
137     PLUGIN_LOG ( "created pwlock_thread\n" );
138 }