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