| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #import "chrome/browser/ui/cocoa/search_engine_list_model.h" | |
| 6 | |
| 7 #include "base/sys_string_conversions.h" | |
| 8 #include "chrome/browser/search_engines/template_url.h" | |
| 9 #include "chrome/browser/search_engines/template_url_model.h" | |
| 10 #include "chrome/browser/search_engines/template_url_model_observer.h" | |
| 11 | |
| 12 NSString* const kSearchEngineListModelChangedNotification = | |
| 13 @"kSearchEngineListModelChangedNotification"; | |
| 14 | |
| 15 @interface SearchEngineListModel(Private) | |
| 16 - (void)buildEngineList; | |
| 17 @end | |
| 18 | |
| 19 // C++ bridge from TemplateURLModel to our Obj-C model. When it's told about | |
| 20 // model changes, notifies us to rebuild the list. | |
| 21 class SearchEngineObserver : public TemplateURLModelObserver { | |
| 22 public: | |
| 23 SearchEngineObserver(SearchEngineListModel* notify) | |
| 24 : notify_(notify) { } | |
| 25 virtual ~SearchEngineObserver() { }; | |
| 26 | |
| 27 private: | |
| 28 // TemplateURLModelObserver methods. | |
| 29 virtual void OnTemplateURLModelChanged() { [notify_ buildEngineList]; } | |
| 30 | |
| 31 SearchEngineListModel* notify_; // weak, owns us | |
| 32 }; | |
| 33 | |
| 34 @implementation SearchEngineListModel | |
| 35 | |
| 36 // The windows code allows for a NULL |model| and checks for it throughout | |
| 37 // the code, though I'm not sure why. We follow suit. | |
| 38 - (id)initWithModel:(TemplateURLModel*)model { | |
| 39 if ((self = [super init])) { | |
| 40 model_ = model; | |
| 41 if (model_) { | |
| 42 observer_.reset(new SearchEngineObserver(self)); | |
| 43 model_->Load(); | |
| 44 model_->AddObserver(observer_.get()); | |
| 45 [self buildEngineList]; | |
| 46 } | |
| 47 } | |
| 48 return self; | |
| 49 } | |
| 50 | |
| 51 - (void)dealloc { | |
| 52 if (model_) | |
| 53 model_->RemoveObserver(observer_.get()); | |
| 54 [super dealloc]; | |
| 55 } | |
| 56 | |
| 57 // Returns an array of NSString's corresponding to the user-visible names of the | |
| 58 // search engines. | |
| 59 - (NSArray*)searchEngines { | |
| 60 return engines_.get(); | |
| 61 } | |
| 62 | |
| 63 - (void)setSearchEngines:(NSArray*)engines { | |
| 64 engines_.reset([engines retain]); | |
| 65 | |
| 66 // Tell anyone who's listening that something has changed so they need to | |
| 67 // adjust the UI. | |
| 68 [[NSNotificationCenter defaultCenter] | |
| 69 postNotificationName:kSearchEngineListModelChangedNotification | |
| 70 object:nil]; | |
| 71 } | |
| 72 | |
| 73 // Walks the model and builds an array of NSStrings to display to the user. | |
| 74 // Assumes there is a non-NULL model. | |
| 75 - (void)buildEngineList { | |
| 76 scoped_nsobject<NSMutableArray> engines([[NSMutableArray alloc] init]); | |
| 77 | |
| 78 typedef std::vector<const TemplateURL*> TemplateURLs; | |
| 79 TemplateURLs modelURLs = model_->GetTemplateURLs(); | |
| 80 for (size_t i = 0; i < modelURLs.size(); ++i) { | |
| 81 if (modelURLs[i]->ShowInDefaultList()) | |
| 82 [engines addObject:base::SysWideToNSString(modelURLs[i]->short_name())]; | |
| 83 } | |
| 84 | |
| 85 [self setSearchEngines:engines.get()]; | |
| 86 } | |
| 87 | |
| 88 // The index into |-searchEngines| of the current default search engine. | |
| 89 // -1 if there is no default. | |
| 90 - (NSInteger)defaultIndex { | |
| 91 if (!model_) return -1; | |
| 92 | |
| 93 NSInteger index = 0; | |
| 94 const TemplateURL* defaultSearchProvider = model_->GetDefaultSearchProvider(); | |
| 95 if (defaultSearchProvider) { | |
| 96 typedef std::vector<const TemplateURL*> TemplateURLs; | |
| 97 TemplateURLs urls = model_->GetTemplateURLs(); | |
| 98 for (std::vector<const TemplateURL*>::iterator it = urls.begin(); | |
| 99 it != urls.end(); ++it) { | |
| 100 const TemplateURL* url = *it; | |
| 101 // Skip all the URLs not shown on the default list. | |
| 102 if (!url->ShowInDefaultList()) | |
| 103 continue; | |
| 104 if (url->id() == defaultSearchProvider->id()) | |
| 105 return index; | |
| 106 ++index; | |
| 107 } | |
| 108 } | |
| 109 return -1; | |
| 110 } | |
| 111 | |
| 112 - (void)setDefaultIndex:(NSInteger)index { | |
| 113 if (model_) { | |
| 114 typedef std::vector<const TemplateURL*> TemplateURLs; | |
| 115 TemplateURLs urls = model_->GetTemplateURLs(); | |
| 116 for (std::vector<const TemplateURL*>::iterator it = urls.begin(); | |
| 117 it != urls.end(); ++it) { | |
| 118 const TemplateURL* url = *it; | |
| 119 // Skip all the URLs not shown on the default list. | |
| 120 if (!url->ShowInDefaultList()) | |
| 121 continue; | |
| 122 if (0 == index) { | |
| 123 model_->SetDefaultSearchProvider(url); | |
| 124 return; | |
| 125 } | |
| 126 --index; | |
| 127 } | |
| 128 DCHECK(false); | |
| 129 } | |
| 130 } | |
| 131 | |
| 132 // Return TRUE if the default is managed via policy. | |
| 133 - (BOOL)isDefaultManaged { | |
| 134 return model_->is_default_search_managed(); | |
| 135 } | |
| 136 @end | |
| OLD | NEW |