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 "base/logging.h"
6#include "base/utf_string_conversions.h"
7#include "chrome/browser/ui/views/user_data_dir_dialog.h"
8#include "grit/chromium_strings.h"
9#include "grit/generated_resources.h"
10#include "ui/base/l10n/l10n_util.h"
11#include "ui/base/message_box_flags.h"
12#include "views/controls/message_box_view.h"
13#include "views/widget/widget.h"
14#include "views/window/window.h"
15
16// static
17FilePath UserDataDirDialog::RunUserDataDirDialog(
18    const FilePath& user_data_dir) {
19  // When the window closes, it will delete itself.
20  UserDataDirDialog* dlg = new UserDataDirDialog(user_data_dir);
21  MessageLoopForUI::current()->Run(dlg);
22  return dlg->user_data_dir();
23}
24
25UserDataDirDialog::UserDataDirDialog(const FilePath& user_data_dir)
26    : ALLOW_THIS_IN_INITIALIZER_LIST(
27          select_file_dialog_(SelectFileDialog::Create(this))),
28      is_blocking_(true) {
29  std::wstring message_text = UTF16ToWide(l10n_util::GetStringFUTF16(
30      IDS_CANT_WRITE_USER_DIRECTORY_SUMMARY,
31      user_data_dir.LossyDisplayName()));
32  const int kDialogWidth = 400;
33  message_box_view_ = new views::MessageBoxView(
34      ui::MessageBoxFlags::kIsConfirmMessageBox,
35      message_text.c_str(), std::wstring(), kDialogWidth);
36
37  views::Window::CreateChromeWindow(NULL, gfx::Rect(), this)->Show();
38}
39
40UserDataDirDialog::~UserDataDirDialog() {
41  select_file_dialog_->ListenerDestroyed();
42}
43
44std::wstring UserDataDirDialog::GetDialogButtonLabel(
45    ui::MessageBoxFlags::DialogButton button) const {
46
47  switch (button) {
48    case ui::MessageBoxFlags::DIALOGBUTTON_OK:
49      return UTF16ToWide(l10n_util::GetStringUTF16(
50          IDS_CANT_WRITE_USER_DIRECTORY_CHOOSE_DIRECTORY_BUTTON));
51    case ui::MessageBoxFlags::DIALOGBUTTON_CANCEL:
52      return UTF16ToWide(l10n_util::GetStringUTF16(
53          IDS_CANT_WRITE_USER_DIRECTORY_EXIT_BUTTON));
54    default:
55      NOTREACHED();
56  }
57
58  return std::wstring();
59}
60
61std::wstring UserDataDirDialog::GetWindowTitle() const {
62  return UTF16ToWide(
63      l10n_util::GetStringUTF16(IDS_CANT_WRITE_USER_DIRECTORY_TITLE));
64}
65
66void UserDataDirDialog::DeleteDelegate() {
67  delete this;
68}
69
70bool UserDataDirDialog::Accept() {
71  // Directory picker
72  std::wstring dialog_title = UTF16ToWide(l10n_util::GetStringUTF16(
73      IDS_CANT_WRITE_USER_DIRECTORY_CHOOSE_DIRECTORY_BUTTON));
74  HWND owning_hwnd =
75      GetAncestor(message_box_view_->GetWidget()->GetNativeView(), GA_ROOT);
76  select_file_dialog_->SelectFile(SelectFileDialog::SELECT_FOLDER,
77                                  dialog_title, FilePath(), NULL, 0,
78                                  std::wstring(), NULL, owning_hwnd, NULL);
79  return false;
80}
81
82bool UserDataDirDialog::Cancel() {
83  is_blocking_ = false;
84  return true;
85}
86
87views::View* UserDataDirDialog::GetContentsView() {
88  return message_box_view_;
89}
90
91bool UserDataDirDialog::Dispatch(const MSG& msg) {
92  TranslateMessage(&msg);
93  DispatchMessage(&msg);
94  return is_blocking_;
95}
96
97void UserDataDirDialog::FileSelected(const FilePath& path,
98                                     int index, void* params) {
99  user_data_dir_ = path;
100  is_blocking_ = false;
101}
102
103void UserDataDirDialog::FileSelectionCanceled(void* params) {
104}
105