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