1/*
2 * Copyright (C) 2006 Apple Computer, 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#import "SearchPopupMenuMac.h"
22
23#include "PopupMenuMac.h"
24#include <wtf/text/AtomicString.h>
25
26using namespace WebCore;
27
28SearchPopupMenuMac::SearchPopupMenuMac(PopupMenuClient* client)
29    : m_popup(adoptRef(new PopupMenuMac(client)))
30{
31}
32
33SearchPopupMenuMac::~SearchPopupMenuMac()
34{
35}
36
37static NSString *autosaveKey(const String& name)
38{
39    return [@"com.apple.WebKit.searchField:" stringByAppendingString:name];
40}
41
42PopupMenu* SearchPopupMenuMac::popupMenu()
43{
44    return m_popup.get();
45}
46
47bool SearchPopupMenuMac::enabled()
48{
49    return true;
50}
51
52void SearchPopupMenuMac::saveRecentSearches(const AtomicString& name, const Vector<String>& searchItems)
53{
54    if (name.isEmpty())
55        return;
56
57    if (searchItems.isEmpty()) {
58        [[NSUserDefaults standardUserDefaults] removeObjectForKey:autosaveKey(name)];
59        return;
60    }
61
62    RetainPtr<NSMutableArray> items = adoptNS([[NSMutableArray alloc] initWithCapacity:searchItems.size()]);
63    for (const auto& searchItem: searchItems)
64        [items addObject:searchItem];
65
66    [[NSUserDefaults standardUserDefaults] setObject:items.get() forKey:autosaveKey(name)];
67}
68
69void SearchPopupMenuMac::loadRecentSearches(const AtomicString& name, Vector<String>& searchItems)
70{
71    if (name.isEmpty())
72        return;
73
74    searchItems.clear();
75    for (NSString *item in [[NSUserDefaults standardUserDefaults] arrayForKey:autosaveKey(name)]) {
76        if ([item isKindOfClass:[NSString class]])
77            searchItems.append(item);
78    }
79}
80