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
3 // found in the LICENSE file.
4
5 #include "chrome/browser/ui/views/local_storage_info_view.h"
6
7 #include <algorithm>
8
9 #include "base/i18n/time_formatting.h"
10 #include "base/utf_string_conversions.h"
11 #include "grit/generated_resources.h"
12 #include "ui/base/l10n/l10n_util.h"
13 #include "ui/gfx/color_utils.h"
14 #include "views/controls/label.h"
15 #include "views/controls/textfield/textfield.h"
16 #include "views/layout/grid_layout.h"
17 #include "views/layout/layout_constants.h"
18
19 static const int kLocalStorageInfoViewBorderSize = 1;
20 static const int kLocalStorageInfoViewInsetSize = 3;
21
22 ///////////////////////////////////////////////////////////////////////////////
23 // LocalStorageInfoView, public:
24
LocalStorageInfoView()25 LocalStorageInfoView::LocalStorageInfoView()
26 : origin_value_field_(NULL),
27 size_value_field_(NULL),
28 last_modified_value_field_(NULL) {
29 }
30
~LocalStorageInfoView()31 LocalStorageInfoView::~LocalStorageInfoView() {
32 }
33
SetLocalStorageInfo(const BrowsingDataLocalStorageHelper::LocalStorageInfo & local_storage_info)34 void LocalStorageInfoView::SetLocalStorageInfo(
35 const BrowsingDataLocalStorageHelper::LocalStorageInfo&
36 local_storage_info) {
37 origin_value_field_->SetText(UTF8ToWide(local_storage_info.origin));
38 size_value_field_->SetText(
39 FormatBytes(local_storage_info.size,
40 GetByteDisplayUnits(local_storage_info.size),
41 true));
42 last_modified_value_field_->SetText(
43 base::TimeFormatFriendlyDateAndTime(local_storage_info.last_modified));
44 EnableLocalStorageDisplay(true);
45 }
46
EnableLocalStorageDisplay(bool enabled)47 void LocalStorageInfoView::EnableLocalStorageDisplay(bool enabled) {
48 origin_value_field_->SetEnabled(enabled);
49 size_value_field_->SetEnabled(enabled);
50 last_modified_value_field_->SetEnabled(enabled);
51 }
52
ClearLocalStorageDisplay()53 void LocalStorageInfoView::ClearLocalStorageDisplay() {
54 std::wstring no_cookie_string =
55 UTF16ToWide(l10n_util::GetStringUTF16(IDS_COOKIES_COOKIE_NONESELECTED));
56 origin_value_field_->SetText(no_cookie_string);
57 size_value_field_->SetText(no_cookie_string);
58 last_modified_value_field_->SetText(no_cookie_string);
59 EnableLocalStorageDisplay(false);
60 }
61
62 ///////////////////////////////////////////////////////////////////////////////
63 // LocalStorageInfoView, views::View overrides:
64
ViewHierarchyChanged(bool is_add,views::View * parent,views::View * child)65 void LocalStorageInfoView::ViewHierarchyChanged(bool is_add,
66 views::View* parent,
67 views::View* child) {
68 if (is_add && child == this)
69 Init();
70 }
71
72 ///////////////////////////////////////////////////////////////////////////////
73 // LocalStorageInfoView, private:
74
Init()75 void LocalStorageInfoView::Init() {
76 SkColor border_color = color_utils::GetSysSkColor(COLOR_3DSHADOW);
77 views::Border* border = views::Border::CreateSolidBorder(
78 kLocalStorageInfoViewBorderSize, border_color);
79 set_border(border);
80
81 views::Label* origin_label = new views::Label(UTF16ToWide(
82 l10n_util::GetStringUTF16(IDS_COOKIES_LOCAL_STORAGE_ORIGIN_LABEL)));
83 origin_value_field_ = new views::Textfield;
84 views::Label* size_label = new views::Label(UTF16ToWide(
85 l10n_util::GetStringUTF16(IDS_COOKIES_LOCAL_STORAGE_SIZE_ON_DISK_LABEL)));
86 size_value_field_ = new views::Textfield;
87 views::Label* last_modified_label = new views::Label(UTF16ToWide(
88 l10n_util::GetStringUTF16(
89 IDS_COOKIES_LOCAL_STORAGE_LAST_MODIFIED_LABEL)));
90 last_modified_value_field_ = new views::Textfield;
91
92 using views::GridLayout;
93
94 GridLayout* layout = new GridLayout(this);
95 layout->SetInsets(kLocalStorageInfoViewInsetSize,
96 kLocalStorageInfoViewInsetSize,
97 kLocalStorageInfoViewInsetSize,
98 kLocalStorageInfoViewInsetSize);
99 SetLayoutManager(layout);
100
101 int three_column_layout_id = 0;
102 views::ColumnSet* column_set = layout->AddColumnSet(three_column_layout_id);
103 column_set->AddColumn(GridLayout::TRAILING, GridLayout::CENTER, 0,
104 GridLayout::USE_PREF, 0, 0);
105 column_set->AddPaddingColumn(0, views::kRelatedControlHorizontalSpacing);
106 column_set->AddColumn(GridLayout::FILL, GridLayout::FILL, 1,
107 GridLayout::USE_PREF, 0, 0);
108
109 layout->StartRow(0, three_column_layout_id);
110 layout->AddView(origin_label);
111 layout->AddView(origin_value_field_);
112 layout->AddPaddingRow(0, views::kRelatedControlSmallVerticalSpacing);
113 layout->StartRow(0, three_column_layout_id);
114 layout->AddView(size_label);
115 layout->AddView(size_value_field_);
116 layout->AddPaddingRow(0, views::kRelatedControlSmallVerticalSpacing);
117 layout->StartRow(0, three_column_layout_id);
118 layout->AddView(last_modified_label);
119 layout->AddView(last_modified_value_field_);
120
121 // Color these borderless text areas the same as the containing dialog.
122 SkColor text_area_background = color_utils::GetSysSkColor(COLOR_3DFACE);
123 // Now that the Textfields are in the view hierarchy, we can initialize them.
124 origin_value_field_->SetReadOnly(true);
125 origin_value_field_->RemoveBorder();
126 origin_value_field_->SetBackgroundColor(text_area_background);
127 size_value_field_->SetReadOnly(true);
128 size_value_field_->RemoveBorder();
129 size_value_field_->SetBackgroundColor(text_area_background);
130 last_modified_value_field_->SetReadOnly(true);
131 last_modified_value_field_->RemoveBorder();
132 last_modified_value_field_->SetBackgroundColor(text_area_background);
133 }
134
135