Click here to Skip to main content
15,884,353 members
Articles / Programming Languages / C++

Export Chrome bookmarks to IE Favorites with JsonCpp

,
Rate me:
Please Sign up or sign in to vote.
4.20/5 (5 votes)
5 May 2010CPOL2 min read 56.1K   947   12   7
One way to export Chrome bookmarks to IE Favorites with the JsonCpp library.

Introduction

Chrome as a new browser has the built-in module to import Firefox and Internet Explorer bookmarks. But Internet Explorer has no such module to do the same thing. Today, I want to fix this problem and export Chrome bookmarks to IE Favorites.

Background

Before showing my code, I do think it's necessary to tell you some background about Internet Explorer Favorites and Chrome bookmarks (JSON).

Internet Explorer Favorites

Typically, your Internet Explorer Favorites are under: C:\Users\UserName\Favorites. And each URL is a file; for example: Google.url is the Google IE bookmark.

And the typical structure of an IE URL file is as follows:

[DEFAULT]
BASEURL=http://www.google.com/
[{000214A0-0000-0000-C000-000000000046}]
Prop3=19,2
[InternetShortcut]
URL=http://www.google.com/
IDList=
IconFile=http://www.google.com/favicon.ico
IconIndex=1

Do you find something there? Yes, it's an ini file. In fact, only these two lines:

[InternetShortcut]
URL=http://www.google.com/

will work, you can try. At this point, we can process IE Favorites; we can read and write an ini file with the help of the GetPrivateProfileString and WritePrivateProfileString APIs.

Google Chrome Bookmarks

Typically, Google Chrome bookmarks are stored at C:\Users\UserName\AppData\Local\Google\Chrome\User Data\Default\Bookmarks.

Let's look at the Chrome bookmarks structure. The content of a bookmark file is as follows:

{
   "checksum": "1ddf61ad79f79c33b6674fc8ccb03ca5",
   "roots": {
      "bookmark_bar": {
         "children": [ {
            "date_added": "12915767966508377",
            "id": "3",
            "name": "Google",
            "type": "url",
            "url": "http://www.google.com/"
         } ],
         "date_added": "0",
         "date_modified": "12915767966508377",
         "id": "1",
         "name": "Bookmarks bar",
         "type": "folder"
      },
      "other": {
         "children": [  ],
         "date_added": "0",
         "date_modified": "12915566290018721",
         "id": "2",
         "name": "Other bookmarks",
         "type": "folder"
      }
   },
   "version": 1
}

Even though there is only a single URL inside the file, it's hard to read. We refine it as follows:

{
   "checksum": "1ddf61ad79f79c33b6674fc8ccb03ca5",
   "roots": {
      "bookmark_bar": FOLDER_OBJ,
      "other": FOLDER_OBJ
   },
   "version": 1
}

while the structure of FOLDER_OBJ is:

{
  "children": [ FOLDER_OBJ, URL_OBJ,...],
  "date_added": "0",
  "date_modified": "12915566290018721",
  "id": "2",
  "name": "Other bookmarks",
  "type": "folder"
}

We can see from the above snippet, FOLDER_OBJ can hold URL_OBJ, and also FOLDER_OBJ. Similar to FOLDER_OBJ, the structure of URL_OBJ is:

{
    "date_added": "12915767966508377",
    "id": "3",
    "name": "Google",
    "type": "url",
    "url": "http://www.google.com/"
}

In fact, the Chrome bookmarks file is a JSON file, which is short for "JavaScript Object Notation". You can get more information from here. There are a lot of libraries to parse JSON, you can get the list from here. After comparing, we finally choose JsonCpp to do this work. At the same time, you can get a copy from my demo project. With the help of the JsonCpp library, it's very easy to parse the Google Chrome bookmarks file.

Implementation Details

Wrapper Class

We write a wrapper chrome class to export it, with only a few functions. It's obviously easy to understand it, so I will just post the code here.

C++
class chrome
{
public:
    chrome() {};

    ///////////////////////////////////////////////////////////////////
    //
    // load
    //
    // Purpose:     load a google chrome bookmarks file.
    //
    // Parameters:  const wchar_t *       --- the file path of chrome bookmarks.
    //
    // Returns:   bool
    //
    bool load(const wchar_t * file_path);


