• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (C) 2006 Apple Computer, Inc.
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#import "config.h"
21#import "SearchPopupMenu.h"
22
23#import "AtomicString.h"
24
25namespace WebCore {
26
27SearchPopupMenu::SearchPopupMenu(PopupMenuClient* client)
28    : PopupMenu(client)
29{
30}
31
32static NSString* autosaveKey(const String& name)
33{
34    return [@"com.apple.WebKit.searchField:" stringByAppendingString:name];
35}
36
37bool SearchPopupMenu::enabled()
38{
39    return true;
40}
41
42void SearchPopupMenu::saveRecentSearches(const AtomicString& name, const Vector<String>& searchItems)
43{
44    if (name.isEmpty())
45        return;
46
47    size_t size = searchItems.size();
48    if (size == 0)
49        [[NSUserDefaults standardUserDefaults] removeObjectForKey:autosaveKey(name)];
50    else {
51        NSMutableArray* items = [[NSMutableArray alloc] initWithCapacity:size];
52        for (size_t i = 0; i < size; ++i)
53            [items addObject:searchItems[i]];
54        [[NSUserDefaults standardUserDefaults] setObject:items forKey:autosaveKey(name)];
55        [items release];
56    }
57}
58
59void SearchPopupMenu::loadRecentSearches(const AtomicString& name, Vector<String>& searchItems)
60{
61    if (name.isEmpty())
62        return;
63
64    searchItems.clear();
65    NSArray* items = [[NSUserDefaults standardUserDefaults] arrayForKey:autosaveKey(name)];
66    size_t size = [items count];
67    for (size_t i = 0; i < size; ++i) {
68        NSString* item = [items objectAtIndex:i];
69        if ([item isKindOfClass:[NSString class]])
70            searchItems.append(item);
71    }
72}
73
74}
75