diff options
author | sky@google.com <sky@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-11-07 04:31:35 +0000 |
---|---|---|
committer | sky@google.com <sky@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-11-07 04:31:35 +0000 |
commit | b504899244b4264994d4daae2bee660706dba652 (patch) | |
tree | eda190cb35dfff7f700a4c636730ff2487cb35d8 /chrome/browser/importer/firefox2_importer.cc | |
parent | e562de106eeab2667eeb6922ddf2d771a0efa55d (diff) | |
download | chromium_src-b504899244b4264994d4daae2bee660706dba652.zip chromium_src-b504899244b4264994d4daae2bee660706dba652.tar.gz chromium_src-b504899244b4264994d4daae2bee660706dba652.tar.bz2 |
Adds import/export of bookmarks to bookmarks.html file.
BUG=1649
TEST=bring up bookmark manager and try out import/export from the
tools menu. Note that import ALWAYS creates a new folder under
the 'Other bookmarks folder' with the name of Imported (x). This
is by design.
Review URL: http://codereview.chromium.org/9471
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@4968 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/importer/firefox2_importer.cc')
-rw-r--r-- | chrome/browser/importer/firefox2_importer.cc | 96 |
1 files changed, 64 insertions, 32 deletions
diff --git a/chrome/browser/importer/firefox2_importer.cc b/chrome/browser/importer/firefox2_importer.cc index e85d347..aef2ba2 100644 --- a/chrome/browser/importer/firefox2_importer.cc +++ b/chrome/browser/importer/firefox2_importer.cc @@ -22,7 +22,7 @@ using base::Time; // Firefox2Importer. -Firefox2Importer::Firefox2Importer() { +Firefox2Importer::Firefox2Importer() : parsing_bookmarks_html_file_(false) { } Firefox2Importer::~Firefox2Importer() { @@ -37,6 +37,8 @@ void Firefox2Importer::StartImport(ProfileInfo profile_info, app_path_ = profile_info.app_path; importer_host_ = host; + parsing_bookmarks_html_file_ = (profile_info.browser_type == BOOKMARKS_HTML); + // The order here is important! NotifyStarted(); if ((items & HOME_PAGE) && !cancelled()) @@ -120,30 +122,29 @@ TemplateURL* Firefox2Importer::CreateTemplateURL(const std::wstring& title, return t_url; } -void Firefox2Importer::ImportBookmarks() { - // Read the whole file. - std::wstring file = source_path_; - file_util::AppendToPath(&file, L"bookmarks.html"); +// static +void Firefox2Importer::ImportBookmarksFile( + const std::wstring& file_path, + const std::set<GURL>& default_urls, + bool first_run, + const std::wstring& first_folder_name, + Importer* importer, + std::vector<ProfileWriter::BookmarkEntry>* bookmarks, + std::vector<TemplateURL*>* template_urls, + std::vector<history::ImportedFavIconUsage>* favicons) { std::string content; - file_util::ReadFileToString(file, &content); + file_util::ReadFileToString(file_path, &content); std::vector<std::string> lines; SplitString(content, '\n', &lines); - - // Load the default bookmarks. - std::set<GURL> default_urls; - LoadDefaultBookmarks(app_path_, &default_urls); - - // Parse the bookmarks.html file. - std::vector<ProfileWriter::BookmarkEntry> bookmarks, toolbar_bookmarks; - std::vector<TemplateURL*> template_urls; - std::vector<history::ImportedFavIconUsage> favicons; - std::wstring last_folder - = l10n_util::GetString(IDS_BOOKMARK_GROUP_FROM_FIREFOX); + + std::vector<ProfileWriter::BookmarkEntry> toolbar_bookmarks; + std::wstring last_folder = first_folder_name; bool last_folder_on_toolbar = false; std::vector<std::wstring> path; size_t toolbar_folder = 0; std::string charset; - for (size_t i = 0; i < lines.size() && !cancelled(); ++i) { + for (size_t i = 0; i < lines.size() && (!importer || !importer->cancelled()); + ++i) { std::string line; TrimString(lines[i], " ", &line); @@ -179,7 +180,7 @@ void Firefox2Importer::ImportBookmarks() { entry.url = url; entry.title = title; - if (first_run() && toolbar_folder) { + if (first_run && toolbar_folder) { // Flatten the items in toolbar. entry.in_toolbar = true; entry.path.assign(path.begin() + toolbar_folder, path.end()); @@ -188,20 +189,23 @@ void Firefox2Importer::ImportBookmarks() { // Insert the item into the "Imported from Firefox" folder after // the first run. entry.path.assign(path.begin(), path.end()); - if (first_run()) + if (first_run) entry.path.erase(entry.path.begin()); - bookmarks.push_back(entry); + bookmarks->push_back(entry); } // Save the favicon. DataURLToFaviconUsage will handle the case where // there is no favicon. - DataURLToFaviconUsage(url, favicon, &favicons); - - // If there is a SHORTCUT attribute for this bookmark, we - // add it as our keywords. - TemplateURL* t_url = CreateTemplateURL(title, shortcut, url); - if (t_url) - template_urls.push_back(t_url); + if (favicons) + DataURLToFaviconUsage(url, favicon, favicons); + + if (template_urls) { + // If there is a SHORTCUT attribute for this bookmark, we + // add it as our keywords. + TemplateURL* t_url = CreateTemplateURL(title, shortcut, url); + if (t_url) + template_urls->push_back(t_url); + } continue; } @@ -221,14 +225,42 @@ void Firefox2Importer::ImportBookmarks() { } } + bookmarks->insert(bookmarks->begin(), toolbar_bookmarks.begin(), + toolbar_bookmarks.end()); +} + +void Firefox2Importer::ImportBookmarks() { + // Load the default bookmarks. + std::set<GURL> default_urls; + if (!parsing_bookmarks_html_file_) + LoadDefaultBookmarks(app_path_, &default_urls); + + // Parse the bookmarks.html file. + std::vector<ProfileWriter::BookmarkEntry> bookmarks, toolbar_bookmarks; + std::vector<TemplateURL*> template_urls; + std::vector<history::ImportedFavIconUsage> favicons; + std::wstring file = source_path_; + if (!parsing_bookmarks_html_file_) + file_util::AppendToPath(&file, L"bookmarks.html"); + std::wstring first_folder_name; + if (parsing_bookmarks_html_file_) + first_folder_name = l10n_util::GetString(IDS_BOOKMARK_GROUP); + else + first_folder_name = l10n_util::GetString(IDS_BOOKMARK_GROUP_FROM_FIREFOX); + + ImportBookmarksFile(file, default_urls, first_run(), + first_folder_name, this, &bookmarks, &template_urls, + &favicons); + // Write data into profile. - bookmarks.insert(bookmarks.begin(), toolbar_bookmarks.begin(), - toolbar_bookmarks.end()); if (!bookmarks.empty() && !cancelled()) { main_loop_->PostTask(FROM_HERE, NewRunnableMethod(writer_, - &ProfileWriter::AddBookmarkEntry, bookmarks, false)); + &ProfileWriter::AddBookmarkEntry, bookmarks, + first_folder_name, + first_run() ? ProfileWriter::FIRST_RUN : 0)); } - if (!template_urls.empty() && !cancelled()) { + if (!parsing_bookmarks_html_file_ && !template_urls.empty() && + !cancelled()) { main_loop_->PostTask(FROM_HERE, NewRunnableMethod(writer_, &ProfileWriter::AddKeywords, template_urls, -1, false)); } else { |