summaryrefslogtreecommitdiffstats
path: root/chrome/browser/cocoa/import_settings_dialog.mm
blob: 7d8babef7ac7b849b5148cc58acc06b22b8f0156 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#import "chrome/browser/cocoa/import_settings_dialog.h"

#include "base/mac_util.h"
#include "base/sys_string_conversions.h"
#include "chrome/browser/importer/importer_list.h"
#include "chrome/browser/profile.h"

@interface ImportSettingsDialogController ()

@property(assign, readwrite, nonatomic) BOOL historyAvailable;
@property(assign, readwrite, nonatomic) BOOL favoritesAvailable;
@property(assign, readwrite, nonatomic) BOOL cookiesAvailable;
@property(assign, readwrite, nonatomic) BOOL passwordsAvailable;
@property(assign, readwrite, nonatomic) BOOL searchEnginesAvailable;

@end

@implementation ImportSettingsProfile

@synthesize browserName = browserName_;
@synthesize services = services_;

+ (id)importSettingsProfileWithBrowserName:(NSString*)browserName
                                  services:(uint16)services {
  id settingsProfile = [[[ImportSettingsProfile alloc]
                        initWithBrowserName:browserName
                         services:services] autorelease];
  return settingsProfile;
}

- (id)initWithBrowserName:(NSString*)browserName
                 services:(uint16)services {
  DCHECK(browserName && services);
  if ((self = [super init])) {
    if (browserName && services != 0) {
      browserName_ = [browserName retain];
      services_ = services;
    } else {
      [self release];
      self = nil;
    }
  }
  return self;
}

- (id)init {
  NOTREACHED();  // Should never be called.
  return [self initWithBrowserName:NULL services:0];
}

- (void)dealloc {
  [browserName_ release];
  [super dealloc];
}

@end

@implementation ImportSettingsDialogController

@synthesize sourceBrowserIndex = sourceBrowserIndex_;
@synthesize importHistory = importHistory_;
@synthesize importFavorites = importFavorites_;
@synthesize importCookies = importCookies_;
@synthesize importPasswords = importPasswords_;
@synthesize importSearchEngines = importSearchEngines_;
@synthesize historyAvailable = historyAvailable_;
@synthesize favoritesAvailable = favoritesAvailable_;
@synthesize cookiesAvailable = cookiesAvailable_;
@synthesize passwordsAvailable = passwordsAvailable_;
@synthesize searchEnginesAvailable = searchEnginesAvailable_;

// Set bindings dependencies for importSomething property.
+ (NSSet*)keyPathsForValuesAffectingImportSomething {
  return [NSSet setWithObjects:@"importHistory", @"importFavorites",
          @"importCookies", @"importPasswords", @"importSearchEngines", nil];
}

- (id)initWithProfile:(Profile*)profile
         parentWindow:(NSWindow*)parentWindow {
  // Collect profile information (profile name and the services which can
  // be imported from each) into an array of ImportSettingsProfile which
  // are bound to the Browser List array controller and the popup name
  // presentation.  The services element is used to indirectly control
  // checkbox enabling.
  importerList_.reset(new ImporterList);
  ImporterList& importerList(*(importerList_.get()));
  importerList.DetectSourceProfiles();
  int profilesCount = importerList.GetAvailableProfileCount();
  // There shoule be at least the default profile so this should never be zero.
  DCHECK(profilesCount > 0);
  NSMutableArray* browserProfiles =
      [NSMutableArray arrayWithCapacity:profilesCount];
  for (int i = 0; i < profilesCount; ++i) {
    const ProfileInfo& sourceProfile = importerList.GetSourceProfileInfoAt(i);
    NSString* browserName =
        base::SysWideToNSString(sourceProfile.description);
    uint16 browserServices = sourceProfile.services_supported;
    ImportSettingsProfile* settingsProfile =
        [ImportSettingsProfile
         importSettingsProfileWithBrowserName:browserName
                                     services:browserServices];
    [browserProfiles addObject:settingsProfile];
  }
  if ((self = [self initWithProfiles:browserProfiles
                        parentWindow:parentWindow])) {
    profile_ = profile;
    parentWindow_ = parentWindow;
  }
  return self;
}

