summaryrefslogtreecommitdiffstats
path: root/chrome/browser/bookmarks/bookmark_folder_editor_controller.cc
blob: 06f3ef49e304d7f3ffb60f922e2f8ce384f3e702 (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
// Copyright (c) 2010 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/bookmarks/bookmark_folder_editor_controller.h"

#include "app/l10n_util.h"
#include "chrome/browser/bookmarks/bookmark_manager.h"
#include "chrome/browser/bookmarks/bookmark_model.h"
#include "chrome/browser/profile.h"
#include "grit/generated_resources.h"

BookmarkFolderEditorController::~BookmarkFolderEditorController() {
  if (model_)
    model_->RemoveObserver(this);
}

// static
void BookmarkFolderEditorController::Show(Profile* profile,
                                          gfx::NativeWindow wnd,
                                          const BookmarkNode* node,
                                          int index,
                                          uint32 details) {
  // BookmarkFolderEditorController deletes itself when done.
  BookmarkFolderEditorController* editor =
      new BookmarkFolderEditorController(profile, wnd, node, index, details);
  editor->Show();
}

BookmarkFolderEditorController::BookmarkFolderEditorController(
    Profile* profile,
    gfx::NativeWindow wnd,
    const BookmarkNode* node,
    int index,
    uint32 details)
    : profile_(profile),
      model_(profile->GetBookmarkModel()),
      node_(node),
      index_(index),
      details_(details) {
  DCHECK(IsNew() || node);

  std::wstring title = IsNew() ?
      l10n_util::GetString(IDS_BOOMARK_FOLDER_EDITOR_WINDOW_TITLE_NEW) :
      l10n_util::GetString(IDS_BOOMARK_FOLDER_EDITOR_WINDOW_TITLE);
  std::wstring label =
      l10n_util::GetString(IDS_BOOMARK_BAR_EDIT_FOLDER_LABEL);
  std::wstring contents = IsNew() ?
      l10n_util::GetString(IDS_BOOMARK_EDITOR_NEW_FOLDER_NAME) :
      UTF16ToWide(node_->GetTitleAsString16());

  dialog_ = InputWindowDialog::Create(wnd, title, label, contents, this);
  model_->AddObserver(this);
}

void BookmarkFolderEditorController::Show() {
  dialog_->Show();
}

bool BookmarkFolderEditorController::IsValid(const std::wstring& text) {
  return !text.empty();
}

void BookmarkFolderEditorController::InputAccepted(const std::wstring& text) {
  if (IsNew()) {
    ALLOW_UNUSED const BookmarkNode* node =
        model_->AddGroup(node_, index_, text);
    if ((details_ & SHOW_IN_MANAGER) != 0)
      BookmarkManager::SelectInTree(profile_, node);
  } else {
    model_->SetTitle(node_, text);
  }
}

void BookmarkFolderEditorController::InputCanceled() {
}

void BookmarkFolderEditorController::BookmarkModelChanged() {
  dialog_->Close();
}

void BookmarkFolderEditorController::BookmarkModelBeingDeleted(
    BookmarkModel* model) {
  model_->RemoveObserver(this);
  model_ = NULL;
  BookmarkModelChanged();
}

bool BookmarkFolderEditorController::IsNew() {
  return (details_ & IS_NEW) != 0;
}