| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/profile_menu_model.h" | 5 #include "chrome/browser/ui/profile_menu_model.h" |
| 6 | 6 |
| 7 #include "base/path_service.h" | 7 #include "base/path_service.h" |
| 8 #include "chrome/browser/browser_process.h" | 8 #include "chrome/browser/browser_process.h" |
| 9 #include "chrome/browser/profiles/profile_info_cache.h" | 9 #include "chrome/browser/profiles/profile_info_cache.h" |
| 10 #include "chrome/browser/profiles/profile_manager.h" | 10 #include "chrome/browser/profiles/profile_manager.h" |
| 11 #include "chrome/browser/ui/browser_window.h" | 11 #include "chrome/browser/ui/browser_window.h" |
| 12 #include "chrome/common/chrome_paths.h" | 12 #include "chrome/common/chrome_paths.h" |
| 13 #include "chrome/common/url_constants.h" | 13 #include "chrome/common/url_constants.h" |
| 14 #include "grit/chromium_strings.h" | 14 #include "grit/chromium_strings.h" |
| 15 #include "grit/generated_resources.h" | 15 #include "grit/generated_resources.h" |
| 16 #include "ui/base/l10n/l10n_util.h" | 16 #include "ui/base/l10n/l10n_util.h" |
| 17 | 17 |
| 18 namespace { | 18 namespace { |
| 19 | 19 |
| 20 enum { | |
| 21 COMMAND_CUSTOMIZE_PROFILE, | |
| 22 COMMAND_DELETE_PROFILE, | |
| 23 COMMAND_CREATE_NEW_PROFILE, | |
| 24 COMMAND_SWITCH_PROFILE_MENU, | |
| 25 // The profiles submenu contains a menu item for each profile. For | |
| 26 // the i'th profile the command ID is COMMAND_SWITCH_TO_PROFILE + i. | |
| 27 // Since there can be any number of profiles this must be the last command id. | |
| 28 COMMAND_SWITCH_TO_PROFILE, | |
| 29 }; | |
| 30 | |
| 31 class SwitchProfileMenuModel : public ui::SimpleMenuModel, | 20 class SwitchProfileMenuModel : public ui::SimpleMenuModel, |
| 32 public ui::SimpleMenuModel::Delegate { | 21 public ui::SimpleMenuModel::Delegate { |
| 33 public: | 22 public: |
| 34 SwitchProfileMenuModel(ui::SimpleMenuModel::Delegate* delegate, | 23 SwitchProfileMenuModel(ui::SimpleMenuModel::Delegate* delegate, |
| 35 Browser* browser); | 24 Browser* browser); |
| 36 | 25 |
| 37 // Overridden from ui::SimpleMenuModel::Delegate. | 26 // Overridden from ui::SimpleMenuModel::Delegate. |
| 38 virtual void ExecuteCommand(int command_id) OVERRIDE; | 27 virtual void ExecuteCommand(int command_id) OVERRIDE; |
| 39 virtual bool IsCommandIdChecked(int command_id) const OVERRIDE; | 28 virtual bool IsCommandIdChecked(int command_id) const OVERRIDE; |
| 40 virtual bool IsCommandIdEnabled(int command_id) const OVERRIDE; | 29 virtual bool IsCommandIdEnabled(int command_id) const OVERRIDE; |
| (...skipping 28 matching lines...) Expand all Loading... |
| 69 SwitchProfileMenuModel::SwitchProfileMenuModel( | 58 SwitchProfileMenuModel::SwitchProfileMenuModel( |
| 70 ui::SimpleMenuModel::Delegate* delegate, | 59 ui::SimpleMenuModel::Delegate* delegate, |
| 71 Browser* browser) | 60 Browser* browser) |
| 72 : SimpleMenuModel(this), | 61 : SimpleMenuModel(this), |
| 73 browser_(browser), | 62 browser_(browser), |
| 74 delegate_(delegate) { | 63 delegate_(delegate) { |
| 75 ProfileInfoCache& cache = | 64 ProfileInfoCache& cache = |
| 76 g_browser_process->profile_manager()->GetProfileInfoCache(); | 65 g_browser_process->profile_manager()->GetProfileInfoCache(); |
| 77 size_t count = cache.GetNumberOfProfiles(); | 66 size_t count = cache.GetNumberOfProfiles(); |
| 78 for (size_t i = 0; i < count; ++i) { | 67 for (size_t i = 0; i < count; ++i) { |
| 79 AddCheckItem(COMMAND_SWITCH_TO_PROFILE + i, | 68 AddCheckItem(ProfileMenuModel::COMMAND_SWITCH_TO_PROFILE + i, |
| 80 cache.GetNameOfProfileAtIndex(i)); | 69 cache.GetNameOfProfileAtIndex(i)); |
| 81 } | 70 } |
| 82 | 71 |
| 83 AddSeparator(); | 72 AddSeparator(); |
| 84 | 73 |
| 85 const string16 short_product_name = | 74 const string16 short_product_name = |
| 86 l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_NAME); | 75 l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_NAME); |
| 87 AddItem(COMMAND_CREATE_NEW_PROFILE, l10n_util::GetStringFUTF16( | 76 AddItem(ProfileMenuModel::COMMAND_CREATE_NEW_PROFILE, |
| 88 IDS_PROFILES_CREATE_NEW_PROFILE_OPTION, short_product_name)); | 77 l10n_util::GetStringFUTF16(IDS_PROFILES_CREATE_NEW_PROFILE_OPTION, |
| 78 short_product_name)); |
| 89 } | 79 } |
| 90 | 80 |
| 91 void SwitchProfileMenuModel::ExecuteCommand(int command_id) { | 81 void SwitchProfileMenuModel::ExecuteCommand(int command_id) { |
| 92 ProfileInfoCache& cache = | 82 ProfileInfoCache& cache = |
| 93 g_browser_process->profile_manager()->GetProfileInfoCache(); | 83 g_browser_process->profile_manager()->GetProfileInfoCache(); |
| 94 if (IsSwitchProfileCommand(command_id)) { | 84 if (IsSwitchProfileCommand(command_id)) { |
| 95 size_t index = GetProfileIndexFromSwitchProfileCommand(command_id); | 85 size_t index = GetProfileIndexFromSwitchProfileCommand(command_id); |
| 96 FilePath profile_path = cache.GetPathOfProfileAtIndex(index); | 86 FilePath profile_path = cache.GetPathOfProfileAtIndex(index); |
| 97 ProfileSwitchObserver* observer = new ProfileSwitchObserver; | 87 ProfileSwitchObserver* observer = new ProfileSwitchObserver; |
| 98 // The observer is deleted by the manager when profile creation is finished. | 88 // The observer is deleted by the manager when profile creation is finished. |
| (...skipping 25 matching lines...) Expand all Loading... |
| 124 | 114 |
| 125 bool SwitchProfileMenuModel::GetAcceleratorForCommandId( | 115 bool SwitchProfileMenuModel::GetAcceleratorForCommandId( |
| 126 int command_id, | 116 int command_id, |
| 127 ui::Accelerator* accelerator) { | 117 ui::Accelerator* accelerator) { |
| 128 if (IsSwitchProfileCommand(command_id)) | 118 if (IsSwitchProfileCommand(command_id)) |
| 129 return false; | 119 return false; |
| 130 return delegate_->GetAcceleratorForCommandId(command_id, accelerator); | 120 return delegate_->GetAcceleratorForCommandId(command_id, accelerator); |
| 131 } | 121 } |
| 132 | 122 |
| 133 bool SwitchProfileMenuModel::IsSwitchProfileCommand(int command_id) const { | 123 bool SwitchProfileMenuModel::IsSwitchProfileCommand(int command_id) const { |
| 134 return command_id >= COMMAND_SWITCH_TO_PROFILE; | 124 return command_id >= ProfileMenuModel::COMMAND_SWITCH_TO_PROFILE; |
| 135 } | 125 } |
| 136 | 126 |
| 137 size_t SwitchProfileMenuModel::GetProfileIndexFromSwitchProfileCommand( | 127 size_t SwitchProfileMenuModel::GetProfileIndexFromSwitchProfileCommand( |
| 138 int command_id) const { | 128 int command_id) const { |
| 139 DCHECK(IsSwitchProfileCommand(command_id)); | 129 DCHECK(IsSwitchProfileCommand(command_id)); |
| 140 return command_id - COMMAND_SWITCH_TO_PROFILE; | 130 return command_id - ProfileMenuModel::COMMAND_SWITCH_TO_PROFILE; |
| 141 } | 131 } |
| 142 | 132 |
| 143 } // namespace | 133 } // namespace |
| 144 | 134 |
| 145 ProfileMenuModel::ProfileMenuModel(Browser* browser) | 135 ProfileMenuModel::ProfileMenuModel(Browser* browser) |
| 146 : ALLOW_THIS_IN_INITIALIZER_LIST(ui::SimpleMenuModel(this)), | 136 : ALLOW_THIS_IN_INITIALIZER_LIST(ui::SimpleMenuModel(this)), |
| 147 browser_(browser) { | 137 browser_(browser) { |
| 138 ProfileInfoCache& cache = |
| 139 g_browser_process->profile_manager()->GetProfileInfoCache(); |
| 140 size_t profile_index = cache.GetIndexOfProfileWithPath( |
| 141 browser_->profile()->GetPath()); |
| 142 AddItem(COMMAND_PROFILE_NAME, cache.GetNameOfProfileAtIndex(profile_index)); |
| 143 |
| 144 // TODO(sail): Need to implement an icon chooser on other platforms too. |
| 145 #if defined(TOOLKIT_VIEWS) |
| 146 AddItem(COMMAND_CHOOSE_AVATAR_ICON, string16()); |
| 147 #endif |
| 148 |
| 148 AddItemWithStringId(COMMAND_CUSTOMIZE_PROFILE, | 149 AddItemWithStringId(COMMAND_CUSTOMIZE_PROFILE, |
| 149 IDS_PROFILES_CUSTOMIZE_PROFILE); | 150 IDS_PROFILES_CUSTOMIZE_PROFILE); |
| 150 AddSeparator(); | 151 AddSeparator(); |
| 151 | 152 |
| 152 const string16 short_product_name = | 153 const string16 short_product_name = |
| 153 l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_NAME); | 154 l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_NAME); |
| 154 ProfileInfoCache& cache = | |
| 155 g_browser_process->profile_manager()->GetProfileInfoCache(); | |
| 156 if (cache.GetNumberOfProfiles() > 1) { | 155 if (cache.GetNumberOfProfiles() > 1) { |
| 157 switch_profiles_sub_menu_model_.reset( | 156 switch_profiles_sub_menu_model_.reset( |
| 158 new SwitchProfileMenuModel(this, browser_)); | 157 new SwitchProfileMenuModel(this, browser_)); |
| 159 AddSubMenu(COMMAND_SWITCH_PROFILE_MENU, l10n_util::GetStringFUTF16( | 158 AddSubMenu(COMMAND_SWITCH_PROFILE_MENU, l10n_util::GetStringFUTF16( |
| 160 IDS_PROFILES_MENU, short_product_name), | 159 IDS_PROFILES_MENU, short_product_name), |
| 161 switch_profiles_sub_menu_model_.get()); | 160 switch_profiles_sub_menu_model_.get()); |
| 162 } else { | 161 } else { |
| 163 switch_profiles_sub_menu_model_.reset(); | 162 switch_profiles_sub_menu_model_.reset(); |
| 164 AddItem(COMMAND_CREATE_NEW_PROFILE, l10n_util::GetStringFUTF16( | 163 AddItem(COMMAND_CREATE_NEW_PROFILE, l10n_util::GetStringFUTF16( |
| 165 IDS_PROFILES_CREATE_NEW_PROFILE_OPTION, short_product_name)); | 164 IDS_PROFILES_CREATE_NEW_PROFILE_OPTION, short_product_name)); |
| 166 } | 165 } |
| 167 | 166 |
| 168 AddItemWithStringId(COMMAND_DELETE_PROFILE, | 167 AddItemWithStringId(COMMAND_DELETE_PROFILE, |
| 169 IDS_PROFILES_DELETE_PROFILE); | 168 IDS_PROFILES_DELETE_PROFILE); |
| 170 } | 169 } |
| 171 | 170 |
| 172 ProfileMenuModel::~ProfileMenuModel() { | 171 ProfileMenuModel::~ProfileMenuModel() { |
| 173 } | 172 } |
| 174 | 173 |
| 175 // ui::SimpleMenuModel::Delegate implementation | 174 // ui::SimpleMenuModel::Delegate implementation |
| 176 bool ProfileMenuModel::IsCommandIdChecked(int command_id) const { | 175 bool ProfileMenuModel::IsCommandIdChecked(int command_id) const { |
| 177 return false; | 176 return false; |
| 178 } | 177 } |
| 179 | 178 |
| 180 bool ProfileMenuModel::IsCommandIdEnabled(int command_id) const { | 179 bool ProfileMenuModel::IsCommandIdEnabled(int command_id) const { |
| 181 return true; | 180 switch (command_id) { |
| 181 case COMMAND_PROFILE_NAME: |
| 182 return false; |
| 183 default: |
| 184 return true; |
| 185 } |
| 182 } | 186 } |
| 183 | 187 |
| 184 bool ProfileMenuModel::GetAcceleratorForCommandId(int command_id, | 188 bool ProfileMenuModel::GetAcceleratorForCommandId(int command_id, |
| 185 ui::Accelerator* accelerator) { | 189 ui::Accelerator* accelerator) { |
| 186 return false; | 190 return false; |
| 187 } | 191 } |
| 188 | 192 |
| 189 void ProfileMenuModel::ExecuteCommand(int command_id) { | 193 void ProfileMenuModel::ExecuteCommand(int command_id) { |
| 190 switch (command_id) { | 194 switch (command_id) { |
| 191 case COMMAND_CUSTOMIZE_PROFILE: | 195 case COMMAND_CUSTOMIZE_PROFILE: |
| 192 browser_->ShowOptionsTab(chrome::kPersonalOptionsSubPage); | 196 browser_->ShowOptionsTab(chrome::kPersonalOptionsSubPage); |
| 193 break; | 197 break; |
| 198 case COMMAND_CHOOSE_AVATAR_ICON: |
| 199 break; |
| 194 case COMMAND_CREATE_NEW_PROFILE: | 200 case COMMAND_CREATE_NEW_PROFILE: |
| 195 ProfileManager::CreateMultiProfileAsync(); | 201 ProfileManager::CreateMultiProfileAsync(); |
| 196 break; | 202 break; |
| 197 case COMMAND_DELETE_PROFILE: | 203 case COMMAND_DELETE_PROFILE: |
| 198 g_browser_process->profile_manager()->ScheduleProfileForDeletion( | 204 g_browser_process->profile_manager()->ScheduleProfileForDeletion( |
| 199 browser_->profile()->GetPath()); | 205 browser_->profile()->GetPath()); |
| 200 break; | 206 break; |
| 201 default: | 207 default: |
| 202 NOTREACHED(); | 208 NOTREACHED(); |
| 203 break; | 209 break; |
| 204 } | 210 } |
| 205 } | 211 } |
| OLD | NEW |