- (id)initWithProfiles:(NSArray*)profiles
          parentWindow:(NSWindow*)parentWindow {
  NSString* nibpath =
      [mac_util::MainAppBundle() pathForResource:@"ImportSettingsDialog"
                                          ofType:@"nib"];
  if ((self = [super initWithWindowNibPath:nibpath owner:self])) {
    sourceBrowsersList_.reset([profiles retain]);
    // Create and initialize an importerList_ when running unit tests.
    if (!importerList_.get()) {
      importerList_.reset(new ImporterList);
      ImporterList& importerList(*(importerList_.get()));
      importerList.DetectSourceProfiles();
    }
  }
  return self;
}

- (id)init {
  return [self initWithProfile:NULL parentWindow:nil];
}

- (void)awakeFromNib {
  // Force an update of the checkbox enabled states.
  [self setSourceBrowserIndex:0];
}

- (void)runModalDialog {
  // If there is no parentWindow_ then this will present as a regular
  // modal dialog not hanging off of any other window.
  [NSApp beginSheet:[self window]
     modalForWindow:parentWindow_
      modalDelegate:self
     didEndSelector:@selector(didEndSheet:returnCode:contextInfo:)
        contextInfo:nil];
}

- (IBAction)ok:(id)sender {
  const ProfileInfo& sourceProfile =
      importerList_.get()->GetSourceProfileInfoAt([self sourceBrowserIndex]);
  uint16 items = sourceProfile.services_supported;
  // ProfileInfo.services_supported is a uint16 while the call to
  // StartImportingWithUI requires an int16.
  int16 servicesToImport = static_cast<int16>(items & [self servicesToImport]);
  if (servicesToImport) {
    if (profile_) {
      ImporterHost* importerHost = new ImporterHost;
      // Note that a side effect of the following call is to cause the
      // importerHost to be disposed once the import has completed.
      StartImportingWithUI(nil, servicesToImport, importerHost,
                           sourceProfile, profile_, nil, false);
    }
  } else {
    LOG(WARNING) << "There were no settings to import from '"
                 << sourceProfile.description << "'.";
  }
  [NSApp endSheet:[self window]];
}

- (IBAction)cancel:(id)sender {
  [NSApp endSheet:[self window]];
}

- (void)didEndSheet:(NSWindow*)sheet
         returnCode:(int)returnCode
        contextInfo:(void*)contextInfo {
  [sheet close];
}

- (void)windowWillClose:(NSNotification*)notification {
  [self autorelease];
}

#pragma mark Accessors

- (NSArray*)sourceBrowsersList {
  return sourceBrowsersList_.get();
}

// Accessor which cascades selected-browser changes into a re-evaluation of the
// available services and the associated checkbox enable and checked states.
- (void)setSourceBrowserIndex:(NSUInteger)browserIndex {
  sourceBrowserIndex_ = browserIndex;
  ImportSettingsProfile* profile =
      [sourceBrowsersList_.get() objectAtIndex:browserIndex];
  uint16 items = [profile services];
  [self setHistoryAvailable:(items & HISTORY) ? YES : NO];
  [self setImportHistory:[self historyAvailable]];
  [self setFavoritesAvailable:(items & FAVORITES) ? YES : NO];
  [self setImportFavorites:[self favoritesAvailable]];
  [self setCookiesAvailable:(items & COOKIES) ? YES : NO];
  [self setImportCookies:[self cookiesAvailable]];
  [self setPasswordsAvailable:(items & PASSWORDS) ? YES : NO];
  [self setImportPasswords:[self passwordsAvailable]];
  [self setSearchEnginesAvailable:(items & SEARCH_ENGINES) ? YES : NO];
  [self setImportSearchEngines:[self searchEnginesAvailable]];
}

- (uint16)servicesToImport {
  uint16 servicesToImport = 0;
  if ([self importHistory]) servicesToImport |= HISTORY;
  if ([self importFavorites]) servicesToImport |= FAVORITES;
  if ([self importCookies]) servicesToImport |= COOKIES;
  if ([self importPasswords]) servicesToImport |= PASSWORDS;
  if ([self importSearchEngines]) servicesToImport |= SEARCH_ENGINES;
  return servicesToImport;
}

// KVO accessor which returns YES if at least one of the services
// provided by the selected profile has been marked for importing
// and bound to the OK button's enable property.
- (BOOL)importSomething {
  return [self importHistory] || [self importFavorites] ||
      [self importCookies] || [self importPasswords] ||
      [self importSearchEngines];
}

@end