Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void SyncBookmarks()
- {
- Browser browser = new Chrome();
- SortableBindingList<Bookmark> marks = browser.ReturnBookmarks();
- SortableBindingList<object> newmarks = new SortableBindingList<object>();
- List<Bookmark> ordered = marks.OrderBy(order => order.fullPath).ToList();
- int count = 0;
- newmarks.Add(new json_top());
- json_top top = newmarks[0] as json_top;
- top.roots = new json_root();
- List<object> roots = new List<object>();
- Folder bookmarks_bar = new Folder();
- Folder other = new Folder();
- List<Object> barlist = new List<Object>();
- List<object> otherlist = new List<object>();
- List<object> children = new List<object>();
- List<string> existingFolders = new List<string>();
- bookmarks_bar.date_added = ToChromeTime(marks[0].added);
- bookmarks_bar.name = "Bookmarks bar";
- other.date_added = ToChromeTime(marks[0].added);
- other.name = "Other bookmarks";
- bool isBookmarkBar = true;
- Folder folder = new Folder();
- foreach (Bookmark mark in ordered)
- {
- if (mark.fullPath.Contains("Bookmark Bar\\"))
- {
- isBookmarkBar = true;
- mark.fullPath = mark.fullPath.Replace("Bookmark Bar\\", "");
- }
- else if (mark.fullPath.Contains("Other Bookmarks\\"))
- {
- isBookmarkBar = false;
- mark.fullPath = mark.fullPath.Replace("Other Bookmarks\\", "");
- }
- else if (mark.fullPath.Contains("Bookmark Bar"))
- {
- isBookmarkBar = true;
- mark.fullPath = mark.fullPath.Replace("Bookmark Bar", "");
- }
- else if (mark.fullPath.Contains("Other Bookmarks"))
- {
- isBookmarkBar = false;
- mark.fullPath = mark.fullPath.Replace("Other Bookmarks", "");
- }
- else
- {
- throw new UriFormatException();
- }
- List<string> fullpathlist = mark.fullPath.Split('\\').ToList();
- int count1 = 0;
- if (currentPath != mark.fullPath)
- {
- folder = (createFolder(fullpathlist, fullpathlist[0], mark));
- folder.children = (setChild(fullpathlist, folder, mark, 1));
- count++;
- }
- json_URL u = new json_URL();
- u.date_added = ToChromeTime(mark.added);
- u.id = id;
- u.name = mark.title;
- u.url = mark.url;
- folder.children = (u);
- if (isBookmarkBar)
- barlist.Add(folder);
- else
- {
- otherlist.Add(folder);
- }
- currentPath = mark.fullPath;
- id++;
- }
- bookmarks_bar.children = barlist;
- other.children = otherlist;
- top.roots.bookmark_bar = (bookmarks_bar);
- top.roots.other = other;
- string json = JsonConvert.SerializeObject(newmarks, Formatting.Indented);
- File.WriteAllText(@"c:\person.json", json);
- }
- private Folder createFolder(List<string> fullpathlist, string s, Bookmark mark)
- {
- Folder folder = new Folder();
- folder.date_added = ToChromeTime(mark.added);
- folder.id = id;
- folder.name = s;
- return folder;
- }
- Folder setChild(List<string> fullPathList, Folder parent, Bookmark mark, int count)
- {
- if (count < fullPathList.Count)
- {
- Folder child = new Folder();
- child.date_added = ToChromeTime(mark.added);
- child.id = id;
- child.name = fullPathList[count];
- Folder LIST = (setChild(fullPathList, child, mark, count + 1));
- child.children = (LIST);
- return child;
- }
- else
- {
- return null;
- }
- }
- class json_root
- {
- public Folder bookmark_bar { get; set; }
- public Folder other { get; set; }
- }
- class json_top
- {
- public string checksum { get { return ""; } }
- public json_root roots { get; set; }
- public int version { get { return 1; } }
- }
- class folderAndURL { }
- class json_URL : folderAndURL
- {
- public long date_added { get; set; }
- public int id { get; set; }
- public string name { get; set; }
- public string type { get { return "url"; } }
- public string url { get; set; }
- }
- public class Folder : folderAndURL
- {
- public object children { get; set; }
- public long date_added { get; set; }
- public long date_modified
- {
- get { return 0; }
- }
- public int id { get; set; }
- public string name { get; set; }
- public string type
- {
- get { return "folder"; }
- }
- }
- public class Bookmark
- {
- public string url { get; set; }
- public string title { get; set; }
- public string browser { get; set; }
- public DateTime added { get; set; }
- public string fullPath { get; set; }
- public object currentPath { get; set; }
- public Bookmark(string url, string title, string browser, DateTime added, string fullPath, object currentPath = null)
- {
- this.url = url;
- this.title = title;
- this.browser = browser;
- this.added = added;
- this.fullPath = fullPath;
- this.currentPath = currentPath;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement