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#ifndef CHROME_BROWSER_BOOKMARKS_BOOKMARK_UTILS_H_
6#define CHROME_BROWSER_BOOKMARKS_BOOKMARK_UTILS_H_
7#pragma once
8
9#include <string>
10#include <vector>
11
12#include "base/string16.h"
13#include "chrome/browser/bookmarks/bookmark_editor.h"
14#include "chrome/browser/bookmarks/bookmark_node_data.h"
15#include "chrome/browser/history/snippet.h"
16#include "ui/gfx/native_widget_types.h"
17#include "webkit/glue/window_open_disposition.h"
18
19class BookmarkModel;
20class BookmarkNode;
21class Browser;
22class PageNavigator;
23class PrefService;
24class Profile;
25class TabContents;
26
27namespace views {
28class DropTargetEvent;
29}
30
31// A collection of bookmark utility functions used by various parts of the UI
32// that show bookmarks: bookmark manager, bookmark bar view ...
33namespace bookmark_utils {
34
35// Calculates the drop operation given |source_operations| and the ideal
36// set of drop operations (|operations|). This prefers the following ordering:
37// COPY, LINK then MOVE.
38int PreferredDropOperation(int source_operations, int operations);
39
40// Returns the drag operations for the specified node.
41int BookmarkDragOperation(Profile* profile, const BookmarkNode* node);
42
43// Returns the preferred drop operation on a bookmark menu/bar.
44// |parent| is the parent node the drop is to occur on and |index| the index the
45// drop is over.
46int BookmarkDropOperation(Profile* profile,
47                          const views::DropTargetEvent& event,
48                          const BookmarkNodeData& data,
49                          const BookmarkNode* parent,
50                          int index);
51
52// Performs a drop of bookmark data onto |parent_node| at |index|. Returns the
53// type of drop the resulted.
54int PerformBookmarkDrop(Profile* profile,
55                        const BookmarkNodeData& data,
56                        const BookmarkNode* parent_node,
57                        int index);
58
59// Returns true if the bookmark data can be dropped on |drop_parent| at
60// |index|. A drop from a separate profile is always allowed, where as
61// a drop from the same profile is only allowed if none of the nodes in
62// |data| are an ancestor of |drop_parent| and one of the nodes isn't already
63// a child of |drop_parent| at |index|.
64bool IsValidDropLocation(Profile* profile,
65                         const BookmarkNodeData& data,
66                         const BookmarkNode* drop_parent,
67                         int index);
68
69// Clones bookmark node, adding newly created nodes to |parent| starting at
70// |index_to_add_at|.
71void CloneBookmarkNode(BookmarkModel* model,
72                       const std::vector<BookmarkNodeData::Element>& elements,
73                       const BookmarkNode* parent,
74                       int index_to_add_at);
75
76// Begins dragging a folder of bookmarks.
77void DragBookmarks(Profile* profile,
78                   const std::vector<const BookmarkNode*>& nodes,
79                   gfx::NativeView view);
80
81// Opens all the bookmarks in |nodes| that are of type url and all the child
82// bookmarks that are of type url for folders in |nodes|. |initial_disposition|
83// dictates how the first URL is opened, all subsequent URLs are opened as
84// background tabs.  |navigator| is used to open the URLs. If |navigator| is
85// NULL the last tabbed browser with the profile |profile| is used. If there is
86// no browser with the specified profile a new one is created.
87void OpenAll(gfx::NativeWindow parent,
88             Profile* profile,
89             PageNavigator* navigator,
90             const std::vector<const BookmarkNode*>& nodes,
91             WindowOpenDisposition initial_disposition);
92
93// Convenience for |OpenAll| with a single BookmarkNode.
94void OpenAll(gfx::NativeWindow parent,
95             Profile* profile,
96             PageNavigator* navigator,
97             const BookmarkNode* node,
98             WindowOpenDisposition initial_disposition);
99
100// Copies nodes onto the clipboard. If |remove_nodes| is true the nodes are
101// removed after copied to the clipboard. The nodes are copied in such a way
102// that if pasted again copies are made.
103void CopyToClipboard(BookmarkModel* model,
104                     const std::vector<const BookmarkNode*>& nodes,
105                     bool remove_nodes);
106
107// Pastes from the clipboard. The new nodes are added to |parent|, unless
108// |parent| is null in which case this does nothing. The nodes are inserted
109// at |index|. If |index| is -1 the nodes are added to the end.
110void PasteFromClipboard(BookmarkModel* model,
111                        const BookmarkNode* parent,
112                        int index);
113
114// Returns true if the user can copy from the pasteboard.
115bool CanPasteFromClipboard(const BookmarkNode* node);
116
117// Returns a name for the given URL. Used for drags into bookmark areas when
118// the source doesn't specify a title.
119string16 GetNameForURL(const GURL& url);
120
121// Returns a vector containing up to |max_count| of the most recently modified
122// folders. This never returns an empty vector.
123std::vector<const BookmarkNode*> GetMostRecentlyModifiedFolders(
124    BookmarkModel* model, size_t max_count);
125
126// Returns the most recently added bookmarks. This does not return folders,
127// only nodes of type url.
128void GetMostRecentlyAddedEntries(BookmarkModel* model,
129                                 size_t count,
130                                 std::vector<const BookmarkNode*>* nodes);
131
132// Used by GetBookmarksMatchingText to return a matching node and the location
133// of the match in the title.
134struct TitleMatch {
135  TitleMatch();
136  ~TitleMatch();
137
138  const BookmarkNode* node;
139
140  // Location of the matching words in the title of the node.
141  Snippet::MatchPositions match_positions;
142};
143
144// Returns true if |n1| was added more recently than |n2|.
145bool MoreRecentlyAdded(const BookmarkNode* n1, const BookmarkNode* n2);
146
147// Returns up to |max_count| bookmarks from |model| whose url or title contains
148// the text |text|.  |languages| is user's accept-language setting to decode
149// IDN.
150void GetBookmarksContainingText(BookmarkModel* model,
151                                const string16& text,
152                                size_t max_count,
153                                const std::string& languages,
154                                std::vector<const BookmarkNode*>* nodes);
155
156// Returns true if |node|'s url or title contains the string |text|.
157// |languages| is user's accept-language setting to decode IDN.
158bool DoesBookmarkContainText(const BookmarkNode* node,
159                             const string16& text,
160                             const std::string& languages);
161
162// Modifies a bookmark node (assuming that there's no magic that needs to be
163// done regarding moving from one folder to another).  If a new node is
164// explicitly being added, returns a pointer to the new node that was created.
165// Otherwise the return value is identically |node|.
166const BookmarkNode* ApplyEditsWithNoFolderChange(
167    BookmarkModel* model,
168    const BookmarkNode* parent,
169    const BookmarkEditor::EditDetails& details,
170    const string16& new_title,
171    const GURL& new_url);
172
173// Modifies a bookmark node assuming that the parent of the node may have
174// changed and the node will need to be removed and reinserted.  If a new node
175// is explicitly being added, returns a pointer to the new node that was
176// created.  Otherwise the return value is identically |node|.
177const BookmarkNode* ApplyEditsWithPossibleFolderChange(
178    BookmarkModel* model,
179    const BookmarkNode* new_parent,
180    const BookmarkEditor::EditDetails& details,
181    const string16& new_title,
182    const GURL& new_url);
183
184// Toggles whether the bookmark bar is shown only on the new tab page or on
185// all tabs.  This is a preference modifier, not a visual modifier.
186void ToggleWhenVisible(Profile* profile);
187
188// Register user preferences for BookmarksBar.
189void RegisterUserPrefs(PrefService* prefs);
190
191// Fills in the URL and title for a bookmark of |tab_contents|.
192void GetURLAndTitleToBookmark(TabContents* tab_contents,
193                              GURL* url,
194                              string16* title);
195
196// Returns, by reference in |urls|, the url and title pairs for each open
197// tab in browser.
198void GetURLsForOpenTabs(Browser* browser,
199                        std::vector<std::pair<GURL, string16> >* urls);
200
201// Returns the parent for newly created folders/bookmarks. If |selection| has
202// one element and it is a folder, |selection[0]| is returned, otherwise
203// |parent| is returned. If |index| is non-null it is set to the index newly
204// added nodes should be added at.
205const BookmarkNode* GetParentForNewNodes(
206    const BookmarkNode* parent,
207    const std::vector<const BookmarkNode*>& selection,
208    int* index);
209
210// Returns true if the specified node is of type URL, or has a descendant
211// of type URL.
212bool NodeHasURLs(const BookmarkNode* node);
213
214// Number of bookmarks we'll open before prompting the user to see if they
215// really want to open all.
216//
217// NOTE: treat this as a const. It is not const as various tests change the
218// value.
219extern int num_urls_before_prompting;
220
221}  // namespace bookmark_utils
222
223#endif  // CHROME_BROWSER_BOOKMARKS_BOOKMARK_UTILS_H_
224