OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2009 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 "chrome/browser/chromeos/network_menu_button.h" |
| 6 |
| 7 #include "app/l10n_util.h" |
| 8 #include "app/resource_bundle.h" |
| 9 #include "base/string_util.h" |
| 10 #include "chrome/browser/browser.h" |
| 11 #include "chrome/browser/browser_window.h" |
| 12 #include "grit/generated_resources.h" |
| 13 #include "grit/theme_resources.h" |
| 14 #include "views/widget/widget.h" |
| 15 #include "views/window/window.h" |
| 16 |
| 17 //////////////////////////////////////////////////////////////////////////////// |
| 18 // NetworkMenuButton |
| 19 |
| 20 // static |
| 21 const int NetworkMenuButton::kNumWifiImages = 8; |
| 22 SkBitmap* NetworkMenuButton::wifi_images_[kNumWifiImages]; |
| 23 SkBitmap* NetworkMenuButton::wired_image_ = NULL; |
| 24 SkBitmap* NetworkMenuButton::disconnected_image_ = NULL; |
| 25 |
| 26 NetworkMenuButton::NetworkMenuButton(Browser* browser) |
| 27 : MenuButton(NULL, std::wstring(), this, false), |
| 28 refreshing_menu_(false), |
| 29 network_menu_(this), |
| 30 browser_(browser) { |
| 31 static bool initialized = false; |
| 32 if (!initialized) { |
| 33 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
| 34 int image_index = IDR_STATUSBAR_WIFI_1; |
| 35 for (int i = 0; i < kNumWifiImages; i++) |
| 36 wifi_images_[i] = rb.GetBitmapNamed(image_index + i); |
| 37 wired_image_ = rb.GetBitmapNamed(IDR_STATUSBAR_WIRED); |
| 38 disconnected_image_ = rb.GetBitmapNamed(IDR_STATUSBAR_DISCONNECTED); |
| 39 initialized = true; |
| 40 } |
| 41 SetIcon(*disconnected_image_); |
| 42 } |
| 43 |
| 44 //////////////////////////////////////////////////////////////////////////////// |
| 45 // NetworkMenuButton, views::Menu2Model implementation: |
| 46 |
| 47 int NetworkMenuButton::GetItemCount() const { |
| 48 // The menu contains the available wifi networks. If there are none, then it |
| 49 // only has one item with a message that no networks are available. |
| 50 return wifi_networks_.empty() ? 1 : static_cast<int>(wifi_networks_.size()); |
| 51 } |
| 52 |
| 53 views::Menu2Model::ItemType NetworkMenuButton::GetTypeAt(int index) const { |
| 54 return views::Menu2Model::TYPE_CHECK; |
| 55 } |
| 56 |
| 57 int NetworkMenuButton::GetCommandIdAt(int index) const { |
| 58 return index; |
| 59 } |
| 60 |
| 61 string16 NetworkMenuButton::GetLabelAt(int index) const { |
| 62 return wifi_networks_.empty() ? |
| 63 l10n_util::GetStringUTF16(IDS_STATUSBAR_NO_NETWORKS_MESSAGE) : |
| 64 wifi_networks_[index].ssid; |
| 65 } |
| 66 |
| 67 bool NetworkMenuButton::IsItemCheckedAt(int index) const { |
| 68 return GetLabelAt(index) == current_ssid_; |
| 69 } |
| 70 |
| 71 bool NetworkMenuButton::IsEnabledAt(int index) const { |
| 72 return !wifi_networks_.empty(); |
| 73 } |
| 74 |
| 75 void NetworkMenuButton::ActivatedAt(int index) { |
| 76 // When we are refreshing the menu, ignore menu item activation. |
| 77 if (refreshing_menu_) |
| 78 return; |
| 79 |
| 80 connecting_ssid_ = wifi_networks_[index].ssid; |
| 81 if (wifi_networks_[index].encryption.empty()) { |
| 82 ConnectToWifiNetwork(connecting_ssid_, string16()); |
| 83 } else { |
| 84 // If network requires password, we open a password dialog window. |
| 85 gfx::NativeWindow parent = browser_->window()->GetNativeHandle(); |
| 86 PasswordDialogView* dialog = new PasswordDialogView(this); |
| 87 views::Window* window = views::Window::CreateChromeWindow( |
| 88 parent, gfx::Rect(), dialog); |
| 89 // Draw the password dialog right below this button and right aligned. |
| 90 gfx::Size size = dialog->GetPreferredSize(); |
| 91 gfx::Rect rect = bounds(); |
| 92 gfx::Point point = gfx::Point(rect.width() - size.width(), rect.height()); |
| 93 ConvertPointToScreen(this, &point); |
| 94 window->SetBounds(gfx::Rect(point, size), parent); |
| 95 window->Show(); |
| 96 } |
| 97 } |
| 98 |
| 99 //////////////////////////////////////////////////////////////////////////////// |
| 100 // NetworkMenuButton, PasswordDialogDelegate implementation: |
| 101 |
| 102 bool NetworkMenuButton::OnPasswordDialogCancel() { |
| 103 connecting_ssid_.clear(); |
| 104 return true; |
| 105 } |
| 106 |
| 107 bool NetworkMenuButton::OnPasswordDialogAccept(const string16& password) { |
| 108 return ConnectToWifiNetwork(connecting_ssid_, password); |
| 109 } |
| 110 |
| 111 //////////////////////////////////////////////////////////////////////////////// |
| 112 // NetworkMenuButton, views::ViewMenuDelegate implementation: |
| 113 |
| 114 void NetworkMenuButton::RunMenu(views::View* source, const gfx::Point& pt, |
| 115 gfx::NativeView hwnd) { |
| 116 RefreshWifiNetworks(); |
| 117 refreshing_menu_ = true; |
| 118 network_menu_.Rebuild(); |
| 119 network_menu_.UpdateStates(); |
| 120 refreshing_menu_ = false; |
| 121 network_menu_.RunMenuAt(pt, views::Menu2::ALIGN_TOPRIGHT); |
| 122 } |
| 123 |
| 124 void NetworkMenuButton::AddWifiNetwork(const string16& ssid, |
| 125 const string16& encryption, |
| 126 int strength) { |
| 127 wifi_networks_.push_back(WifiNetwork(ssid, encryption, strength)); |
| 128 } |
| 129 |
| 130 void NetworkMenuButton::RefreshWifiNetworks() { |
| 131 // TODO(chocobo): Refresh wifi model here. |
| 132 wifi_networks_.clear(); |
| 133 AddWifiNetwork(ASCIIToUTF16("Wifi 12"), string16(), 12); |
| 134 AddWifiNetwork(ASCIIToUTF16("Wifi 28"), string16(), 28); |
| 135 AddWifiNetwork(ASCIIToUTF16("Wifi 70"), string16(), 70); |
| 136 AddWifiNetwork(ASCIIToUTF16("Wifi 99"), ASCIIToUTF16("WPA-PSK"), 99); |
| 137 |
| 138 // TODO(chocobo): Handle the case where current_ssid_ or connecting_ssid_ are |
| 139 // no longer found in the list of wifi networks. |
| 140 |
| 141 // Refresh the menu button image. |
| 142 if (current_ssid_.empty()) { |
| 143 SetIcon(*disconnected_image_); |
| 144 } else { |
| 145 int size = static_cast<int>(wifi_networks_.size()); |
| 146 for (int i = 0; i < size; i++) { |
| 147 if (wifi_networks_[i].ssid == current_ssid_) { |
| 148 SetIcon(GetWifiImage(wifi_networks_[i])); |
| 149 } |
| 150 } |
| 151 } |
| 152 SchedulePaint(); |
| 153 } |
| 154 |
| 155 bool NetworkMenuButton::ConnectToWifiNetwork(const string16& ssid, |
| 156 const string16& password) { |
| 157 // TODO(chocobo): Connect to wifi here. |
| 158 current_ssid_ = ssid; |
| 159 connecting_ssid_.clear(); |
| 160 RefreshWifiNetworks(); |
| 161 return true; |
| 162 } |
| 163 |
| 164 SkBitmap NetworkMenuButton::GetWifiImage(WifiNetwork wifi_network) const { |
| 165 // Returns the wifi image of 1-8 bars depending on signal strength. |
| 166 // Since signal strenght is from 0 to 100, we need to convert that to 0 to 7. |
| 167 int index = floor(wifi_network.strength / (100.0 / kNumWifiImages)); |
| 168 // This can happen if the signal strength is 100. |
| 169 if (index == kNumWifiImages) |
| 170 index--; |
| 171 return *wifi_images_[index]; |
| 172 } |
OLD | NEW |