Home | History | Annotate | Download | only in views
      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/uninstall_view.h"
      6 
      7 #include "base/message_loop.h"
      8 #include "base/process_util.h"
      9 #include "base/string16.h"
     10 #include "base/utf_string_conversions.h"
     11 #include "chrome/browser/shell_integration.h"
     12 #include "chrome/installer/util/browser_distribution.h"
     13 #include "chrome/installer/util/shell_util.h"
     14 #include "content/common/result_codes.h"
     15 #include "grit/chromium_strings.h"
     16 #include "ui/base/l10n/l10n_util.h"
     17 #include "views/controls/button/checkbox.h"
     18 #include "views/controls/label.h"
     19 #include "views/layout/grid_layout.h"
     20 #include "views/layout/layout_constants.h"
     21 
     22 UninstallView::UninstallView(int& user_selection)
     23     : confirm_label_(NULL),
     24       delete_profile_(NULL),
     25       change_default_browser_(NULL),
     26       browsers_combo_(NULL),
     27       browsers_(NULL),
     28       user_selection_(user_selection) {
     29   SetupControls();
     30 }
     31 
     32 UninstallView::~UninstallView() {
     33   // Exit the message loop we were started with so that uninstall can continue.
     34   MessageLoop::current()->Quit();
     35 }
     36 
     37 void UninstallView::SetupControls() {
     38   using views::ColumnSet;
     39   using views::GridLayout;
     40 
     41   GridLayout* layout = GridLayout::CreatePanel(this);
     42   SetLayoutManager(layout);
     43 
     44   // Message to confirm uninstallation.
     45   int column_set_id = 0;
     46   ColumnSet* column_set = layout->AddColumnSet(column_set_id);
     47   column_set->AddColumn(GridLayout::LEADING, GridLayout::CENTER, 0,
     48                         GridLayout::USE_PREF, 0, 0);
     49   layout->StartRow(0, column_set_id);
     50   confirm_label_ = new views::Label(UTF16ToWide(
     51       l10n_util::GetStringUTF16(IDS_UNINSTALL_VERIFY)));
     52   confirm_label_->SetHorizontalAlignment(views::Label::ALIGN_LEFT);
     53   layout->AddView(confirm_label_);
     54 
     55   layout->AddPaddingRow(0, views::kUnrelatedControlVerticalSpacing);
     56 
     57   // The "delete profile" check box.
     58   ++column_set_id;
     59   column_set = layout->AddColumnSet(column_set_id);
     60   column_set->AddPaddingColumn(0, views::kRelatedControlHorizontalSpacing);
     61   column_set->AddColumn(GridLayout::LEADING, GridLayout::CENTER, 0,
     62                         GridLayout::USE_PREF, 0, 0);
     63   layout->StartRow(0, column_set_id);
     64   delete_profile_ = new views::Checkbox(
     65       UTF16ToWide(l10n_util::GetStringUTF16(IDS_UNINSTALL_DELETE_PROFILE)));
     66   layout->AddView(delete_profile_);
     67 
     68   // Set default browser combo box
     69   BrowserDistribution* dist = BrowserDistribution::GetDistribution();
     70   if (dist->CanSetAsDefault() &&
     71       ShellIntegration::IsDefaultBrowser()) {
     72     browsers_.reset(new BrowsersMap());
     73     ShellUtil::GetRegisteredBrowsers(dist, browsers_.get());
     74     if (!browsers_->empty()) {
     75       layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing);
     76 
     77       ++column_set_id;
     78       column_set = layout->AddColumnSet(column_set_id);
     79       column_set->AddPaddingColumn(0, views::kRelatedControlHorizontalSpacing);
     80       column_set->AddColumn(GridLayout::LEADING, GridLayout::CENTER, 0,
     81                             GridLayout::USE_PREF, 0, 0);
     82       column_set->AddPaddingColumn(0, views::kRelatedControlHorizontalSpacing);
     83       column_set->AddColumn(GridLayout::LEADING, GridLayout::CENTER, 0,
     84                             GridLayout::USE_PREF, 0, 0);
     85       layout->StartRow(0, column_set_id);
     86       change_default_browser_ = new views::Checkbox(UTF16ToWide(
     87           l10n_util::GetStringUTF16(IDS_UNINSTALL_SET_DEFAULT_BROWSER)));
     88       change_default_browser_->set_listener(this);
     89       layout->AddView(change_default_browser_);
     90       browsers_combo_ = new views::Combobox(this);
     91       layout->AddView(browsers_combo_);
     92       browsers_combo_->SetEnabled(false);
     93     }
     94   }
     95 
     96   layout->AddPaddingRow(0, views::kRelatedControlSmallVerticalSpacing);
     97 }
     98 
     99 bool UninstallView::Accept() {
    100   user_selection_ = ResultCodes::NORMAL_EXIT;
    101   if (delete_profile_->checked())
    102     user_selection_ = ResultCodes::UNINSTALL_DELETE_PROFILE;
    103   if (change_default_browser_ && change_default_browser_->checked()) {
    104     int index = browsers_combo_->selected_item();
    105     BrowsersMap::const_iterator it = browsers_->begin();
    106     std::advance(it, index);
    107     base::LaunchApp((*it).second, false, true, NULL);
    108   }
    109   return true;
    110 }
    111 
    112 bool UninstallView::Cancel() {
    113   user_selection_ = ResultCodes::UNINSTALL_USER_CANCEL;
    114   return true;
    115 }
    116 
    117 std::wstring UninstallView::GetDialogButtonLabel(
    118     MessageBoxFlags::DialogButton button) const {
    119   // We only want to give custom name to OK button - 'Uninstall'. Cancel
    120   // button remains same.
    121   std::wstring label = L"";
    122   if (button == MessageBoxFlags::DIALOGBUTTON_OK)
    123     label = UTF16ToWide(l10n_util::GetStringUTF16(IDS_UNINSTALL_BUTTON_TEXT));
    124   return label;
    125 }
    126 
    127 void UninstallView::ButtonPressed(
    128     views::Button* sender, const views::Event& event) {
    129   if (change_default_browser_ == sender) {
    130     // Disable the browsers combobox if the user unchecks the checkbox.
    131     DCHECK(browsers_combo_);
    132     browsers_combo_->SetEnabled(change_default_browser_->checked());
    133   }
    134 }
    135 
    136 std::wstring UninstallView::GetWindowTitle() const {
    137   return UTF16ToWide(l10n_util::GetStringUTF16(IDS_UNINSTALL_CHROME));
    138 }
    139 
    140 views::View* UninstallView::GetContentsView() {
    141   return this;
    142 }
    143 
    144 int UninstallView::GetItemCount() {
    145   DCHECK(!browsers_->empty());
    146   return browsers_->size();
    147 }
    148 
    149 string16 UninstallView::GetItemAt(int index) {
    150   DCHECK(index < (int) browsers_->size());
    151   BrowsersMap::const_iterator it = browsers_->begin();
    152   std::advance(it, index);
    153   return WideToUTF16Hack((*it).first);
    154 }
    155