| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 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/importer/import_progress_dialog_cocoa.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 #include "base/mac/bundle_locations.h" | |
| 9 #include "base/mac/mac_util.h" | |
| 10 #include "base/message_loop.h" | |
| 11 #import "base/memory/scoped_nsobject.h" | |
| 12 #import "base/strings/sys_string_conversions.h" | |
| 13 #include "base/utf_string_conversions.h" | |
| 14 #include "chrome/browser/importer/importer_host.h" | |
| 15 #include "chrome/browser/importer/importer_observer.h" | |
| 16 #include "grit/chromium_strings.h" | |
| 17 #include "grit/generated_resources.h" | |
| 18 #include "ui/base/l10n/l10n_util.h" | |
| 19 #include "ui/base/l10n/l10n_util_mac.h" | |
| 20 | |
| 21 namespace { | |
| 22 | |
| 23 // Convert ImportItem enum into the name of the ImportProgressDialogController | |
| 24 // property corresponding to the text for that item, this makes the code to | |
| 25 // change the values for said properties much more readable. | |
| 26 NSString* keyForImportItem(importer::ImportItem item) { | |
| 27 switch(item) { | |
| 28 case importer::HISTORY: | |
| 29 return @"historyStatusText"; | |
| 30 case importer::FAVORITES: | |
| 31 return @"favoritesStatusText"; | |
| 32 case importer::PASSWORDS: | |
| 33 return @"savedPasswordStatusText"; | |
| 34 case importer::SEARCH_ENGINES: | |
| 35 return @"searchStatusText"; | |
| 36 default: | |
| 37 DCHECK(false); | |
| 38 break; | |
| 39 } | |
| 40 return nil; | |
| 41 } | |
| 42 | |
| 43 } // namespace | |
| 44 | |
| 45 @implementation ImportProgressDialogController | |
| 46 | |
| 47 @synthesize explanatoryText = explanatory_text_; | |
| 48 @synthesize favoritesStatusText = favorites_status_text_; | |
| 49 @synthesize searchStatusText = search_status_text_; | |
| 50 @synthesize savedPasswordStatusText = saved_password_status_text_; | |
| 51 @synthesize historyStatusText = history_status_text_; | |
| 52 | |
| 53 @synthesize favoritesImportEnabled = favorites_import_enabled_; | |
| 54 @synthesize searchImportEnabled = search_import_enabled_; | |
| 55 @synthesize passwordImportEnabled = password_import_enabled_; | |
| 56 @synthesize historyImportEnabled = history_import_enabled_; | |
| 57 | |
| 58 - (id)initWithImporterHost:(ImporterHost*)host | |
| 59 importerName:(string16)importerName | |
| 60 observer:(ImporterObserver*)observer | |
| 61 itemsEnabled:(int16)items { | |
| 62 NSString* nib_path = | |
| 63 [base::mac::FrameworkBundle() pathForResource:@"ImportProgressDialog" | |
| 64 ofType:@"nib"]; | |
| 65 self = [super initWithWindowNibPath:nib_path owner:self]; | |
| 66 if (self != nil) { | |
| 67 importer_host_ = host; | |
| 68 observer_ = observer; | |
| 69 import_host_observer_bridge_.reset(new ImporterObserverBridge(self)); | |
| 70 importer_host_->SetObserver(import_host_observer_bridge_.get()); | |
| 71 | |
| 72 string16 productName = l10n_util::GetStringUTF16(IDS_PRODUCT_NAME); | |
| 73 NSString* explanatory_text = l10n_util::GetNSStringF( | |
| 74 IDS_IMPORT_PROGRESS_EXPLANATORY_TEXT_MAC, | |
| 75 productName, | |
| 76 importerName); | |
| 77 [self setExplanatoryText:explanatory_text]; | |
| 78 | |
| 79 progress_text_ = | |
| 80 [l10n_util::GetNSStringWithFixup(IDS_IMPORT_IMPORTING_PROGRESS_TEXT_MAC) | |
| 81 retain]; | |
| 82 done_text_ = | |
| 83 [l10n_util::GetNSStringWithFixup(IDS_IMPORT_IMPORTING_DONE_TEXT_MAC) | |
| 84 retain]; | |
| 85 | |
| 86 // Enable/disable item titles. | |
| 87 NSColor* disabled = [NSColor disabledControlTextColor]; | |
| 88 NSColor* active = [NSColor textColor]; | |
| 89 [self setFavoritesImportEnabled:items & importer::FAVORITES ? active : | |
| 90 disabled]; | |
| 91 [self setSearchImportEnabled:items & importer::SEARCH_ENGINES ? active : | |
| 92 disabled]; | |
| 93 [self setPasswordImportEnabled:items & importer::PASSWORDS ? active : | |
| 94 disabled]; | |
| 95 [self setHistoryImportEnabled:items & importer::HISTORY ? active : | |
| 96 disabled]; | |
| 97 } | |
| 98 return self; | |
| 99 } | |
| 100 | |
| 101 - (void)dealloc { | |
| 102 [explanatory_text_ release]; | |
| 103 [favorites_status_text_ release]; | |
| 104 [search_status_text_ release]; | |
| 105 [saved_password_status_text_ release]; | |
| 106 [history_status_text_ release]; | |
| 107 | |
| 108 [favorites_import_enabled_ release]; | |
| 109 [search_import_enabled_ release]; | |
| 110 [password_import_enabled_ release]; | |
| 111 [history_import_enabled_ release]; | |
| 112 | |
| 113 [progress_text_ release]; | |
| 114 [done_text_ release]; | |
| 115 | |
| 116 [super dealloc]; | |
| 117 } | |
| 118 | |
| 119 - (IBAction)showWindow:(id)sender { | |
| 120 NSWindow* win = [self window]; | |
| 121 [win center]; | |
| 122 [super showWindow:nil]; | |
| 123 } | |
| 124 | |
| 125 - (void)closeDialog { | |
| 126 if ([[self window] isVisible]) { | |
| 127 [[self window] close]; | |
| 128 } | |
| 129 } | |
| 130 | |
| 131 - (IBAction)cancel:(id)sender { | |
| 132 // The ImporterHost will notify import_host_observer_bridge_ that import has | |
| 133 // ended, which will trigger the ImportEnded method, in which this object is | |
| 134 // released. | |
| 135 importer_host_->Cancel(); | |
| 136 } | |
| 137 | |
| 138 - (void)ImportItemStarted:(importer::ImportItem)item { | |
| 139 [self setValue:progress_text_ forKey:keyForImportItem(item)]; | |
| 140 } | |
| 141 | |
| 142 - (void)ImportItemEnded:(importer::ImportItem)item { | |
| 143 [self setValue:done_text_ forKey:keyForImportItem(item)]; | |
| 144 } | |
| 145 | |
| 146 - (void)ImportEnded { | |
| 147 importer_host_->SetObserver(NULL); | |
| 148 if (observer_) | |
| 149 observer_->ImportCompleted(); | |
| 150 [self closeDialog]; | |
| 151 [self release]; | |
| 152 | |
| 153 // Break out of modal event loop. | |
| 154 [NSApp stopModal]; | |
| 155 } | |
| 156 | |
| 157 @end | |
| 158 | |
| 159 ImporterObserverBridge::ImporterObserverBridge( | |
| 160 ImportProgressDialogController* owner) | |
| 161 : owner_(owner) {} | |
| 162 | |
| 163 ImporterObserverBridge::~ImporterObserverBridge() {} | |
| 164 | |
| 165 void ImporterObserverBridge::ImportStarted() { | |
| 166 // Not needed for out of process import. | |
| 167 } | |
| 168 | |
| 169 void ImporterObserverBridge::ImportItemStarted(importer::ImportItem item) { | |
| 170 [owner_ ImportItemStarted:item]; | |
| 171 } | |
| 172 | |
| 173 void ImporterObserverBridge::ImportItemEnded(importer::ImportItem item) { | |
| 174 [owner_ ImportItemEnded:item]; | |
| 175 } | |
| 176 | |
| 177 void ImporterObserverBridge::ImportEnded() { | |
| 178 [owner_ ImportEnded]; | |
| 179 } | |
| 180 | |
| 181 namespace importer { | |
| 182 | |
| 183 void ShowImportProgressDialog(uint16 items, | |
| 184 ImporterHost* importer_host, | |
| 185 ImporterObserver* importer_observer, | |
| 186 const SourceProfile& source_profile, | |
| 187 Profile* target_profile, | |
| 188 bool first_run) { | |
| 189 DCHECK(items != 0); | |
| 190 | |
| 191 // |progress_dialog| is responsible for deleting itself. | |
| 192 ImportProgressDialogController* progress_dialog = | |
| 193 [[ImportProgressDialogController alloc] | |
| 194 initWithImporterHost:importer_host | |
| 195 importerName:source_profile.importer_name | |
| 196 observer:importer_observer | |
| 197 itemsEnabled:items]; | |
| 198 // Call is async. | |
| 199 importer_host->StartImportSettings( | |
| 200 source_profile, target_profile, items, new ProfileWriter(target_profile), | |
| 201 first_run); | |
| 202 | |
| 203 // Display the window while spinning a message loop. | |
| 204 // For details on why we need a modal message loop see http://crbug.com/19169 | |
| 205 NSWindow* progress_window = [progress_dialog window]; | |
| 206 NSModalSession session = [NSApp beginModalSessionForWindow:progress_window]; | |
| 207 [progress_dialog showWindow:nil]; | |
| 208 while (true) { | |
| 209 if ([NSApp runModalSession:session] != NSRunContinuesResponse) | |
| 210 break; | |
| 211 MessageLoop::current()->RunUntilIdle(); | |
| 212 } | |
| 213 [NSApp endModalSession:session]; | |
| 214 } | |
| 215 | |
| 216 } // namespace importer | |
| OLD | NEW |