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