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/bookmarks/bookmark_folder_editor_controller.h"
6
7 #include "base/string16.h"
8 #include "base/utf_string_conversions.h"
9 #include "chrome/browser/bookmarks/bookmark_model.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "grit/generated_resources.h"
12 #include "ui/base/l10n/l10n_util.h"
13
~BookmarkFolderEditorController()14 BookmarkFolderEditorController::~BookmarkFolderEditorController() {
15 if (model_)
16 model_->RemoveObserver(this);
17 }
18
19 // static
Show(Profile * profile,gfx::NativeWindow wnd,const BookmarkNode * node,int index,Type type)20 void BookmarkFolderEditorController::Show(Profile* profile,
21 gfx::NativeWindow wnd,
22 const BookmarkNode* node,
23 int index,
24 Type type) {
25 // BookmarkFolderEditorController deletes itself when done.
26 new BookmarkFolderEditorController(profile, wnd, node, index, type);
27 }
28
BookmarkFolderEditorController(Profile * profile,gfx::NativeWindow wnd,const BookmarkNode * node,int index,Type type)29 BookmarkFolderEditorController::BookmarkFolderEditorController(
30 Profile* profile,
31 gfx::NativeWindow wnd,
32 const BookmarkNode* node,
33 int index,
34 Type type)
35 : profile_(profile),
36 model_(profile->GetBookmarkModel()),
37 node_(node),
38 index_(index),
39 is_new_(type == NEW_BOOKMARK) {
40 DCHECK(is_new_ || node);
41
42 string16 title = is_new_ ?
43 l10n_util::GetStringUTF16(IDS_BOOMARK_FOLDER_EDITOR_WINDOW_TITLE_NEW) :
44 l10n_util::GetStringUTF16(IDS_BOOMARK_FOLDER_EDITOR_WINDOW_TITLE);
45 string16 label =
46 l10n_util::GetStringUTF16(IDS_BOOMARK_BAR_EDIT_FOLDER_LABEL);
47 string16 contents = is_new_ ?
48 l10n_util::GetStringUTF16(IDS_BOOMARK_EDITOR_NEW_FOLDER_NAME) :
49 node_->GetTitle();
50
51 dialog_ = InputWindowDialog::Create(wnd,
52 UTF16ToWideHack(title),
53 UTF16ToWideHack(label),
54 UTF16ToWideHack(contents),
55 this);
56 model_->AddObserver(this);
57
58 dialog_->Show();
59 }
60
IsValid(const std::wstring & text)61 bool BookmarkFolderEditorController::IsValid(const std::wstring& text) {
62 return !text.empty();
63 }
64
InputAccepted(const std::wstring & text)65 void BookmarkFolderEditorController::InputAccepted(const std::wstring& text) {
66 if (is_new_)
67 model_->AddFolder(node_, index_, WideToUTF16Hack(text));
68 else
69 model_->SetTitle(node_, WideToUTF16Hack(text));
70 }
71
InputCanceled()72 void BookmarkFolderEditorController::InputCanceled() {
73 }
74
BookmarkModelChanged()75 void BookmarkFolderEditorController::BookmarkModelChanged() {
76 dialog_->Close();
77 }
78
BookmarkModelBeingDeleted(BookmarkModel * model)79 void BookmarkFolderEditorController::BookmarkModelBeingDeleted(
80 BookmarkModel* model) {
81 model_->RemoveObserver(this);
82 model_ = NULL;
83 BookmarkModelChanged();
84 }
85