    ///////////////////////////////////////////////////////////////////
    //
    // export_2_folder
    //
    // Purpose:     this function is provided to export all bookmarks to a folder
    //
    // Parameters:  const wchar_t* folder_path     --- the folder path
    //
    // Returns:   
    //
    bool export_2_folder(const wchar_t* folder_path);



    ///////////////////////////////////////////////////////////////////
    //
    // is_installed_chrome
    //
    // Purpose:     to determine if chrome is installed in current OS
    //
    // Parameters: wchar_t *     --- if chrome is installed, the parameter get the file path. 
    //                               it's the user's responsibility to allocate space,
    //                               for example:   wchar_t path_out[260]={0};
    //
    // Returns:   bool
    //
    static bool is_installed_chrome(wchar_t * path);

    static bool is_chrome_running();
    static bool close_chrome();


private:

    //////////////////////////////////////////////////////////
    // this two classes are only provided to use
    // in the class member functions.
    //////////////////////////////////////////////////////////
    //
    // export_url
    //
    // Purpose:     export an url to a specific folder path.
    //
    // Parameters: Json::Value&       ---  it must be an url object value. 
    //                       const wchar_t*   ---  the folder path to export.
    //
    // Returns:   void
    //
    void export_url(Json::Value& url_obj, const wchar_t* path);

    ///////////////////////////////////////////////////////////////////
    //
    // export_url
    //
    // Purpose:     export a folder object to a specific folder path.
    //
    // Parameters: Json::Value&       ---  it must be an folder object value. 
    //             const wchar_t*   ---  the folder path to export.
    //
    // Returns:   void
    //
    void export_folder(Json::Value& folder_obj, const wchar_t* path);

    Json::Value chrome_;
};

How To Use

It's very easy to use this code, only three steps will do:

C++
chrome obj;
obj.load(bkmk);
// If you have not installed chrome. you can give
// an absolute to the bookmarks file I provided to try.
// bkmk is a file path of google chrome bookmarks file

obj.export_2_folder(path);
// path is the destination folder path to export
// for example, IE favorites folder.

If you have not installed Chrome in your OS, a Google Chrome Bookmarks file is attached in the demo project. You can give the load function an absolute file path of the bookmarks file attached.

Others

Unicode support is very important. So I wrote two functions to make it available to other languages.

C++
// convert UTF8 string to wchar_t string.
bool convert_utf8_2_wchar(const char* utf8, wchar_t * wchar_out);
// convert wchar_t string to UTF8 string.
size_t convert_wchar_2_utf8(const wchar_t* wchar_in, char * utf8);

By the way, this article only provides a way to export Chrome bookmarks to IE Favorites. But based on our demo project, it's very easy to import IE Favorites to Chrome bookmarks and even make bookmarks interchanged among IE, Chrome, and Firefox.

The screenshot of the result after running the demo project is:

see.png

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
China China
In Guangzhou China

Written By
Other Nanchang University
China China
I am a post-graduate student in Nanchang University in China.My major is machinery and electronics engineering.

Comments and Discussions

 
QuestionA question about write chrome bookmarks file Pin
regainworld25-Jul-13 18:01
regainworld25-Jul-13 18:01 
QuestionBeginner question Pin
zulkefliaris16-Jun-13 19:12
zulkefliaris16-Jun-13 19:12 
QuestionUse on VB6 or VBNet Pin
afdoal5-Jan-12 7:24
afdoal5-Jan-12 7:24 
Questioncan't get it to find my chrome bookmarks Pin
RichTrawinski279-Apr-11 9:00
RichTrawinski279-Apr-11 9:00 
GeneralI made some changes to the code Pin
axusgrad2-Oct-10 12:59
axusgrad2-Oct-10 12:59 
GeneralGood sample but some memory issues. Pin
krishnakumartm18-Jun-10 1:51
krishnakumartm18-Jun-10 1:51 
GeneralRe: Good sample but some memory issues. Pin
365days.enjoy29-Sep-10 17:51
365days.enjoy29-Sep-10 17:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.