summaryrefslogtreecommitdiffstats
path: root/chrome/browser/views/new_profile_dialog.cc
blob: 9672a7aeead6660921acd29cf54d3bbb7e8f0f5c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/views/new_profile_dialog.h"

#include <string>

#include "app/l10n_util.h"
#include "app/message_box_flags.h"
#include "base/file_util.h"
#include "base/i18n/file_util_icu.h"
#include "base/logging.h"
#include "base/path_service.h"
#include "chrome/browser/user_data_manager.h"
#include "grit/chromium_strings.h"
#include "grit/generated_resources.h"
#include "grit/locale_settings.h"
#include "views/controls/message_box_view.h"
#include "views/controls/textfield/textfield.h"
#include "views/view.h"
#include "views/window/window.h"

#if defined(OS_WIN)
#include "base/win_util.h"
#endif  // defined(OS_WIN)

namespace browser {

// Declared in browser_dialogs.h so others don't have to depend on our header.
void ShowNewProfileDialog() {
  NewProfileDialog::RunDialog();
}

}  // namespace browser

// static
void NewProfileDialog::RunDialog() {
  NewProfileDialog* dlg = new NewProfileDialog();
  views::Window::CreateChromeWindow(NULL, gfx::Rect(), dlg)->Show();
}

NewProfileDialog::NewProfileDialog() {
  std::wstring message_text = l10n_util::GetString(
      IDS_NEW_PROFILE_DIALOG_LABEL_TEXT);
  const int kDialogWidth = views::Window::GetLocalizedContentsWidth(
      IDS_NEW_PROFILE_DIALOG_WIDTH_CHARS);
  const int kMessageBoxFlags = MessageBoxFlags::kFlagHasOKButton |
                               MessageBoxFlags::kFlagHasCancelButton |
                               MessageBoxFlags::kFlagHasPromptField;
  message_box_view_ = new MessageBoxView(kMessageBoxFlags,
                                         message_text.c_str(),
                                         std::wstring(),
                                         kDialogWidth);
  message_box_view_->SetCheckBoxLabel(
      l10n_util::GetString(IDS_NEW_PROFILE_DIALOG_CREATE_SHORTCUT_TEXT));
  message_box_view_->SetCheckBoxSelected(true);
  message_box_view_->text_box()->SetController(this);
}

NewProfileDialog::~NewProfileDialog() {
}

views::View* NewProfileDialog::GetInitiallyFocusedView() {
  views::Textfield* text_box = message_box_view_->text_box();
  DCHECK(text_box);
  return text_box;
}

bool NewProfileDialog::IsDialogButtonEnabled(
    MessageBoxFlags::DialogButton button) const {
  if (button == MessageBoxFlags::DIALOGBUTTON_OK) {
    std::wstring profile_name = message_box_view_->GetInputText();

#if defined(OS_POSIX)
    std::string profile_name_narrow = WideToUTF8(profile_name);
    file_util::ReplaceIllegalCharactersInPath(&profile_name_narrow, '_');
    profile_name = UTF8ToWide(profile_name_narrow);
#else
    file_util::ReplaceIllegalCharactersInPath(&profile_name, '_');
#endif
    return !profile_name.empty() &&
        profile_name == message_box_view_->GetInputText();
  }
  return true;
}

std::wstring NewProfileDialog::GetWindowTitle() const {
  return l10n_util::GetString(IDS_NEW_PROFILE_DIALOG_TITLE);
}

void NewProfileDialog::DeleteDelegate() {
  delete this;
}

void NewProfileDialog::ContentsChanged(views::Textfield* sender,
                                       const std::wstring& new_contents) {
  GetDialogClientView()->UpdateDialogButtons();
}

bool NewProfileDialog::Accept() {
  std::wstring profile_name = message_box_view_->GetInputText();
  if (profile_name.empty()) {
    NOTREACHED();
    return true;
  }
  // Create a desktop shortcut if the corresponding checkbox is checked.
  if (message_box_view_->IsCheckBoxSelected()) {
    UserDataManager::Get()->CreateDesktopShortcutForProfile(
        profile_name);
  } else {
#if defined(OS_WIN)
    if (win_util::GetWinVersion() >= win_util::WINVERSION_WIN7) {
      // For Win7, we need to have a shortcut in a place that Windows would
      // index to provide correct relaunch info.
      // See http://crbug.com/32106
      FilePath temp_path;
      if (PathService::Get(base::DIR_APP_DATA, &temp_path)) {
        temp_path = temp_path.Append(
            L"Microsoft\\Internet Explorer\\Quick Launch\\User Pinned");
        UserDataManager::Get()->CreateShortcutForProfileInFolder(
            temp_path,
            profile_name);
      }
    }
#endif  // defined(OS_WIN)
  }

  UserDataManager::Get()->LaunchChromeForProfile(profile_name);
  UserDataManager::Get()->RefreshUserDataDirProfiles();
  return true;
}

views::View* NewProfileDialog::GetContentsView() {
  return message_box_view_;
}