Tizen 2.1 base
[platform/framework/native/image.git] / src / FMedia_ImageUriDataEvent.cpp
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 /**
19  * @file   FMedia_ImageUriDataEvent.cpp
20  * @brief  This file contains the implementation of _ImageUriDataEvent class,
21  *               required internally by Image::DecodeUrl.
22  */
23
24 #include <unique_ptr.h>
25 #include <FMediaImage.h>
26 #include <FMediaIImageEventListener.h>
27
28 #include <FBaseSysLog.h>
29 #include <FMedia_ImageImpl.h>
30
31 #include "FMedia_ImageUriDataFactory.h"
32 #include "FMedia_ImageUriDataEvent.h"
33 #include "FMedia_ImageUriDataEventArg.h"
34
35
36 using namespace Tizen::Base;
37 using namespace Tizen::Graphics;
38
39 namespace Tizen{ namespace Media{
40
41
42 _ImageUriDataEvent::_ImageUriDataEvent(void):__cntListener(0)
43 {
44
45 }
46
47 _ImageUriDataEvent::~_ImageUriDataEvent(void)
48 {
49
50 }
51
52 result
53 _ImageUriDataEvent::Construct(void)
54 {
55         result r = E_SUCCESS;
56         r = _Event::Initialize();
57         return r;
58 }
59
60 void
61 _ImageUriDataEvent::FireImpl(Runtime::IEventListener& listener, const Runtime::IEventArg& arg)
62 {
63         RequestId reqId = -1;
64
65         Runtime::IEventArg* pTempArg = const_cast <Runtime::IEventArg*>(&arg);
66         SysTryReturn(NID_MEDIA, pTempArg != null, ,E_INVALID_ARG,
67                   "[E_INVALID_ARG] Received invalid EventArg.");
68         _ImageUriDataEventTypeArg* pEventTypeArg = dynamic_cast<_ImageUriDataEventTypeArg*>(pTempArg);
69         SysTryReturn(NID_MEDIA, pEventTypeArg != null, ,E_INVALID_ARG,
70                   "[E_INVALID_ARG] Received invalid EventArg.");
71
72         SysLog(NID_MEDIA, "Event Type(): %d", pEventTypeArg->GetEventType());
73         reqId = pEventTypeArg->GetRequestId();
74
75         switch (pEventTypeArg->GetEventType())
76         {
77         case IMAGE_URI_DATA_EVENT_ERROR:
78         {
79                 _ImageUriDataErrorArg* pArg = dynamic_cast<_ImageUriDataErrorArg*>(pEventTypeArg);
80                 SysTryReturn(NID_MEDIA, pArg != null, ,E_INVALID_ARG,
81                         "[E_INVALID_ARG] Received invalid EventTypeArg.");
82                 IImageDecodeUrlEventListener* pEventListener = dynamic_cast<IImageDecodeUrlEventListener*>(&listener);
83                 SysTryReturn(NID_MEDIA, pEventListener != null, ,E_INVALID_ARG,
84                         "[E_INVALID_ARG] Received invalid ImageDecodeUrlEventListener.");
85
86                 pEventListener->OnImageDecodeUrlReceived(reqId, null,
87                 pArg->GetError(), pArg->GetErrorCode(), pArg->GetErrorMessage());
88         }
89         break;
90
91         case IMAGE_URI_DATA_EVENT_SUCCESS:
92         {
93                 _ImageUriDataEventArg* pArg = dynamic_cast<_ImageUriDataEventArg*>(pEventTypeArg);
94                 SysTryReturn(NID_MEDIA, pArg != null, ,E_INVALID_ARG,
95                         "[E_INVALID_ARG] Received invalid EventTypeArg.");
96                 IImageDecodeUrlEventListener* pEventListener = dynamic_cast<IImageDecodeUrlEventListener*>(&listener);
97                 SysTryReturn(NID_MEDIA, pEventListener != null, ,E_INVALID_ARG,
98                         "[E_INVALID_ARG] Received invalid ImageDecodeUrlEventListener.");
99
100                 if (pArg->GetData() != null)            // if the network sends only file data.
101                 {
102                         // decode data to bitmap & call the listener.
103                         std::unique_ptr<Bitmap> pBitmap(_ImageImpl::DecodeToBitmapN(*((pArg)->GetData()),
104                                 pArg->GetColorFormat(),
105                                 pArg->GetDestDimension(),
106                                 BUFFER_SCALING_AUTO,
107                                 pArg->GetImageFormat()));
108
109                         if (pBitmap.get() != null)
110                         {
111                                 pEventListener->OnImageDecodeUrlReceived(
112                                 reqId, pBitmap.get(), E_SUCCESS, L"E_SUCCESS", L"E_SUCCESS");
113                         }
114                         else            // decode fail
115                         {
116                                 pEventListener->OnImageDecodeUrlReceived(
117                                 reqId, null, E_DECODING_FAILED, L"E_DECODING_FAILED", L"E_DECODING_FAILED");
118                         }
119                 }
120                 else            // if E_SUCCESS but the data is null
121                 {
122                         pEventListener->OnImageDecodeUrlReceived(
123                         reqId, null, E_SYSTEM, L"E_SYSTEM", L"E_SYSTEM");
124                 }
125         }
126         break;
127
128         case IMAGE_URI_DATA_EVENT_CANCEL:
129         {
130                 IImageDecodeUrlEventListener* pEventListener = dynamic_cast<IImageDecodeUrlEventListener*>(&listener);
131                 SysTryReturn(NID_MEDIA, pEventListener != null, ,E_INVALID_ARG,
132                         "[E_INVALID_ARG] Received invalid ImageDecodeUrlEventListener.");
133                 _ImageUriDataEventArg* pArg = dynamic_cast<_ImageUriDataEventArg*>(pEventTypeArg);
134                 SysTryReturn(NID_MEDIA, pArg != null, ,E_INVALID_ARG,
135                         "[E_INVALID_ARG] Received invalid EventTypeArg.");
136                 pEventListener->OnImageDecodeUrlReceived(reqId, null, pArg->GetResult(),
137                 GetErrorMessage(pArg->GetResult()), GetErrorMessage(pArg->GetResult()));
138         }
139         break;
140
141         case IMAGE_URI_DATA_EVENT_DESTROY:
142         {
143         _ImageUriDataFactory* pFactory = _ImageUriDataFactory::GetInstance();
144
145                 if (pFactory != null)
146                 {
147                         pFactory->OnMediaSessionEnded(reqId);
148                 }
149
150         }
151         break;
152
153         default:
154         break;
155 }
156
157 }
158
159 }} // Tizen::Media