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/database_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 kDatabaseInfoViewBorderSize = 1;
20 static const int kDatabaseInfoViewInsetSize = 3;
21
22 ///////////////////////////////////////////////////////////////////////////////
23 // DatabaseInfoView, public:
24
DatabaseInfoView()25 DatabaseInfoView::DatabaseInfoView()
26 : name_value_field_(NULL),
27 description_value_field_(NULL),
28 size_value_field_(NULL),
29 last_modified_value_field_(NULL) {
30 }
31
~DatabaseInfoView()32 DatabaseInfoView::~DatabaseInfoView() {
33 }
34
SetDatabaseInfo(const BrowsingDataDatabaseHelper::DatabaseInfo & database_info)35 void DatabaseInfoView::SetDatabaseInfo(
36 const BrowsingDataDatabaseHelper::DatabaseInfo& database_info) {
37 name_value_field_->SetText(database_info.database_name.empty() ?
38 UTF16ToWide(
39 l10n_util::GetStringUTF16(IDS_COOKIES_WEB_DATABASE_UNNAMED_NAME)) :
40 UTF8ToWide(database_info.database_name));
41 description_value_field_->SetText(UTF8ToWide(database_info.description));
42 size_value_field_->SetText(
43 FormatBytes(database_info.size,
44 GetByteDisplayUnits(database_info.size),
45 true));
46 last_modified_value_field_->SetText(
47 base::TimeFormatFriendlyDateAndTime(database_info.last_modified));
48 EnableDatabaseDisplay(true);
49 }
50
EnableDatabaseDisplay(bool enabled)51 void DatabaseInfoView::EnableDatabaseDisplay(bool enabled) {
52 name_value_field_->SetEnabled(enabled);
53 description_value_field_->SetEnabled(enabled);
54 size_value_field_->SetEnabled(enabled);
55 last_modified_value_field_->SetEnabled(enabled);
56 }
57
ClearDatabaseDisplay()58 void DatabaseInfoView::ClearDatabaseDisplay() {
59 const std::wstring kEmpty;
60 description_value_field_->SetText(kEmpty);
61 size_value_field_->SetText(kEmpty);
62 last_modified_value_field_->SetText(kEmpty);
63 EnableDatabaseDisplay(false);
64 }
65
66 ///////////////////////////////////////////////////////////////////////////////
67 // DatabaseInfoView, views::View overrides:
68
ViewHierarchyChanged(bool is_add,views::View * parent,views::View * child)69 void DatabaseInfoView::ViewHierarchyChanged(bool is_add,
70 views::View* parent,
71 views::View* child) {
72 if (is_add && child == this)
73 Init();
74 }
75
76 ///////////////////////////////////////////////////////////////////////////////
77 // DatabaseInfoView, private:
78
Init()79 void DatabaseInfoView::Init() {
80 SkColor border_color = color_utils::GetSysSkColor(COLOR_3DSHADOW);
81 views::Border* border = views::Border::CreateSolidBorder(
82 kDatabaseInfoViewBorderSize, border_color);
83 set_border(border);
84
85 views::Label* name_label = new views::Label(
86 UTF16ToWide(l10n_util::GetStringUTF16(IDS_COOKIES_COOKIE_NAME_LABEL)));
87 name_value_field_ = new views::Textfield;
88 views::Label* description_label = new views::Label(
89 UTF16ToWide(l10n_util::GetStringUTF16(
90 IDS_COOKIES_WEB_DATABASE_DESCRIPTION_LABEL)));
91 description_value_field_ = new views::Textfield;
92 views::Label* size_label = new views::Label(
93 UTF16ToWide(l10n_util::GetStringUTF16(
94 IDS_COOKIES_LOCAL_STORAGE_SIZE_ON_DISK_LABEL)));
95 size_value_field_ = new views::Textfield;
96 views::Label* last_modified_label = new views::Label(
97 UTF16ToWide(l10n_util::GetStringUTF16(
98 IDS_COOKIES_LOCAL_STORAGE_LAST_MODIFIED_LABEL)));
99 last_modified_value_field_ = new views::Textfield;
100
101 using views::GridLayout;
102
103 GridLayout* layout = new GridLayout(this);
104 layout->SetInsets(kDatabaseInfoViewInsetSize,
105 kDatabaseInfoViewInsetSize,
106 kDatabaseInfoViewInsetSize,
107 kDatabaseInfoViewInsetSize);
108 SetLayoutManager(layout);
109
110 int three_column_layout_id = 0;
111 views::ColumnSet* column_set = layout->AddColumnSet(three_column_layout_id);
112 column_set->AddColumn(GridLayout::TRAILING, GridLayout::CENTER, 0,
113 GridLayout::USE_PREF, 0, 0);
114 column_set->AddPaddingColumn(0, views::kRelatedControlHorizontalSpacing);
115 column_set->AddColumn(GridLayout::FILL, GridLayout::FILL, 1,
116 GridLayout::USE_PREF, 0, 0);
117
118 layout->StartRow(0, three_column_layout_id);
119 layout->AddView(name_label);
120 layout->AddView(name_value_field_);
121 layout->StartRow(0, three_column_layout_id);
122 layout->AddView(description_label);
123 layout->AddView(description_value_field_);
124 layout->AddPaddingRow(0, views::kRelatedControlSmallVerticalSpacing);
125 layout->StartRow(0, three_column_layout_id);
126 layout->AddView(size_label);
127 layout->AddView(size_value_field_);
128 layout->AddPaddingRow(0, views::kRelatedControlSmallVerticalSpacing);
129 layout->StartRow(0, three_column_layout_id);
130 layout->AddView(last_modified_label);
131 layout->AddView(last_modified_value_field_);
132
133 // Color these borderless text areas the same as the containing dialog.
134 SkColor text_area_background = color_utils::GetSysSkColor(COLOR_3DFACE);
135 // Now that the Textfields are in the view hierarchy, we can initialize them.
136 name_value_field_->SetReadOnly(true);
137 name_value_field_->RemoveBorder();
138 name_value_field_->SetBackgroundColor(text_area_background);
139 description_value_field_->SetReadOnly(true);
140 description_value_field_->RemoveBorder();
141 description_value_field_->SetBackgroundColor(text_area_background);
142 size_value_field_->SetReadOnly(true);
143 size_value_field_->RemoveBorder();
144 size_value_field_->SetBackgroundColor(text_area_background);
145 last_modified_value_field_->SetReadOnly(true);
146 last_modified_value_field_->RemoveBorder();
147 last_modified_value_field_->SetBackgroundColor(text_area_background);
148 }
149