[Release] wrt_0.8.274
[platform/framework/web/wrt.git] / src / wrt-client / client_service_support.cpp
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 #include "client_service_support.h"
17 #include <app.h>
18 #include <string>
19 #include <dpl/log/log.h>
20 #include <dpl/exception.h>
21
22 namespace ClientModule {
23 namespace {
24 class Exception
25 {
26   public:
27     DECLARE_EXCEPTION_TYPE(DPL::Exception, Base)
28     DECLARE_EXCEPTION_TYPE(Base, ServiceHandleCreateError)
29     DECLARE_EXCEPTION_TYPE(Base, ServiceHandleSetError)
30 };
31 bool isError(int error);
32
33 bool isError(int error) {
34     if (SERVICE_ERROR_NONE == error) {
35         return false;
36     } else {
37         LogError("Service error [" << error << "]");
38         return true;
39     }
40 }
41 }
42
43 bool ServiceSupport::launchViewService(unsigned int windowHandle,
44                                        const std::string& url)
45 {
46     if (url.empty() || url.size() == 0) {
47         LogError("Input url is invalid");
48         return false;
49     }
50
51     service_h serviceHandle = NULL;
52     Try {
53         int error = SERVICE_ERROR_NONE;
54         error = service_create(&serviceHandle);
55         if (isError(error)) {
56             Throw(Exception::ServiceHandleCreateError);
57         }
58
59         error = service_set_operation(serviceHandle, SERVICE_OPERATION_VIEW);
60         if (isError(error)) {
61             ThrowMsg(Exception::ServiceHandleSetError,
62                      "service_set_operation error");
63         }
64
65         error = service_set_uri(serviceHandle, url.c_str());
66         if (isError(error)) {
67             ThrowMsg(Exception::ServiceHandleSetError,
68                      "service_set_uri error");
69         }
70
71         if (windowHandle != 0) {
72             error = service_set_window(serviceHandle, windowHandle);
73             if (isError(error)) {
74                 ThrowMsg(Exception::ServiceHandleSetError,
75                          "service_set_window error");
76             }
77         }
78
79         error = service_send_launch_request(serviceHandle, NULL, NULL);
80         if (isError(error)) {
81             ThrowMsg(Exception::ServiceHandleSetError,
82                      "service_send_launch_request error");
83         }
84         service_destroy(serviceHandle);
85     } Catch(Exception::ServiceHandleCreateError) {
86         LogError("Fail to create service handle");
87         return false;
88     } Catch(Exception::ServiceHandleSetError) {
89         LogError(_rethrown_exception.GetMessage());
90         service_destroy(serviceHandle);
91         return false;
92     } Catch(DPL::Exception) {
93         LogError(_rethrown_exception.GetMessage());
94         return false;
95     }
96
97     return true;
98 }
99
100 bool ServiceSupport::launchShareService(unsigned int windowHandle,
101                                         const std::string& url)
102 {
103     if (url.empty() || url.size() == 0) {
104         LogError("Input url is invalid");
105         return false;
106     }
107
108     service_h serviceHandle = NULL;
109     Try {
110         int error = SERVICE_ERROR_NONE;
111         error = service_create(&serviceHandle);
112         if (isError(error)) {
113             Throw(Exception::ServiceHandleCreateError);
114         }
115
116         error = service_set_operation(serviceHandle,
117                                       SERVICE_OPERATION_SHARE_TEXT);
118         if (isError(error)) {
119             ThrowMsg(Exception::ServiceHandleSetError,
120                      "service_set_operation error");
121         }
122
123         if (windowHandle != 0) {
124             error = service_set_window(serviceHandle, windowHandle);
125             if (isError(error)) {
126                 ThrowMsg(Exception::ServiceHandleSetError,
127                          "service_set_window error");
128             }
129         }
130
131         error = service_add_extra_data(serviceHandle,
132                                        SERVICE_DATA_TEXT,
133                                        url.c_str());
134         if (isError(error)) {
135             ThrowMsg(Exception::ServiceHandleSetError,
136                      "service_add_extra_data error");
137         }
138
139         error = service_send_launch_request(serviceHandle, NULL, NULL);
140         if (isError(error)) {
141             ThrowMsg(Exception::ServiceHandleSetError,
142                      "service_send_launch_request error");
143         }
144         service_destroy(serviceHandle);
145     } Catch(Exception::ServiceHandleCreateError) {
146         LogError("Fail to create service handle");
147         return false;
148     } Catch(Exception::ServiceHandleSetError) {
149         LogError(_rethrown_exception.GetMessage());
150         service_destroy(serviceHandle);
151         return false;
152     } Catch(DPL::Exception) {
153         LogError(_rethrown_exception.GetMessage());
154         return false;
155     }
156     return true;
157 }
158
159 } // ClientModule