Home | History | Annotate | Download | only in user
      1 // Copyright 2014 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 #include "ash/system/user/accounts_detailed_view.h"
      6 
      7 #include <vector>
      8 
      9 #include "ash/multi_profile_uma.h"
     10 #include "ash/session/user_info.h"
     11 #include "ash/shell.h"
     12 #include "ash/system/tray/fixed_sized_scroll_view.h"
     13 #include "ash/system/tray/hover_highlight_view.h"
     14 #include "ash/system/tray/system_tray.h"
     15 #include "ash/system/tray/system_tray_delegate.h"
     16 #include "ash/system/tray/tray_constants.h"
     17 #include "ash/system/tray/tray_popup_header_button.h"
     18 #include "ash/system/user/config.h"
     19 #include "ash/system/user/tray_user.h"
     20 #include "base/strings/utf_string_conversions.h"
     21 #include "grit/ash_resources.h"
     22 #include "grit/ui_resources.h"
     23 #include "ui/base/resource/resource_bundle.h"
     24 #include "ui/views/border.h"
     25 #include "ui/views/layout/box_layout.h"
     26 #include "ui/views/layout/grid_layout.h"
     27 
     28 namespace ash {
     29 namespace tray {
     30 
     31 namespace {
     32 
     33 const int kAccountsViewVerticalPadding = 12;
     34 const int kPrimaryAccountColumnSetID = 0;
     35 const int kSecondaryAccountColumnSetID = 1;
     36 const int kPaddingBetweenAccounts = 20;
     37 
     38 }  // namespace
     39 
     40 AccountsDetailedView::AccountsDetailedView(TrayUser* owner,
     41                                            user::LoginStatus login_status)
     42     : TrayDetailsView(owner),
     43       delegate_(NULL),
     44       account_list_(NULL),
     45       add_account_button_(NULL),
     46       add_user_button_(NULL) {
     47   std::string user_id = Shell::GetInstance()
     48                             ->session_state_delegate()
     49                             ->GetUserInfo(0)
     50                             ->GetUserID();
     51   delegate_ =
     52       Shell::GetInstance()->system_tray_delegate()->GetUserAccountsDelegate(
     53           user_id);
     54   delegate_->AddObserver(this);
     55   AddHeader(login_status);
     56   CreateScrollableList();
     57   AddAccountList();
     58   AddAddAccountButton();
     59   AddFooter();
     60 }
     61 
     62 AccountsDetailedView::~AccountsDetailedView() {
     63   delegate_->RemoveObserver(this);
     64 }
     65 
     66 void AccountsDetailedView::OnViewClicked(views::View* sender) {
     67   if (sender == footer()->content())
     68     TransitionToDefaultView();
     69   else if (sender == add_account_button_)
     70     delegate_->LaunchAddAccountDialog();
     71   else
     72     NOTREACHED();
     73 }
     74 
     75 void AccountsDetailedView::ButtonPressed(views::Button* sender,
     76                                          const ui::Event& event) {
     77   std::map<views::View*, std::string>::iterator it =
     78       delete_button_to_account_id_.find(sender);
     79   if (it != delete_button_to_account_id_.end()) {
     80     delegate_->DeleteAccount(it->second);
     81   } else if (add_user_button_ && add_user_button_ == sender) {
     82     MultiProfileUMA::RecordSigninUser(MultiProfileUMA::SIGNIN_USER_BY_TRAY);
     83     Shell::GetInstance()->system_tray_delegate()->ShowUserLogin();
     84     owner()->system_tray()->CloseSystemBubble();
     85   } else {
     86     NOTREACHED();
     87   }
     88 }
     89 
     90 void AccountsDetailedView::AccountListChanged() { UpdateAccountList(); }
     91 
     92 void AccountsDetailedView::AddHeader(user::LoginStatus login_status) {
     93   views::View* user_view_container = new views::View;
     94   user_view_container->SetLayoutManager(
     95       new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 0));
     96   user_view_container->SetBorder(
     97       views::Border::CreateSolidSidedBorder(0, 0, 1, 0, kBorderLightColor));
     98   user_view_container->AddChildView(
     99       new tray::UserView(owner(), login_status, 0, true));
    100   AddChildView(user_view_container);
    101 }
    102 
    103 void AccountsDetailedView::AddAccountList() {
    104   scroll_content()->SetBorder(
    105       views::Border::CreateEmptyBorder(kAccountsViewVerticalPadding,
    106                                        kTrayPopupPaddingHorizontal,
    107                                        kAccountsViewVerticalPadding,
    108                                        kTrayPopupPaddingHorizontal));
    109   views::Label* account_list_title = new views::Label(
    110       l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_ACCOUNT_LIST_TITLE));
    111   account_list_title->SetEnabledColor(SkColorSetARGB(0x7f, 0, 0, 0));
    112   account_list_title->SetHorizontalAlignment(gfx::ALIGN_LEFT);
    113   scroll_content()->AddChildView(account_list_title);
    114   account_list_ = new views::View();
    115   UpdateAccountList();
    116   scroll_content()->AddChildView(account_list_);
    117 }
    118 
    119 void AccountsDetailedView::AddAddAccountButton() {
    120   SessionStateDelegate* session_state_delegate =
    121       Shell::GetInstance()->session_state_delegate();
    122   HoverHighlightView* add_account_button = new HoverHighlightView(this);
    123   const UserInfo* user_info = session_state_delegate->GetUserInfo(0);
    124   base::string16 user_name = user_info->GetGivenName();
    125   if (user_name.empty())
    126     user_name = user_info->GetDisplayName();
    127   if (user_name.empty())
    128     user_name = base::ASCIIToUTF16(user_info->GetEmail());
    129   add_account_button->AddLabel(
    130       l10n_util::GetStringFUTF16(IDS_ASH_STATUS_TRAY_ADD_ACCOUNT_LABEL,
    131                                  user_name),
    132       gfx::ALIGN_CENTER,
    133       gfx::Font::NORMAL);
    134   AddChildView(add_account_button);
    135   add_account_button_ = add_account_button;
    136 }
    137 
    138 void AccountsDetailedView::AddFooter() {
    139   CreateSpecialRow(IDS_ASH_STATUS_TRAY_ACCOUNTS_TITLE, this);
    140   if (!IsMultiProfileSupportedAndUserActive())
    141     return;
    142   TrayPopupHeaderButton* add_user_button =
    143       new TrayPopupHeaderButton(this,
    144                                 IDR_AURA_UBER_TRAY_ADD_PROFILE,
    145                                 IDR_AURA_UBER_TRAY_ADD_PROFILE,
    146                                 IDR_AURA_UBER_TRAY_ADD_PROFILE_HOVER,
    147                                 IDR_AURA_UBER_TRAY_ADD_PROFILE_HOVER,
    148                                 IDS_ASH_STATUS_TRAY_SIGN_IN_ANOTHER_ACCOUNT);
    149   add_user_button->SetTooltipText(
    150       l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_SIGN_IN_ANOTHER_ACCOUNT));
    151   footer()->AddButton(add_user_button);
    152   add_user_button_ = add_user_button;
    153 }
    154 
    155 void AccountsDetailedView::UpdateAccountList() {
    156   // Clear existing view.
    157   delete_button_to_account_id_.clear();
    158   account_list_->RemoveAllChildViews(true);
    159 
    160   // Configuring layout manager.
    161   views::GridLayout* layout = new views::GridLayout(account_list_);
    162   account_list_->SetLayoutManager(layout);
    163   views::ColumnSet* primary_account_row =
    164       layout->AddColumnSet(kPrimaryAccountColumnSetID);
    165   primary_account_row->AddColumn(views::GridLayout::LEADING,
    166                                  views::GridLayout::CENTER,
    167                                  1.0,
    168                                  views::GridLayout::USE_PREF,
    169                                  0,
    170                                  0);
    171   views::ColumnSet* secondary_account_row =
    172       layout->AddColumnSet(kSecondaryAccountColumnSetID);
    173   secondary_account_row->AddColumn(views::GridLayout::FILL,
    174                                    views::GridLayout::CENTER,
    175                                    1.0,
    176                                    views::GridLayout::USE_PREF,
    177                                    0,
    178                                    0);
    179   secondary_account_row->AddPaddingColumn(0.0, kTrayPopupPaddingBetweenItems);
    180   secondary_account_row->AddColumn(views::GridLayout::FILL,
    181                                    views::GridLayout::CENTER,
    182                                    0.0,
    183                                    views::GridLayout::USE_PREF,
    184                                    0,
    185                                    0);
    186 
    187   // Adding primary account.
    188   layout->AddPaddingRow(0.0, kPaddingBetweenAccounts);
    189   layout->StartRow(0.0, kPrimaryAccountColumnSetID);
    190   const std::string& primary_account = delegate_->GetPrimaryAccountId();
    191   views::Label* primary_account_label =
    192       new views::Label(l10n_util::GetStringFUTF16(
    193           IDS_ASH_STATUS_TRAY_PRIMARY_ACCOUNT_LABEL,
    194           base::ASCIIToUTF16(
    195               delegate_->GetAccountDisplayName(primary_account))));
    196   layout->AddView(primary_account_label);
    197 
    198   // Adding secondary accounts.
    199   const std::vector<std::string>& secondary_accounts =
    200       delegate_->GetSecondaryAccountIds();
    201   for (size_t i = 0; i < secondary_accounts.size(); ++i) {
    202     layout->AddPaddingRow(0.0, kPaddingBetweenAccounts);
    203     layout->StartRow(0.0, kSecondaryAccountColumnSetID);
    204     const std::string& account_id = secondary_accounts[i];
    205     views::Label* account_label = new views::Label(
    206         base::ASCIIToUTF16(delegate_->GetAccountDisplayName(account_id)));
    207     account_label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
    208     layout->AddView(account_label);
    209     views::View* delete_button = CreateDeleteButton();
    210     delete_button_to_account_id_[delete_button] = account_id;
    211     layout->AddView(delete_button);
    212   }
    213 
    214   scroll_content()->SizeToPreferredSize();
    215   scroller()->Layout();
    216 }
    217 
    218 views::View* AccountsDetailedView::CreateDeleteButton() {
    219   ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
    220   views::ImageButton* delete_button = new views::ImageButton(this);
    221   delete_button->SetImage(
    222       views::Button::STATE_NORMAL,
    223       rb.GetImageNamed(IDR_AURA_UBER_TRAY_REMOVE_ACCOUNT).ToImageSkia());
    224   delete_button->SetImage(
    225       views::Button::STATE_HOVERED,
    226       rb.GetImageNamed(IDR_AURA_UBER_TRAY_REMOVE_ACCOUNT_HOVER).ToImageSkia());
    227   delete_button->SetImage(
    228       views::Button::STATE_PRESSED,
    229       rb.GetImageNamed(IDR_AURA_UBER_TRAY_REMOVE_ACCOUNT_HOVER).ToImageSkia());
    230   return delete_button;
    231 }
    232 
    233 }  // namespace tray
    234 }  // namespace ash
    235