tizen 2.3.1 release
[framework/web/mobile/wrt-plugins-tizen.git] / src / Content / ContentFindCallback.h
1 //
2 // Tizen Web Device API
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 #ifndef __TIZEN_CONTENT_FIND_CALLBACK_H__
19 #define __TIZEN_CONTENT_FIND_CALLBACK_H__
20
21 #include <string>
22 #include <memory>
23
24 #include <AbstractFilter.h>
25 #include <SortMode.h>
26
27 #include <JavaScriptCore/JavaScript.h>
28 #include <JSUtil.h>
29 #include <JSWebAPIErrorFactory.h>
30 #include "JSContent.h"
31 #include "JSImageContent.h"
32 #include "JSVideoContent.h"
33 #include "JSAudioContent.h"
34
35
36
37
38 #include "ContentCallback.h"
39 #include "Content.h"
40
41
42 using namespace DeviceAPI::Common;
43 using namespace DeviceAPI::Tizen;
44
45 namespace DeviceAPI {
46 namespace Content {
47
48 class ContentFindCallback : public ContentCallback
49 {
50 public:
51     ContentFindCallback(JSContextRef globalCtx ) : ContentCallback(globalCtx){
52         m_count = -1;
53         m_offset =  0;
54     };
55
56     virtual ~ContentFindCallback(){
57         m_contents.clear();
58     };
59
60     void onSuccess(){
61         JSValueRef valueArray[m_contents.size()];
62         for( unsigned int i = 0 ; i < m_contents.size(); i++){
63             if(std::dynamic_pointer_cast<ImageContent>(m_contents[i])){
64                 valueArray[i] = JSImageContent::CreateJSObject(m_callback->getContext(), m_contents[i]);
65             }
66             else if(std::dynamic_pointer_cast<VideoContent>(m_contents[i])){
67                 valueArray[i] = JSVideoContent::CreateJSObject(m_callback->getContext(), m_contents[i]);
68             }
69             else if(std::dynamic_pointer_cast<AudioContent>(m_contents[i])){
70                 valueArray[i] = JSAudioContent::CreateJSObject(m_callback->getContext(), m_contents[i]);
71             }
72             else if(m_contents[i]){
73                 valueArray[i] = JSContent::CreateJSObject(m_callback->getContext(), m_contents[i]);
74             }
75         }
76         JSValueRef exception = NULL;
77         JSObjectRef jsResult = JSObjectMakeArray(m_callback->getContext(), m_contents.size(), valueArray, &exception);
78         if (exception != NULL) {
79             throw DeviceAPI::Common::UnknownException(m_callback->getContext(), exception);
80         }
81         m_callback->invokeCallback("onsuccess", jsResult);
82         TIME_TRACER_ITEM_END(CONTENT_TT_FIND_TOTAL, CONTENT_TIME_TRACER_SHOW);
83     };
84
85     std::string getDirectoryId(){
86         return m_directoryId;
87     }
88
89     void setDirectoryId(std::string& directoryId){
90         m_directoryId = directoryId;
91     }
92
93     AbstractFilterPtr& getFilter(){
94         return m_filter;
95     };
96
97     void  setFilter(AbstractFilterPtr& filter){
98         m_filter = filter;
99     };
100
101     SortModePtr& getSortMode(){
102         return m_sortMode;
103     }
104
105     void  setSortMode(SortModePtr& sortMode){
106         m_sortMode = sortMode;
107     }
108
109     unsigned long getCount(){
110         return m_count;
111     }
112
113     void setCount(unsigned long count){
114         m_count = count;
115     }
116
117     unsigned long getOffset(){
118         return m_offset;
119     }
120
121     void setOffset(unsigned long offset){
122         m_offset = offset;
123     }
124
125     ContentLst* getContents(){
126         return &m_contents;
127     };
128
129 private:
130     std::string m_directoryId;
131     AbstractFilterPtr m_filter;
132     SortModePtr m_sortMode;
133     unsigned long m_count;
134     unsigned long m_offset;
135
136     ContentLst m_contents;
137 };
138
139 typedef std::shared_ptr<ContentFindCallback> ContentFindCallbackPtr;
140
141 } // Content
142 } // DeviceAPI
143
144 #endif // __TIZEN_CONTENT_FIND_CALLBACK_H__