Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/importer/profile_writer.h" | 5 #include "chrome/browser/importer/profile_writer.h" |
| 6 | 6 |
| 7 #include "base/stringprintf.h" | 7 #include "base/stringprintf.h" |
| 8 #include "base/threading/thread.h" | 8 #include "base/threading/thread.h" |
| 9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
| 10 #include "chrome/browser/bookmarks/bookmark_model.h" | 10 #include "chrome/browser/bookmarks/bookmark_model.h" |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 53 | 53 |
| 54 PrefService* prefs = profile_->GetPrefs(); | 54 PrefService* prefs = profile_->GetPrefs(); |
| 55 // NOTE: We set the kHomePage value, but keep the NewTab page as the homepage. | 55 // NOTE: We set the kHomePage value, but keep the NewTab page as the homepage. |
| 56 const PrefService::Preference* pref = prefs->FindPreference(prefs::kHomePage); | 56 const PrefService::Preference* pref = prefs->FindPreference(prefs::kHomePage); |
| 57 if (pref && !pref->IsManaged()) { | 57 if (pref && !pref->IsManaged()) { |
| 58 prefs->SetString(prefs::kHomePage, home_page.spec()); | 58 prefs->SetString(prefs::kHomePage, home_page.spec()); |
| 59 prefs->ScheduleSavePersistentPrefs(); | 59 prefs->ScheduleSavePersistentPrefs(); |
| 60 } | 60 } |
| 61 } | 61 } |
| 62 | 62 |
| 63 void ProfileWriter::AddBookmarkEntry( | 63 void ProfileWriter::AddBookmarks(const std::vector<BookmarkEntry>& bookmarks, |
| 64 const std::vector<BookmarkEntry>& bookmark, | 64 const string16& first_folder_name, |
| 65 const string16& first_folder_name, | 65 int options) { |
| 66 int options) { | 66 if (bookmarks.empty()) |
| 67 return; | |
| 68 | |
| 67 BookmarkModel* model = profile_->GetBookmarkModel(); | 69 BookmarkModel* model = profile_->GetBookmarkModel(); |
| 68 DCHECK(model->IsLoaded()); | 70 DCHECK(model->IsLoaded()); |
| 69 | 71 |
| 70 bool import_to_bookmark_bar = ((options & IMPORT_TO_BOOKMARK_BAR) != 0); | 72 bool import_to_bookmark_bar = ((options & IMPORT_TO_BOOKMARK_BAR) != 0); |
| 71 string16 real_first_folder = import_to_bookmark_bar ? first_folder_name : | 73 string16 real_first_folder = import_to_bookmark_bar ? first_folder_name : |
| 72 GenerateUniqueFolderName(model, first_folder_name); | 74 GenerateUniqueFolderName(model, first_folder_name); |
| 73 | 75 |
| 74 bool show_bookmark_toolbar = false; | 76 bool show_bookmark_toolbar = false; |
| 75 std::set<const BookmarkNode*> folders_added_to; | 77 std::set<const BookmarkNode*> folders_added_to; |
| 76 bool import_mode = false; | 78 |
| 77 if (bookmark.size() > 1) { | 79 model->BeginImportMode(); |
| 78 model->BeginImportMode(); | 80 |
| 79 import_mode = true; | 81 for (std::vector<BookmarkEntry>::const_iterator it = bookmarks.begin(); |
| 80 } | 82 it != bookmarks.end(); ++it) { |
| 81 for (std::vector<BookmarkEntry>::const_iterator it = bookmark.begin(); | |
| 82 it != bookmark.end(); ++it) { | |
| 83 // Don't insert this url if it isn't valid. | 83 // Don't insert this url if it isn't valid. |
| 84 if (!it->is_folder && !it->url.is_valid()) | 84 if (!it->is_folder && !it->url.is_valid()) |
| 85 continue; | 85 continue; |
| 86 | 86 |
| 87 // We suppose that bookmarks are unique by Title, URL, and Folder. Since | 87 // We suppose that bookmarks are unique by Title, URL, and Folder. Since |
| 88 // checking for uniqueness may not be always the user's intention we have | 88 // checking for uniqueness may not be always the user's intention we have |
| 89 // this as an option. | 89 // this as an option. |
| 90 if (options & ADD_IF_UNIQUE && DoesBookmarkExist(model, *it, | 90 if (options & ADD_IF_UNIQUE && DoesBookmarkExist(model, *it, |
| 91 real_first_folder, import_to_bookmark_bar)) | 91 real_first_folder, import_to_bookmark_bar)) |
| 92 continue; | 92 continue; |
| 93 | 93 |
| 94 // Set up folders in BookmarkModel in such a way that path[i] is | 94 // Set up folders in BookmarkModel in such a way that path[i] is |
| 95 // the subfolder of path[i-1]. Finally they construct a path in the | 95 // the subfolder of path[i-1]. Finally they construct a path in the |
| 96 // model: | 96 // model: |
| 97 // path[0] \ path[1] \ ... \ path[size() - 1] | 97 // path[0] \ path[1] \ ... \ path[size() - 1] |
| 98 const BookmarkNode* parent = | 98 const BookmarkNode* parent = |
| 99 (it->in_toolbar ? model->GetBookmarkBarNode() : model->other_node()); | 99 (it->in_toolbar ? model->GetBookmarkBarNode() : model->other_node()); |
| 100 for (std::vector<string16>::const_iterator i = it->path.begin(); | 100 for (std::vector<string16>::const_iterator i = it->path.begin(); |
| 101 i != it->path.end(); ++i) { | 101 i != it->path.end(); ++i) { |
| 102 const BookmarkNode* child = NULL; | 102 const BookmarkNode* child = NULL; |
| 103 const string16& folder_name = (!import_to_bookmark_bar && | 103 const string16& folder_name = (!import_to_bookmark_bar && |
| 104 !it->in_toolbar && (i == it->path.begin())) ? real_first_folder : *i; | 104 !it->in_toolbar && (i == it->path.begin())) ? real_first_folder : *i; |
| 105 | 105 |
| 106 for (int index = 0; index < parent->child_count(); ++index) { | 106 for (int index = 0; index < parent->child_count(); ++index) { |
| 107 const BookmarkNode* node = parent->GetChild(index); | 107 const BookmarkNode* node = parent->GetChild(index); |
| 108 if ((node->type() == BookmarkNode::BOOKMARK_BAR || | 108 if (node->is_folder() && node->GetTitle() == folder_name) { |
| 109 node->type() == BookmarkNode::FOLDER) && | |
| 110 node->GetTitle() == folder_name) { | |
| 111 child = node; | 109 child = node; |
| 112 break; | 110 break; |
| 113 } | 111 } |
| 114 } | 112 } |
| 115 if (child == NULL) | 113 if (!child) |
| 116 child = model->AddFolder(parent, parent->child_count(), folder_name); | 114 child = model->AddFolder(parent, parent->child_count(), folder_name); |
| 117 parent = child; | 115 parent = child; |
| 118 } | 116 } |
| 117 | |
| 119 folders_added_to.insert(parent); | 118 folders_added_to.insert(parent); |
| 120 if (it->is_folder) { | 119 if (it->is_folder) { |
| 121 model->AddFolder(parent, parent->child_count(), it->title); | 120 model->AddFolder(parent, parent->child_count(), it->title); |
| 122 } else { | 121 } else { |
| 123 model->AddURLWithCreationTime(parent, parent->child_count(), | 122 model->AddURLWithCreationTime(parent, parent->child_count(), |
| 124 it->title, it->url, it->creation_time); | 123 it->title, it->url, it->creation_time); |
| 125 } | 124 } |
| 126 | 125 |
| 127 // If some items are put into toolbar, it looks like the user was using | 126 // If some items are put into toolbar, it looks like the user was using |
| 128 // it in their last browser. We turn on the bookmarks toolbar. | 127 // it in their last browser. We turn on the bookmarks toolbar. |
| 129 if (it->in_toolbar) | 128 if (it->in_toolbar) |
| 130 show_bookmark_toolbar = true; | 129 show_bookmark_toolbar = true; |
| 131 } | 130 } |
| 132 | 131 |
| 133 // Reset the date modified time of the folders we added to. We do this to | 132 // In order to avoid the imported-to folders from appearing in the 'recently |
|
Miranda Callahan
2011/05/21 11:34:21
nit: s/avoid/keep
Ilya Sherman
2011/05/24 06:01:13
Done.
| |
| 134 // make sure the 'recently added to' combobox in the bubble doesn't get random | 133 // added to' combobox, reset their modified times. |
| 135 // folders. | |
| 136 for (std::set<const BookmarkNode*>::const_iterator i = | 134 for (std::set<const BookmarkNode*>::const_iterator i = |
| 137 folders_added_to.begin(); | 135 folders_added_to.begin(); |
| 138 i != folders_added_to.end(); ++i) { | 136 i != folders_added_to.end(); ++i) { |
| 139 model->ResetDateFolderModified(*i); | 137 model->ResetDateFolderModified(*i); |
| 140 } | 138 } |
| 141 | 139 |
| 142 if (import_mode) { | 140 model->EndImportMode(); |
| 143 model->EndImportMode(); | |
| 144 } | |
| 145 | 141 |
| 146 if (show_bookmark_toolbar && !(options & BOOKMARK_BAR_DISABLED)) | 142 if (show_bookmark_toolbar && !(options & BOOKMARK_BAR_DISABLED)) |
| 147 ShowBookmarkBar(); | 143 ShowBookmarkBar(); |
| 148 } | 144 } |
| 149 | 145 |
| 150 void ProfileWriter::AddFavicons( | 146 void ProfileWriter::AddFavicons( |
| 151 const std::vector<history::ImportedFaviconUsage>& favicons) { | 147 const std::vector<history::ImportedFaviconUsage>& favicons) { |
| 152 profile_->GetFaviconService(Profile::EXPLICIT_ACCESS)-> | 148 profile_->GetFaviconService(Profile::EXPLICIT_ACCESS)-> |
| 153 SetImportedFavicons(favicons); | 149 SetImportedFavicons(favicons); |
| 154 } | 150 } |
| (...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 353 ((!import_to_bookmark_bar || !entry.in_toolbar) && | 349 ((!import_to_bookmark_bar || !entry.in_toolbar) && |
| 354 parent != model->other_node()))) { | 350 parent != model->other_node()))) { |
| 355 found_match = false; | 351 found_match = false; |
| 356 } | 352 } |
| 357 | 353 |
| 358 if (found_match) | 354 if (found_match) |
| 359 return true; // Found a match with the same url path and title. | 355 return true; // Found a match with the same url path and title. |
| 360 } | 356 } |
| 361 return false; | 357 return false; |
| 362 } | 358 } |
| OLD | NEW |