1 // Copyright (c) 2013 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 "ui/message_center/message_center_tray.h" 6 7 #include "base/observer_list.h" 8 #include "base/strings/utf_string_conversions.h" 9 #include "grit/ui_strings.h" 10 #include "ui/base/l10n/l10n_util.h" 11 #include "ui/message_center/message_center.h" 12 #include "ui/message_center/message_center_tray_delegate.h" 13 14 namespace message_center { 15 namespace { 16 17 // Menu commands 18 const int kToggleQuietMode = 0; 19 const int kEnableQuietModeHour = 1; 20 const int kEnableQuietModeDay = 2; 21 22 } 23 24 MessageCenterTray::MessageCenterTray( 25 MessageCenterTrayDelegate* delegate, 26 message_center::MessageCenter* message_center) 27 : message_center_(message_center), 28 message_center_visible_(false), 29 popups_visible_(false), 30 delegate_(delegate) { 31 message_center_->AddObserver(this); 32 } 33 34 MessageCenterTray::~MessageCenterTray() { 35 message_center_->RemoveObserver(this); 36 } 37 38 bool MessageCenterTray::ShowMessageCenterBubble() { 39 if (message_center_visible_) 40 return true; 41 42 HidePopupBubble(); 43 44 message_center_visible_ = delegate_->ShowMessageCenter(); 45 message_center_->SetMessageCenterVisible(message_center_visible_); 46 return message_center_visible_; 47 } 48 49 bool MessageCenterTray::HideMessageCenterBubble() { 50 if (!message_center_visible_) 51 return false; 52 delegate_->HideMessageCenter(); 53 MarkMessageCenterHidden(); 54 return true; 55 } 56 57 void MessageCenterTray::MarkMessageCenterHidden() { 58 if (!message_center_visible_) 59 return; 60 message_center_visible_ = false; 61 message_center_->SetMessageCenterVisible(false); 62 // Some notifications (like system ones) should appear as popups again 63 // after the message center is closed. 64 if (message_center_->HasPopupNotifications()) 65 ShowPopupBubble(); 66 NotifyMessageCenterTrayChanged(); 67 } 68 69 void MessageCenterTray::ToggleMessageCenterBubble() { 70 if (message_center_visible_) 71 HideMessageCenterBubble(); 72 else 73 ShowMessageCenterBubble(); 74 } 75 76 void MessageCenterTray::ShowPopupBubble() { 77 if (message_center_visible_) 78 return; 79 80 if (popups_visible_) { 81 NotifyMessageCenterTrayChanged(); 82 return; 83 } 84 85 if (!message_center_->HasPopupNotifications()) 86 return; 87 88 popups_visible_ = delegate_->ShowPopups(); 89 NotifyMessageCenterTrayChanged(); 90 } 91 92 bool MessageCenterTray::HidePopupBubble() { 93 if (!popups_visible_) 94 return false; 95 96 delegate_->HidePopups(); 97 popups_visible_ = false; 98 NotifyMessageCenterTrayChanged(); 99 100 return true; 101 } 102 103 void MessageCenterTray::ShowNotifierSettingsBubble() { 104 if (popups_visible_) 105 HidePopupBubble(); 106 107 message_center_visible_ = delegate_->ShowNotifierSettings(); 108 } 109 110 ui::MenuModel* MessageCenterTray::CreateQuietModeMenu() { 111 ui::SimpleMenuModel* menu = new ui::SimpleMenuModel(this); 112 113 menu->AddCheckItem(kToggleQuietMode, 114 l10n_util::GetStringUTF16(IDS_MESSAGE_CENTER_QUIET_MODE)); 115 menu->AddItem(kEnableQuietModeHour, 116 l10n_util::GetStringUTF16(IDS_MESSAGE_CENTER_QUIET_MODE_1HOUR)); 117 menu->AddItem(kEnableQuietModeDay, 118 l10n_util::GetStringUTF16(IDS_MESSAGE_CENTER_QUIET_MODE_1DAY)); 119 return menu; 120 } 121 122 void MessageCenterTray::OnNotificationAdded( 123 const std::string& notification_id) { 124 OnMessageCenterChanged(); 125 } 126 127 void MessageCenterTray::OnNotificationRemoved( 128 const std::string& notification_id, 129 bool by_user) { 130 OnMessageCenterChanged(); 131 } 132 133 void MessageCenterTray::OnNotificationUpdated( 134 const std::string& notification_id) { 135 OnMessageCenterChanged(); 136 } 137 138 void MessageCenterTray::OnNotificationClicked( 139 const std::string& notification_id) { 140 if (popups_visible_) 141 OnMessageCenterChanged(); 142 } 143 144 void MessageCenterTray::OnNotificationButtonClicked( 145 const std::string& notification_id, 146 int button_index) { 147 if (popups_visible_) 148 OnMessageCenterChanged(); 149 } 150 151 void MessageCenterTray::OnNotificationDisplayed( 152 const std::string& notification_id) { 153 NotifyMessageCenterTrayChanged(); 154 } 155 156 void MessageCenterTray::OnMessageCenterChanged() { 157 if (message_center_visible_) { 158 if (message_center_->NotificationCount() == 0) 159 HideMessageCenterBubble(); 160 } 161 162 if (popups_visible_ && !message_center_->HasPopupNotifications()) 163 HidePopupBubble(); 164 else if (message_center_->HasPopupNotifications()) 165 ShowPopupBubble(); 166 167 NotifyMessageCenterTrayChanged(); 168 } 169 170 bool MessageCenterTray::IsCommandIdChecked(int command_id) const { 171 if (command_id != kToggleQuietMode) 172 return false; 173 return message_center()->IsQuietMode(); 174 } 175 176 bool MessageCenterTray::IsCommandIdEnabled(int command_id) const { 177 return true; 178 } 179 180 bool MessageCenterTray::GetAcceleratorForCommandId( 181 int command_id, 182 ui::Accelerator* accelerator) { 183 return false; 184 } 185 186 void MessageCenterTray::ExecuteCommand(int command_id, int event_flags) { 187 if (command_id == kToggleQuietMode) { 188 bool in_quiet_mode = message_center()->IsQuietMode(); 189 message_center()->SetQuietMode(!in_quiet_mode); 190 NotifyMessageCenterTrayChanged(); 191 return; 192 } 193 base::TimeDelta expires_in = command_id == kEnableQuietModeDay ? 194 base::TimeDelta::FromDays(1): 195 base::TimeDelta::FromHours(1); 196 message_center()->EnterQuietModeWithExpire(expires_in); 197 NotifyMessageCenterTrayChanged(); 198 } 199 200 void MessageCenterTray::NotifyMessageCenterTrayChanged() { 201 delegate_->OnMessageCenterTrayChanged(); 202 } 203 204 } // namespace message_center 205