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    return String("com.apple.WebKit.searchField:" + name).createCFString();
55}
56#endif
57
58void SearchPopupMenuWin::saveRecentSearches(const AtomicString& name, const Vector<String>& searchItems)
59{
60    if (name.isEmpty())
61        return;
62
63#if USE(CF)
64    RetainPtr<CFMutableArrayRef> items;
65
66    size_t size = searchItems.size();
67    if (size) {
68        items = adoptCF(CFArrayCreateMutable(0, size, &kCFTypeArrayCallBacks));
69        for (size_t i = 0; i < size; ++i)
70            CFArrayAppendValue(items.get(), searchItems[i].createCFString().get());
71    }
72
73    CFPreferencesSetAppValue(autosaveKey(name).get(), items.get(), kCFPreferencesCurrentApplication);
74    CFPreferencesAppSynchronize(kCFPreferencesCurrentApplication);
75#endif
76}
77
78void SearchPopupMenuWin::loadRecentSearches(const AtomicString& name, Vector<String>& searchItems)
79{
80    if (name.isEmpty())
81        return;
82
83#if USE(CF)
84    searchItems.clear();
85    RetainPtr<CFArrayRef> items = adoptCF(reinterpret_cast<CFArrayRef>(CFPreferencesCopyAppValue(autosaveKey(name).get(), kCFPreferencesCurrentApplication)));
86
87    if (!items || CFGetTypeID(items.get()) != CFArrayGetTypeID())
88        return;
89
90    size_t size = CFArrayGetCount(items.get());
91    for (size_t i = 0; i < size; ++i) {
92        CFStringRef item = (CFStringRef)CFArrayGetValueAtIndex(items.get(), i);
93        if (CFGetTypeID(item) == CFStringGetTypeID())
94            searchItems.append(item);
95    }
96#endif
97}
98
99}
100