1/*
2 * Copyright (C) 2012 Intel Corporation. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "ewk_form_submission_request.h"
28
29#include "WKAPICast.h"
30#include "WKArray.h"
31#include "WKBase.h"
32#include "WKString.h"
33#include "ewk_form_submission_request_private.h"
34
35using namespace WebKit;
36
37EwkFormSubmissionRequest::EwkFormSubmissionRequest(WKDictionaryRef values, WKFormSubmissionListenerRef listener)
38    : m_wkValues(values)
39    , m_wkListener(listener)
40    , m_handledRequest(false)
41{ }
42
43EwkFormSubmissionRequest::~EwkFormSubmissionRequest()
44{
45    // Make sure the request is always handled before destroying.
46    if (!m_handledRequest)
47        WKFormSubmissionListenerContinue(m_wkListener.get());
48}
49
50Eina_Stringshare* EwkFormSubmissionRequest::copyFieldValue(const char* fieldName) const
51{
52    ASSERT(fieldName);
53    WKRetainPtr<WKStringRef> wkFieldName = adoptWK(WKStringCreateWithUTF8CString(fieldName));
54    WKStringRef wkValue = static_cast<WKStringRef>(WKDictionaryGetItemForKey(m_wkValues.get(), wkFieldName.get()));
55
56    return WKEinaSharedString(wkValue).leakString();
57}
58
59WKRetainPtr<WKArrayRef> EwkFormSubmissionRequest::fieldNames() const
60{
61    return adoptWK(WKDictionaryCopyKeys(m_wkValues.get()));
62}
63
64void EwkFormSubmissionRequest::submit()
65{
66    WKFormSubmissionListenerContinue(m_wkListener.get());
67    m_handledRequest = true;
68}
69
70Eina_List* ewk_form_submission_request_field_names_get(Ewk_Form_Submission_Request* request)
71{
72    EWK_OBJ_GET_IMPL_OR_RETURN(EwkFormSubmissionRequest, request, impl, 0);
73
74    Eina_List* fieldNames = 0;
75
76    WKRetainPtr<WKArrayRef> wkFieldNames = impl->fieldNames();
77    const size_t numKeys = WKArrayGetSize(wkFieldNames.get());
78    for (size_t i = 0; i < numKeys; ++i) {
79        WKStringRef wkFieldName = static_cast<WKStringRef>(WKArrayGetItemAtIndex(wkFieldNames.get(), i));
80        fieldNames = eina_list_append(fieldNames, WKEinaSharedString(wkFieldName).leakString());
81    }
82
83    return fieldNames;
84}
85
86const char* ewk_form_submission_request_field_value_get(Ewk_Form_Submission_Request* request, const char* name)
87{
88    EWK_OBJ_GET_IMPL_OR_RETURN(EwkFormSubmissionRequest, request, impl, 0);
89    EINA_SAFETY_ON_NULL_RETURN_VAL(name, 0);
90
91    return impl->copyFieldValue(name);
92}
93
94Eina_Bool ewk_form_submission_request_submit(Ewk_Form_Submission_Request* request)
95{
96    EWK_OBJ_GET_IMPL_OR_RETURN(EwkFormSubmissionRequest, request, impl, false);
97
98    impl->submit();
99
100    return true;
101}
102