Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(185)

Side by Side Diff: chrome/browser/ui/cocoa/custom_home_pages_model.mm

Issue 6339002: [Mac] Consolidate all files relating to preferences in a subdir of c/b/ui/coc... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' Created 9 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2010 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/custom_home_pages_model.h"
6
7 #include "base/sys_string_conversions.h"
8 #include "chrome/browser/net/url_fixer_upper.h"
9 #include "chrome/browser/prefs/session_startup_pref.h"
10
11 NSString* const kHomepageEntryChangedNotification =
12 @"kHomepageEntryChangedNotification";
13
14 @interface CustomHomePagesModel (Private)
15 - (void)setURLsInternal:(const std::vector<GURL>&)urls;
16 @end
17
18 @implementation CustomHomePagesModel
19
20 - (id)initWithProfile:(Profile*)profile {
21 if ((self = [super init])) {
22 profile_ = profile;
23 entries_.reset([[NSMutableArray alloc] init]);
24 }
25 return self;
26 }
27
28 - (NSUInteger)countOfCustomHomePages {
29 return [entries_ count];
30 }
31
32 - (id)objectInCustomHomePagesAtIndex:(NSUInteger)index {
33 return [entries_ objectAtIndex:index];
34 }
35
36 - (void)insertObject:(id)object inCustomHomePagesAtIndex:(NSUInteger)index {
37 [entries_ insertObject:object atIndex:index];
38 }
39
40 - (void)removeObjectFromCustomHomePagesAtIndex:(NSUInteger)index {
41 [entries_ removeObjectAtIndex:index];
42 // Force a save.
43 [self validateURLs];
44 }
45
46 // Get/set the urls the model currently contains as a group. These will weed
47 // out any URLs that are empty and not add them to the model. As a result,
48 // the next time they're persisted to the prefs backend, they'll disappear.
49 - (std::vector<GURL>)URLs {
50 std::vector<GURL> urls;
51 for (CustomHomePageEntry* entry in entries_.get()) {
52 const char* urlString = [[entry URL] UTF8String];
53 if (urlString && std::strlen(urlString)) {
54 urls.push_back(GURL(std::string(urlString)));
55 }
56 }
57 return urls;
58 }
59
60 - (void)setURLs:(const std::vector<GURL>&)urls {
61 [self willChangeValueForKey:@"customHomePages"];
62 [self setURLsInternal:urls];
63 SessionStartupPref pref(SessionStartupPref::GetStartupPref(profile_));
64 pref.urls = urls;
65 SessionStartupPref::SetStartupPref(profile_, pref);
66 [self didChangeValueForKey:@"customHomePages"];
67 }
68
69 // Converts the C++ URLs to Cocoa objects without notifying KVO.
70 - (void)setURLsInternal:(const std::vector<GURL>&)urls {
71 [entries_ removeAllObjects];
72 for (size_t i = 0; i < urls.size(); ++i) {
73 scoped_nsobject<CustomHomePageEntry> entry(
74 [[CustomHomePageEntry alloc] init]);
75 const char* urlString = urls[i].spec().c_str();
76 if (urlString && std::strlen(urlString)) {
77 [entry setURL:[NSString stringWithCString:urlString
78 encoding:NSUTF8StringEncoding]];
79 [entries_ addObject:entry];
80 }
81 }
82 }
83
84 - (void)reloadURLs {
85 [self willChangeValueForKey:@"customHomePages"];
86 SessionStartupPref pref(SessionStartupPref::GetStartupPref(profile_));
87 [self setURLsInternal:pref.urls];
88 [self didChangeValueForKey:@"customHomePages"];
89 }
90
91 - (void)validateURLs {
92 [self setURLs:[self URLs]];
93 }
94
95 - (void)setURLStringEmptyAt:(NSUInteger)index {
96 // This replaces the data at |index| with an empty (invalid) URL string.
97 CustomHomePageEntry* entry = [entries_ objectAtIndex:index];
98 [entry setURL:[NSString stringWithString:@""]];
99 }
100
101 @end
102
103 //---------------------------------------------------------------------------
104
105 @implementation CustomHomePageEntry
106
107 - (void)setURL:(NSString*)url {
108 // |url| can be nil if the user cleared the text from the edit field.
109 if (!url)
110 url = [NSString stringWithString:@""];
111
112 // Make sure the url is valid before setting it by fixing it up.
113 std::string fixedUrl(URLFixerUpper::FixupURL(
114 base::SysNSStringToUTF8(url), std::string()).possibly_invalid_spec());
115 url_.reset([base::SysUTF8ToNSString(fixedUrl) retain]);
116
117 // Broadcast that an individual item has changed.
118 [[NSNotificationCenter defaultCenter]
119 postNotificationName:kHomepageEntryChangedNotification object:nil];
120
121 // TODO(pinkerton): fetch favicon, convert to NSImage http://crbug.com/34642
122 }
123
124 - (NSString*)URL {
125 return url_.get();
126 }
127
128 - (void)setImage:(NSImage*)image {
129 icon_.reset(image);
130 }
131
132 - (NSImage*)image {
133 return icon_.get();
134 }
135
136 - (NSString*)description {
137 return url_.get();
138 }
139
140 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698