2007-03-08 09:29:19 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2006-07-18 16:38:51 +00:00
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is Mozilla History System
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Google Inc.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2005
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Brett Wilson <brettw@gmail.com>
|
2007-05-03 08:56:41 +00:00
|
|
|
* Dietrich Ayala <dietrich@mozilla.com>
|
2009-04-17 12:42:34 +00:00
|
|
|
* Drew Willcoxon <adw@mozilla.com>
|
2010-01-15 14:04:02 +00:00
|
|
|
* Marco Bonardo <mak77@bonardo.net>
|
2006-07-18 16:38:51 +00:00
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Importer/exporter between the mozStorage-based bookmarks and the old-style
|
|
|
|
* "bookmarks.html"
|
|
|
|
*
|
|
|
|
* Format:
|
|
|
|
*
|
|
|
|
* Primary heading := h1
|
|
|
|
* Old version used this to set attributes on the bookmarks RDF root, such
|
|
|
|
* as the last modified date. We only use H1 to check for the attribute
|
|
|
|
* PLACES_ROOT, which tells us that this hierarchy root is the places root.
|
|
|
|
* For backwards compatability, if we don't find this, we assume that the
|
|
|
|
* hierarchy is rooted at the bookmarks menu.
|
|
|
|
* Heading := any heading other than h1
|
|
|
|
* Old version used this to set attributes on the current container. We only
|
|
|
|
* care about the content of the heading container, which contains the title
|
|
|
|
* of the bookmark container.
|
|
|
|
* Bookmark := a
|
|
|
|
* HREF is the destination of the bookmark
|
2006-07-18 16:41:11 +00:00
|
|
|
* FEEDURL is the URI of the RSS feed if this is a livemark.
|
2007-07-09 02:22:50 +00:00
|
|
|
* LAST_CHARSET is stored as an annotation so that the next time we go to
|
|
|
|
* that page we remember the user's preference.
|
2007-03-08 09:29:19 +00:00
|
|
|
* WEB_PANEL is set to "true" if the bookmark should be loaded in the sidebar.
|
2006-07-18 16:40:02 +00:00
|
|
|
* ICON will be stored in the favicon service
|
2006-07-18 16:46:13 +00:00
|
|
|
* ICON_URI is new for places bookmarks.html, it refers to the original
|
|
|
|
* URI of the favicon so we don't have to make up favicon URLs.
|
2006-07-18 16:38:51 +00:00
|
|
|
* Text of the <a> container is the name of the bookmark
|
2007-05-19 00:40:55 +00:00
|
|
|
* Ignored: LAST_VISIT, ID (writing out non-RDF IDs can confuse Firefox 2)
|
2006-07-18 16:38:51 +00:00
|
|
|
* Bookmark comment := dd
|
|
|
|
* This affects the previosly added bookmark
|
|
|
|
* Separator := hr
|
2006-07-18 16:45:49 +00:00
|
|
|
* Insert a separator into the current container
|
2006-07-18 16:38:51 +00:00
|
|
|
* The folder hierarchy is defined by <dl>/<ul>/<menu> (the old importing code
|
|
|
|
* handles all these cases, when we write, use <dl>).
|
2006-07-18 16:40:02 +00:00
|
|
|
*
|
|
|
|
* Overall design
|
|
|
|
* --------------
|
|
|
|
*
|
|
|
|
* We need to emulate a recursive parser. A "Bookmark import frame" is created
|
|
|
|
* corresponding to each folder we encounter. These are arranged in a stack,
|
|
|
|
* and contain all the state we need to keep track of.
|
|
|
|
*
|
|
|
|
* A frame is created when we find a heading, which defines a new container.
|
|
|
|
* The frame also keeps track of the nesting of <DL>s, (in well-formed
|
|
|
|
* bookmarks files, these will have a 1-1 correspondence with frames, but we
|
|
|
|
* try to be a little more flexible here). When the nesting count decreases
|
|
|
|
* to 0, then we know a frame is complete and to pop back to the previous
|
|
|
|
* frame.
|
|
|
|
*
|
|
|
|
* Note that a lot of things happen when tags are CLOSED because we need to
|
|
|
|
* get the text from the content of the tag. For example, link and heading tags
|
|
|
|
* both require the content (= title) before actually creating it.
|
2006-07-18 16:38:51 +00:00
|
|
|
*/
|
|
|
|
|
2007-05-03 08:56:41 +00:00
|
|
|
#include "nsPlacesImportExportService.h"
|
2006-07-18 16:38:51 +00:00
|
|
|
#include "nsNetUtil.h"
|
|
|
|
#include "nsParserCIID.h"
|
2007-05-03 08:56:41 +00:00
|
|
|
#include "nsStringAPI.h"
|
2006-07-18 16:38:51 +00:00
|
|
|
#include "nsUnicharUtils.h"
|
2007-05-03 08:56:41 +00:00
|
|
|
#include "nsAppDirectoryServiceDefs.h"
|
|
|
|
#include "nsDirectoryServiceUtils.h"
|
|
|
|
#include "nsIPrefService.h"
|
|
|
|
#include "nsToolkitCompsCID.h"
|
|
|
|
#include "nsIHTMLContentSink.h"
|
|
|
|
#include "nsIParser.h"
|
2007-06-26 02:38:34 +00:00
|
|
|
#include "prprf.h"
|
2007-11-27 00:08:50 +00:00
|
|
|
#include "nsIBrowserGlue.h"
|
2009-04-17 12:42:34 +00:00
|
|
|
#include "nsIObserverService.h"
|
|
|
|
#include "nsISupportsPrimitives.h"
|
2006-07-18 16:38:51 +00:00
|
|
|
|
|
|
|
static NS_DEFINE_CID(kParserCID, NS_PARSER_CID);
|
|
|
|
|
|
|
|
#define KEY_TOOLBARFOLDER_LOWER "personal_toolbar_folder"
|
2007-06-26 02:38:34 +00:00
|
|
|
#define KEY_BOOKMARKSMENU_LOWER "bookmarks_menu"
|
2008-04-01 18:26:09 +00:00
|
|
|
#define KEY_UNFILEDFOLDER_LOWER "unfiled_bookmarks_folder"
|
2006-07-18 16:38:51 +00:00
|
|
|
#define KEY_PLACESROOT_LOWER "places_root"
|
|
|
|
#define KEY_HREF_LOWER "href"
|
2006-07-18 16:41:11 +00:00
|
|
|
#define KEY_FEEDURL_LOWER "feedurl"
|
2007-03-08 09:29:19 +00:00
|
|
|
#define KEY_WEB_PANEL_LOWER "web_panel"
|
2006-07-18 16:38:51 +00:00
|
|
|
#define KEY_LASTCHARSET_LOWER "last_charset"
|
|
|
|
#define KEY_ICON_LOWER "icon"
|
2006-07-18 16:46:13 +00:00
|
|
|
#define KEY_ICON_URI_LOWER "icon_uri"
|
2006-07-18 16:43:53 +00:00
|
|
|
#define KEY_SHORTCUTURL_LOWER "shortcuturl"
|
2007-05-15 03:59:38 +00:00
|
|
|
#define KEY_POST_DATA_LOWER "post_data"
|
2007-05-15 04:18:25 +00:00
|
|
|
#define KEY_NAME_LOWER "name"
|
2007-05-15 23:23:51 +00:00
|
|
|
#define KEY_MICSUM_GEN_URI_LOWER "micsum_gen_uri"
|
2007-05-19 00:40:55 +00:00
|
|
|
#define KEY_DATE_ADDED_LOWER "add_date"
|
|
|
|
#define KEY_LAST_MODIFIED_LOWER "last_modified"
|
2007-10-30 16:02:23 +00:00
|
|
|
#define KEY_GENERATED_TITLE_LOWER "generated_title"
|
2006-07-18 16:38:51 +00:00
|
|
|
|
2007-03-08 09:29:19 +00:00
|
|
|
#define LOAD_IN_SIDEBAR_ANNO NS_LITERAL_CSTRING("bookmarkProperties/loadInSidebar")
|
2007-05-11 19:26:14 +00:00
|
|
|
#define DESCRIPTION_ANNO NS_LITERAL_CSTRING("bookmarkProperties/description")
|
2007-12-05 01:56:55 +00:00
|
|
|
#define POST_DATA_ANNO NS_LITERAL_CSTRING("bookmarkProperties/POSTData")
|
2007-10-30 16:02:23 +00:00
|
|
|
#define STATIC_TITLE_ANNO NS_LITERAL_CSTRING("bookmarks/staticTitle")
|
2007-03-08 09:29:19 +00:00
|
|
|
|
2007-06-26 02:38:34 +00:00
|
|
|
#define BOOKMARKS_MENU_ICON_URI "chrome://browser/skin/places/bookmarksMenu.png"
|
2006-07-18 16:41:57 +00:00
|
|
|
|
2009-04-17 12:42:34 +00:00
|
|
|
// The RESTORE_*_NSIOBSERVER_TOPIC #defines should match the constants of the
|
|
|
|
// same names in toolkit/components/places/src/utils.js
|
|
|
|
#define RESTORE_BEGIN_NSIOBSERVER_TOPIC "bookmarks-restore-begin"
|
|
|
|
#define RESTORE_SUCCESS_NSIOBSERVER_TOPIC "bookmarks-restore-success"
|
|
|
|
#define RESTORE_FAILED_NSIOBSERVER_TOPIC "bookmarks-restore-failed"
|
|
|
|
#define RESTORE_NSIOBSERVER_DATA NS_LITERAL_STRING("html")
|
|
|
|
#define RESTORE_INITIAL_NSIOBSERVER_DATA NS_LITERAL_STRING("html-initial")
|
|
|
|
|
2010-01-15 14:04:02 +00:00
|
|
|
// Maximum number of backups to retain.
|
|
|
|
#define BROWSER_BOOKMARKS_MAX_BACKUPS_PREF "browser.bookmarks.max_backups"
|
|
|
|
|
2007-05-17 06:06:48 +00:00
|
|
|
// define to get debugging messages on console about import/export
|
2006-07-18 16:46:13 +00:00
|
|
|
//#define DEBUG_IMPORT
|
2007-05-17 06:06:48 +00:00
|
|
|
//#define DEBUG_EXPORT
|
2006-07-18 16:46:13 +00:00
|
|
|
|
2010-01-15 14:57:50 +00:00
|
|
|
#if defined(XP_WIN) || defined(XP_OS2)
|
|
|
|
#define NS_LINEBREAK "\015\012"
|
|
|
|
#else
|
|
|
|
#define NS_LINEBREAK "\012"
|
|
|
|
#endif
|
|
|
|
|
2007-05-03 08:56:41 +00:00
|
|
|
class nsIOutputStream;
|
|
|
|
static const char kWhitespace[] = " \r\n\t\b";
|
|
|
|
static nsresult WriteEscapedUrl(const nsCString &aString, nsIOutputStream* aOutput);
|
2006-07-18 16:47:23 +00:00
|
|
|
|
2006-07-18 16:38:51 +00:00
|
|
|
class BookmarkImportFrame
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
BookmarkImportFrame(PRInt64 aID) :
|
|
|
|
mContainerID(aID),
|
|
|
|
mContainerNesting(0),
|
|
|
|
mLastContainerType(Container_Normal),
|
2007-05-19 00:40:55 +00:00
|
|
|
mInDescription(PR_FALSE),
|
|
|
|
mPreviousId(0),
|
|
|
|
mPreviousDateAdded(0),
|
|
|
|
mPreviousLastModifiedDate(0)
|
2006-07-18 16:38:51 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
enum ContainerType { Container_Normal,
|
2006-07-18 16:46:13 +00:00
|
|
|
Container_Places,
|
2006-07-18 16:38:51 +00:00
|
|
|
Container_Menu,
|
2008-04-01 18:26:09 +00:00
|
|
|
Container_Toolbar,
|
|
|
|
Container_Unfiled};
|
2006-07-18 16:38:51 +00:00
|
|
|
|
|
|
|
PRInt64 mContainerID;
|
|
|
|
|
|
|
|
// How many <dl>s have been nested. Each frame/container should start
|
|
|
|
// with a heading, and is then followed by a <dl>, <ul>, or <menu>. When
|
|
|
|
// that list is complete, then it is the end of this container and we need
|
|
|
|
// to pop back up one level for new items. If we never get an open tag for
|
|
|
|
// one of these things, we should assume that the container is empty and
|
|
|
|
// that things we find should be siblings of it. Normally, these <dl>s won't
|
|
|
|
// be nested so this will be 0 or 1.
|
|
|
|
PRInt32 mContainerNesting;
|
|
|
|
|
|
|
|
// when we find a heading tag, it actually affects the title of the NEXT
|
|
|
|
// container in the list. This stores that heading tag and whether it was
|
|
|
|
// special. 'ConsumeHeading' resets this.
|
|
|
|
ContainerType mLastContainerType;
|
|
|
|
|
|
|
|
// this contains the text from the last begin tag until now. It is reset
|
|
|
|
// at every begin tag. We can check it when we see a </a>, or </h3>
|
|
|
|
// to see what the text content of that node should be.
|
|
|
|
nsString mPreviousText;
|
|
|
|
|
|
|
|
// true when we hit a <dd>, which contains the description for the preceeding
|
|
|
|
// <a> tag. We can't just check for </dd> like we can for </a> or </h3>
|
|
|
|
// because if there is a sub-folder, it is actually a child of the <dd>
|
|
|
|
// because the tag is never explicitly closed. If this is true and we see a
|
|
|
|
// new open tag, that means to commit the description to the previous
|
|
|
|
// bookmark.
|
|
|
|
//
|
|
|
|
// Additional weirdness happens when the previous <dt> tag contains a <h3>:
|
|
|
|
// this means there is a new folder with the given description, and whose
|
|
|
|
// children are contained in the following <dl> list.
|
|
|
|
//
|
|
|
|
// This is handled in OpenContainer(), which commits previous text if
|
|
|
|
// necessary.
|
|
|
|
PRBool mInDescription;
|
|
|
|
|
|
|
|
// contains the URL of the previous bookmark created. This is used so that
|
|
|
|
// when we encounter a <dd>, we know what bookmark to associate the text with.
|
|
|
|
// This is cleared whenever we hit a <h3>, so that we know NOT to save this
|
|
|
|
// with a bookmark, but to keep it until
|
|
|
|
nsCOMPtr<nsIURI> mPreviousLink;
|
|
|
|
|
2006-07-18 16:41:11 +00:00
|
|
|
// contains the URL of the previous livemark, so that when the link ends,
|
|
|
|
// and the livemark title is known, we can create it.
|
|
|
|
nsCOMPtr<nsIURI> mPreviousFeed;
|
2006-07-18 16:38:51 +00:00
|
|
|
|
2007-10-30 16:02:23 +00:00
|
|
|
// contains the text content of the previous microsummary, so that when the
|
|
|
|
// link ends, we can replace the bookmark's title with it and store the user's
|
|
|
|
// title in the staticTitle annotation.
|
|
|
|
nsString mPreviousMicrosummaryText;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIMicrosummary> mPreviousMicrosummary;
|
|
|
|
|
2007-06-26 02:38:34 +00:00
|
|
|
void ConsumeHeading(nsAString* aHeading, ContainerType* aContainerType)
|
2006-07-18 16:38:51 +00:00
|
|
|
{
|
|
|
|
*aHeading = mPreviousText;
|
|
|
|
*aContainerType = mLastContainerType;
|
2007-05-03 08:56:41 +00:00
|
|
|
mPreviousText.Truncate();
|
2006-07-18 16:38:51 +00:00
|
|
|
}
|
2007-02-26 00:54:36 +00:00
|
|
|
|
2007-03-31 23:34:23 +00:00
|
|
|
// Contains the id of an imported, or newly created bookmark.
|
2007-02-26 00:54:36 +00:00
|
|
|
PRInt64 mPreviousId;
|
2007-05-19 00:40:55 +00:00
|
|
|
|
|
|
|
// Contains the date-added and last-modified-date of an imported item.
|
2007-05-23 01:06:31 +00:00
|
|
|
// Used to override the values set by insertBookmark, createFolder, etc.
|
2007-05-19 00:40:55 +00:00
|
|
|
PRTime mPreviousDateAdded;
|
|
|
|
PRTime mPreviousLastModifiedDate;
|
2006-07-18 16:38:51 +00:00
|
|
|
};
|
|
|
|
|
2007-05-03 08:56:41 +00:00
|
|
|
/**
|
2010-01-15 14:04:02 +00:00
|
|
|
* Copied from nsEscape.cpp, which requires internal string API.
|
2007-05-03 08:56:41 +00:00
|
|
|
*/
|
2010-01-15 14:04:02 +00:00
|
|
|
char*
|
|
|
|
nsEscapeHTML(const char* string)
|
2007-05-03 08:56:41 +00:00
|
|
|
{
|
2010-01-15 14:04:02 +00:00
|
|
|
/* XXX Hardcoded max entity len. The +1 is for the trailing null. */
|
|
|
|
char* escaped = nsnull;
|
|
|
|
PRUint32 len = strlen(string);
|
|
|
|
if (len >= (PR_UINT32_MAX / 6))
|
|
|
|
return nsnull;
|
|
|
|
|
|
|
|
escaped = (char*)NS_Alloc((len * 6) + 1);
|
|
|
|
if (escaped) {
|
|
|
|
char* ptr = escaped;
|
|
|
|
for (; *string != '\0'; string++) {
|
|
|
|
switch(*string) {
|
|
|
|
case '<':
|
|
|
|
*ptr++ = '&';
|
|
|
|
*ptr++ = 'l';
|
|
|
|
*ptr++ = 't';
|
|
|
|
*ptr++ = ';';
|
|
|
|
break;
|
|
|
|
case '>':
|
|
|
|
*ptr++ = '&';
|
|
|
|
*ptr++ = 'g';
|
|
|
|
*ptr++ = 't';
|
|
|
|
*ptr++ = ';';
|
|
|
|
break;
|
|
|
|
case '&':
|
|
|
|
*ptr++ = '&';
|
|
|
|
*ptr++ = 'a';
|
|
|
|
*ptr++ = 'm';
|
|
|
|
*ptr++ = 'p';
|
|
|
|
*ptr++ = ';';
|
|
|
|
break;
|
|
|
|
case '"':
|
|
|
|
*ptr++ = '&';
|
|
|
|
*ptr++ = 'q';
|
|
|
|
*ptr++ = 'u';
|
|
|
|
*ptr++ = 'o';
|
|
|
|
*ptr++ = 't';
|
|
|
|
*ptr++ = ';';
|
|
|
|
break;
|
|
|
|
case '\'':
|
|
|
|
*ptr++ = '&';
|
|
|
|
*ptr++ = '#';
|
|
|
|
*ptr++ = '3';
|
|
|
|
*ptr++ = '9';
|
|
|
|
*ptr++ = ';';
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
*ptr++ = *string;
|
2008-10-24 23:15:33 +00:00
|
|
|
}
|
2010-01-15 14:04:02 +00:00
|
|
|
*ptr = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return escaped;
|
2007-05-03 08:56:41 +00:00
|
|
|
}
|
|
|
|
|
2010-01-15 14:04:02 +00:00
|
|
|
|
2007-06-01 00:44:43 +00:00
|
|
|
NS_IMPL_ISUPPORTS2(nsPlacesImportExportService, nsIPlacesImportExportService,
|
|
|
|
nsINavHistoryBatchCallback)
|
2007-05-03 08:56:41 +00:00
|
|
|
|
2010-01-15 14:04:02 +00:00
|
|
|
|
2007-05-03 08:56:41 +00:00
|
|
|
nsPlacesImportExportService::nsPlacesImportExportService()
|
|
|
|
{
|
2010-01-15 14:04:02 +00:00
|
|
|
// Be sure to call EnsureServiceState() before using services.
|
|
|
|
mHistoryService = do_GetService(NS_NAVHISTORYSERVICE_CONTRACTID);
|
|
|
|
NS_WARN_IF_FALSE(mHistoryService, "could not get history service");
|
|
|
|
mFaviconService = do_GetService(NS_FAVICONSERVICE_CONTRACTID);
|
|
|
|
NS_WARN_IF_FALSE(mFaviconService, "could not get favicon service");
|
|
|
|
mAnnotationService = do_GetService(NS_ANNOTATIONSERVICE_CONTRACTID);
|
|
|
|
NS_WARN_IF_FALSE(mAnnotationService, "could not get annotation service");
|
|
|
|
mBookmarksService = do_GetService(NS_NAVBOOKMARKSSERVICE_CONTRACTID);
|
|
|
|
NS_WARN_IF_FALSE(mBookmarksService, "could not get bookmarks service");
|
|
|
|
mLivemarkService = do_GetService(NS_LIVEMARKSERVICE_CONTRACTID);
|
|
|
|
NS_WARN_IF_FALSE(mLivemarkService, "could not get livemark service");
|
|
|
|
mMicrosummaryService = do_GetService("@mozilla.org/microsummary/service;1");
|
|
|
|
NS_WARN_IF_FALSE(mMicrosummaryService, "could not get microsummary service");
|
2007-05-03 08:56:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsPlacesImportExportService::~nsPlacesImportExportService()
|
|
|
|
{
|
|
|
|
}
|
2006-07-18 16:38:51 +00:00
|
|
|
|
2010-01-15 14:04:02 +00:00
|
|
|
|
2006-07-18 16:38:51 +00:00
|
|
|
/**
|
2010-01-15 14:04:02 +00:00
|
|
|
* The content sink stuff is based loosely on nsIHTMLContentSink.
|
2006-07-18 16:38:51 +00:00
|
|
|
*/
|
|
|
|
class BookmarkContentSink : public nsIHTMLContentSink
|
|
|
|
{
|
|
|
|
public:
|
2008-03-05 10:31:55 +00:00
|
|
|
BookmarkContentSink();
|
|
|
|
|
2006-07-18 16:38:51 +00:00
|
|
|
nsresult Init(PRBool aAllowRootChanges,
|
2006-12-14 17:28:52 +00:00
|
|
|
PRInt64 aFolder,
|
|
|
|
PRBool aIsImportDefaults);
|
2006-07-18 16:38:51 +00:00
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
// nsIContentSink (superclass of nsIHTMLContentSink)
|
2008-10-30 21:31:00 +00:00
|
|
|
NS_IMETHOD WillParse() { return NS_OK; }
|
2006-07-18 16:38:51 +00:00
|
|
|
NS_IMETHOD WillInterrupt() { return NS_OK; }
|
|
|
|
NS_IMETHOD WillResume() { return NS_OK; }
|
|
|
|
NS_IMETHOD SetParser(nsIParser* aParser) { return NS_OK; }
|
|
|
|
virtual void FlushPendingNotifications(mozFlushType aType) { }
|
|
|
|
NS_IMETHOD SetDocumentCharset(nsACString& aCharset) { return NS_OK; }
|
|
|
|
virtual nsISupports *GetTarget() { return nsnull; }
|
|
|
|
|
|
|
|
// nsIHTMLContentSink
|
|
|
|
NS_IMETHOD OpenHead() { return NS_OK; }
|
|
|
|
NS_IMETHOD BeginContext(PRInt32 aPosition) { return NS_OK; }
|
|
|
|
NS_IMETHOD EndContext(PRInt32 aPosition) { return NS_OK; }
|
|
|
|
NS_IMETHOD IsEnabled(PRInt32 aTag, PRBool* aReturn)
|
|
|
|
{ *aReturn = PR_TRUE; return NS_OK; }
|
|
|
|
NS_IMETHOD DidProcessTokens() { return NS_OK; }
|
|
|
|
NS_IMETHOD WillProcessAToken() { return NS_OK; }
|
|
|
|
NS_IMETHOD DidProcessAToken() { return NS_OK; }
|
|
|
|
NS_IMETHOD OpenContainer(const nsIParserNode& aNode);
|
|
|
|
NS_IMETHOD CloseContainer(const nsHTMLTag aTag);
|
|
|
|
NS_IMETHOD AddLeaf(const nsIParserNode& aNode);
|
|
|
|
NS_IMETHOD AddComment(const nsIParserNode& aNode) { return NS_OK; }
|
|
|
|
NS_IMETHOD AddProcessingInstruction(const nsIParserNode& aNode) { return NS_OK; }
|
|
|
|
NS_IMETHOD AddDocTypeDecl(const nsIParserNode& aNode) { return NS_OK; }
|
|
|
|
NS_IMETHOD NotifyTagObservers(nsIParserNode* aNode) { return NS_OK; }
|
|
|
|
NS_IMETHOD_(PRBool) IsFormOnStack() { return PR_FALSE; }
|
|
|
|
|
|
|
|
protected:
|
|
|
|
nsCOMPtr<nsINavBookmarksService> mBookmarksService;
|
2006-07-18 16:40:48 +00:00
|
|
|
nsCOMPtr<nsINavHistoryService> mHistoryService;
|
2006-07-18 16:38:51 +00:00
|
|
|
nsCOMPtr<nsIAnnotationService> mAnnotationService;
|
2006-07-18 16:41:11 +00:00
|
|
|
nsCOMPtr<nsILivemarkService> mLivemarkService;
|
2007-05-15 23:23:51 +00:00
|
|
|
nsCOMPtr<nsIMicrosummaryService> mMicrosummaryService;
|
2006-07-18 16:38:51 +00:00
|
|
|
|
2007-03-07 21:22:54 +00:00
|
|
|
// If set, we will move root items to from their existing position
|
|
|
|
// in the hierarchy, to where we find them in the bookmarks file
|
|
|
|
// being imported. This should be set when we are loading
|
|
|
|
// the default places html file, and should be unset when doing
|
2007-06-26 02:38:34 +00:00
|
|
|
// normal imports so that root folders will not get moved when
|
2007-03-07 21:22:54 +00:00
|
|
|
// importing bookmarks.html files.
|
2006-07-18 16:38:51 +00:00
|
|
|
PRBool mAllowRootChanges;
|
|
|
|
|
2008-03-13 19:25:49 +00:00
|
|
|
// If set, this is an import of initial bookmarks.html content,
|
2006-12-14 17:28:52 +00:00
|
|
|
// so we don't want to kick off HTTP traffic
|
2007-06-26 02:38:34 +00:00
|
|
|
// and we want the imported personal toolbar folder
|
2008-03-13 19:25:49 +00:00
|
|
|
// to be set as the personal toolbar folder. (If not set
|
2007-06-26 02:38:34 +00:00
|
|
|
// we will treat it as a normal folder.)
|
2006-12-14 17:28:52 +00:00
|
|
|
PRBool mIsImportDefaults;
|
|
|
|
|
2006-07-18 16:42:20 +00:00
|
|
|
// If a folder was specified to import into, then ignore flags to put
|
|
|
|
// bookmarks in the bookmarks menu or toolbar and keep them inside
|
|
|
|
// the folder.
|
|
|
|
PRBool mFolderSpecified;
|
|
|
|
|
2006-07-18 16:38:51 +00:00
|
|
|
void HandleContainerBegin(const nsIParserNode& node);
|
|
|
|
void HandleContainerEnd();
|
|
|
|
void HandleHead1Begin(const nsIParserNode& node);
|
|
|
|
void HandleHeadBegin(const nsIParserNode& node);
|
|
|
|
void HandleHeadEnd();
|
|
|
|
void HandleLinkBegin(const nsIParserNode& node);
|
|
|
|
void HandleLinkEnd();
|
2007-05-15 04:18:25 +00:00
|
|
|
void HandleSeparator(const nsIParserNode& node);
|
2006-07-18 16:38:51 +00:00
|
|
|
|
|
|
|
// This is a list of frames. We really want a recursive parser, but the HTML
|
|
|
|
// parser gives us tags as a stream. This implements all the state on a stack
|
|
|
|
// so we can get the recursive information we need. Use "CurFrame" to get the
|
|
|
|
// top "stack frame" with the current state in it.
|
|
|
|
nsTArray<BookmarkImportFrame> mFrames;
|
|
|
|
BookmarkImportFrame& CurFrame()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mFrames.Length() > 0, "Asking for frame when there are none!");
|
|
|
|
return mFrames[mFrames.Length() - 1];
|
|
|
|
}
|
2007-05-19 00:40:55 +00:00
|
|
|
BookmarkImportFrame& PreviousFrame()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mFrames.Length() > 1, "Asking for frame when there are not enough!");
|
|
|
|
return mFrames[mFrames.Length() - 2];
|
|
|
|
}
|
2006-07-18 16:38:51 +00:00
|
|
|
nsresult NewFrame();
|
|
|
|
nsresult PopFrame();
|
2006-07-18 16:39:39 +00:00
|
|
|
|
2006-07-18 16:46:13 +00:00
|
|
|
nsresult SetFaviconForURI(nsIURI* aPageURI, nsIURI* aFaviconURI,
|
2008-07-03 18:07:56 +00:00
|
|
|
const nsString& aData);
|
2006-07-18 16:46:13 +00:00
|
|
|
|
2007-05-19 00:40:55 +00:00
|
|
|
PRTime ConvertImportedDateToInternalDate(const nsACString& aDate);
|
2007-03-31 23:34:23 +00:00
|
|
|
|
2006-07-18 16:46:13 +00:00
|
|
|
#ifdef DEBUG_IMPORT
|
|
|
|
// prints spaces for indenting to the current frame depth
|
|
|
|
void PrintNesting()
|
|
|
|
{
|
|
|
|
for (PRUint32 i = 0; i < mFrames.Length(); i ++)
|
|
|
|
printf(" ");
|
|
|
|
}
|
|
|
|
#endif
|
2006-07-18 16:38:51 +00:00
|
|
|
};
|
|
|
|
|
2010-01-15 14:04:02 +00:00
|
|
|
|
2008-03-05 10:31:55 +00:00
|
|
|
BookmarkContentSink::BookmarkContentSink() : mFrames(16)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2006-07-18 16:38:51 +00:00
|
|
|
|
|
|
|
nsresult
|
|
|
|
BookmarkContentSink::Init(PRBool aAllowRootChanges,
|
2006-12-14 17:28:52 +00:00
|
|
|
PRInt64 aFolder,
|
|
|
|
PRBool aIsImportDefaults)
|
2006-07-18 16:38:51 +00:00
|
|
|
{
|
2010-01-15 14:04:02 +00:00
|
|
|
mBookmarksService = do_GetService(NS_NAVBOOKMARKSSERVICE_CONTRACTID);
|
|
|
|
NS_ENSURE_TRUE(mBookmarksService, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
mHistoryService = do_GetService(NS_NAVHISTORYSERVICE_CONTRACTID);
|
|
|
|
NS_ENSURE_TRUE(mHistoryService, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
mAnnotationService = do_GetService(NS_ANNOTATIONSERVICE_CONTRACTID);
|
|
|
|
NS_ENSURE_TRUE(mAnnotationService, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
mLivemarkService = do_GetService(NS_LIVEMARKSERVICE_CONTRACTID);
|
|
|
|
NS_ENSURE_TRUE(mLivemarkService, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
mMicrosummaryService = do_GetService("@mozilla.org/microsummary/service;1");
|
|
|
|
NS_ENSURE_TRUE(mMicrosummaryService, NS_ERROR_OUT_OF_MEMORY);
|
2006-07-18 16:38:51 +00:00
|
|
|
|
|
|
|
mAllowRootChanges = aAllowRootChanges;
|
2006-12-14 17:28:52 +00:00
|
|
|
mIsImportDefaults = aIsImportDefaults;
|
2006-07-18 16:38:51 +00:00
|
|
|
|
|
|
|
// initialize the root frame with the menu root
|
|
|
|
PRInt64 menuRoot;
|
2010-01-15 14:04:02 +00:00
|
|
|
nsresult rv;
|
2006-07-18 16:42:20 +00:00
|
|
|
if (aFolder == 0) {
|
2007-11-20 02:01:53 +00:00
|
|
|
rv = mBookmarksService->GetBookmarksMenuFolder(&menuRoot);
|
2006-07-18 16:42:20 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
mFolderSpecified = false;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
menuRoot = aFolder;
|
|
|
|
mFolderSpecified = true;
|
|
|
|
}
|
2007-05-03 08:56:41 +00:00
|
|
|
if (!mFrames.AppendElement(BookmarkImportFrame(menuRoot)))
|
2006-07-18 16:38:51 +00:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS2(BookmarkContentSink,
|
|
|
|
nsIContentSink,
|
2006-07-18 16:39:14 +00:00
|
|
|
nsIHTMLContentSink)
|
2006-07-18 16:38:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
BookmarkContentSink::OpenContainer(const nsIParserNode& aNode)
|
|
|
|
{
|
|
|
|
switch(aNode.GetNodeType()) {
|
|
|
|
case eHTMLTag_h1:
|
|
|
|
HandleHead1Begin(aNode);
|
|
|
|
break;
|
|
|
|
case eHTMLTag_h2:
|
|
|
|
case eHTMLTag_h3:
|
|
|
|
case eHTMLTag_h4:
|
|
|
|
case eHTMLTag_h5:
|
|
|
|
case eHTMLTag_h6:
|
|
|
|
HandleHeadBegin(aNode);
|
|
|
|
break;
|
|
|
|
case eHTMLTag_a:
|
|
|
|
HandleLinkBegin(aNode);
|
|
|
|
break;
|
|
|
|
case eHTMLTag_dl:
|
|
|
|
case eHTMLTag_ul:
|
|
|
|
case eHTMLTag_menu:
|
|
|
|
HandleContainerBegin(aNode);
|
|
|
|
break;
|
2007-05-11 19:26:14 +00:00
|
|
|
case eHTMLTag_dd:
|
|
|
|
CurFrame().mInDescription = PR_TRUE;
|
|
|
|
break;
|
2006-07-18 16:38:51 +00:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-01-15 14:04:02 +00:00
|
|
|
|
2006-07-18 16:38:51 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
BookmarkContentSink::CloseContainer(const nsHTMLTag aTag)
|
|
|
|
{
|
2007-05-19 00:40:55 +00:00
|
|
|
BookmarkImportFrame& frame = CurFrame();
|
|
|
|
|
2007-05-11 19:26:14 +00:00
|
|
|
// see the comment for the definition of mInDescription. Basically, we commit
|
|
|
|
// any text in mPreviousText to the description of the node/folder if there
|
|
|
|
// is any.
|
|
|
|
if (frame.mInDescription) {
|
|
|
|
frame.mPreviousText.Trim(kWhitespace); // important!
|
|
|
|
if (!frame.mPreviousText.IsEmpty()) {
|
2007-05-19 00:40:55 +00:00
|
|
|
|
2010-01-15 14:04:02 +00:00
|
|
|
PRInt64 itemId = !frame.mPreviousLink ? frame.mContainerID
|
|
|
|
: frame.mPreviousId;
|
2007-05-11 19:26:14 +00:00
|
|
|
|
|
|
|
PRBool hasDescription = PR_FALSE;
|
|
|
|
nsresult rv = mAnnotationService->ItemHasAnnotation(itemId,
|
|
|
|
DESCRIPTION_ANNO,
|
|
|
|
&hasDescription);
|
|
|
|
if (NS_SUCCEEDED(rv) && !hasDescription) {
|
|
|
|
mAnnotationService->SetItemAnnotationString(itemId, DESCRIPTION_ANNO,
|
|
|
|
frame.mPreviousText, 0,
|
|
|
|
nsIAnnotationService::EXPIRE_NEVER);
|
|
|
|
}
|
|
|
|
frame.mPreviousText.Truncate();
|
2007-05-19 00:40:55 +00:00
|
|
|
|
|
|
|
// Set last-modified a 2nd time for all items with descriptions
|
|
|
|
// we need to set last-modified as the *last* step in processing
|
|
|
|
// any item type in the bookmarks.html file, so that we do
|
|
|
|
// not overwrite the imported value. for items without descriptions,
|
|
|
|
// setting this value after setting the item title is that
|
|
|
|
// last point at which we can save this value before it gets reset.
|
|
|
|
// for items with descriptions, it must set after that point.
|
|
|
|
// however, at the point at which we set the title, there's no way
|
|
|
|
// to determine if there will be a description following,
|
|
|
|
// so we need to set the last-modified-date at both places.
|
|
|
|
|
|
|
|
PRTime lastModified;
|
|
|
|
if (!frame.mPreviousLink) {
|
|
|
|
lastModified = PreviousFrame().mPreviousLastModifiedDate;
|
|
|
|
} else {
|
|
|
|
lastModified = frame.mPreviousLastModifiedDate;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (itemId > 0 && lastModified > 0) {
|
|
|
|
rv = mBookmarksService->SetItemLastModified(itemId, lastModified);
|
2010-01-15 14:04:02 +00:00
|
|
|
NS_WARN_IF_FALSE(NS_SUCCEEDED(rv), "SetItemLastModified failed");
|
2007-05-19 00:40:55 +00:00
|
|
|
}
|
2007-05-11 19:26:14 +00:00
|
|
|
}
|
|
|
|
frame.mInDescription = PR_FALSE;
|
|
|
|
}
|
|
|
|
|
2006-07-18 16:38:51 +00:00
|
|
|
switch (aTag) {
|
|
|
|
case eHTMLTag_dl:
|
|
|
|
case eHTMLTag_ul:
|
|
|
|
case eHTMLTag_menu:
|
|
|
|
HandleContainerEnd();
|
|
|
|
break;
|
|
|
|
case eHTMLTag_dt:
|
|
|
|
break;
|
|
|
|
case eHTMLTag_h1:
|
|
|
|
// ignore
|
|
|
|
break;
|
|
|
|
case eHTMLTag_h2:
|
|
|
|
case eHTMLTag_h3:
|
|
|
|
case eHTMLTag_h4:
|
|
|
|
case eHTMLTag_h5:
|
|
|
|
case eHTMLTag_h6:
|
|
|
|
HandleHeadEnd();
|
|
|
|
break;
|
|
|
|
case eHTMLTag_a:
|
|
|
|
HandleLinkEnd();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2006-07-18 16:44:39 +00:00
|
|
|
|
|
|
|
// BookmarkContentSink::AddLeaf
|
|
|
|
//
|
|
|
|
// XXX on the branch, we should be calling CollectSkippedContent as in
|
|
|
|
// nsHTMLFragmentContentSink.cpp:AddLeaf when we encounter title, script,
|
|
|
|
// style, or server tags. Apparently if we don't, we'll leak the next DOM
|
|
|
|
// node. However, this requires that we keep a reference to the parser we'll
|
|
|
|
// introduce a circular reference because it has a reference to us.
|
|
|
|
//
|
|
|
|
// This is annoying to fix and these elements are not allowed in bookmarks
|
|
|
|
// files anyway. So if somebody tries to import a crazy bookmarks file, it
|
|
|
|
// will leak a little bit.
|
|
|
|
|
2006-07-18 16:38:51 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
BookmarkContentSink::AddLeaf(const nsIParserNode& aNode)
|
|
|
|
{
|
2006-07-18 16:43:30 +00:00
|
|
|
switch (aNode.GetNodeType()) {
|
|
|
|
case eHTMLTag_text:
|
|
|
|
// save any text we find
|
2006-07-18 16:44:39 +00:00
|
|
|
CurFrame().mPreviousText += aNode.GetText();
|
|
|
|
break;
|
|
|
|
case eHTMLTag_entity: {
|
|
|
|
nsAutoString tmp;
|
|
|
|
PRInt32 unicode = aNode.TranslateToUnicodeStr(tmp);
|
|
|
|
if (unicode < 0) {
|
|
|
|
// invalid entity - just use the text of it
|
2006-07-18 16:43:30 +00:00
|
|
|
CurFrame().mPreviousText += aNode.GetText();
|
2006-07-18 16:44:39 +00:00
|
|
|
} else {
|
|
|
|
CurFrame().mPreviousText.Append(unicode);
|
2006-07-18 16:43:30 +00:00
|
|
|
}
|
|
|
|
break;
|
2006-07-18 16:44:39 +00:00
|
|
|
}
|
|
|
|
case eHTMLTag_whitespace:
|
|
|
|
CurFrame().mPreviousText.Append(PRUnichar(' '));
|
|
|
|
break;
|
2006-07-18 16:43:30 +00:00
|
|
|
case eHTMLTag_hr:
|
2007-05-15 04:18:25 +00:00
|
|
|
HandleSeparator(aNode);
|
2006-07-18 16:43:30 +00:00
|
|
|
break;
|
2006-07-18 16:38:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
BookmarkContentSink::HandleContainerBegin(const nsIParserNode& node)
|
|
|
|
{
|
|
|
|
CurFrame().mContainerNesting ++;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// BookmarkContentSink::HandleContainerEnd
|
|
|
|
//
|
|
|
|
// Our "indent" count has decreased, and when we hit 0 that means that this
|
|
|
|
// container is complete and we need to pop back to the outer frame. Never
|
|
|
|
// pop the toplevel frame
|
|
|
|
|
|
|
|
void
|
|
|
|
BookmarkContentSink::HandleContainerEnd()
|
|
|
|
{
|
|
|
|
BookmarkImportFrame& frame = CurFrame();
|
|
|
|
if (frame.mContainerNesting > 0)
|
|
|
|
frame.mContainerNesting --;
|
2007-07-11 22:38:13 +00:00
|
|
|
if (mFrames.Length() > 1 && frame.mContainerNesting == 0) {
|
|
|
|
// we also need to re-set the imported last-modified date here. Otherwise
|
|
|
|
// the addition of items will override the imported field.
|
|
|
|
BookmarkImportFrame& prevFrame = PreviousFrame();
|
|
|
|
if (prevFrame.mPreviousLastModifiedDate > 0) {
|
|
|
|
nsresult rv = mBookmarksService->SetItemLastModified(frame.mContainerID,
|
|
|
|
prevFrame.mPreviousLastModifiedDate);
|
2010-01-15 14:04:02 +00:00
|
|
|
NS_WARN_IF_FALSE(NS_SUCCEEDED(rv), "SetItemLastModified failed");
|
2007-07-11 22:38:13 +00:00
|
|
|
}
|
2006-07-18 16:38:51 +00:00
|
|
|
PopFrame();
|
2007-07-11 22:38:13 +00:00
|
|
|
}
|
2006-07-18 16:38:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// BookmarkContentSink::HandleHead1Begin
|
|
|
|
//
|
|
|
|
// Handles <H1>. We check for the attribute PLACES_ROOT and reset the
|
|
|
|
// container id if it's found. Otherwise, the default bookmark menu
|
|
|
|
// root is assumed and imported things will go into the bookmarks menu.
|
|
|
|
|
|
|
|
void
|
|
|
|
BookmarkContentSink::HandleHead1Begin(const nsIParserNode& node)
|
|
|
|
{
|
|
|
|
PRInt32 attrCount = node.GetAttributeCount();
|
|
|
|
for (PRInt32 i = 0; i < attrCount; i ++) {
|
|
|
|
if (node.GetKeyAt(i).LowerCaseEqualsLiteral(KEY_PLACESROOT_LOWER)) {
|
|
|
|
if (mFrames.Length() > 1) {
|
|
|
|
NS_WARNING("Trying to set the places root from the middle of the hierarchy. "
|
|
|
|
"This can only be set at the beginning.");
|
|
|
|
return;
|
|
|
|
}
|
2007-05-18 22:00:36 +00:00
|
|
|
|
|
|
|
PRInt64 placesRoot;
|
|
|
|
nsresult rv = mBookmarksService->GetPlacesRoot(&placesRoot);
|
|
|
|
NS_WARN_IF_FALSE(NS_SUCCEEDED(rv), "could not get placesRoot");
|
|
|
|
CurFrame().mContainerID = placesRoot;
|
2006-07-18 16:38:51 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// BookmarkContentSink::HandleHeadBegin
|
|
|
|
//
|
|
|
|
// Called for h2,h3,h4,h5,h6. This just stores the correct information in
|
|
|
|
// the current frame; the actual new frame corresponding to the container
|
|
|
|
// associated with the heading will be created when the tag has been closed
|
|
|
|
// and we know the title (we don't know to create a new folder or to merge
|
|
|
|
// with an existing one until we have the title).
|
|
|
|
|
|
|
|
void
|
|
|
|
BookmarkContentSink::HandleHeadBegin(const nsIParserNode& node)
|
|
|
|
{
|
|
|
|
BookmarkImportFrame& frame = CurFrame();
|
|
|
|
|
|
|
|
// after a heading, a previous bookmark is not applicable (for example, for
|
|
|
|
// the descriptions contained in a <dd>). Neither is any previous head type
|
|
|
|
frame.mPreviousLink = nsnull;
|
|
|
|
frame.mLastContainerType = BookmarkImportFrame::Container_Normal;
|
|
|
|
|
|
|
|
// It is syntactically possible for a heading to appear after another heading
|
|
|
|
// but before the <dl> that encloses that folder's contents. This should not
|
|
|
|
// happen in practice, as the file will contain "<dl></dl>" sequence for
|
|
|
|
// empty containers.
|
|
|
|
//
|
|
|
|
// Just to be on the safe side, if we encounter
|
|
|
|
// <h3>FOO</h3>
|
|
|
|
// <h3>BAR</h3>
|
|
|
|
// <dl>...content 1...</dl>
|
|
|
|
// <dl>...content 2...</dl>
|
|
|
|
// we'll pop the stack when we find the h3 for BAR, treating that as an
|
|
|
|
// implicit ending of the FOO container. The output will be FOO and BAR as
|
|
|
|
// siblings. If there's another <dl> following (as in "content 2"), those
|
|
|
|
// items will be treated as further siblings of FOO and BAR
|
|
|
|
if (frame.mContainerNesting == 0)
|
|
|
|
PopFrame();
|
|
|
|
|
|
|
|
// We have to check for some attributes to see if this is a "special"
|
|
|
|
// folder, which will have different creation rules when the end tag is
|
|
|
|
// processed.
|
|
|
|
PRInt32 attrCount = node.GetAttributeCount();
|
|
|
|
frame.mLastContainerType = BookmarkImportFrame::Container_Normal;
|
2006-07-18 16:42:20 +00:00
|
|
|
if (!mFolderSpecified) {
|
|
|
|
for (PRInt32 i = 0; i < attrCount; i ++) {
|
|
|
|
if (node.GetKeyAt(i).LowerCaseEqualsLiteral(KEY_TOOLBARFOLDER_LOWER)) {
|
2007-06-26 02:38:34 +00:00
|
|
|
if (mIsImportDefaults)
|
|
|
|
frame.mLastContainerType = BookmarkImportFrame::Container_Toolbar;
|
2006-07-18 16:42:20 +00:00
|
|
|
break;
|
2010-01-15 14:04:02 +00:00
|
|
|
}
|
|
|
|
else if (node.GetKeyAt(i).LowerCaseEqualsLiteral(KEY_BOOKMARKSMENU_LOWER)) {
|
2007-06-26 02:38:34 +00:00
|
|
|
if (mIsImportDefaults)
|
|
|
|
frame.mLastContainerType = BookmarkImportFrame::Container_Menu;
|
2006-07-18 16:42:20 +00:00
|
|
|
break;
|
2010-01-15 14:04:02 +00:00
|
|
|
}
|
|
|
|
else if (node.GetKeyAt(i).LowerCaseEqualsLiteral(KEY_UNFILEDFOLDER_LOWER)) {
|
2008-04-01 18:26:09 +00:00
|
|
|
if (mIsImportDefaults)
|
|
|
|
frame.mLastContainerType = BookmarkImportFrame::Container_Unfiled;
|
|
|
|
break;
|
2010-01-15 14:04:02 +00:00
|
|
|
}
|
|
|
|
else if (node.GetKeyAt(i).LowerCaseEqualsLiteral(KEY_PLACESROOT_LOWER)) {
|
2007-06-26 02:38:34 +00:00
|
|
|
if (mIsImportDefaults)
|
|
|
|
frame.mLastContainerType = BookmarkImportFrame::Container_Places;
|
2006-07-18 16:46:13 +00:00
|
|
|
break;
|
2010-01-15 14:04:02 +00:00
|
|
|
}
|
|
|
|
else if (node.GetKeyAt(i).LowerCaseEqualsLiteral(KEY_DATE_ADDED_LOWER)) {
|
2007-05-19 00:40:55 +00:00
|
|
|
frame.mPreviousDateAdded =
|
|
|
|
ConvertImportedDateToInternalDate(NS_ConvertUTF16toUTF8(node.GetValueAt(i)));
|
2010-01-15 14:04:02 +00:00
|
|
|
}
|
|
|
|
else if (node.GetKeyAt(i).LowerCaseEqualsLiteral(KEY_LAST_MODIFIED_LOWER)) {
|
2007-05-19 00:40:55 +00:00
|
|
|
frame.mPreviousLastModifiedDate =
|
|
|
|
ConvertImportedDateToInternalDate(NS_ConvertUTF16toUTF8(node.GetValueAt(i)));
|
2006-07-18 16:42:20 +00:00
|
|
|
}
|
2006-07-18 16:38:51 +00:00
|
|
|
}
|
|
|
|
}
|
2007-05-03 08:56:41 +00:00
|
|
|
CurFrame().mPreviousText.Truncate();
|
2006-07-18 16:38:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// BookmarkContentSink::HandleHeadEnd
|
|
|
|
//
|
|
|
|
// Creates the new frame for this heading now that we know the name of the
|
|
|
|
// container (tokens since the heading open tag will have been placed in
|
|
|
|
// mPreviousText).
|
|
|
|
|
|
|
|
void
|
|
|
|
BookmarkContentSink::HandleHeadEnd()
|
|
|
|
{
|
|
|
|
NewFrame();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// BookmarkContentSink::HandleLinkBegin
|
|
|
|
//
|
|
|
|
// Handles "<a" tags by creating a new bookmark. The title of the bookmark
|
|
|
|
// will be the text content, which will be stuffed in mPreviousText for us
|
|
|
|
// and which will be saved by HandleLinkEnd
|
|
|
|
|
|
|
|
void
|
|
|
|
BookmarkContentSink::HandleLinkBegin(const nsIParserNode& node)
|
|
|
|
{
|
|
|
|
BookmarkImportFrame& frame = CurFrame();
|
|
|
|
|
2010-01-15 14:04:02 +00:00
|
|
|
// Make sure that the feed URIs from previous frames are emptied.
|
2006-07-18 16:44:16 +00:00
|
|
|
frame.mPreviousFeed = nsnull;
|
2010-01-15 14:04:02 +00:00
|
|
|
// Make sure that the bookmark id from previous frames are emptied.
|
2007-03-31 23:34:23 +00:00
|
|
|
frame.mPreviousId = 0;
|
2010-01-15 14:04:02 +00:00
|
|
|
// mPreviousText will hold link text, clear it.
|
2006-07-18 16:38:51 +00:00
|
|
|
frame.mPreviousText.Truncate();
|
2010-01-15 14:04:02 +00:00
|
|
|
// Empty microsummary items from previous frames.
|
2007-10-30 16:02:23 +00:00
|
|
|
frame.mPreviousMicrosummary = nsnull;
|
|
|
|
frame.mPreviousMicrosummaryText.Truncate();
|
2010-01-15 14:04:02 +00:00
|
|
|
|
|
|
|
// Get the attributes we care about.
|
2006-07-18 16:38:51 +00:00
|
|
|
nsAutoString href;
|
2006-07-18 16:41:11 +00:00
|
|
|
nsAutoString feedUrl;
|
2006-07-18 16:38:51 +00:00
|
|
|
nsAutoString icon;
|
2006-07-18 16:46:13 +00:00
|
|
|
nsAutoString iconUri;
|
2006-07-18 16:38:51 +00:00
|
|
|
nsAutoString lastCharset;
|
2006-07-18 16:43:53 +00:00
|
|
|
nsAutoString keyword;
|
2007-05-15 03:59:38 +00:00
|
|
|
nsAutoString postData;
|
2007-03-08 09:29:19 +00:00
|
|
|
nsAutoString webPanel;
|
2007-05-15 23:23:51 +00:00
|
|
|
nsAutoString micsumGenURI;
|
2007-10-30 16:02:23 +00:00
|
|
|
nsAutoString generatedTitle;
|
2007-05-19 00:40:55 +00:00
|
|
|
nsAutoString dateAdded;
|
|
|
|
nsAutoString lastModified;
|
|
|
|
|
2006-07-18 16:38:51 +00:00
|
|
|
PRInt32 attrCount = node.GetAttributeCount();
|
2010-01-15 14:04:02 +00:00
|
|
|
for (PRInt32 i = 0; i < attrCount; i++) {
|
2006-07-18 16:38:51 +00:00
|
|
|
const nsAString& key = node.GetKeyAt(i);
|
2010-01-15 14:04:02 +00:00
|
|
|
nsAutoString value(node.GetValueAt(i));
|
|
|
|
value.Trim(kWhitespace);
|
|
|
|
|
|
|
|
if (key.LowerCaseEqualsLiteral(KEY_HREF_LOWER))
|
|
|
|
href = value;
|
|
|
|
else if (key.LowerCaseEqualsLiteral(KEY_FEEDURL_LOWER))
|
|
|
|
feedUrl = value;
|
|
|
|
else if (key.LowerCaseEqualsLiteral(KEY_ICON_LOWER))
|
|
|
|
icon = value;
|
|
|
|
else if (key.LowerCaseEqualsLiteral(KEY_ICON_URI_LOWER))
|
|
|
|
iconUri = value;
|
|
|
|
else if (key.LowerCaseEqualsLiteral(KEY_LASTCHARSET_LOWER))
|
|
|
|
lastCharset = value;
|
|
|
|
else if (key.LowerCaseEqualsLiteral(KEY_SHORTCUTURL_LOWER))
|
|
|
|
keyword = value;
|
|
|
|
else if (key.LowerCaseEqualsLiteral(KEY_POST_DATA_LOWER))
|
|
|
|
postData = value;
|
|
|
|
else if (key.LowerCaseEqualsLiteral(KEY_WEB_PANEL_LOWER))
|
|
|
|
webPanel = value;
|
|
|
|
else if (key.LowerCaseEqualsLiteral(KEY_MICSUM_GEN_URI_LOWER))
|
|
|
|
micsumGenURI = value;
|
|
|
|
else if (key.LowerCaseEqualsLiteral(KEY_GENERATED_TITLE_LOWER))
|
|
|
|
generatedTitle = value;
|
|
|
|
else if (key.LowerCaseEqualsLiteral(KEY_DATE_ADDED_LOWER))
|
|
|
|
dateAdded = value;
|
|
|
|
else if (key.LowerCaseEqualsLiteral(KEY_LAST_MODIFIED_LOWER))
|
|
|
|
lastModified = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
// For feeds, get the feed URL. If it is invalid, mPreviousFeed will be
|
|
|
|
// NULL and we'll create it as a normal bookmark.
|
2007-05-03 08:56:41 +00:00
|
|
|
if (!feedUrl.IsEmpty()) {
|
2006-07-18 16:45:49 +00:00
|
|
|
NS_NewURI(getter_AddRefs(frame.mPreviousFeed),
|
|
|
|
NS_ConvertUTF16toUTF8(feedUrl), nsnull);
|
2006-07-18 16:38:51 +00:00
|
|
|
}
|
|
|
|
|
2010-01-15 14:04:02 +00:00
|
|
|
// Ignore <a> tags that have no href.
|
2006-07-18 16:45:49 +00:00
|
|
|
if (href.IsEmpty()) {
|
2006-07-18 16:38:51 +00:00
|
|
|
frame.mPreviousLink = nsnull;
|
2006-07-18 16:45:49 +00:00
|
|
|
// The exception is for feeds, where the href is an optional component
|
|
|
|
// indicating the source web site.
|
2007-05-03 08:56:41 +00:00
|
|
|
if (!frame.mPreviousFeed)
|
2006-07-18 16:45:49 +00:00
|
|
|
return;
|
2010-01-15 14:04:02 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Save the address if it's valid. Note that we ignore errors if this is a
|
|
|
|
// feed since href is optional for them.
|
|
|
|
nsresult rv = NS_NewURI(getter_AddRefs(frame.mPreviousLink), href, nsnull);
|
2007-05-03 08:56:41 +00:00
|
|
|
if (NS_FAILED(rv) && !frame.mPreviousFeed) {
|
2006-07-18 16:45:49 +00:00
|
|
|
frame.mPreviousLink = nsnull;
|
2010-01-15 14:04:02 +00:00
|
|
|
return;
|
2006-07-18 16:45:49 +00:00
|
|
|
}
|
2006-07-18 16:38:51 +00:00
|
|
|
}
|
2006-07-18 16:45:49 +00:00
|
|
|
|
2010-01-15 14:04:02 +00:00
|
|
|
// Save bookmark's last modified date.
|
2007-05-19 00:40:55 +00:00
|
|
|
if (!lastModified.IsEmpty()) {
|
2010-01-15 14:04:02 +00:00
|
|
|
frame.mPreviousLastModifiedDate =
|
|
|
|
ConvertImportedDateToInternalDate(NS_ConvertUTF16toUTF8(lastModified));
|
2007-05-19 00:40:55 +00:00
|
|
|
}
|
2007-03-31 23:34:23 +00:00
|
|
|
|
2010-01-15 14:04:02 +00:00
|
|
|
// If this is a live bookmark, we will handle it in HandleLinkEnd(), so we
|
|
|
|
// can skip bookmark creation.
|
2006-07-18 16:45:49 +00:00
|
|
|
if (frame.mPreviousFeed)
|
2006-07-18 16:41:11 +00:00
|
|
|
return;
|
2006-07-18 16:38:51 +00:00
|
|
|
|
2010-01-15 14:04:02 +00:00
|
|
|
// Create the bookmark. The title is unknown for now, we will set it later.
|
|
|
|
nsresult rv = mBookmarksService->InsertBookmark(frame.mContainerID,
|
|
|
|
frame.mPreviousLink,
|
|
|
|
mBookmarksService->DEFAULT_INDEX,
|
|
|
|
EmptyCString(),
|
|
|
|
&frame.mPreviousId);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
// If inserting bookmark failed, there's nothing more we can do.
|
|
|
|
NS_WARNING("InserBookmark failed");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the date added value, if we have it.
|
|
|
|
if (!dateAdded.IsEmpty()) {
|
|
|
|
PRTime convertedDateAdded =
|
|
|
|
ConvertImportedDateToInternalDate(NS_ConvertUTF16toUTF8(dateAdded));
|
|
|
|
if (convertedDateAdded) {
|
|
|
|
rv = mBookmarksService->SetItemDateAdded(frame.mPreviousId, convertedDateAdded);
|
|
|
|
NS_WARN_IF_FALSE(NS_SUCCEEDED(rv), "SetItemDateAdded failed");
|
2007-05-19 00:40:55 +00:00
|
|
|
}
|
2007-03-31 23:34:23 +00:00
|
|
|
}
|
2006-07-18 16:38:51 +00:00
|
|
|
|
2010-01-15 14:04:02 +00:00
|
|
|
// Save the favicon.
|
2007-05-03 08:56:41 +00:00
|
|
|
if (!icon.IsEmpty() || !iconUri.IsEmpty()) {
|
2006-07-18 16:46:13 +00:00
|
|
|
nsCOMPtr<nsIURI> iconUriObject;
|
2008-12-04 14:44:14 +00:00
|
|
|
rv = NS_NewURI(getter_AddRefs(iconUriObject), iconUri);
|
|
|
|
if (!icon.IsEmpty() || NS_SUCCEEDED(rv)) {
|
2008-07-03 18:07:56 +00:00
|
|
|
rv = SetFaviconForURI(frame.mPreviousLink, iconUriObject, icon);
|
2008-12-04 14:44:14 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
nsCAutoString warnMsg;
|
|
|
|
warnMsg.Append("Bookmarks Import: unable to set favicon '");
|
|
|
|
warnMsg.Append(NS_ConvertUTF16toUTF8(iconUri));
|
|
|
|
warnMsg.Append("' for page '");
|
|
|
|
nsCAutoString spec;
|
|
|
|
rv = frame.mPreviousLink->GetSpec(spec);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
warnMsg.Append(spec);
|
|
|
|
warnMsg.Append("'");
|
|
|
|
NS_WARNING(warnMsg.get());
|
|
|
|
}
|
2007-05-03 08:56:41 +00:00
|
|
|
}
|
2006-07-18 16:46:13 +00:00
|
|
|
}
|
2006-07-18 16:39:39 +00:00
|
|
|
|
2010-01-15 14:04:02 +00:00
|
|
|
// Save the keyword.
|
2007-05-15 03:59:38 +00:00
|
|
|
if (!keyword.IsEmpty()) {
|
2010-01-15 14:04:02 +00:00
|
|
|
rv = mBookmarksService->SetKeywordForBookmark(frame.mPreviousId, keyword);
|
|
|
|
NS_WARN_IF_FALSE(NS_SUCCEEDED(rv), "SetKeywordForBookmark failed");
|
|
|
|
if (NS_SUCCEEDED(rv) && !postData.IsEmpty()) {
|
|
|
|
rv = mAnnotationService->SetItemAnnotationString(frame.mPreviousId,
|
|
|
|
POST_DATA_ANNO,
|
|
|
|
postData, 0,
|
|
|
|
nsIAnnotationService::EXPIRE_NEVER);
|
|
|
|
NS_WARN_IF_FALSE(NS_SUCCEEDED(rv), "SetItemAnnotationString failed");
|
2007-05-15 03:59:38 +00:00
|
|
|
}
|
|
|
|
}
|
2007-05-15 23:23:51 +00:00
|
|
|
|
2010-01-15 14:04:02 +00:00
|
|
|
// Set load-in-sidebar annotation for the bookmark.
|
2007-03-08 09:29:19 +00:00
|
|
|
if (webPanel.LowerCaseEqualsLiteral("true")) {
|
2010-01-15 14:04:02 +00:00
|
|
|
|
|
|
|
rv = mAnnotationService->SetItemAnnotationInt32(frame.mPreviousId,
|
|
|
|
LOAD_IN_SIDEBAR_ANNO,
|
|
|
|
1, 0,
|
|
|
|
nsIAnnotationService::EXPIRE_NEVER);
|
|
|
|
NS_WARN_IF_FALSE(NS_SUCCEEDED(rv), "SetItemAnnotationInt32 failed");
|
2007-03-08 09:29:19 +00:00
|
|
|
}
|
2007-05-15 23:23:51 +00:00
|
|
|
|
2010-01-15 14:04:02 +00:00
|
|
|
// Import microsummary.
|
2007-05-15 23:23:51 +00:00
|
|
|
if (!micsumGenURI.IsEmpty()) {
|
|
|
|
nsCOMPtr<nsIURI> micsumGenURIObject;
|
2010-01-15 14:04:02 +00:00
|
|
|
rv = NS_NewURI(getter_AddRefs(micsumGenURIObject), micsumGenURI);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = mMicrosummaryService->CreateMicrosummary(frame.mPreviousLink,
|
|
|
|
micsumGenURIObject,
|
|
|
|
getter_AddRefs(frame.mPreviousMicrosummary));
|
|
|
|
NS_WARN_IF_FALSE(NS_SUCCEEDED(rv), "CreateMicrosummary failed");
|
2007-10-30 16:02:23 +00:00
|
|
|
frame.mPreviousMicrosummaryText = generatedTitle;
|
2007-05-15 23:23:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-15 14:04:02 +00:00
|
|
|
// Import last charset.
|
2007-07-09 02:22:50 +00:00
|
|
|
if (!lastCharset.IsEmpty()) {
|
2008-04-08 18:41:04 +00:00
|
|
|
rv = mHistoryService->SetCharsetForURI(frame.mPreviousLink,lastCharset);
|
2010-01-15 14:04:02 +00:00
|
|
|
NS_WARN_IF_FALSE(NS_SUCCEEDED(rv), "setCharsetForURI failed");
|
2007-07-09 02:22:50 +00:00
|
|
|
}
|
2006-07-18 16:38:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// BookmarkContentSink::HandleLinkEnd
|
|
|
|
//
|
2006-07-18 16:40:25 +00:00
|
|
|
// Saves the title for the given bookmark. This only writes the user title.
|
|
|
|
// Any previous title will be untouched. If this is a new entry, it will have
|
|
|
|
// an empty "official" title until you visit it.
|
2006-07-18 16:38:51 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
BookmarkContentSink::HandleLinkEnd()
|
|
|
|
{
|
2007-03-31 23:34:23 +00:00
|
|
|
nsresult rv;
|
2006-07-18 16:38:51 +00:00
|
|
|
BookmarkImportFrame& frame = CurFrame();
|
|
|
|
frame.mPreviousText.Trim(kWhitespace);
|
2010-01-15 14:04:02 +00:00
|
|
|
|
2006-07-18 16:41:11 +00:00
|
|
|
if (frame.mPreviousFeed) {
|
2010-01-15 14:04:02 +00:00
|
|
|
// The is a live bookmark. We create it here since in HandleLinkBegin we
|
|
|
|
// don't know the title.
|
|
|
|
|
|
|
|
if (mIsImportDefaults) {
|
|
|
|
// Create the live bookmark but don't update it immediately.
|
|
|
|
rv = mLivemarkService->CreateLivemarkFolderOnly(frame.mContainerID,
|
|
|
|
frame.mPreviousText,
|
|
|
|
frame.mPreviousLink,
|
|
|
|
frame.mPreviousFeed,
|
|
|
|
-1,
|
|
|
|
&frame.mPreviousId);
|
|
|
|
NS_WARN_IF_FALSE(NS_SUCCEEDED(rv), "CreateLivemarkFolderOnly failed!");
|
2007-05-17 06:06:48 +00:00
|
|
|
}
|
2010-01-15 14:04:02 +00:00
|
|
|
else {
|
|
|
|
rv = mLivemarkService->CreateLivemark(frame.mContainerID,
|
|
|
|
frame.mPreviousText,
|
|
|
|
frame.mPreviousLink,
|
|
|
|
frame.mPreviousFeed,
|
|
|
|
-1,
|
|
|
|
&frame.mPreviousId);
|
|
|
|
NS_WARN_IF_FALSE(NS_SUCCEEDED(rv), "CreateLivemark failed!");
|
2007-05-17 06:06:48 +00:00
|
|
|
}
|
2007-05-19 00:40:55 +00:00
|
|
|
#ifdef DEBUG_IMPORT
|
2010-01-15 14:04:02 +00:00
|
|
|
PrintNesting();
|
|
|
|
printf("Created livemark '%s' %lld\n",
|
|
|
|
NS_ConvertUTF16toUTF8(frame.mPreviousText).get(), frame.mPreviousId);
|
2007-05-19 00:40:55 +00:00
|
|
|
#endif
|
2006-07-18 16:41:11 +00:00
|
|
|
}
|
2006-07-18 16:45:26 +00:00
|
|
|
else if (frame.mPreviousLink) {
|
2010-01-15 14:04:02 +00:00
|
|
|
// This is a common bookmark.
|
2006-07-18 16:46:13 +00:00
|
|
|
#ifdef DEBUG_IMPORT
|
|
|
|
PrintNesting();
|
2010-01-15 14:04:02 +00:00
|
|
|
printf("Created bookmark '%s' %lld\n",
|
2007-05-19 00:40:55 +00:00
|
|
|
NS_ConvertUTF16toUTF8(frame.mPreviousText).get(), frame.mPreviousId);
|
2006-07-18 16:46:13 +00:00
|
|
|
#endif
|
2007-10-30 16:02:23 +00:00
|
|
|
if (frame.mPreviousMicrosummary) {
|
2010-01-15 14:04:02 +00:00
|
|
|
// This bookmark has a microsummary.
|
|
|
|
rv = mAnnotationService->SetItemAnnotationString(frame.mPreviousId,
|
|
|
|
STATIC_TITLE_ANNO,
|
2007-10-30 16:02:23 +00:00
|
|
|
frame.mPreviousText, 0,
|
|
|
|
nsIAnnotationService::EXPIRE_NEVER);
|
2010-01-15 14:04:02 +00:00
|
|
|
NS_WARN_IF_FALSE(NS_SUCCEEDED(rv), "Could not store user's static bookmark title!");
|
|
|
|
rv = mBookmarksService->SetItemTitle(frame.mPreviousId,
|
|
|
|
NS_ConvertUTF16toUTF8(frame.mPreviousMicrosummaryText));
|
|
|
|
NS_WARN_IF_FALSE(NS_SUCCEEDED(rv), "SetItemTitle failed");
|
|
|
|
rv = mMicrosummaryService->SetMicrosummary(frame.mPreviousId,
|
|
|
|
frame.mPreviousMicrosummary);
|
|
|
|
NS_WARN_IF_FALSE(NS_SUCCEEDED(rv), "SetMicrosummary failed");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rv = mBookmarksService->SetItemTitle(frame.mPreviousId,
|
|
|
|
NS_ConvertUTF16toUTF8(frame.mPreviousText));
|
|
|
|
NS_WARN_IF_FALSE(NS_SUCCEEDED(rv), "SetItemTitle failed");
|
2007-10-30 16:02:23 +00:00
|
|
|
}
|
2006-07-18 16:38:51 +00:00
|
|
|
}
|
2007-05-19 00:40:55 +00:00
|
|
|
|
2010-01-15 14:04:02 +00:00
|
|
|
// Set last modified date as the last change.
|
2007-05-19 00:40:55 +00:00
|
|
|
if (frame.mPreviousId > 0 && frame.mPreviousLastModifiedDate > 0) {
|
2010-01-15 14:04:02 +00:00
|
|
|
rv = mBookmarksService->SetItemLastModified(frame.mPreviousId,
|
|
|
|
frame.mPreviousLastModifiedDate);
|
|
|
|
NS_WARN_IF_FALSE(NS_SUCCEEDED(rv), "SetItemLastModified failed");
|
2007-05-19 00:40:55 +00:00
|
|
|
// Note: don't clear mPreviousLastModifiedDate, because if this item has a
|
|
|
|
// description, we'll need to set it again.
|
|
|
|
}
|
|
|
|
|
2007-05-03 08:56:41 +00:00
|
|
|
frame.mPreviousText.Truncate();
|
2006-07-18 16:38:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-07-18 16:43:30 +00:00
|
|
|
// BookmarkContentSink::HandleSeparator
|
|
|
|
//
|
|
|
|
// Inserts a separator into the current container
|
|
|
|
void
|
2007-05-17 06:06:48 +00:00
|
|
|
BookmarkContentSink::HandleSeparator(const nsIParserNode& aNode)
|
2006-07-18 16:43:30 +00:00
|
|
|
{
|
|
|
|
BookmarkImportFrame& frame = CurFrame();
|
|
|
|
|
2007-06-26 02:38:34 +00:00
|
|
|
// create the separator
|
2007-05-17 06:06:48 +00:00
|
|
|
|
2006-07-18 16:46:13 +00:00
|
|
|
#ifdef DEBUG_IMPORT
|
2007-06-26 02:38:34 +00:00
|
|
|
PrintNesting();
|
|
|
|
printf("--------\n");
|
2006-07-18 16:46:13 +00:00
|
|
|
#endif
|
2007-05-17 06:06:48 +00:00
|
|
|
|
2010-01-15 14:04:02 +00:00
|
|
|
nsresult rv = mBookmarksService->InsertSeparator(frame.mContainerID,
|
|
|
|
mBookmarksService->DEFAULT_INDEX,
|
|
|
|
&frame.mPreviousId);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("InsertSeparator failed");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Import separator title if set.
|
|
|
|
// Note that Places does not use separator titles, nor backup/restore them.
|
2007-06-26 02:38:34 +00:00
|
|
|
PRInt32 attrCount = aNode.GetAttributeCount();
|
|
|
|
for (PRInt32 i = 0; i < attrCount; i ++) {
|
|
|
|
const nsAString& key = aNode.GetKeyAt(i);
|
2010-01-15 14:04:02 +00:00
|
|
|
|
|
|
|
if (key.LowerCaseEqualsLiteral(KEY_NAME_LOWER)) {
|
|
|
|
nsAutoString name;
|
2007-06-26 02:38:34 +00:00
|
|
|
name = aNode.GetValueAt(i);
|
2010-01-15 14:04:02 +00:00
|
|
|
name.Trim(kWhitespace);
|
|
|
|
if (!name.IsEmpty()) {
|
|
|
|
rv = mBookmarksService->SetItemTitle(frame.mPreviousId,
|
|
|
|
NS_ConvertUTF16toUTF8(name));
|
|
|
|
NS_WARN_IF_FALSE(NS_SUCCEEDED(rv), "SetItemTitle failed");
|
|
|
|
}
|
|
|
|
}
|
2007-06-26 02:38:34 +00:00
|
|
|
}
|
2007-05-19 00:40:55 +00:00
|
|
|
|
2007-06-26 02:38:34 +00:00
|
|
|
// Note: we do not need to import ADD_DATE or LAST_MODIFIED for separators
|
|
|
|
// because pre-Places bookmarks does not support them.
|
|
|
|
// and we can't write them out because attributes other than NAME
|
2010-01-15 14:04:02 +00:00
|
|
|
// will make Firefox 2.x crash/hang due to bug #381129
|
2006-07-18 16:43:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-07-18 16:38:51 +00:00
|
|
|
// BookmarkContentSink::NewFrame
|
|
|
|
//
|
|
|
|
// This is called when there is a new folder found. The folder takes the
|
|
|
|
// name from the previous frame's heading.
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
BookmarkContentSink::NewFrame()
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
2007-03-31 23:34:23 +00:00
|
|
|
PRInt64 ourID = 0;
|
2006-07-18 16:38:51 +00:00
|
|
|
nsString containerName;
|
|
|
|
BookmarkImportFrame::ContainerType containerType;
|
2007-11-20 02:01:53 +00:00
|
|
|
BookmarkImportFrame& frame = CurFrame();
|
|
|
|
frame.ConsumeHeading(&containerName, &containerType);
|
2006-07-18 16:38:51 +00:00
|
|
|
|
|
|
|
PRBool updateFolder = PR_FALSE;
|
2007-06-26 02:38:34 +00:00
|
|
|
|
|
|
|
switch (containerType) {
|
|
|
|
case BookmarkImportFrame::Container_Normal:
|
|
|
|
// append a new folder
|
|
|
|
rv = mBookmarksService->CreateFolder(CurFrame().mContainerID,
|
2008-01-09 03:54:37 +00:00
|
|
|
NS_ConvertUTF16toUTF8(containerName),
|
2007-06-26 02:38:34 +00:00
|
|
|
mBookmarksService->DEFAULT_INDEX,
|
|
|
|
&ourID);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
break;
|
|
|
|
case BookmarkImportFrame::Container_Places:
|
|
|
|
// places root, never reparent here, when we're building the initial
|
|
|
|
// hierarchy, it will only be defined at the top level
|
|
|
|
rv = mBookmarksService->GetPlacesRoot(&ourID);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
break;
|
|
|
|
case BookmarkImportFrame::Container_Menu:
|
2008-04-01 18:26:09 +00:00
|
|
|
// menu folder
|
2007-11-20 02:01:53 +00:00
|
|
|
rv = mBookmarksService->GetBookmarksMenuFolder(&ourID);
|
2007-06-26 02:38:34 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-11-20 02:01:53 +00:00
|
|
|
if (mAllowRootChanges)
|
2007-06-26 02:38:34 +00:00
|
|
|
updateFolder = PR_TRUE;
|
|
|
|
break;
|
2008-04-01 18:26:09 +00:00
|
|
|
case BookmarkImportFrame::Container_Unfiled:
|
|
|
|
// unfiled bookmarks folder
|
|
|
|
rv = mBookmarksService->GetUnfiledBookmarksFolder(&ourID);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
if (mAllowRootChanges)
|
|
|
|
updateFolder = PR_TRUE;
|
|
|
|
break;
|
2007-06-26 02:38:34 +00:00
|
|
|
case BookmarkImportFrame::Container_Toolbar:
|
|
|
|
// get toolbar folder
|
2007-11-20 02:01:53 +00:00
|
|
|
rv = mBookmarksService->GetToolbarFolder(&ourID);
|
2007-06-26 02:38:34 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-11-20 02:01:53 +00:00
|
|
|
|
|
|
|
// In Fx2, the toolbar folder is a child of the bookmarks menu, listed
|
|
|
|
// between two separators:
|
|
|
|
// 1) Get Bookmarks Addons 2) Separator 3) Bookmarks Toolbar Folder
|
|
|
|
// 4) Separator 5) Mozilla Firefox Folder
|
|
|
|
// In Places, the toolbar folder is a direct child of the places root,
|
|
|
|
// meaning that we end up with two sequential separators.
|
|
|
|
if (frame.mPreviousId > 0) {
|
|
|
|
PRUint16 itemType;
|
|
|
|
rv = mBookmarksService->GetItemType(frame.mPreviousId, &itemType);
|
2007-03-07 21:22:54 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-11-20 02:01:53 +00:00
|
|
|
if (itemType == nsINavBookmarksService::TYPE_SEPARATOR) {
|
|
|
|
// remove it
|
|
|
|
rv = mBookmarksService->RemoveItem(frame.mPreviousId);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
}
|
2007-06-26 02:38:34 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
NS_NOTREACHED("Unknown container type");
|
2006-07-18 16:38:51 +00:00
|
|
|
}
|
2007-06-26 02:38:34 +00:00
|
|
|
|
2006-07-18 16:46:13 +00:00
|
|
|
#ifdef DEBUG_IMPORT
|
|
|
|
PrintNesting();
|
|
|
|
printf("Folder %lld \'%s\'", ourID, NS_ConvertUTF16toUTF8(containerName).get());
|
|
|
|
#endif
|
2006-07-18 16:38:51 +00:00
|
|
|
|
|
|
|
if (updateFolder) {
|
2007-03-07 21:22:54 +00:00
|
|
|
// move the menu folder to the current position
|
2010-01-15 14:04:02 +00:00
|
|
|
rv = mBookmarksService->MoveItem(ourID, CurFrame().mContainerID, -1);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
rv = mBookmarksService->SetItemTitle(ourID, NS_ConvertUTF16toUTF8(containerName));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2006-07-18 16:46:13 +00:00
|
|
|
#ifdef DEBUG_IMPORT
|
|
|
|
printf(" [reparenting]");
|
|
|
|
#endif
|
2006-07-18 16:38:51 +00:00
|
|
|
}
|
|
|
|
|
2006-07-18 16:46:13 +00:00
|
|
|
#ifdef DEBUG_IMPORT
|
|
|
|
printf("\n");
|
|
|
|
#endif
|
|
|
|
|
2007-05-19 00:40:55 +00:00
|
|
|
if (frame.mPreviousDateAdded > 0) {
|
2010-01-15 14:04:02 +00:00
|
|
|
rv = mBookmarksService->SetItemDateAdded(ourID, frame.mPreviousDateAdded);
|
|
|
|
NS_WARN_IF_FALSE(NS_SUCCEEDED(rv), "SetItemDateAdded failed");
|
2007-05-19 00:40:55 +00:00
|
|
|
frame.mPreviousDateAdded = 0;
|
|
|
|
}
|
|
|
|
if (frame.mPreviousLastModifiedDate > 0) {
|
2010-01-15 14:04:02 +00:00
|
|
|
rv = mBookmarksService->SetItemLastModified(ourID, frame.mPreviousLastModifiedDate);
|
|
|
|
NS_WARN_IF_FALSE(NS_SUCCEEDED(rv), "SetItemLastModified failed");
|
2007-05-19 00:40:55 +00:00
|
|
|
// don't clear last-modified, in case there's a description
|
|
|
|
}
|
|
|
|
|
2008-03-05 10:31:55 +00:00
|
|
|
frame.mPreviousId = ourID;
|
|
|
|
|
2007-05-03 08:56:41 +00:00
|
|
|
if (!mFrames.AppendElement(BookmarkImportFrame(ourID)))
|
2006-07-18 16:38:51 +00:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2008-03-05 10:31:55 +00:00
|
|
|
|
2006-07-18 16:38:51 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
BookmarkContentSink::PopFrame()
|
|
|
|
{
|
|
|
|
// we must always have one frame
|
|
|
|
if (mFrames.Length() <= 1) {
|
|
|
|
NS_NOTREACHED("Trying to complete more bookmark folders than you started");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
mFrames.RemoveElementAt(mFrames.Length() - 1);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-07-18 16:39:39 +00:00
|
|
|
// BookmarkContentSink::SetFaviconForURI
|
|
|
|
//
|
|
|
|
// aData is a string that is a data URI for the favicon. Our job is to
|
2006-07-18 16:46:13 +00:00
|
|
|
// decode it and store it in the favicon service.
|
|
|
|
//
|
|
|
|
// When aIconURI is non-null, we will use that as the URI of the favicon
|
|
|
|
// when storing in the favicon service.
|
|
|
|
//
|
|
|
|
// When aIconURI is null, we have to make up a URI for this favicon so that
|
|
|
|
// it can be stored in the service. The real one will be set the next time
|
|
|
|
// the user visits the page. Our made up one should get expired when the
|
|
|
|
// page no longer references it.
|
2006-07-18 16:39:39 +00:00
|
|
|
nsresult
|
2006-07-18 16:46:13 +00:00
|
|
|
BookmarkContentSink::SetFaviconForURI(nsIURI* aPageURI, nsIURI* aIconURI,
|
2008-07-03 18:07:56 +00:00
|
|
|
const nsString& aData)
|
2006-07-18 16:39:39 +00:00
|
|
|
{
|
|
|
|
nsresult rv;
|
2006-07-18 16:46:13 +00:00
|
|
|
static PRUint32 serialNumber = 0; // for made-up favicon URIs
|
2006-07-18 16:39:39 +00:00
|
|
|
|
2010-01-15 14:04:02 +00:00
|
|
|
nsCOMPtr<nsIFaviconService> faviconService =
|
|
|
|
do_GetService(NS_FAVICONSERVICE_CONTRACTID);
|
|
|
|
NS_ENSURE_TRUE(faviconService, NS_ERROR_OUT_OF_MEMORY);
|
2006-07-18 16:39:39 +00:00
|
|
|
|
2006-07-18 16:46:13 +00:00
|
|
|
// if the input favicon URI is a chrome: URI, then we just save it and don't
|
|
|
|
// worry about data
|
|
|
|
if (aIconURI) {
|
|
|
|
nsCString faviconScheme;
|
2006-07-18 16:46:36 +00:00
|
|
|
aIconURI->GetScheme(faviconScheme);
|
2006-07-18 16:46:13 +00:00
|
|
|
if (faviconScheme.EqualsLiteral("chrome")) {
|
|
|
|
return faviconService->SetFaviconUrlForPage(aPageURI, aIconURI);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-18 16:46:36 +00:00
|
|
|
// some bookmarks have placeholder URIs that contain just "data:"
|
|
|
|
// ignore these
|
|
|
|
if (aData.Length() <= 5)
|
|
|
|
return NS_OK;
|
|
|
|
|
2006-07-18 16:39:39 +00:00
|
|
|
nsCOMPtr<nsIURI> faviconURI;
|
2006-07-18 16:46:13 +00:00
|
|
|
if (aIconURI) {
|
|
|
|
faviconURI = aIconURI;
|
2010-01-15 14:04:02 +00:00
|
|
|
}
|
|
|
|
else {
|
2006-07-18 16:46:13 +00:00
|
|
|
// make up favicon URL
|
|
|
|
nsCAutoString faviconSpec;
|
|
|
|
faviconSpec.AssignLiteral("http://www.mozilla.org/2005/made-up-favicon/");
|
|
|
|
faviconSpec.AppendInt(serialNumber);
|
|
|
|
faviconSpec.AppendLiteral("-");
|
2007-06-26 02:38:34 +00:00
|
|
|
char buf[32];
|
|
|
|
PR_snprintf(buf, sizeof(buf), "%lld", PR_Now());
|
|
|
|
faviconSpec.Append(buf);
|
2006-07-18 16:46:13 +00:00
|
|
|
rv = NS_NewURI(getter_AddRefs(faviconURI), faviconSpec);
|
2008-12-04 14:44:14 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
nsCAutoString warnMsg;
|
|
|
|
warnMsg.Append("Bookmarks Import: Unable to make up new favicon '");
|
|
|
|
warnMsg.Append(faviconSpec);
|
|
|
|
warnMsg.Append("' for page '");
|
|
|
|
nsCAutoString spec;
|
|
|
|
rv = aPageURI->GetSpec(spec);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
warnMsg.Append(spec);
|
|
|
|
warnMsg.Append("'");
|
|
|
|
NS_WARNING(warnMsg.get());
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2007-06-26 02:38:34 +00:00
|
|
|
serialNumber++;
|
2006-07-18 16:46:13 +00:00
|
|
|
}
|
2006-07-18 16:39:39 +00:00
|
|
|
|
2008-09-02 11:59:59 +00:00
|
|
|
// save the favicon data
|
|
|
|
// This could fail if the favicon is bigger than defined limit, in such a
|
|
|
|
// case data will not be saved to the db but we will still continue.
|
|
|
|
(void) faviconService->SetFaviconDataFromDataURL(faviconURI, aData, 0);
|
2006-07-18 16:39:39 +00:00
|
|
|
|
2008-07-03 18:07:56 +00:00
|
|
|
rv = faviconService->SetFaviconUrlForPage(aPageURI, faviconURI);
|
2006-07-18 16:39:39 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2007-05-03 08:56:41 +00:00
|
|
|
return NS_OK;
|
2006-07-18 16:39:39 +00:00
|
|
|
}
|
|
|
|
|
2007-03-31 23:34:23 +00:00
|
|
|
|
2007-05-19 00:40:55 +00:00
|
|
|
// Converts a string date in seconds to an int date in microseconds
|
|
|
|
PRTime
|
|
|
|
BookmarkContentSink::ConvertImportedDateToInternalDate(const nsACString& aDate) {
|
|
|
|
PRTime convertedDate = 0;
|
|
|
|
if (!aDate.IsEmpty()) {
|
|
|
|
nsresult rv;
|
|
|
|
convertedDate = aDate.ToInteger(&rv);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
convertedDate *= 1000000; // in bookmarks.html this value is in seconds, not microseconds
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
convertedDate = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return convertedDate;
|
|
|
|
}
|
2006-07-18 16:41:57 +00:00
|
|
|
|
2010-01-15 14:04:02 +00:00
|
|
|
|
2006-07-18 16:38:51 +00:00
|
|
|
// SyncChannelStatus
|
|
|
|
//
|
|
|
|
// If a function returns an error, we need to set the channel status to be
|
|
|
|
// the same, but only if the channel doesn't have its own error. This returns
|
|
|
|
// the error code that should be sent to OnStopRequest.
|
|
|
|
static nsresult
|
|
|
|
SyncChannelStatus(nsIChannel* channel, nsresult status)
|
|
|
|
{
|
|
|
|
nsresult channelStatus;
|
|
|
|
channel->GetStatus(&channelStatus);
|
|
|
|
if (NS_FAILED(channelStatus))
|
|
|
|
return channelStatus;
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(status))
|
|
|
|
return NS_OK; // caller and the channel are happy
|
|
|
|
|
|
|
|
// channel was OK, but caller wasn't: set the channel state
|
|
|
|
channel->Cancel(status);
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-07-18 16:46:13 +00:00
|
|
|
static char kFileIntro[] =
|
|
|
|
"<!DOCTYPE NETSCAPE-Bookmark-file-1>" NS_LINEBREAK
|
|
|
|
// Note: we write bookmarks in UTF-8
|
2007-05-20 01:00:19 +00:00
|
|
|
"<!-- This is an automatically generated file." NS_LINEBREAK
|
|
|
|
" It will be read and overwritten." NS_LINEBREAK
|
|
|
|
" DO NOT EDIT! -->" NS_LINEBREAK
|
2006-07-18 16:46:13 +00:00
|
|
|
"<META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=UTF-8\">" NS_LINEBREAK
|
|
|
|
"<TITLE>Bookmarks</TITLE>" NS_LINEBREAK;
|
|
|
|
static const char kRootIntro[] = "<H1";
|
|
|
|
static const char kCloseRootH1[] = "</H1>" NS_LINEBREAK NS_LINEBREAK;
|
|
|
|
|
|
|
|
static const char kBookmarkIntro[] = "<DL><p>" NS_LINEBREAK;
|
|
|
|
static const char kBookmarkClose[] = "</DL><p>" NS_LINEBREAK;
|
|
|
|
static const char kContainerIntro[] = "<DT><H3";
|
|
|
|
static const char kContainerClose[] = "</H3>" NS_LINEBREAK;
|
|
|
|
static const char kItemOpen[] = "<DT><A";
|
|
|
|
static const char kItemClose[] = "</A>" NS_LINEBREAK;
|
2007-05-15 04:18:25 +00:00
|
|
|
static const char kSeparator[] = "<HR";
|
2006-07-18 16:46:13 +00:00
|
|
|
static const char kQuoteStr[] = "\"";
|
|
|
|
static const char kCloseAngle[] = ">";
|
|
|
|
static const char kIndent[] = " ";
|
2007-05-11 19:26:14 +00:00
|
|
|
static const char kDescriptionIntro[] = "<DD>";
|
|
|
|
static const char kDescriptionClose[] = NS_LINEBREAK;
|
2006-07-18 16:46:13 +00:00
|
|
|
|
|
|
|
static const char kPlacesRootAttribute[] = " PLACES_ROOT=\"true\"";
|
2007-06-26 02:38:34 +00:00
|
|
|
static const char kBookmarksRootAttribute[] = " BOOKMARKS_MENU=\"true\"";
|
2007-03-07 21:22:54 +00:00
|
|
|
static const char kToolbarFolderAttribute[] = " PERSONAL_TOOLBAR_FOLDER=\"true\"";
|
2008-04-01 18:26:09 +00:00
|
|
|
static const char kUnfiledBookmarksFolderAttribute[] = " UNFILED_BOOKMARKS_FOLDER=\"true\"";
|
2006-07-18 16:46:13 +00:00
|
|
|
static const char kIconAttribute[] = " ICON=\"";
|
|
|
|
static const char kIconURIAttribute[] = " ICON_URI=\"";
|
|
|
|
static const char kHrefAttribute[] = " HREF=\"";
|
|
|
|
static const char kFeedURIAttribute[] = " FEEDURL=\"";
|
2007-03-08 09:29:19 +00:00
|
|
|
static const char kWebPanelAttribute[] = " WEB_PANEL=\"true\"";
|
2007-03-26 23:11:09 +00:00
|
|
|
static const char kKeywordAttribute[] = " SHORTCUTURL=\"";
|
2007-05-15 03:59:38 +00:00
|
|
|
static const char kPostDataAttribute[] = " POST_DATA=\"";
|
2007-05-15 04:18:25 +00:00
|
|
|
static const char kNameAttribute[] = " NAME=\"";
|
2007-05-19 00:40:55 +00:00
|
|
|
static const char kMicsumGenURIAttribute[] = " MICSUM_GEN_URI=\"";
|
|
|
|
static const char kDateAddedAttribute[] = " ADD_DATE=\"";
|
|
|
|
static const char kLastModifiedAttribute[] = " LAST_MODIFIED=\"";
|
2007-07-09 02:22:50 +00:00
|
|
|
static const char kLastCharsetAttribute[] = " LAST_CHARSET=\"";
|
2006-07-18 16:46:13 +00:00
|
|
|
|
2010-01-15 14:04:02 +00:00
|
|
|
|
2006-07-18 16:46:13 +00:00
|
|
|
// WriteContainerPrologue
|
|
|
|
//
|
|
|
|
// <DL><p>
|
|
|
|
//
|
|
|
|
// Goes after the container header (<H3...) but before the contents
|
|
|
|
static nsresult
|
2007-05-03 08:56:41 +00:00
|
|
|
WriteContainerPrologue(const nsACString& aIndent, nsIOutputStream* aOutput)
|
2006-07-18 16:46:13 +00:00
|
|
|
{
|
|
|
|
PRUint32 dummy;
|
2007-05-03 08:56:41 +00:00
|
|
|
nsresult rv = aOutput->Write(PromiseFlatCString(aIndent).get(), aIndent.Length(), &dummy);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2006-07-18 16:46:13 +00:00
|
|
|
rv = aOutput->Write(kBookmarkIntro, sizeof(kBookmarkIntro)-1, &dummy);
|
2007-05-03 08:56:41 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2006-07-18 16:46:13 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// WriteContainerEpilogue
|
|
|
|
//
|
|
|
|
// </DL><p>
|
|
|
|
//
|
|
|
|
// Goes after the container contents to close the container
|
|
|
|
static nsresult
|
2007-05-03 08:56:41 +00:00
|
|
|
WriteContainerEpilogue(const nsACString& aIndent, nsIOutputStream* aOutput)
|
2006-07-18 16:46:13 +00:00
|
|
|
{
|
|
|
|
PRUint32 dummy;
|
2007-05-03 08:56:41 +00:00
|
|
|
nsresult rv = aOutput->Write(PromiseFlatCString(aIndent).get(), aIndent.Length(), &dummy);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2006-07-18 16:46:13 +00:00
|
|
|
rv = aOutput->Write(kBookmarkClose, sizeof(kBookmarkClose)-1, &dummy);
|
2007-05-03 08:56:41 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2006-07-18 16:46:13 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// WriteFaviconAttribute
|
|
|
|
//
|
|
|
|
// This writes the 'ICON="data:asdlfkjas;ldkfja;skdljfasdf"' attribute for
|
|
|
|
// an item. We special-case chrome favicon URIs by just writing the chrome:
|
|
|
|
// URI.
|
|
|
|
static nsresult
|
|
|
|
WriteFaviconAttribute(const nsACString& aURI, nsIOutputStream* aOutput)
|
|
|
|
{
|
|
|
|
PRUint32 dummy;
|
|
|
|
|
2008-12-04 14:44:14 +00:00
|
|
|
// if favicon uri is invalid we skip the attribute silently, to avoid
|
|
|
|
// creating a corrupt file.
|
2006-07-18 16:46:13 +00:00
|
|
|
nsCOMPtr<nsIURI> uri;
|
2010-01-15 14:04:02 +00:00
|
|
|
nsresult rv = NS_NewURI(getter_AddRefs(uri), aURI);
|
2008-12-04 14:44:14 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
nsCAutoString warnMsg;
|
|
|
|
warnMsg.Append("Bookmarks Export: Found invalid favicon '");
|
|
|
|
warnMsg.Append(aURI);
|
|
|
|
warnMsg.Append("'");
|
|
|
|
NS_WARNING(warnMsg.get());
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2006-07-18 16:46:13 +00:00
|
|
|
|
|
|
|
// get favicon
|
2007-05-03 08:56:41 +00:00
|
|
|
nsCOMPtr<nsIFaviconService> faviconService = do_GetService(NS_FAVICONSERVICE_CONTRACTID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2006-07-18 16:46:13 +00:00
|
|
|
nsCOMPtr<nsIURI> faviconURI;
|
|
|
|
rv = faviconService->GetFaviconForPage(uri, getter_AddRefs(faviconURI));
|
|
|
|
if (rv == NS_ERROR_NOT_AVAILABLE)
|
|
|
|
return NS_OK; // no favicon
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv); // anything else is error
|
|
|
|
|
|
|
|
nsCAutoString faviconScheme;
|
|
|
|
nsCAutoString faviconSpec;
|
|
|
|
rv = faviconURI->GetSpec(faviconSpec);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
rv = faviconURI->GetScheme(faviconScheme);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// write favicon URI: 'ICON_URI="..."'
|
|
|
|
rv = aOutput->Write(kIconURIAttribute, sizeof(kIconURIAttribute)-1, &dummy);
|
2007-05-03 08:56:41 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2006-07-18 16:47:23 +00:00
|
|
|
rv = WriteEscapedUrl(faviconSpec, aOutput);
|
2007-05-03 08:56:41 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2006-07-18 16:46:13 +00:00
|
|
|
rv = aOutput->Write(kQuoteStr, sizeof(kQuoteStr)-1, &dummy);
|
2007-05-03 08:56:41 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2006-07-18 16:46:13 +00:00
|
|
|
|
2007-05-03 08:56:41 +00:00
|
|
|
if (!faviconScheme.EqualsLiteral("chrome")) {
|
2006-07-18 16:46:13 +00:00
|
|
|
// only store data for non-chrome URIs
|
|
|
|
|
2008-07-03 18:07:56 +00:00
|
|
|
nsAutoString faviconContents;
|
|
|
|
rv = faviconService->GetFaviconDataAsDataURL(faviconURI, faviconContents);
|
2006-07-18 16:46:13 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2008-07-03 18:07:56 +00:00
|
|
|
if (faviconContents.Length() > 0) {
|
2006-07-18 16:46:13 +00:00
|
|
|
rv = aOutput->Write(kIconAttribute, sizeof(kIconAttribute)-1, &dummy);
|
2007-05-03 08:56:41 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2008-07-03 18:07:56 +00:00
|
|
|
NS_ConvertUTF16toUTF8 utf8Favicon(faviconContents);
|
|
|
|
rv = aOutput->Write(utf8Favicon.get(), utf8Favicon.Length(), &dummy);
|
2007-05-03 08:56:41 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2006-07-18 16:46:13 +00:00
|
|
|
rv = aOutput->Write(kQuoteStr, sizeof(kQuoteStr)-1, &dummy);
|
2007-05-03 08:56:41 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2006-07-18 16:46:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-01-15 14:04:02 +00:00
|
|
|
|
2007-05-19 00:40:55 +00:00
|
|
|
// WriteDateAttribute
|
|
|
|
//
|
|
|
|
// This writes the '{attr value=}"{time in seconds}"' attribute for
|
|
|
|
// an item.
|
|
|
|
static nsresult
|
|
|
|
WriteDateAttribute(const char aAttributeStart[], PRInt32 aLength, PRTime aAttributeValue, nsIOutputStream* aOutput)
|
|
|
|
{
|
|
|
|
// write attribute start
|
|
|
|
PRUint32 dummy;
|
|
|
|
nsresult rv = aOutput->Write(aAttributeStart, aLength, &dummy);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-06-26 02:38:34 +00:00
|
|
|
|
|
|
|
// in bookmarks.html this value is in seconds, not microseconds
|
|
|
|
aAttributeValue /= 1000000;
|
|
|
|
|
2007-05-19 00:40:55 +00:00
|
|
|
// write attribute value
|
2007-06-26 02:38:34 +00:00
|
|
|
char dateInSeconds[32];
|
|
|
|
PR_snprintf(dateInSeconds, sizeof(dateInSeconds), "%lld", aAttributeValue);
|
|
|
|
rv = aOutput->Write(dateInSeconds, strlen(dateInSeconds), &dummy);
|
2007-05-19 00:40:55 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-01-15 14:04:02 +00:00
|
|
|
rv = aOutput->Write(kQuoteStr, sizeof(kQuoteStr)-1, &dummy);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
2007-05-19 00:40:55 +00:00
|
|
|
}
|
|
|
|
|
2006-07-18 16:46:13 +00:00
|
|
|
|
2007-05-03 08:56:41 +00:00
|
|
|
// nsPlacesImportExportService::WriteContainer
|
2006-07-18 16:46:13 +00:00
|
|
|
//
|
|
|
|
// Writes out all the necessary parts of a bookmarks folder.
|
|
|
|
nsresult
|
2010-01-15 14:04:02 +00:00
|
|
|
nsPlacesImportExportService::WriteContainer(nsINavHistoryResultNode* aFolder,
|
|
|
|
const nsACString& aIndent,
|
|
|
|
nsIOutputStream* aOutput)
|
2006-07-18 16:46:13 +00:00
|
|
|
{
|
|
|
|
nsresult rv = WriteContainerHeader(aFolder, aIndent, aOutput);
|
2007-05-03 08:56:41 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2006-07-18 16:46:13 +00:00
|
|
|
rv = WriteContainerPrologue(aIndent, aOutput);
|
2007-05-03 08:56:41 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2006-07-18 16:46:13 +00:00
|
|
|
rv = WriteContainerContents(aFolder, aIndent, aOutput);
|
2007-05-03 08:56:41 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2006-07-18 16:46:13 +00:00
|
|
|
rv = WriteContainerEpilogue(aIndent, aOutput);
|
2007-05-03 08:56:41 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2006-07-18 16:46:13 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-05-03 08:56:41 +00:00
|
|
|
// nsPlacesImportExportService::WriteContainerHeader
|
2006-07-18 16:46:13 +00:00
|
|
|
//
|
|
|
|
// This writes '<DL><H3>Title</H3>'
|
|
|
|
// Remember folders can also have favicons, which we put in the H3 tag
|
|
|
|
nsresult
|
2010-01-15 14:04:02 +00:00
|
|
|
nsPlacesImportExportService::WriteContainerHeader(nsINavHistoryResultNode* aFolder,
|
|
|
|
const nsACString& aIndent,
|
|
|
|
nsIOutputStream* aOutput)
|
2006-07-18 16:46:13 +00:00
|
|
|
{
|
|
|
|
PRUint32 dummy;
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
// indent
|
2007-05-03 08:56:41 +00:00
|
|
|
if (!aIndent.IsEmpty()) {
|
|
|
|
rv = aOutput->Write(PromiseFlatCString(aIndent).get(), aIndent.Length(), &dummy);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2006-07-18 16:46:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// "<DL H3"
|
|
|
|
rv = aOutput->Write(kContainerIntro, sizeof(kContainerIntro)-1, &dummy);
|
2007-05-03 08:56:41 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2006-07-18 16:46:13 +00:00
|
|
|
|
2007-05-25 05:41:49 +00:00
|
|
|
// get folder id
|
|
|
|
PRInt64 folderId;
|
|
|
|
rv = aFolder->GetItemId(&folderId);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2007-05-19 00:40:55 +00:00
|
|
|
// write ADD_DATE
|
|
|
|
PRTime dateAdded = 0;
|
2007-05-25 05:41:49 +00:00
|
|
|
rv = aFolder->GetDateAdded(&dateAdded);
|
2007-05-19 00:40:55 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (dateAdded) {
|
|
|
|
rv = WriteDateAttribute(kDateAddedAttribute, sizeof(kDateAddedAttribute)-1, dateAdded, aOutput);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
// write LAST_MODIFIED
|
|
|
|
PRTime lastModified = 0;
|
2007-05-25 05:41:49 +00:00
|
|
|
rv = aFolder->GetLastModified(&lastModified);
|
2007-05-19 00:40:55 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (lastModified) {
|
|
|
|
rv = WriteDateAttribute(kLastModifiedAttribute, sizeof(kLastModifiedAttribute)-1, lastModified, aOutput);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
2007-05-18 22:00:36 +00:00
|
|
|
PRInt64 placesRoot;
|
|
|
|
rv = mBookmarksService->GetPlacesRoot(&placesRoot);
|
2010-01-15 14:04:02 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-05-18 22:00:36 +00:00
|
|
|
|
2007-11-20 02:01:53 +00:00
|
|
|
PRInt64 bookmarksMenuFolder;
|
|
|
|
rv = mBookmarksService->GetBookmarksMenuFolder(&bookmarksMenuFolder);
|
2010-01-15 14:04:02 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-05-18 22:00:36 +00:00
|
|
|
|
|
|
|
PRInt64 toolbarFolder;
|
|
|
|
rv = mBookmarksService->GetToolbarFolder(&toolbarFolder);
|
2010-01-15 14:04:02 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-05-18 22:00:36 +00:00
|
|
|
|
2008-04-01 18:26:09 +00:00
|
|
|
PRInt64 unfiledBookmarksFolder;
|
|
|
|
rv = mBookmarksService->GetUnfiledBookmarksFolder(&unfiledBookmarksFolder);
|
2010-01-15 14:04:02 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2008-04-01 18:26:09 +00:00
|
|
|
|
2006-07-18 16:46:13 +00:00
|
|
|
// " PERSONAL_TOOLBAR_FOLDER="true"", etc.
|
2007-05-25 05:41:49 +00:00
|
|
|
if (folderId == placesRoot) {
|
2006-07-18 16:46:13 +00:00
|
|
|
rv = aOutput->Write(kPlacesRootAttribute, sizeof(kPlacesRootAttribute)-1, &dummy);
|
2007-05-03 08:56:41 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-01-15 14:04:02 +00:00
|
|
|
}
|
|
|
|
else if (folderId == bookmarksMenuFolder) {
|
2006-07-18 16:46:13 +00:00
|
|
|
rv = aOutput->Write(kBookmarksRootAttribute, sizeof(kBookmarksRootAttribute)-1, &dummy);
|
2007-05-03 08:56:41 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-01-15 14:04:02 +00:00
|
|
|
}
|
|
|
|
else if (folderId == unfiledBookmarksFolder) {
|
2008-04-01 18:26:09 +00:00
|
|
|
rv = aOutput->Write(kUnfiledBookmarksFolderAttribute, sizeof(kUnfiledBookmarksFolderAttribute)-1, &dummy);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-01-15 14:04:02 +00:00
|
|
|
}
|
|
|
|
else if (folderId == toolbarFolder) {
|
2007-03-07 21:22:54 +00:00
|
|
|
rv = aOutput->Write(kToolbarFolderAttribute, sizeof(kToolbarFolderAttribute)-1, &dummy);
|
2007-05-03 08:56:41 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2006-07-18 16:46:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ">"
|
|
|
|
rv = aOutput->Write(kCloseAngle, sizeof(kCloseAngle)-1, &dummy);
|
2007-05-03 08:56:41 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2006-07-18 16:46:13 +00:00
|
|
|
|
|
|
|
// title
|
2007-05-25 05:41:49 +00:00
|
|
|
rv = WriteTitle(aFolder, aOutput);
|
2007-05-03 08:56:41 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2006-07-18 16:46:13 +00:00
|
|
|
|
|
|
|
// "</H3>\n"
|
|
|
|
rv = aOutput->Write(kContainerClose, sizeof(kContainerClose)-1, &dummy);
|
2007-05-11 19:26:14 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// description
|
2007-05-25 05:41:49 +00:00
|
|
|
rv = WriteDescription(folderId, nsINavBookmarksService::TYPE_FOLDER, aOutput);
|
2007-05-11 19:26:14 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2006-07-18 16:46:13 +00:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-05-25 05:41:49 +00:00
|
|
|
// nsPlacesImportExportService::WriteTitle
|
2006-07-18 16:46:13 +00:00
|
|
|
//
|
2007-05-25 05:41:49 +00:00
|
|
|
// Retrieves, escapes and writes the title to the stream.
|
2006-07-18 16:46:13 +00:00
|
|
|
nsresult
|
2010-01-15 14:04:02 +00:00
|
|
|
nsPlacesImportExportService::WriteTitle(nsINavHistoryResultNode* aItem,
|
|
|
|
nsIOutputStream* aOutput)
|
2006-07-18 16:46:13 +00:00
|
|
|
{
|
2007-05-25 05:41:49 +00:00
|
|
|
// XXX Bug 381767 - support titles for separators
|
|
|
|
PRUint32 type = 0;
|
|
|
|
nsresult rv = aItem->GetType(&type);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
if (type == nsINavHistoryResultNode::RESULT_TYPE_SEPARATOR)
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
|
|
|
|
nsCAutoString title;
|
|
|
|
rv = aItem->GetTitle(title);
|
2006-07-18 16:46:13 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2007-05-25 05:41:49 +00:00
|
|
|
char* escapedTitle = nsEscapeHTML(title.get());
|
2006-07-18 16:46:13 +00:00
|
|
|
if (escapedTitle) {
|
|
|
|
PRUint32 dummy;
|
|
|
|
rv = aOutput->Write(escapedTitle, strlen(escapedTitle), &dummy);
|
|
|
|
nsMemory::Free(escapedTitle);
|
2007-05-03 08:56:41 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2006-07-18 16:46:13 +00:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-05-25 05:41:49 +00:00
|
|
|
// nsPlacesImportExportService::WriteDescription
|
|
|
|
//
|
|
|
|
// Write description out for all item types.
|
2007-05-11 19:26:14 +00:00
|
|
|
nsresult
|
|
|
|
nsPlacesImportExportService::WriteDescription(PRInt64 aItemId, PRInt32 aType,
|
|
|
|
nsIOutputStream* aOutput)
|
|
|
|
{
|
|
|
|
PRBool hasDescription = PR_FALSE;
|
|
|
|
nsresult rv = mAnnotationService->ItemHasAnnotation(aItemId,
|
|
|
|
DESCRIPTION_ANNO,
|
|
|
|
&hasDescription);
|
|
|
|
if (NS_FAILED(rv) || !hasDescription)
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
nsAutoString description;
|
|
|
|
rv = mAnnotationService->GetItemAnnotationString(aItemId, DESCRIPTION_ANNO,
|
|
|
|
description);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
char* escapedDesc = nsEscapeHTML(NS_ConvertUTF16toUTF8(description).get());
|
|
|
|
if (escapedDesc) {
|
|
|
|
PRUint32 dummy;
|
|
|
|
rv = aOutput->Write(kDescriptionIntro, sizeof(kDescriptionIntro)-1, &dummy);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
nsMemory::Free(escapedDesc);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
rv = aOutput->Write(escapedDesc, strlen(escapedDesc), &dummy);
|
|
|
|
nsMemory::Free(escapedDesc);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-01-15 14:04:02 +00:00
|
|
|
rv = aOutput->Write(kDescriptionClose, sizeof(kDescriptionClose)-1, &dummy);
|
2007-05-11 19:26:14 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2007-05-03 08:56:41 +00:00
|
|
|
// nsBookmarks::WriteItem
|
2006-07-18 16:46:13 +00:00
|
|
|
//
|
|
|
|
// "<DT><A HREF="..." ICON="...">Name</A>"
|
2007-03-08 09:29:19 +00:00
|
|
|
nsresult
|
2007-05-03 08:56:41 +00:00
|
|
|
nsPlacesImportExportService::WriteItem(nsINavHistoryResultNode* aItem,
|
2010-01-15 14:04:02 +00:00
|
|
|
const nsACString& aIndent,
|
|
|
|
nsIOutputStream* aOutput)
|
2006-07-18 16:46:13 +00:00
|
|
|
{
|
2008-12-04 14:44:14 +00:00
|
|
|
// before doing any attempt to write the item check that uri is valid, if the
|
|
|
|
// item has a bad uri we skip it silently, otherwise we could stop while
|
|
|
|
// exporting, generating a corrupt file.
|
|
|
|
nsCAutoString uri;
|
2010-01-15 14:04:02 +00:00
|
|
|
nsresult rv = aItem->GetUri(uri);
|
2008-12-04 14:44:14 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
nsCOMPtr<nsIURI> pageURI;
|
|
|
|
rv = NS_NewURI(getter_AddRefs(pageURI), uri, nsnull);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
nsCAutoString warnMsg;
|
|
|
|
warnMsg.Append("Bookmarks Export: Found invalid item uri '");
|
|
|
|
warnMsg.Append(uri);
|
|
|
|
warnMsg.Append("'");
|
|
|
|
NS_WARNING(warnMsg.get());
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2006-07-18 16:46:13 +00:00
|
|
|
// indent
|
2010-01-15 14:04:02 +00:00
|
|
|
PRUint32 dummy;
|
2007-05-03 08:56:41 +00:00
|
|
|
if (!aIndent.IsEmpty()) {
|
|
|
|
rv = aOutput->Write(PromiseFlatCString(aIndent).get(), aIndent.Length(), &dummy);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2006-07-18 16:46:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// '<DT><A'
|
|
|
|
rv = aOutput->Write(kItemOpen, sizeof(kItemOpen)-1, &dummy);
|
2007-05-03 08:56:41 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2006-07-18 16:46:13 +00:00
|
|
|
|
|
|
|
// ' HREF="http://..."' - note that we need to call GetURI on the result
|
|
|
|
// node because some nodes (eg queries) generate this lazily.
|
|
|
|
rv = aOutput->Write(kHrefAttribute, sizeof(kHrefAttribute)-1, &dummy);
|
2007-05-03 08:56:41 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2006-07-18 16:47:23 +00:00
|
|
|
rv = WriteEscapedUrl(uri, aOutput);
|
2007-05-03 08:56:41 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2006-07-18 16:46:13 +00:00
|
|
|
rv = aOutput->Write(kQuoteStr, sizeof(kQuoteStr)-1, &dummy);
|
2007-05-03 08:56:41 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2006-07-18 16:46:13 +00:00
|
|
|
|
2007-05-19 00:40:55 +00:00
|
|
|
// write ADD_DATE
|
|
|
|
PRTime dateAdded = 0;
|
|
|
|
rv = aItem->GetDateAdded(&dateAdded);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (dateAdded) {
|
|
|
|
rv = WriteDateAttribute(kDateAddedAttribute, sizeof(kDateAddedAttribute)-1, dateAdded, aOutput);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
// write LAST_MODIFIED
|
|
|
|
PRTime lastModified = 0;
|
|
|
|
rv = aItem->GetLastModified(&lastModified);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (lastModified) {
|
|
|
|
rv = WriteDateAttribute(kLastModifiedAttribute, sizeof(kLastModifiedAttribute)-1, lastModified, aOutput);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
2006-07-18 16:46:13 +00:00
|
|
|
// ' ICON="..."'
|
|
|
|
rv = WriteFaviconAttribute(uri, aOutput);
|
2007-05-03 08:56:41 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2006-07-18 16:46:13 +00:00
|
|
|
|
2007-05-10 08:05:19 +00:00
|
|
|
// get item id
|
|
|
|
PRInt64 itemId;
|
|
|
|
rv = aItem->GetItemId(&itemId);
|
2007-03-08 09:29:19 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2007-03-26 23:11:09 +00:00
|
|
|
// keyword (shortcuturl)
|
|
|
|
nsAutoString keyword;
|
2007-05-10 08:05:19 +00:00
|
|
|
rv = mBookmarksService->GetKeywordForBookmark(itemId, keyword);
|
2007-05-03 08:56:41 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-03-26 23:11:09 +00:00
|
|
|
if (!keyword.IsEmpty()) {
|
|
|
|
rv = aOutput->Write(kKeywordAttribute, sizeof(kKeywordAttribute)-1, &dummy);
|
2007-05-03 08:56:41 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-03-26 23:11:09 +00:00
|
|
|
char* escapedKeyword = nsEscapeHTML(NS_ConvertUTF16toUTF8(keyword).get());
|
|
|
|
rv = aOutput->Write(escapedKeyword, strlen(escapedKeyword), &dummy);
|
|
|
|
nsMemory::Free(escapedKeyword);
|
2007-05-03 08:56:41 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-03-26 23:11:09 +00:00
|
|
|
rv = aOutput->Write(kQuoteStr, sizeof(kQuoteStr)-1, &dummy);
|
2007-05-03 08:56:41 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-03-26 23:11:09 +00:00
|
|
|
}
|
|
|
|
|
2007-05-15 03:59:38 +00:00
|
|
|
// post data
|
|
|
|
PRBool hasPostData;
|
2007-12-05 01:56:55 +00:00
|
|
|
rv = mAnnotationService->ItemHasAnnotation(itemId, POST_DATA_ANNO,
|
2007-05-15 03:59:38 +00:00
|
|
|
&hasPostData);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
if (hasPostData) {
|
|
|
|
nsAutoString postData;
|
2007-12-05 01:56:55 +00:00
|
|
|
rv = mAnnotationService->GetItemAnnotationString(itemId, POST_DATA_ANNO,
|
2007-05-15 03:59:38 +00:00
|
|
|
postData);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
rv = aOutput->Write(kPostDataAttribute, sizeof(kPostDataAttribute)-1, &dummy);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
char* escapedPostData = nsEscapeHTML(NS_ConvertUTF16toUTF8(postData).get());
|
|
|
|
rv = aOutput->Write(escapedPostData, strlen(escapedPostData), &dummy);
|
|
|
|
nsMemory::Free(escapedPostData);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
rv = aOutput->Write(kQuoteStr, sizeof(kQuoteStr)-1, &dummy);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
2007-03-08 09:29:19 +00:00
|
|
|
// Write WEB_PANEL="true" if the load-in-sidebar annotation is set for the
|
|
|
|
// item
|
|
|
|
PRBool loadInSidebar = PR_FALSE;
|
2007-05-10 08:05:19 +00:00
|
|
|
rv = mAnnotationService->ItemHasAnnotation(itemId, LOAD_IN_SIDEBAR_ANNO,
|
|
|
|
&loadInSidebar);
|
2007-03-08 09:29:19 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
if (loadInSidebar)
|
|
|
|
aOutput->Write(kWebPanelAttribute, sizeof(kWebPanelAttribute)-1, &dummy);
|
|
|
|
|
2007-05-15 23:23:51 +00:00
|
|
|
// microsummary
|
|
|
|
nsCOMPtr<nsIMicrosummary> microsummary;
|
|
|
|
rv = mMicrosummaryService->GetMicrosummary(itemId, getter_AddRefs(microsummary));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
if (microsummary) {
|
|
|
|
nsCOMPtr<nsIMicrosummaryGenerator> generator;
|
|
|
|
rv = microsummary->GetGenerator(getter_AddRefs(generator));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
nsCOMPtr<nsIURI> genURI;
|
|
|
|
rv = generator->GetUri(getter_AddRefs(genURI));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
nsCAutoString spec;
|
|
|
|
rv = genURI->GetSpec(spec);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// write out generator URI
|
2007-05-19 00:40:55 +00:00
|
|
|
rv = aOutput->Write(kMicsumGenURIAttribute, sizeof(kMicsumGenURIAttribute)-1, &dummy);
|
2007-05-15 23:23:51 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
rv = WriteEscapedUrl(spec, aOutput);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
rv = aOutput->Write(kQuoteStr, sizeof(kQuoteStr)-1, &dummy);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
2007-07-09 02:22:50 +00:00
|
|
|
// last charset
|
2008-04-08 18:41:04 +00:00
|
|
|
nsAutoString lastCharset;
|
|
|
|
if (NS_SUCCEEDED(mHistoryService->GetCharsetForURI(pageURI, lastCharset)) &&
|
|
|
|
!lastCharset.IsEmpty()) {
|
2007-07-09 02:22:50 +00:00
|
|
|
rv = aOutput->Write(kLastCharsetAttribute, sizeof(kLastCharsetAttribute)-1, &dummy);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
char* escapedLastCharset = nsEscapeHTML(NS_ConvertUTF16toUTF8(lastCharset).get());
|
|
|
|
rv = aOutput->Write(escapedLastCharset, strlen(escapedLastCharset), &dummy);
|
|
|
|
nsMemory::Free(escapedLastCharset);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
rv = aOutput->Write(kQuoteStr, sizeof(kQuoteStr)-1, &dummy);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
2006-07-18 16:46:13 +00:00
|
|
|
|
|
|
|
// '>'
|
|
|
|
rv = aOutput->Write(kCloseAngle, sizeof(kCloseAngle)-1, &dummy);
|
2007-05-03 08:56:41 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2006-07-18 16:46:13 +00:00
|
|
|
|
|
|
|
// title
|
2007-05-03 08:56:41 +00:00
|
|
|
nsCAutoString title;
|
|
|
|
rv = aItem->GetTitle(title);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
char* escapedTitle = nsEscapeHTML(title.get());
|
2006-07-18 16:46:13 +00:00
|
|
|
if (escapedTitle) {
|
|
|
|
rv = aOutput->Write(escapedTitle, strlen(escapedTitle), &dummy);
|
|
|
|
nsMemory::Free(escapedTitle);
|
2007-05-03 08:56:41 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2006-07-18 16:46:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// '</A>\n'
|
|
|
|
rv = aOutput->Write(kItemClose, sizeof(kItemClose)-1, &dummy);
|
2007-05-03 08:56:41 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2006-07-18 16:46:13 +00:00
|
|
|
|
2007-05-11 19:26:14 +00:00
|
|
|
// description
|
|
|
|
rv = WriteDescription(itemId, nsINavBookmarksService::TYPE_BOOKMARK, aOutput);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2006-07-18 16:46:13 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// WriteLivemark
|
|
|
|
//
|
|
|
|
// Similar to WriteItem, this has an additional FEEDURL attribute and
|
|
|
|
// the HREF is optional and points to the source page.
|
|
|
|
nsresult
|
2007-05-25 05:41:49 +00:00
|
|
|
nsPlacesImportExportService::WriteLivemark(nsINavHistoryResultNode* aFolder, const nsACString& aIndent,
|
2006-07-18 16:46:13 +00:00
|
|
|
nsIOutputStream* aOutput)
|
|
|
|
{
|
|
|
|
PRUint32 dummy;
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
// indent
|
2007-05-03 08:56:41 +00:00
|
|
|
if (!aIndent.IsEmpty()) {
|
|
|
|
rv = aOutput->Write(PromiseFlatCString(aIndent).get(), aIndent.Length(), &dummy);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2006-07-18 16:46:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// '<DT><A'
|
|
|
|
rv = aOutput->Write(kItemOpen, sizeof(kItemOpen)-1, &dummy);
|
2007-05-03 08:56:41 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2006-07-18 16:46:13 +00:00
|
|
|
|
2007-05-25 05:41:49 +00:00
|
|
|
// get folder id
|
|
|
|
PRInt64 folderId;
|
|
|
|
rv = aFolder->GetItemId(&folderId);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2006-07-18 16:46:13 +00:00
|
|
|
// get feed URI
|
|
|
|
nsCOMPtr<nsIURI> feedURI;
|
2007-05-25 05:41:49 +00:00
|
|
|
rv = mLivemarkService->GetFeedURI(folderId, getter_AddRefs(feedURI));
|
2006-07-18 16:46:13 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-05-28 03:50:34 +00:00
|
|
|
if (feedURI) {
|
|
|
|
nsCString feedSpec;
|
|
|
|
rv = feedURI->GetSpec(feedSpec);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2006-07-18 16:46:13 +00:00
|
|
|
|
2007-05-28 03:50:34 +00:00
|
|
|
// write feed URI
|
|
|
|
rv = aOutput->Write(kFeedURIAttribute, sizeof(kFeedURIAttribute)-1, &dummy);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
rv = WriteEscapedUrl(feedSpec, aOutput);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
rv = aOutput->Write(kQuoteStr, sizeof(kQuoteStr)-1, &dummy);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
2006-07-18 16:46:13 +00:00
|
|
|
|
|
|
|
// get the optional site URI
|
|
|
|
nsCOMPtr<nsIURI> siteURI;
|
2007-05-25 05:41:49 +00:00
|
|
|
rv = mLivemarkService->GetSiteURI(folderId, getter_AddRefs(siteURI));
|
2006-07-18 16:46:13 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
if (siteURI) {
|
|
|
|
nsCString siteSpec;
|
|
|
|
rv = siteURI->GetSpec(siteSpec);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// write site URI
|
|
|
|
rv = aOutput->Write(kHrefAttribute, sizeof(kHrefAttribute)-1, &dummy);
|
2007-05-03 08:56:41 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2006-07-18 16:47:23 +00:00
|
|
|
rv = WriteEscapedUrl(siteSpec, aOutput);
|
2007-05-03 08:56:41 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2006-07-18 16:46:13 +00:00
|
|
|
rv = aOutput->Write(kQuoteStr, sizeof(kQuoteStr)-1, &dummy);
|
2007-05-03 08:56:41 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2006-07-18 16:46:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// '>'
|
|
|
|
rv = aOutput->Write(kCloseAngle, sizeof(kCloseAngle)-1, &dummy);
|
2007-05-03 08:56:41 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2006-07-18 16:46:13 +00:00
|
|
|
|
|
|
|
// title
|
2007-05-25 05:41:49 +00:00
|
|
|
rv = WriteTitle(aFolder, aOutput);
|
2007-05-03 08:56:41 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2006-07-18 16:46:13 +00:00
|
|
|
|
|
|
|
// '</A>\n'
|
|
|
|
rv = aOutput->Write(kItemClose, sizeof(kItemClose)-1, &dummy);
|
2007-05-03 08:56:41 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2006-07-18 16:46:13 +00:00
|
|
|
|
2007-05-11 19:26:14 +00:00
|
|
|
// description
|
2007-05-25 05:41:49 +00:00
|
|
|
rv = WriteDescription(folderId, nsINavBookmarksService::TYPE_BOOKMARK, aOutput);
|
2007-05-11 19:26:14 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2006-07-18 16:46:13 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-05-15 04:18:25 +00:00
|
|
|
// nsPlacesImportExportService::WriteSeparator
|
2006-07-18 16:46:13 +00:00
|
|
|
//
|
2007-05-15 04:18:25 +00:00
|
|
|
// "<HR NAME="...">"
|
2006-07-18 16:46:13 +00:00
|
|
|
nsresult
|
2007-05-15 04:18:25 +00:00
|
|
|
nsPlacesImportExportService::WriteSeparator(nsINavHistoryResultNode* aItem,
|
|
|
|
const nsACString& aIndent,
|
|
|
|
nsIOutputStream* aOutput)
|
2006-07-18 16:46:13 +00:00
|
|
|
{
|
|
|
|
PRUint32 dummy;
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
// indent
|
2007-05-03 08:56:41 +00:00
|
|
|
if (!aIndent.IsEmpty()) {
|
2007-05-15 04:18:25 +00:00
|
|
|
rv = aOutput->Write(PromiseFlatCString(aIndent).get(), aIndent.Length(),
|
|
|
|
&dummy);
|
2007-05-03 08:56:41 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2006-07-18 16:46:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rv = aOutput->Write(kSeparator, sizeof(kSeparator)-1, &dummy);
|
2010-01-15 14:04:02 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-05-15 04:18:25 +00:00
|
|
|
|
|
|
|
// XXX: separator result nodes don't support the title getter yet
|
|
|
|
PRInt64 itemId;
|
|
|
|
rv = aItem->GetItemId(&itemId);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2007-05-18 22:00:36 +00:00
|
|
|
// Note: we can't write the separator ID or anything else other than NAME
|
2007-05-19 00:40:55 +00:00
|
|
|
// because it makes Firefox 2.x crash/hang - see bug #381129
|
2007-05-17 06:06:48 +00:00
|
|
|
|
2008-01-09 03:54:37 +00:00
|
|
|
nsCAutoString title;
|
2007-05-15 04:18:25 +00:00
|
|
|
rv = mBookmarksService->GetItemTitle(itemId, title);
|
|
|
|
if (NS_SUCCEEDED(rv) && !title.IsEmpty()) {
|
|
|
|
rv = aOutput->Write(kNameAttribute, strlen(kNameAttribute), &dummy);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2008-01-09 03:54:37 +00:00
|
|
|
char* escapedTitle = nsEscapeHTML(title.get());
|
2007-05-15 04:18:25 +00:00
|
|
|
if (escapedTitle) {
|
|
|
|
PRUint32 dummy;
|
|
|
|
rv = aOutput->Write(escapedTitle, strlen(escapedTitle), &dummy);
|
|
|
|
nsMemory::Free(escapedTitle);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
rv = aOutput->Write(kQuoteStr, sizeof(kQuoteStr)-1, &dummy);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// '>'
|
|
|
|
rv = aOutput->Write(kCloseAngle, sizeof(kCloseAngle)-1, &dummy);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// line break
|
|
|
|
rv = aOutput->Write(NS_LINEBREAK, sizeof(NS_LINEBREAK)-1, &dummy);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2006-07-18 16:46:13 +00:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-07-18 16:47:23 +00:00
|
|
|
// WriteEscapedUrl
|
|
|
|
//
|
|
|
|
// Writes the given string to the stream escaped as necessary for URLs.
|
|
|
|
//
|
|
|
|
// Unfortunately, the old bookmarks system uses a custom hardcoded and
|
|
|
|
// braindead escaping scheme that we need to emulate. It just replaces
|
|
|
|
// quotes with %22 and that's it.
|
|
|
|
nsresult
|
|
|
|
WriteEscapedUrl(const nsCString& aString, nsIOutputStream* aOutput)
|
|
|
|
{
|
|
|
|
nsCAutoString escaped(aString);
|
|
|
|
PRInt32 offset;
|
|
|
|
while ((offset = escaped.FindChar('\"')) >= 0) {
|
|
|
|
escaped.Cut(offset, 1);
|
|
|
|
escaped.Insert(NS_LITERAL_CSTRING("%22"), offset);
|
|
|
|
}
|
|
|
|
PRUint32 dummy;
|
|
|
|
return aOutput->Write(escaped.get(), escaped.Length(), &dummy);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-05-03 08:56:41 +00:00
|
|
|
// nsPlacesImportExportService::WriteContainerContents
|
2006-07-18 16:46:13 +00:00
|
|
|
//
|
|
|
|
// The indent here is the indent of the parent. We will add an additional
|
|
|
|
// indent before writing data.
|
|
|
|
nsresult
|
2010-01-15 14:04:02 +00:00
|
|
|
nsPlacesImportExportService::WriteContainerContents(nsINavHistoryResultNode* aFolder,
|
|
|
|
const nsACString& aIndent,
|
|
|
|
nsIOutputStream* aOutput)
|
2006-07-18 16:46:13 +00:00
|
|
|
{
|
2007-05-03 08:56:41 +00:00
|
|
|
nsCAutoString myIndent(aIndent);
|
2006-07-18 16:46:13 +00:00
|
|
|
myIndent.Append(kIndent);
|
|
|
|
|
2007-05-25 05:41:49 +00:00
|
|
|
PRInt64 folderId;
|
|
|
|
nsresult rv = aFolder->GetItemId(&folderId);
|
2006-07-18 16:46:13 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2007-05-25 05:41:49 +00:00
|
|
|
nsCOMPtr<nsINavHistoryContainerResultNode> folderNode = do_QueryInterface(aFolder, &rv);
|
2007-05-03 08:56:41 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2007-05-25 05:41:49 +00:00
|
|
|
rv = folderNode->SetContainerOpen(PR_TRUE);
|
2006-07-18 16:46:13 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2007-05-03 08:56:41 +00:00
|
|
|
PRUint32 childCount = 0;
|
2007-05-25 05:41:49 +00:00
|
|
|
folderNode->GetChildCount(&childCount);
|
2007-05-03 08:56:41 +00:00
|
|
|
for (PRUint32 i = 0; i < childCount; ++i) {
|
|
|
|
nsCOMPtr<nsINavHistoryResultNode> child;
|
2007-05-25 05:41:49 +00:00
|
|
|
rv = folderNode->GetChild(i, getter_AddRefs(child));
|
2007-05-03 08:56:41 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
PRUint32 type = 0;
|
|
|
|
rv = child->GetType(&type);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
if (type == nsINavHistoryResultNode::RESULT_TYPE_FOLDER) {
|
2006-07-18 16:46:13 +00:00
|
|
|
// bookmarks folder
|
2007-05-25 05:41:49 +00:00
|
|
|
PRInt64 childFolderId;
|
|
|
|
rv = child->GetItemId(&childFolderId);
|
2007-05-03 08:56:41 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2006-07-18 16:46:13 +00:00
|
|
|
|
|
|
|
// it could be a regular folder or it could be a livemark
|
|
|
|
PRBool isLivemark;
|
2007-05-25 05:41:49 +00:00
|
|
|
rv = mLivemarkService->IsLivemark(childFolderId, &isLivemark);
|
2006-07-18 16:46:13 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (isLivemark)
|
2007-05-25 05:41:49 +00:00
|
|
|
rv = WriteLivemark(child, myIndent, aOutput);
|
2006-07-18 16:46:13 +00:00
|
|
|
else
|
2007-05-25 05:41:49 +00:00
|
|
|
rv = WriteContainer(child, myIndent, aOutput);
|
2010-01-15 14:04:02 +00:00
|
|
|
}
|
|
|
|
else if (type == nsINavHistoryResultNode::RESULT_TYPE_SEPARATOR) {
|
2007-05-15 04:18:25 +00:00
|
|
|
rv = WriteSeparator(child, myIndent, aOutput);
|
2010-01-15 14:04:02 +00:00
|
|
|
}
|
|
|
|
else {
|
2007-05-03 08:56:41 +00:00
|
|
|
rv = WriteItem(child, myIndent, aOutput);
|
2006-07-18 16:46:13 +00:00
|
|
|
}
|
2007-05-03 08:56:41 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2006-07-18 16:46:13 +00:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-01-15 14:04:02 +00:00
|
|
|
|
2009-04-17 12:42:34 +00:00
|
|
|
// NotifyImportObservers
|
|
|
|
//
|
|
|
|
// Notifies bookmarks-restore observers using nsIObserverService. This
|
|
|
|
// function is void and we simply return on failure because we don't want
|
|
|
|
// the import itself to fail if notifying observers does.
|
|
|
|
static void
|
|
|
|
NotifyImportObservers(const char* aTopic,
|
|
|
|
PRInt64 aFolderId,
|
|
|
|
PRBool aIsInitialImport)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIObserverService> obs =
|
2010-01-15 14:04:02 +00:00
|
|
|
do_GetService(NS_OBSERVERSERVICE_CONTRACTID);
|
|
|
|
if (!obs)
|
2009-04-17 12:42:34 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
nsCOMPtr<nsISupports> folderIdSupp = nsnull;
|
|
|
|
if (aFolderId > 0) {
|
|
|
|
nsCOMPtr<nsISupportsPRInt64> folderIdInt =
|
2010-01-15 14:04:02 +00:00
|
|
|
do_CreateInstance(NS_SUPPORTS_PRINT64_CONTRACTID);
|
|
|
|
if (!folderIdInt)
|
2009-04-17 12:42:34 +00:00
|
|
|
return;
|
|
|
|
|
2010-01-15 14:04:02 +00:00
|
|
|
if (NS_FAILED(folderIdInt->SetData(aFolderId)))
|
2009-04-17 12:42:34 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
folderIdSupp = do_QueryInterface(folderIdInt);
|
|
|
|
}
|
|
|
|
|
|
|
|
obs->NotifyObservers(folderIdSupp,
|
|
|
|
aTopic,
|
2010-01-15 14:04:02 +00:00
|
|
|
(aIsInitialImport ? RESTORE_INITIAL_NSIOBSERVER_DATA
|
|
|
|
: RESTORE_NSIOBSERVER_DATA).get());
|
2009-04-17 12:42:34 +00:00
|
|
|
}
|
2007-05-25 05:41:49 +00:00
|
|
|
|
2010-01-15 14:04:02 +00:00
|
|
|
|
2007-05-03 08:56:41 +00:00
|
|
|
NS_IMETHODIMP
|
2010-01-15 14:04:02 +00:00
|
|
|
nsPlacesImportExportService::ImportHTMLFromFile(nsILocalFile* aFile,
|
|
|
|
PRBool aIsInitialImport)
|
2007-05-03 08:56:41 +00:00
|
|
|
{
|
2009-04-17 12:42:34 +00:00
|
|
|
NotifyImportObservers(RESTORE_BEGIN_NSIOBSERVER_TOPIC, -1, aIsInitialImport);
|
|
|
|
|
2007-05-03 08:56:41 +00:00
|
|
|
// this version is exposed on the interface and disallows changing of roots
|
2009-04-17 12:42:34 +00:00
|
|
|
nsresult rv = ImportHTMLFromFileInternal(aFile,
|
|
|
|
PR_FALSE,
|
|
|
|
0,
|
|
|
|
aIsInitialImport);
|
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NotifyImportObservers(RESTORE_FAILED_NSIOBSERVER_TOPIC,
|
|
|
|
-1,
|
|
|
|
aIsInitialImport);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
NotifyImportObservers(RESTORE_SUCCESS_NSIOBSERVER_TOPIC,
|
|
|
|
-1,
|
|
|
|
aIsInitialImport);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
2007-05-03 08:56:41 +00:00
|
|
|
}
|
2006-07-18 16:46:13 +00:00
|
|
|
|
2010-01-15 14:04:02 +00:00
|
|
|
|
2007-05-03 08:56:41 +00:00
|
|
|
NS_IMETHODIMP
|
2010-01-15 14:04:02 +00:00
|
|
|
nsPlacesImportExportService::ImportHTMLFromFileToFolder(nsILocalFile* aFile,
|
|
|
|
PRInt64 aFolderId,
|
|
|
|
PRBool aIsInitialImport)
|
2007-05-03 08:56:41 +00:00
|
|
|
{
|
2009-04-17 12:42:34 +00:00
|
|
|
NotifyImportObservers(RESTORE_BEGIN_NSIOBSERVER_TOPIC,
|
|
|
|
aFolderId,
|
|
|
|
aIsInitialImport);
|
|
|
|
|
2007-05-03 08:56:41 +00:00
|
|
|
// this version is exposed on the interface and disallows changing of roots
|
2009-04-17 12:42:34 +00:00
|
|
|
nsresult rv = ImportHTMLFromFileInternal(aFile,
|
|
|
|
PR_FALSE,
|
|
|
|
aFolderId,
|
|
|
|
aIsInitialImport);
|
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NotifyImportObservers(RESTORE_FAILED_NSIOBSERVER_TOPIC,
|
|
|
|
aFolderId,
|
|
|
|
aIsInitialImport);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
NotifyImportObservers(RESTORE_SUCCESS_NSIOBSERVER_TOPIC,
|
|
|
|
aFolderId,
|
|
|
|
aIsInitialImport);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
2007-05-03 08:56:41 +00:00
|
|
|
}
|
2006-07-18 16:38:51 +00:00
|
|
|
|
2010-01-15 14:04:02 +00:00
|
|
|
|
2007-05-03 08:56:41 +00:00
|
|
|
nsresult
|
|
|
|
nsPlacesImportExportService::ImportHTMLFromFileInternal(nsILocalFile* aFile,
|
2010-01-15 14:04:02 +00:00
|
|
|
PRBool aAllowRootChanges,
|
|
|
|
PRInt64 aFolder,
|
|
|
|
PRBool aIsImportDefaults)
|
2007-05-03 08:56:41 +00:00
|
|
|
{
|
2007-05-10 23:52:47 +00:00
|
|
|
nsresult rv = EnsureServiceState();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2010-01-15 14:04:02 +00:00
|
|
|
nsCOMPtr<nsIFile> file = do_QueryInterface(aFile);
|
|
|
|
NS_ENSURE_STATE(file);
|
|
|
|
|
2007-05-03 08:56:41 +00:00
|
|
|
#ifdef DEBUG_IMPORT
|
|
|
|
nsAutoString path;
|
|
|
|
file->GetPath(path);
|
|
|
|
printf("\nImporting %s\n", NS_ConvertUTF16toUTF8(path).get());
|
|
|
|
#endif
|
|
|
|
|
2010-01-15 14:04:02 +00:00
|
|
|
// Confirm file to be imported exists.
|
2007-05-17 06:06:48 +00:00
|
|
|
PRBool exists;
|
|
|
|
rv = file->Exists(&exists);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
if (!exists) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
2010-01-15 14:04:02 +00:00
|
|
|
nsCOMPtr<nsIParser> parser = do_CreateInstance(kParserCID);
|
|
|
|
NS_ENSURE_TRUE(parser, NS_ERROR_OUT_OF_MEMORY);
|
2007-05-03 08:56:41 +00:00
|
|
|
|
2010-01-15 14:04:02 +00:00
|
|
|
nsCOMPtr<BookmarkContentSink> sink = new BookmarkContentSink();
|
2007-05-03 08:56:41 +00:00
|
|
|
NS_ENSURE_TRUE(sink, NS_ERROR_OUT_OF_MEMORY);
|
2010-01-15 14:04:02 +00:00
|
|
|
rv = sink->Init(aAllowRootChanges, aFolder, aIsImportDefaults);
|
2007-05-03 08:56:41 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
parser->SetContentSink(sink);
|
|
|
|
|
2010-01-15 14:04:02 +00:00
|
|
|
// Set the content type on the channel, otherwise the default "unknown" type
|
|
|
|
// will confuse the parser.
|
2007-05-03 08:56:41 +00:00
|
|
|
nsCOMPtr<nsIIOService> ioservice = do_GetIOService(&rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
nsCOMPtr<nsIURI> fileURI;
|
|
|
|
rv = ioservice->NewFileURI(file, getter_AddRefs(fileURI));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-06-01 00:44:43 +00:00
|
|
|
rv = ioservice->NewChannelFromURI(fileURI, getter_AddRefs(mImportChannel));
|
2007-05-03 08:56:41 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-06-01 00:44:43 +00:00
|
|
|
rv = mImportChannel->SetContentType(NS_LITERAL_CSTRING("text/html"));
|
2007-05-03 08:56:41 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2010-01-15 14:04:02 +00:00
|
|
|
// Init parser.
|
2007-06-01 00:44:43 +00:00
|
|
|
rv = parser->Parse(fileURI, nsnull);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2010-01-15 14:04:02 +00:00
|
|
|
// Run the import in batch mode, so it will be executed in a transaction
|
|
|
|
// and will be faster.
|
2007-06-01 00:44:43 +00:00
|
|
|
mIsImportDefaults = aIsImportDefaults;
|
|
|
|
mBookmarksService->RunInBatchMode(this, parser);
|
|
|
|
mImportChannel = nsnull;
|
2010-01-15 14:04:02 +00:00
|
|
|
|
2007-06-01 00:44:43 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-01-15 14:04:02 +00:00
|
|
|
|
2007-06-01 00:44:43 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPlacesImportExportService::RunBatched(nsISupports* aUserData)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
if (mIsImportDefaults) {
|
2007-11-20 02:01:53 +00:00
|
|
|
PRInt64 bookmarksMenuFolder;
|
|
|
|
rv = mBookmarksService->GetBookmarksMenuFolder(&bookmarksMenuFolder);
|
2010-01-15 14:04:02 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-06-01 00:44:43 +00:00
|
|
|
|
2007-11-20 02:01:53 +00:00
|
|
|
rv = mBookmarksService->RemoveFolderChildren(bookmarksMenuFolder);
|
2007-06-01 00:44:43 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-11-21 03:31:10 +00:00
|
|
|
|
|
|
|
PRInt64 toolbarFolder;
|
|
|
|
rv = mBookmarksService->GetToolbarFolder(&toolbarFolder);
|
2010-01-15 14:04:02 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-11-21 03:31:10 +00:00
|
|
|
|
|
|
|
rv = mBookmarksService->RemoveFolderChildren(toolbarFolder);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-11-27 00:08:50 +00:00
|
|
|
|
2008-04-01 18:26:09 +00:00
|
|
|
PRInt64 unfiledBookmarksFolder;
|
|
|
|
rv = mBookmarksService->GetUnfiledBookmarksFolder(&unfiledBookmarksFolder);
|
2010-01-15 14:04:02 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2008-04-01 18:26:09 +00:00
|
|
|
|
|
|
|
rv = mBookmarksService->RemoveFolderChildren(unfiledBookmarksFolder);
|
2007-11-27 00:08:50 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2010-01-15 14:04:02 +00:00
|
|
|
// Add the smart bookmarks.
|
|
|
|
nsCOMPtr<nsIBrowserGlue> glue =
|
|
|
|
do_GetService("@mozilla.org/browser/browserglue;1");
|
|
|
|
NS_ENSURE_TRUE(glue, NS_ERROR_OUT_OF_MEMORY);
|
2007-11-27 00:08:50 +00:00
|
|
|
rv = glue->EnsurePlacesDefaultQueriesInitialized();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-06-01 00:44:43 +00:00
|
|
|
}
|
|
|
|
|
2007-05-03 08:56:41 +00:00
|
|
|
// streams
|
|
|
|
nsCOMPtr<nsIInputStream> stream;
|
2007-06-01 00:44:43 +00:00
|
|
|
rv = mImportChannel->Open(getter_AddRefs(stream));
|
2007-05-03 08:56:41 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
nsCOMPtr<nsIInputStream> bufferedstream;
|
|
|
|
rv = NS_NewBufferedInputStream(getter_AddRefs(bufferedstream), stream, 4096);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// feed the parser the data
|
|
|
|
// Note: on error, we always need to set the channel's status to be the
|
|
|
|
// same, and to always call OnStopRequest with the channel error.
|
2007-06-01 00:44:43 +00:00
|
|
|
nsCOMPtr<nsIStreamListener> listener = do_QueryInterface(aUserData, &rv);
|
2007-05-03 08:56:41 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-06-01 00:44:43 +00:00
|
|
|
rv = listener->OnStartRequest(mImportChannel, nsnull);
|
2010-01-15 14:04:02 +00:00
|
|
|
NS_WARN_IF_FALSE(NS_SUCCEEDED(rv), "OnStartRequest failed");
|
2007-06-01 00:44:43 +00:00
|
|
|
rv = SyncChannelStatus(mImportChannel, rv);
|
2010-01-15 14:04:02 +00:00
|
|
|
NS_WARN_IF_FALSE(NS_SUCCEEDED(rv), "SyncChannelStatus failed");
|
|
|
|
|
2007-06-01 00:44:43 +00:00
|
|
|
while (NS_SUCCEEDED(rv))
|
2007-05-03 08:56:41 +00:00
|
|
|
{
|
|
|
|
PRUint32 available;
|
|
|
|
rv = bufferedstream->Available(&available);
|
|
|
|
if (rv == NS_BASE_STREAM_CLOSED) {
|
|
|
|
rv = NS_OK;
|
|
|
|
available = 0;
|
|
|
|
}
|
|
|
|
if (NS_FAILED(rv)) {
|
2007-06-01 00:44:43 +00:00
|
|
|
mImportChannel->Cancel(rv);
|
2007-05-03 08:56:41 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!available)
|
|
|
|
break; // blocking input stream has none available when done
|
|
|
|
|
2007-06-01 00:44:43 +00:00
|
|
|
rv = listener->OnDataAvailable(mImportChannel, nsnull, bufferedstream, 0,
|
|
|
|
available);
|
2010-01-15 14:04:02 +00:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
break;
|
2007-06-01 00:44:43 +00:00
|
|
|
rv = SyncChannelStatus(mImportChannel, rv);
|
2007-05-03 08:56:41 +00:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
break;
|
|
|
|
}
|
2010-01-15 14:04:02 +00:00
|
|
|
|
|
|
|
rv = listener->OnStopRequest(mImportChannel, nsnull, rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2007-05-03 08:56:41 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-01-15 14:04:02 +00:00
|
|
|
|
2006-07-18 16:38:51 +00:00
|
|
|
NS_IMETHODIMP
|
2007-05-03 08:56:41 +00:00
|
|
|
nsPlacesImportExportService::ExportHTMLToFile(nsILocalFile* aBookmarksFile)
|
2006-07-18 16:38:51 +00:00
|
|
|
{
|
2010-01-15 14:04:02 +00:00
|
|
|
NS_ENSURE_ARG(aBookmarksFile);
|
2006-07-18 16:46:13 +00:00
|
|
|
|
2007-05-17 06:06:48 +00:00
|
|
|
#ifdef DEBUG_EXPORT
|
2007-05-18 07:00:00 +00:00
|
|
|
nsAutoString path;
|
2007-05-17 06:06:48 +00:00
|
|
|
aBookmarksFile->GetPath(path);
|
|
|
|
printf("\nExporting %s\n", NS_ConvertUTF16toUTF8(path).get());
|
2007-05-25 05:41:49 +00:00
|
|
|
|
|
|
|
PRTime startTime = PR_Now();
|
|
|
|
printf("\nStart time: %lld\n", startTime);
|
2007-05-17 06:06:48 +00:00
|
|
|
#endif
|
|
|
|
|
2007-05-10 23:52:47 +00:00
|
|
|
nsresult rv = EnsureServiceState();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2006-07-18 16:46:13 +00:00
|
|
|
// get a safe output stream, so we don't clobber the bookmarks file unless
|
|
|
|
// all the writes succeeded.
|
|
|
|
nsCOMPtr<nsIOutputStream> out;
|
2007-05-10 23:52:47 +00:00
|
|
|
rv = NS_NewSafeLocalFileOutputStream(getter_AddRefs(out),
|
|
|
|
aBookmarksFile,
|
|
|
|
PR_WRONLY | PR_CREATE_FILE,
|
2010-01-15 14:04:02 +00:00
|
|
|
0600, 0);
|
2007-05-03 08:56:41 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2006-07-18 16:46:13 +00:00
|
|
|
|
|
|
|
// We need a buffered output stream for performance.
|
|
|
|
// See bug 202477.
|
|
|
|
nsCOMPtr<nsIOutputStream> strm;
|
|
|
|
rv = NS_NewBufferedOutputStream(getter_AddRefs(strm), out, 4096);
|
2007-05-03 08:56:41 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2006-07-18 16:46:13 +00:00
|
|
|
|
2010-01-15 14:04:02 +00:00
|
|
|
// Get a new query object.
|
|
|
|
nsCOMPtr<nsINavHistoryQuery> query;
|
|
|
|
rv = mHistoryService->GetNewQuery(getter_AddRefs(query));
|
2007-05-03 08:56:41 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-05-25 05:41:49 +00:00
|
|
|
nsCOMPtr<nsINavHistoryQueryOptions> options;
|
|
|
|
rv = mHistoryService->GetNewQueryOptions(getter_AddRefs(options));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-01-15 14:04:02 +00:00
|
|
|
nsCOMPtr<nsINavHistoryResult> result;
|
2007-05-25 05:41:49 +00:00
|
|
|
|
2010-01-15 14:04:02 +00:00
|
|
|
// We need the bookmarks menu root node to write out the title.
|
|
|
|
PRInt64 bookmarksMenuFolder;
|
|
|
|
rv = mBookmarksService->GetBookmarksMenuFolder(&bookmarksMenuFolder);
|
2007-05-25 05:41:49 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-11-20 02:01:53 +00:00
|
|
|
rv = query->SetFolders(&bookmarksMenuFolder, 1);
|
2007-05-25 05:41:49 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
rv = mHistoryService->ExecuteQuery(query, options, getter_AddRefs(result));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-08-02 20:19:44 +00:00
|
|
|
nsCOMPtr<nsINavHistoryContainerResultNode> rootNode;
|
2007-05-25 05:41:49 +00:00
|
|
|
rv = result->GetRoot(getter_AddRefs(rootNode));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2010-01-15 14:04:02 +00:00
|
|
|
// file header
|
|
|
|
PRUint32 dummy;
|
|
|
|
rv = strm->Write(kFileIntro, sizeof(kFileIntro)-1, &dummy);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2006-07-18 16:46:13 +00:00
|
|
|
// '<H1'
|
|
|
|
rv = strm->Write(kRootIntro, sizeof(kRootIntro)-1, &dummy); // <H1
|
2007-05-03 08:56:41 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2006-07-18 16:46:13 +00:00
|
|
|
|
|
|
|
// '>Bookmarks</H1>
|
|
|
|
rv = strm->Write(kCloseAngle, sizeof(kCloseAngle)-1, &dummy); // >
|
2007-05-03 08:56:41 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-05-25 05:41:49 +00:00
|
|
|
rv = WriteTitle(rootNode, strm);
|
2007-05-03 08:56:41 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2006-07-18 16:46:13 +00:00
|
|
|
rv = strm->Write(kCloseRootH1, sizeof(kCloseRootH1)-1, &dummy); // </H1>
|
2007-05-03 08:56:41 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2006-07-18 16:46:13 +00:00
|
|
|
|
2010-01-15 14:04:02 +00:00
|
|
|
// Container's prologue.
|
2006-07-18 16:46:13 +00:00
|
|
|
rv = WriteContainerPrologue(EmptyCString(), strm);
|
2007-05-03 08:56:41 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2006-07-18 16:46:13 +00:00
|
|
|
|
|
|
|
// indents
|
|
|
|
nsCAutoString indent;
|
|
|
|
indent.Assign(kIndent);
|
|
|
|
|
2010-01-15 14:04:02 +00:00
|
|
|
// Bookmarks Menu.
|
2007-05-25 05:41:49 +00:00
|
|
|
rv = WriteContainerContents(rootNode, EmptyCString(), strm);
|
2007-05-03 08:56:41 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2006-07-18 16:46:13 +00:00
|
|
|
|
2010-01-15 14:04:02 +00:00
|
|
|
// Bookmarks Toolbar.
|
|
|
|
// We write this folder under the bookmarks-menu for backwards compatibility.
|
|
|
|
PRInt64 toolbarFolder;
|
|
|
|
rv = mBookmarksService->GetToolbarFolder(&toolbarFolder);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-11-20 02:01:53 +00:00
|
|
|
rv = query->SetFolders(&toolbarFolder, 1);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
rv = mHistoryService->ExecuteQuery(query, options, getter_AddRefs(result));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
rv = result->GetRoot(getter_AddRefs(rootNode));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-01-15 14:04:02 +00:00
|
|
|
// Write it out only if it's not empty.
|
|
|
|
rv = rootNode->SetContainerOpen(PR_TRUE);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
PRUint32 childCount = 0;
|
|
|
|
rv = rootNode->GetChildCount(&childCount);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
rv = rootNode->SetContainerOpen(PR_FALSE);
|
2007-11-20 02:01:53 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-01-15 14:04:02 +00:00
|
|
|
if (childCount) {
|
|
|
|
rv = WriteContainer(rootNode, nsDependentCString(kIndent), strm);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
2007-11-20 02:01:53 +00:00
|
|
|
|
2010-01-15 14:04:02 +00:00
|
|
|
// Unfiled Bookmarks.
|
|
|
|
// We write this folder under the bookmarks-menu for backwards compatibility.
|
|
|
|
PRInt64 unfiledBookmarksFolder;
|
|
|
|
rv = mBookmarksService->GetUnfiledBookmarksFolder(&unfiledBookmarksFolder);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2008-04-01 18:26:09 +00:00
|
|
|
rv = query->SetFolders(&unfiledBookmarksFolder, 1);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
rv = mHistoryService->ExecuteQuery(query, options, getter_AddRefs(result));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
rv = result->GetRoot(getter_AddRefs(rootNode));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-01-15 14:04:02 +00:00
|
|
|
// Write it out only if it's not empty.
|
2008-04-01 18:26:09 +00:00
|
|
|
rv = rootNode->SetContainerOpen(PR_TRUE);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-01-15 14:04:02 +00:00
|
|
|
childCount = 0;
|
|
|
|
rootNode->GetChildCount(&childCount);
|
2008-04-01 18:26:09 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-01-15 14:04:02 +00:00
|
|
|
rv = rootNode->SetContainerOpen(PR_FALSE);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
if (childCount) {
|
2008-04-01 18:26:09 +00:00
|
|
|
rv = WriteContainer(rootNode, nsDependentCString(kIndent), strm);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
2010-01-15 14:04:02 +00:00
|
|
|
// Container's epilogue.
|
2006-07-18 16:46:13 +00:00
|
|
|
rv = WriteContainerEpilogue(EmptyCString(), strm);
|
2007-05-03 08:56:41 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2006-07-18 16:46:13 +00:00
|
|
|
|
|
|
|
// commit the write
|
|
|
|
nsCOMPtr<nsISafeOutputStream> safeStream = do_QueryInterface(strm, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-05-25 05:41:49 +00:00
|
|
|
rv = safeStream->Finish();
|
2010-01-15 14:04:02 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2007-05-25 05:41:49 +00:00
|
|
|
#ifdef DEBUG_EXPORT
|
|
|
|
printf("\nTotal time in seconds: %lld\n", (PR_Now() - startTime)/1000000);
|
|
|
|
#endif
|
2010-01-15 14:04:02 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
2006-07-18 16:38:51 +00:00
|
|
|
}
|
2007-05-03 08:56:41 +00:00
|
|
|
|
2007-11-09 20:11:12 +00:00
|
|
|
|
2007-05-03 08:56:41 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPlacesImportExportService::BackupBookmarksFile()
|
|
|
|
{
|
2007-05-10 23:52:47 +00:00
|
|
|
nsresult rv = EnsureServiceState();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2007-11-09 20:11:12 +00:00
|
|
|
nsCOMPtr<nsIPrefBranch> prefs(do_GetService(NS_PREFSERVICE_CONTRACTID, &rv));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2007-05-03 08:56:41 +00:00
|
|
|
// get bookmarks file
|
|
|
|
nsCOMPtr<nsIFile> bookmarksFileDir;
|
2007-05-10 23:52:47 +00:00
|
|
|
rv = NS_GetSpecialDirectory(NS_APP_BOOKMARKS_50_FILE,
|
|
|
|
getter_AddRefs(bookmarksFileDir));
|
|
|
|
|
2007-05-03 08:56:41 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-01-15 14:04:02 +00:00
|
|
|
nsCOMPtr<nsILocalFile> bookmarksFile = do_QueryInterface(bookmarksFileDir);
|
|
|
|
NS_ENSURE_STATE(bookmarksFile);
|
2007-05-03 08:56:41 +00:00
|
|
|
|
2010-01-15 14:04:02 +00:00
|
|
|
// Create the file if it doesn't exist.
|
2007-05-03 08:56:41 +00:00
|
|
|
PRBool exists;
|
|
|
|
rv = bookmarksFile->Exists(&exists);
|
2010-01-15 14:04:02 +00:00
|
|
|
if (NS_FAILED(rv) || !exists) {
|
2007-05-03 08:56:41 +00:00
|
|
|
rv = bookmarksFile->Create(nsIFile::NORMAL_FILE_TYPE, 0600);
|
2010-01-15 14:04:02 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Unable to create bookmarks.html!");
|
|
|
|
return rv;
|
|
|
|
}
|
2007-05-03 08:56:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// export bookmarks.html
|
|
|
|
rv = ExportHTMLToFile(bookmarksFile);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|