50c513f9c7e087785aa0dacc61785b3957dc5d80
[framework/osp/social.git] / src / FScl_ContactDbMonitor.h
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 /**
18 * @file         FScl_ContactDbMonitor.h
19 * @brief        This is the header file for the _ContactDbMonitor class.
20 *
21 * This header file contains the declarations of the _ContactDbMonitor class.
22 */
23
24 #ifndef _FSCL_INTERNAL_CONTACT_DB_MONITOR_H_
25 #define _FSCL_INTERNAL_CONTACT_DB_MONITOR_H_
26
27 #include <new>
28 #include <pthread.h>
29 #include <unique_ptr.h>
30 #include <contacts.h>
31 #include <FBaseTypes.h>
32 #include <FBaseResult.h>
33 #include <FBaseObject.h>
34
35 namespace Tizen { namespace Base {
36
37 namespace Collection
38 {
39 template<typename Type>
40 class ArrayListT;
41 }
42
43 namespace Runtime
44 {
45 class Mutex;
46 }
47
48 }}
49
50 namespace Tizen { namespace Social
51 {
52 class _IContactDbChangeEventListener;
53 class _ContactDbChangeEvent;
54
55 /**
56  * @class       _ContactDbMonitor
57  * @brief       This class handles the _ContactDbMonitor operations.
58  * @since       2.0
59  * @see   _AddressbookImpl
60  *
61  */
62 class _ContactDbMonitor
63         : public Tizen::Base::Object
64 {
65 public:
66         result AddListener(const _IContactDbChangeEventListener& listener);
67
68         result RemoveListener(const _IContactDbChangeEventListener& listener);
69
70         int GetAccountIdByAddressbookId(int addressbookId);
71
72         static _ContactDbMonitor* GetInstance(void);
73
74 private:
75         _ContactDbMonitor(void);
76
77         virtual ~_ContactDbMonitor(void);
78
79         result Construct(void);
80
81         _ContactDbMonitor(const _ContactDbMonitor& rhs);
82
83         static void OnContactDbChanged(const char* viewUri, void* pUserData);
84
85         _ContactDbMonitor& operator =(const _ContactDbMonitor& rhs);
86
87         static void InitSingleton(void);
88
89         static void DestroySingleton(void);
90
91         result UpdateAddressbookList(void);
92
93
94 private:
95         class __Callback
96         {
97         public:
98                 __Callback(const char* uri, contacts_db_changed_cb callback, void* pData);
99                 result Register(void);
100                 ~__Callback(void);
101
102         private:
103                 char* __uri;
104                 contacts_db_changed_cb __callback;
105                 void* __pData;
106                 bool __registerred;
107         };
108
109         class __List
110         {
111                 public:
112                         __List(contacts_list_h list)
113                         : __list(list)
114                         {
115                         }
116
117                         contacts_list_h Release(void)
118                         {
119                                 contacts_list_h list = __list;
120
121                                 __list = null;
122
123                                 return list;
124                         }
125
126                         ~__List(void)
127                         {
128                                 if (__list != null)
129                                 {
130                                         contacts_list_destroy(__list, true);
131                                 }
132                         }
133                 private:
134                         contacts_list_h __list;
135         };
136
137         class __Lock
138         {
139                 public:
140                         __Lock(Tizen::Base::Runtime::Mutex* pMutex)
141                         :__pMutex(null)
142                         {
143                                 result r = pMutex->Acquire();
144                                 SysTryReturnVoidResult(NID_SCL, r == E_SUCCESS, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
145
146                                 __pMutex = pMutex;
147                         }
148
149                         ~__Lock(void)
150                         {
151                                 if (__pMutex != null)
152                                 {
153                                         result r = __pMutex->Release();
154                                         SysTryReturnVoidResult(NID_SCL, r == E_SUCCESS, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
155                                 }
156                         }
157
158                 private:
159                         Tizen::Base::Runtime::Mutex* __pMutex;
160         };
161
162 private:
163         std::unique_ptr<_ContactDbChangeEvent> __pEvent;
164         std::unique_ptr<Tizen::Base::Runtime::Mutex> __pMutex;
165         std::unique_ptr<__Callback> __pContactChangeCallback;
166         std::unique_ptr<__Callback> __pGroupChangeCallback;
167         std::unique_ptr<__Callback> __pGroupRelationChagneCallback;
168         std::unique_ptr<__Callback> __pAddressbookChagneCallback;
169         contacts_list_h __pAddressbookList;
170
171         static _ContactDbMonitor* __pTheInstance;
172         friend struct std::default_delete< _ContactDbMonitor >;
173 };      // _ContactDbMonitor
174
175 }}  // Tizen::Social
176
177 #endif //_FSCL_INTERNAL_CONTACT_DB_MONITOR_H_