SearchPopupMenuMac.mm 2.4 KB

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