1/*
2 Copyright (C) 2012 Intel Corporation
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
18*/
19
20#include "config.h"
21#include "ewk_intent.h"
22
23#include "Intent.h"
24#include "NotImplemented.h"
25#include "SerializedScriptValue.h"
26#include "ewk_intent_private.h"
27#include "ewk_private.h"
28#include <eina_safety_checks.h>
29#include <wtf/HashMap.h>
30#include <wtf/text/CString.h>
31
32/**
33 * \struct _Ewk_Intent
34 * @brief Contains the intent data.
35 */
36struct _Ewk_Intent {
37#if ENABLE(WEB_INTENTS)
38 WebCore::Intent* core; /**< WebCore object which is responsible for the intent */
39#endif
40 const char* action;
41 const char* type;
42 const char* data;
43 const char* service;
44};
45
46#define EWK_INTENT_CORE_GET_OR_RETURN(intent, core_, ...) \
47 if (!(intent)) { \
48 CRITICAL("intent is NULL."); \
49 return __VA_ARGS__; \
50 } \
51 if (!(intent)->core) { \
52 CRITICAL("intent->core is NULL."); \
53 return __VA_ARGS__; \
54 } \
55 WebCore::Intent* core_ = (intent)->core
56
57const char* ewk_intent_action_get(const Ewk_Intent* intent)
58{
59#if ENABLE(WEB_INTENTS)
60 EWK_INTENT_CORE_GET_OR_RETURN(intent, core, 0);
61
62 // hide the following optimzation from outside
63 Ewk_Intent* ewkIntent = const_cast<Ewk_Intent*>(intent);
64 eina_stringshare_replace(&ewkIntent->action,
65 core->action().utf8().data());
66 return ewkIntent->action;
67#else
68 return 0;
69#endif
70}
71
72const char* ewk_intent_type_get(const Ewk_Intent* intent)
73{
74#if ENABLE(WEB_INTENTS)
75 EWK_INTENT_CORE_GET_OR_RETURN(intent, core, 0);
76
77 // hide the following optimzation from outside
78 Ewk_Intent* ewkIntent = const_cast<Ewk_Intent*>(intent);
79 eina_stringshare_replace(&ewkIntent->type,
80 core->type().utf8().data());
81 return ewkIntent->type;
82#else
83 return 0;
84#endif
85}
86
87const char* ewk_intent_data_get(const Ewk_Intent* intent)
88{
89#if ENABLE(WEB_INTENTS)
90 notImplemented();
91 return 0;
92#else
93 return 0;
94#endif
95}
96
97const char* ewk_intent_service_get(const Ewk_Intent* intent)
98{
99#if ENABLE(WEB_INTENTS)
100 EWK_INTENT_CORE_GET_OR_RETURN(intent, core, 0);
101
102 // hide the following optimzation from outside
103 Ewk_Intent* ewkIntent = const_cast<Ewk_Intent*>(intent);
104 eina_stringshare_replace(&ewkIntent->service,
105 core->service().string().utf8().data());
106 return ewkIntent->service;
107#else
108 return 0;
109#endif
110}
111
112char* ewk_intent_extra_get(const Ewk_Intent* intent, const char* key)
113{
114#if ENABLE(WEB_INTENTS)
115 EWK_INTENT_CORE_GET_OR_RETURN(intent, core, 0);
116 WTF::HashMap<String, String>::const_iterator val = core->extras().find(String::fromUTF8(key));
117 if (val == core->extras().end())
118 return 0;
119 return strdup(val->second.utf8().data());
120#else
121 return 0;
122#endif
123}
124
125#if ENABLE(WEB_INTENTS)
126/**
127 * @internal
128 *
129 * Creates a new Ewk_Intent object.
130 *
131 * @param core WebCore::Intent instance to use internally.
132 * @return a new allocated the Ewk_Intent object on sucess or @c 0 on failure
133 */
134Ewk_Intent* ewk_intent_new(WebCore::Intent* core)
135{
136 EINA_SAFETY_ON_NULL_RETURN_VAL(core, 0);
137 Ewk_Intent* intent = new Ewk_Intent;
138 intent->core = core;
139
140 return intent;
141}
142
143/**
144 * @internal
145 *
146 * Free given Ewk_Intent instance.
147 *
148 * @param intent the object to free.
149 */
150void ewk_intent_free(Ewk_Intent* intent)
151{
152 EINA_SAFETY_ON_NULL_RETURN(intent);
153
154 delete intent;
155}
156#endif