1/*
2 * Copyright (C) 2006, 2007 Apple Inc.
3 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB.  If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
20
21#include "config.h"
22#include "SearchPopupMenuWin.h"
23
24#include <wtf/text/AtomicString.h>
25
26#if USE(CF)
27#include <wtf/RetainPtr.h>
28#endif
29
30namespace WebCore {
31
32SearchPopupMenuWin::SearchPopupMenuWin(PopupMenuClient* client)
33    : m_popup(adoptRef(new PopupMenuWin(client)))
34{
35}
36
37PopupMenu* SearchPopupMenuWin::popupMenu()
38{
39    return m_popup.get();
40}
41
42bool SearchPopupMenuWin::enabled()
43{
44#if USE(CF)
45    return true;
46#else
47    return false;
48#endif
49}
50
51#if USE(CF)
52static RetainPtr<CFStringRef> autosaveKey(const String& name)
53{
54    String key = "com.apple.WebKit.searchField:" + name;
55    return RetainPtr<CFStringRef>(AdoptCF, key.createCFString());
56}
57#endif
58
59void SearchPopupMenuWin::saveRecentSearches(const AtomicString& name, const Vector<String>& searchItems)
60{
61    if (name.isEmpty())
62        return;
63
64#if USE(CF)
65    RetainPtr<CFMutableArrayRef> items;
66
67    size_t size = searchItems.size();
68    if (size) {
69        items.adoptCF(CFArrayCreateMutable(0, size, &kCFTypeArrayCallBacks));
70        for (size_t i = 0; i < size; ++i) {
71            RetainPtr<CFStringRef> item(AdoptCF, searchItems[i].createCFString());
72            CFArrayAppendValue(items.get(), item.get());
73        }
74    }
75
76    CFPreferencesSetAppValue(autosaveKey(name).get(), items.get(), kCFPreferencesCurrentApplication);
77    CFPreferencesAppSynchronize(kCFPreferencesCurrentApplication);
78#endif
79}
80
81void SearchPopupMenuWin::loadRecentSearches(const AtomicString& name, Vector<String>& searchItems)
82{
83    if (name.isEmpty())
84        return;
85
86#if USE(CF)
87    searchItems.clear();
88    RetainPtr<CFArrayRef> items(AdoptCF, reinterpret_cast<CFArrayRef>(CFPreferencesCopyAppValue(autosaveKey(name).get(), kCFPreferencesCurrentApplication)));
89
90    if (!items || CFGetTypeID(items.get()) != CFArrayGetTypeID())
91        return;
92
93    size_t size = CFArrayGetCount(items.get());
94    for (size_t i = 0; i < size; ++i) {
95        CFStringRef item = (CFStringRef)CFArrayGetValueAtIndex(items.get(), i);
96        if (CFGetTypeID(item) == CFStringGetTypeID())
97            searchItems.append(item);
98    }
99#endif
100}
101
102}
103