Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(37)

Side by Side Diff: chrome/browser/chromeos/settings_contents_view.cc

Issue 251099: Refactor cros library code into central location and have the UI elements obs... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 11 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. 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 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/chromeos/settings_contents_view.h" 5 #include "chrome/browser/chromeos/settings_contents_view.h"
6 6
7 #include <map> 7 #include <map>
8 #include <string> 8 #include <string>
9 #include <vector> 9 #include <vector>
10 10
11 #include "app/combobox_model.h" 11 #include "app/combobox_model.h"
12 #include "app/l10n_util.h" 12 #include "app/l10n_util.h"
13 #include "app/resource_bundle.h" 13 #include "app/resource_bundle.h"
14 #include "base/basictypes.h" 14 #include "base/basictypes.h"
15 #include "base/string_util.h" 15 #include "base/string_util.h"
16 #include "chrome/browser/chromeos/cros_network_library.h"
16 #include "chrome/browser/chromeos/password_dialog_view.h" 17 #include "chrome/browser/chromeos/password_dialog_view.h"
17 #include "chrome/common/pref_member.h" 18 #include "chrome/common/pref_member.h"
18 #include "chrome/common/pref_names.h" 19 #include "chrome/common/pref_names.h"
19 #include "grit/chromium_strings.h" 20 #include "grit/chromium_strings.h"
20 #include "grit/generated_resources.h" 21 #include "grit/generated_resources.h"
21 #include "views/background.h" 22 #include "views/background.h"
22 #include "views/controls/button/checkbox.h" 23 #include "views/controls/button/checkbox.h"
23 #include "views/controls/combobox/combobox.h" 24 #include "views/controls/combobox/combobox.h"
24 #include "views/controls/slider/slider.h" 25 #include "views/controls/slider/slider.h"
25 #include "views/grid_layout.h" 26 #include "views/grid_layout.h"
26 #include "views/standard_layout.h" 27 #include "views/standard_layout.h"
27 #include "views/window/window.h" 28 #include "views/window/window.h"
28 29
29 using views::GridLayout; 30 using views::GridLayout;
30 using views::ColumnSet; 31 using views::ColumnSet;
31 32
32 namespace { 33 namespace {
33 34
34 //////////////////////////////////////////////////////////////////////////////// 35 ////////////////////////////////////////////////////////////////////////////////
35 // WifiSSIDComboModel 36 // WifiNetworkComboModel
36 37
37 // The Combobox model for the list of wifi networks 38 // The Combobox model for the list of wifi networks
38 class WifiSSIDComboModel : public ComboboxModel { 39 class WifiNetworkComboModel : public ComboboxModel {
39 public: 40 public:
40 struct NetworkData { 41 WifiNetworkComboModel();
41 NetworkData() { }
42 NetworkData(const string16& encryption, const int& strength)
43 : encryption(encryption),
44 strength(strength) { }
45
46 string16 encryption;
47 int strength;
48 };
49 typedef std::map<std::string, NetworkData> NetworkDataMap;
50
51 WifiSSIDComboModel();
52 42
53 virtual int GetItemCount(); 43 virtual int GetItemCount();
54 virtual std::wstring GetItemAt(int index); 44 virtual std::wstring GetItemAt(int index);
55 45
56 const std::string& GetSSIDAt(int index); 46 bool HasWifiNetworks();
57 bool RequiresPassword(const std::string& ssid); 47 const WifiNetwork& GetWifiNetworkAt(int index);
58 48
59 private: 49 private:
60 std::vector<std::string> ssids_; 50 WifiNetworkVector wifi_networks_;
61 51
62 // A map of some extra data (NetworkData) keyed off the ssids. 52 DISALLOW_COPY_AND_ASSIGN(WifiNetworkComboModel);
63 NetworkDataMap ssids_map_;
64
65 void AddWifiNetwork(const std::string& ssid,
66 const string16& encryption,
67 int strength);
68
69 DISALLOW_COPY_AND_ASSIGN(WifiSSIDComboModel);
70 }; 53 };
71 54
72 WifiSSIDComboModel::WifiSSIDComboModel() { 55 WifiNetworkComboModel::WifiNetworkComboModel() {
73 // TODO(chocobo): Load wifi info from conman. 56 wifi_networks_ = CrosNetworkLibrary::Get()->GetWifiNetworks();
74 // This is just temporary data until we hook this up to real data.
75 AddWifiNetwork("Wifi Combobox Mock", string16(), 80);
76 AddWifiNetwork("Wifi WPA-PSK Password is chronos",
77 ASCIIToUTF16("WPA-PSK"), 60);
78 AddWifiNetwork("Wifi No Encryption", string16(), 90);
79 } 57 }
80 58
81 int WifiSSIDComboModel::GetItemCount() { 59 int WifiNetworkComboModel::GetItemCount() {
82 return static_cast<int>(ssids_.size()); 60 // Item count is always 1 more than number of networks.
61 // If there are no networks, then we show a message stating that.
62 // If there are networks, the first item is empty.
63 return static_cast<int>(wifi_networks_.size()) + 1;
83 } 64 }
84 65
85 std::wstring WifiSSIDComboModel::GetItemAt(int index) { 66 std::wstring WifiNetworkComboModel::GetItemAt(int index) {
86 DCHECK(static_cast<int>(ssids_.size()) > index); 67 if (index == 0) {
87 68 return wifi_networks_.empty() ?
88 NetworkDataMap::const_iterator it = ssids_map_.find(ssids_[index]); 69 l10n_util::GetString(IDS_STATUSBAR_NO_NETWORKS_MESSAGE) :
89 DCHECK(it != ssids_map_.end()); 70 ASCIIToWide("");
90 71 }
91 // TODO(chocobo): Finalize UI, then put strings in resource file. 72 // If index is greater than one, we get the corresponding network, which is
92 std::vector<string16> subst; 73 // in the wifi_networks_ list in [index-1] position.
93 subst.push_back(ASCIIToUTF16(it->first)); // $1 74 return ASCIIToWide(wifi_networks_[index-1].ssid);
94 // The "None" string is just temporary for now. Have not finalized the UI yet.
95 if (it->second.encryption.empty())
96 subst.push_back(ASCIIToUTF16("None")); // $2
97 else
98 subst.push_back(it->second.encryption); // $2
99 subst.push_back(IntToString16(it->second.strength)); // $3
100
101 return UTF16ToWide(
102 ReplaceStringPlaceholders(ASCIIToUTF16("$1 ($2, $3)"), subst, NULL));
103 } 75 }
104 76
105 const std::string& WifiSSIDComboModel::GetSSIDAt(int index) { 77 bool WifiNetworkComboModel::HasWifiNetworks() {
106 DCHECK(static_cast<int>(ssids_.size()) > index); 78 return !wifi_networks_.empty();
107 return ssids_[index];
108 } 79 }
109 80
110 bool WifiSSIDComboModel::RequiresPassword(const std::string& ssid) { 81 const WifiNetwork& WifiNetworkComboModel::GetWifiNetworkAt(int index) {
111 NetworkDataMap::const_iterator it = ssids_map_.find(ssid); 82 return wifi_networks_[index-1];
112 DCHECK(it != ssids_map_.end());
113 return !it->second.encryption.empty();
114 }
115
116 void WifiSSIDComboModel::AddWifiNetwork(const std::string& ssid,
117 const string16& encryption,
118 int strength) {
119 ssids_.push_back(ssid);
120 ssids_map_[ssid] = NetworkData(encryption, strength);
121 } 83 }
122 84
123 //////////////////////////////////////////////////////////////////////////////// 85 ////////////////////////////////////////////////////////////////////////////////
124 // NetworkSection 86 // NetworkSection
125 87
126 // Network section for wifi settings 88 // Network section for wifi settings
127 class NetworkSection : public OptionsPageView, 89 class NetworkSection : public OptionsPageView,
128 public views::Combobox::Listener, 90 public views::Combobox::Listener,
129 public PasswordDialogDelegate { 91 public PasswordDialogDelegate,
92 public CrosNetworkLibrary::Observer {
130 public: 93 public:
131 explicit NetworkSection(Profile* profile); 94 explicit NetworkSection(Profile* profile);
132 virtual ~NetworkSection() {} 95 virtual ~NetworkSection();
133 96
134 // Overridden from views::Combobox::Listener: 97 // Overridden from views::Combobox::Listener:
135 virtual void ItemChanged(views::Combobox* sender, 98 virtual void ItemChanged(views::Combobox* sender,
136 int prev_index, 99 int prev_index,
137 int new_index); 100 int new_index);
138 101
139 // PasswordDialogDelegate implementation. 102 // PasswordDialogDelegate implementation.
140 virtual bool OnPasswordDialogCancel(); 103 virtual bool OnPasswordDialogCancel();
141 virtual bool OnPasswordDialogAccept(const std::string& ssid, 104 virtual bool OnPasswordDialogAccept(const std::string& ssid,
142 const string16& password); 105 const string16& password);
143 106
144 bool ConnectToWifi(const std::string& ssid, const string16& password); 107 // CrosNetworkLibrary::Observer implementation.
108 virtual void NetworkChanged(CrosNetworkLibrary* obj);
145 109
146 protected: 110 protected:
147 // OptionsPageView overrides: 111 // OptionsPageView overrides:
148 virtual void InitControlLayout(); 112 virtual void InitControlLayout();
149 virtual void InitContents(); 113 virtual void InitContents();
150 114
151 private: 115 private:
152 // The View that contains the contents of the section. 116 // The View that contains the contents of the section.
153 views::View* contents_; 117 views::View* contents_;
154 118
155 // Controls for this section: 119 // Controls for this section:
156 views::Combobox* wifi_ssid_combobox_; 120 views::Combobox* wifi_ssid_combobox_;
157 121
158 // Used to store the index (in combobox) of the currently connected wifi. 122 // Used to store the index (in combobox) of the currently connected wifi.
159 int last_selected_wifi_ssid_index_; 123 int last_selected_wifi_ssid_index_;
160 124
161 // Dummy for now. Used to populate wifi ssid models. 125 // The activated wifi network.
162 WifiSSIDComboModel wifi_ssid_model_; 126 WifiNetwork activated_wifi_network_;
127
128 // The wifi ssid models.
129 WifiNetworkComboModel wifi_ssid_model_;
163 130
164 DISALLOW_COPY_AND_ASSIGN(NetworkSection); 131 DISALLOW_COPY_AND_ASSIGN(NetworkSection);
165 }; 132 };
166 133
167 //////////////////////////////////////////////////////////////////////////////// 134 ////////////////////////////////////////////////////////////////////////////////
168 // NetworkSection 135 // NetworkSection
169 136
170 NetworkSection::NetworkSection(Profile* profile) 137 NetworkSection::NetworkSection(Profile* profile)
171 : OptionsPageView(profile), 138 : OptionsPageView(profile),
172 contents_(NULL), 139 contents_(NULL),
173 wifi_ssid_combobox_(NULL), 140 wifi_ssid_combobox_(NULL),
174 last_selected_wifi_ssid_index_(0) { 141 last_selected_wifi_ssid_index_(0) {
142 CrosNetworkLibrary::Get()->AddObserver(this);
143 }
144
145 NetworkSection::~NetworkSection() {
146 CrosNetworkLibrary::Get()->RemoveObserver(this);
175 } 147 }
176 148
177 void NetworkSection::ItemChanged(views::Combobox* sender, 149 void NetworkSection::ItemChanged(views::Combobox* sender,
178 int prev_index, 150 int prev_index,
179 int new_index) { 151 int new_index) {
180 if (new_index == prev_index) 152 if (new_index == prev_index)
181 return; 153 return;
182 if (sender == wifi_ssid_combobox_) { 154
183 last_selected_wifi_ssid_index_ = prev_index; 155 // Don't allow switching to the first item (which is empty).
184 std::string ssid = wifi_ssid_model_.GetSSIDAt(new_index); 156 if (new_index == 0) {
185 // Connect to wifi here. Open password page if appropriate 157 wifi_ssid_combobox_->SetSelectedItem(prev_index);
186 if (wifi_ssid_model_.RequiresPassword(ssid)) { 158 return;
187 views::Window* window = views::Window::CreateChromeWindow( 159 }
188 NULL, 160
189 gfx::Rect(), 161 if (!wifi_ssid_model_.HasWifiNetworks())
190 new PasswordDialogView(this, ssid)); 162 return;
191 window->SetIsAlwaysOnTop(true); 163
192 window->Show(); 164 last_selected_wifi_ssid_index_ = prev_index;
193 } else { 165 activated_wifi_network_ = wifi_ssid_model_.GetWifiNetworkAt(new_index);
194 ConnectToWifi(ssid, string16()); 166 // Connect to wifi here. Open password page if appropriate.
195 } 167 if (activated_wifi_network_.encrypted) {
168 views::Window* window = views::Window::CreateChromeWindow(
169 NULL,
170 gfx::Rect(),
171 new PasswordDialogView(this, activated_wifi_network_.ssid));
172 window->SetIsAlwaysOnTop(true);
173 window->Show();
174 } else {
175 CrosNetworkLibrary::Get()->ConnectToWifiNetwork(activated_wifi_network_,
176 string16());
196 } 177 }
197 } 178 }
198 179
199 bool NetworkSection::OnPasswordDialogCancel() { 180 bool NetworkSection::OnPasswordDialogCancel() {
200 // Change combobox to previous setting 181 // Change combobox to previous setting.
201 wifi_ssid_combobox_->SetSelectedItem(last_selected_wifi_ssid_index_); 182 wifi_ssid_combobox_->SetSelectedItem(last_selected_wifi_ssid_index_);
202 return true; 183 return true;
203 } 184 }
204 185
205 bool NetworkSection::OnPasswordDialogAccept(const std::string& ssid, 186 bool NetworkSection::OnPasswordDialogAccept(const std::string& ssid,
206 const string16& password) { 187 const string16& password) {
207 // Try connecting to wifi 188 CrosNetworkLibrary::Get()->ConnectToWifiNetwork(activated_wifi_network_,
208 return ConnectToWifi(ssid, password); 189 password);
190 return true;
209 } 191 }
210 192
211 bool NetworkSection::ConnectToWifi(const std::string& ssid, 193 void NetworkSection::NetworkChanged(CrosNetworkLibrary* obj) {
212 const string16& password) { 194 if (wifi_ssid_model_.HasWifiNetworks()) {
213 // TODO(chocobo): Connect to wifi 195 for (int i = 1; i < wifi_ssid_model_.GetItemCount(); i++) {
214 return password == ASCIIToUTF16("chronos"); 196 if (wifi_ssid_model_.GetWifiNetworkAt(i).ssid == obj->wifi_ssid()) {
197 last_selected_wifi_ssid_index_ = i;
198 if (wifi_ssid_combobox_)
199 wifi_ssid_combobox_->SetSelectedItem(i);
200 return;
201 }
202 }
203 }
215 } 204 }
216 205
217 void NetworkSection::InitControlLayout() { 206 void NetworkSection::InitControlLayout() {
218 GridLayout* layout = new GridLayout(this); 207 GridLayout* layout = new GridLayout(this);
219 SetLayoutManager(layout); 208 SetLayoutManager(layout);
220 209
221 int single_column_layout_id = 0; 210 int single_column_layout_id = 0;
222 ColumnSet* column_set = layout->AddColumnSet(single_column_layout_id); 211 ColumnSet* column_set = layout->AddColumnSet(single_column_layout_id);
223 column_set->AddColumn(GridLayout::LEADING, GridLayout::LEADING, 0, 212 column_set->AddColumn(GridLayout::LEADING, GridLayout::LEADING, 0,
224 GridLayout::USE_PREF, 0, 0); 213 GridLayout::USE_PREF, 0, 0);
(...skipping 16 matching lines...) Expand all
241 InitContents(); 230 InitContents();
242 layout->AddView(contents_); 231 layout->AddView(contents_);
243 } 232 }
244 233
245 void NetworkSection::InitContents() { 234 void NetworkSection::InitContents() {
246 contents_ = new views::View; 235 contents_ = new views::View;
247 GridLayout* layout = new GridLayout(contents_); 236 GridLayout* layout = new GridLayout(contents_);
248 contents_->SetLayoutManager(layout); 237 contents_->SetLayoutManager(layout);
249 238
250 wifi_ssid_combobox_ = new views::Combobox(&wifi_ssid_model_); 239 wifi_ssid_combobox_ = new views::Combobox(&wifi_ssid_model_);
240 wifi_ssid_combobox_->SetSelectedItem(last_selected_wifi_ssid_index_);
251 wifi_ssid_combobox_->set_listener(this); 241 wifi_ssid_combobox_->set_listener(this);
252 242
253 int single_column_view_set_id = 0; 243 int single_column_view_set_id = 0;
254 ColumnSet* column_set = layout->AddColumnSet(single_column_view_set_id); 244 ColumnSet* column_set = layout->AddColumnSet(single_column_view_set_id);
255 column_set->AddColumn(GridLayout::FILL, GridLayout::CENTER, 1, 245 column_set->AddColumn(GridLayout::FILL, GridLayout::CENTER, 1,
256 GridLayout::USE_PREF, 0, 0); 246 GridLayout::USE_PREF, 0, 0);
257 247
258 layout->StartRow(0, single_column_view_set_id); 248 layout->StartRow(0, single_column_view_set_id);
259 layout->AddView(wifi_ssid_combobox_); 249 layout->AddView(wifi_ssid_combobox_);
260 layout->AddPaddingRow(0, kRelatedControlVerticalSpacing); 250 layout->AddPaddingRow(0, kRelatedControlVerticalSpacing);
(...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after
467 457
468 void SettingsContentsView::InitControlLayout() { 458 void SettingsContentsView::InitControlLayout() {
469 GridLayout* layout = CreatePanelGridLayout(this); 459 GridLayout* layout = CreatePanelGridLayout(this);
470 SetLayoutManager(layout); 460 SetLayoutManager(layout);
471 461
472 int single_column_view_set_id = 0; 462 int single_column_view_set_id = 0;
473 ColumnSet* column_set = layout->AddColumnSet(single_column_view_set_id); 463 ColumnSet* column_set = layout->AddColumnSet(single_column_view_set_id);
474 column_set->AddColumn(GridLayout::FILL, GridLayout::FILL, 1, 464 column_set->AddColumn(GridLayout::FILL, GridLayout::FILL, 1,
475 GridLayout::USE_PREF, 0, 0); 465 GridLayout::USE_PREF, 0, 0);
476 466
477 // TODO(chocobo): Add NetworkSection back in when we finalized the UI. 467 layout->StartRow(0, single_column_view_set_id);
478 // layout->StartRow(0, single_column_view_set_id); 468 layout->AddView(new NetworkSection(profile()));
479 // layout->AddView(new NetworkSection(profile())); 469 layout->AddPaddingRow(0, kRelatedControlVerticalSpacing);
480 // layout->AddPaddingRow(0, kRelatedControlVerticalSpacing);
481 layout->StartRow(0, single_column_view_set_id); 470 layout->StartRow(0, single_column_view_set_id);
482 layout->AddView(new TouchpadSection(profile())); 471 layout->AddView(new TouchpadSection(profile()));
483 layout->AddPaddingRow(0, kRelatedControlVerticalSpacing); 472 layout->AddPaddingRow(0, kRelatedControlVerticalSpacing);
484 } 473 }
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/power_menu_button.cc ('k') | chrome/browser/chromeos/status_area_view.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698