27d03bf57d7fa3b81d3a998fe0770935aea4e62e
[framework/web/wrt-plugins-common.git] / src / modules / tizen / Messaging / MsgServiceHandleMgr.h
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  *
18  *
19  * @file       CallbackMgr.h
20  * @author     Pawel Misiak (p.misiak@samsung.com)
21  * @version    0.1
22  * @brief
23  */
24
25 #ifndef MSGSERVICEHANDLEMGR_H
26 #define MSGSERVICEHANDLEMGR_H
27
28 #include <dpl/singleton.h>
29 #include <dpl/log/log.h>
30 #include <dpl/shared_ptr.h>
31 #include <dpl/thread.h>
32 #include <Commons/Exception.h>
33
34 extern "C" {
35 #include <MapiControl.h>
36 }
37
38 namespace WrtDeviceApis {
39 namespace Messaging {
40 /*
41  * Class used for msg service handle manager
42  */
43 class MsgServiceHandleMgr
44 {
45   public:
46     MsgServiceHandleMgr() : m_msgHandle(NULL)
47     {
48         LogInfo("enter");
49         // open one handle per all messaging
50         if (MSG_SUCCESS != msg_open_msg_handle(&m_msgHandle)) {
51             LogError("Unable to open handle");
52             Throw(Commons::PlatformException);
53         }
54     }
55
56   public:
57     ~MsgServiceHandleMgr()
58     {
59         LogInfo("enter");
60         if (!!m_msgHandle) {
61             msg_close_msg_handle(&m_msgHandle);
62         }
63     }
64
65     MSG_HANDLE_T getHandle() const
66     {
67         if (!m_msgHandle) {
68             LogError("Messaging handle not initialized");
69             Throw(Commons::PlatformException);
70         }
71         return m_msgHandle;
72     }
73
74   private:
75     MSG_HANDLE_T m_msgHandle;
76 };
77
78 typedef DPL::SharedPtr<MsgServiceHandleMgr> MsgServiceHandleMgrPtr;
79 typedef DPL::ThreadLocalVariable<MsgServiceHandleMgrPtr> TlvMsgServiceHandleMgr;
80
81 class MsgServiceHandle
82 {
83   protected:
84     MsgServiceHandle()
85     {
86     }
87
88   public:
89     ~MsgServiceHandle()
90     {
91     }
92
93     MSG_HANDLE_T getHandle()
94     {
95         if (m_tlvHandleMgr.IsNull()) {
96             m_tlvHandleMgr = MsgServiceHandleMgrPtr(new MsgServiceHandleMgr());
97         }
98         return (*m_tlvHandleMgr)->getHandle();
99     }
100
101   private:
102     TlvMsgServiceHandleMgr m_tlvHandleMgr;
103 };
104
105 typedef DPL::Singleton<MsgServiceHandle> MsgServiceHandleSingleton;
106
107 #define MsgGetCommonHandle() MsgServiceHandleSingleton::Instance().getHandle()
108 }
109 }
110
111 #endif /* MSGSERVICEHANDLEMGR_H */
112