public void DeleteBookmark(BookmarkItem item) { //delete in item pool if (bookmarkItemPool.ContainsKey(item.href)) { bookmarkItemPool.Remove(item.href); } //delete in storage string path = URL2Path(item.href); if (App.store.FileExists(path + "/BookmarkItemRecord.json")) { App.store.DeleteFile(path + "/BookmarkItemRecord.json"); } //delete in index if (item.isUnReaded) { GetItemListHolder(Views.IndexPage.PageStatus.UNREAD).Remove(item); } else { GetItemListHolder(Views.IndexPage.PageStatus.READ).Remove(item); } if (item.isStar) { GetItemListHolder(Views.IndexPage.PageStatus.STAR).Remove(item); } }
/// <summary> /// Add a new bookmark. /// </summary> /// <returns>添加成功,返回"done";否则返回错误信息</returns> public Task <string> AddBookmark(BookmarkItem it) { string extended = ContentEncoder(it.extended); Dictionary <string, string> data = new Dictionary <string, string>() { { "url", it.href }, { "description", it.description }, { "tags", it.tag }, { "extended", extended }, { "replace", "yes" } }; return(PostAsync(host + "/v1/posts/add", data).ContinueWith <string>(t => { if (t.Status == TaskStatus.RanToCompletion && t.Result != null) { XDocument doc = XDocument.Parse(t.Result); return doc.Root.Attribute("code").Value; } else if (t.Status == TaskStatus.Faulted) { throw t.Exception.InnerException; } return "error"; })); }
/// <return>返回添加的新元素的个数</return> public int AddBunchItemRecords(List <BookmarkItem> itemList, int position = 0) { if (itemList.Count == 0) { return(0); } int count = itemList.Count; int add = 0; for (int i = 0; i < count; i++) { BookmarkItem item = itemList[i]; add += AddBookmarkItem(ref item, position + i, true) ? 1 : 0; itemList[i] = item; } if (add > 0 && ListIndexChanged != null) { if (itemList[0].isUnReaded) { ListIndexChanged(Views.IndexPage.PageStatus.UNREAD); } else { ListIndexChanged(Views.IndexPage.PageStatus.READ); } if (itemList[0].isStar) { ListIndexChanged(Views.IndexPage.PageStatus.STAR); } } return(add); }
/// <returns>返回是否添加了新元素到列表</returns> public bool AddBookmarkItem(ref BookmarkItem item, int position = 0, bool isBunchAdd = false) { string url = item.href.Trim(); if (bookmarkItemPool.ContainsKey(url)) { //更新原链接元素 var oldItem = bookmarkItemPool[url]; oldItem.description = item.description; oldItem.hash = item.hash; oldItem.isUnReaded = item.isUnReaded; oldItem.isStar = item.isStar; if (!String.IsNullOrWhiteSpace(item.cacheHtml)) { oldItem.cacheHtml = item.cacheHtml; } item.Dispose(); item = oldItem; } else { bookmarkItemPool.Add(url, item); item.PropertyChanged += App.pocketApi.BookmarkItem_PropertyChanged; item.PropertyChanged += App.localFileCache.BookmarkItem_PropertyChanged; SaveLinkItemRecord(item); } return(UpdateIndex(item, position, "", isBunchAdd)); }
public void DeleteItem(BookmarkItem item) { if (ItemStatusChanged != null) { ItemStatusChanged(this, item, Views.IndexPage.PageStatus.OTHER); } ModifyItem(item.hash, "delete"); }
public void SetFavorite(BookmarkItem item) { if (ItemStatusChanged != null) { ItemStatusChanged(this, item, Views.IndexPage.PageStatus.STAR); } ModifyItem(item.hash, "favorite"); }
public void SetArchive(BookmarkItem item) { if (ItemStatusChanged != null) { ItemStatusChanged(this, item, Views.IndexPage.PageStatus.READ); } ModifyItem(item.hash, "archive"); }
public void SetUnRead(BookmarkItem item) { if (ItemStatusChanged != null) { ItemStatusChanged(this, item, Views.IndexPage.PageStatus.UNREAD); } ModifyItem(item.hash, "readd"); }
public void AddNewItem(BookmarkItem item) { if (ItemStatusChanged != null) { ItemStatusChanged(this, item, Views.IndexPage.PageStatus.UNREAD); } AddItem(item.href); }
///// <summary> ///// Check to see when a user last posted an item. ///// </summary> ///// <returns></returns> //public Task<string> GetUpdates(){ // return GetAsync(host + "/v1/posts/update"); //} /// <summary> /// Fetch all bookmarks by date or index range. /// </summary> /// <param name="start">从第start个链接开始显示,最低为0</param> /// <param name="count">显示连续的count个链接,最高为100000</param> /// <returns>返回链接列表;请求异常则抛出,其中特殊处理了权限异常,通常为错误的用户名或密码</returns> public Task <List <BookmarkItem> > GetAll(int start = 0, int count = 300, string tag = null) { return(GetAsync(host + "/v1/posts/all?start=" + start + "&results=" + count + "&tag=" + tag).ContinueWith <List <BookmarkItem> >(t => { if (t.Status == TaskStatus.RanToCompletion && t.Result != null) { XDocument doc = XDocument.Parse(t.Result); List <BookmarkItem> result = new List <BookmarkItem>(); if (doc.Root.Name == "result") { return result; } if (doc.Root.Name == "posts") { foreach (XElement node in doc.Descendants("post")) { BookmarkItem item = new BookmarkItem(); item.href = node.Attribute("href").Value; item.description = node.Attribute("description").Value; item.extended = ContentDecoder(node.Attribute("extended").Value); item.tag = node.Attribute("tag").Value; item.time = node.Attribute("time").Value; item.time = item.time.Replace('T', ' ').Replace('Z', ' ').Trim(); //may slow down the deal speed, better to use regex if (IsExits(item.href)) { BookmarkItem pItem = LoadLinkItemRecord(item.href); item.isUnReaded = pItem.isUnReaded; if (item.time.CompareTo(pItem.time) > 0) { SaveLinkItemRecord(item); } } else { if (Regex.IsMatch(item.tag, "(^|\\W)Readed($|\\W)")) { item.isUnReaded = "0"; } else { item.isUnReaded = "1"; } SaveLinkItemRecord(item); } result.Add(item); } } return result; } else if (t.Status == TaskStatus.Faulted) { throw t.Exception.InnerException; } return new List <BookmarkItem>(); })); }
private void SaveLinkItemRecord(BookmarkItem item) { string path = URL2Path(item.href); if (App.store.DirectoryExists(path) == false) { App.store.CreateDirectory(path); } using (var s = App.store.CreateFile(path + "/BookmarkItemRecord.json")) bookmarkItem_ser.WriteObject(s, item); }
private bool UpdateIndex(BookmarkItem item, int position, string property, bool isBunchUpdate = false) { if (item == null) { return(false); } bool ret = false; if (property == "" || property == "isUnReaded") { if (item.isUnReaded) { if (GetItemListHolder(Views.IndexPage.PageStatus.READ).Remove(item)) { ret = true; } if (GetItemListHolder(Views.IndexPage.PageStatus.UNREAD).Insert(item, position)) { ret = true; } } else { if (GetItemListHolder(Views.IndexPage.PageStatus.UNREAD).Remove(item)) { ret = true; } if (GetItemListHolder(Views.IndexPage.PageStatus.READ).Insert(item, position)) { ret = true; } } } if (property == "" || property == "isStar") { if (item.isStar) { if (GetItemListHolder(Views.IndexPage.PageStatus.STAR).Insert(item, position)) { ret = true; } } else { if (GetItemListHolder(Views.IndexPage.PageStatus.STAR).Remove(item)) { ret = true; } } } return(ret); }
public void SaveLinkItemRecord(BookmarkItem item) { string path = item.href.GetHashCode().ToString(); if (store.DirectoryExists(path) == false) { store.CreateDirectory(path); } XmlSerializer ser = new XmlSerializer(typeof(BookmarkItem)); using (var s = store.CreateFile(path + "/BookmarkItemRecord.xml")) ser.Serialize(s, item); }
public bool Remove(BookmarkItem item) { if (item == null) { return(false); } if (itemHashSet.Remove(item.href) == false) { return(false); } itemList.Remove(item); SaveIndex(); return(true); }
public BookmarkItem PocketItem2BookmarkItem(PocketItem item, bool?isUnReaded = null, bool?isStar = null) { BookmarkItem newItem = new BookmarkItem( String.IsNullOrEmpty(item.resolved_url) ? item.given_url : item.resolved_url, String.IsNullOrEmpty(item.given_title) ? item.resolved_title : item.given_title, item.item_id, (isUnReaded == null) ? (item.status == "0") : (isUnReaded == true), (isStar == null) ? (item.favorite == "1") : (isStar == true) ); if (String.IsNullOrEmpty(newItem.href)) { newItem.Dispose(); return(null); } return(newItem); }
/// <summary> /// 从本地加载链接记录. /// </summary> /// <returns>目录名为链接的哈希码,所以返回的记录列表为乱序</returns> public SortedSet <BookmarkItem> LoadLinkItemsRecord() { SortedSet <BookmarkItem> result = new SortedSet <BookmarkItem>(); XmlSerializer ser = new XmlSerializer(typeof(BookmarkItem)); foreach (var i in store.GetDirectoryNames("*")) { if (store.FileExists(i + "/BookmarkItemRecord.xml")) { using (var s = store.OpenFile(i + "/BookmarkItemRecord.xml", FileMode.Open, FileAccess.Read, FileShare.Read)) { BookmarkItem it = (BookmarkItem)ser.Deserialize(s); result.Add(it); } } } return(result); }
public bool Insert(BookmarkItem item, int position) { if (item == null) { return(false); } if (itemHashSet.Add(item.href) == false) { return(false); } if (position < itemList.Count) { itemList.Insert(position, item); } else { itemList.Add(item); } SaveIndex(); return(true); }
/// <return>返回添加的新元素</return> public BookmarkItem AddBookmarkItem(string url, string description, string cacheHtml = "", int position = 0) { url = url.Trim(); if (bookmarkItemPool.ContainsKey(url)) { //更新原链接元素 UpdateItem(url, description, "", cacheHtml); } var item = new BookmarkItem( url, description, "", true, false, cacheHtml ); bookmarkItemPool.Add(url, item); item.PropertyChanged += App.pocketApi.BookmarkItem_PropertyChanged; item.PropertyChanged += App.localFileCache.BookmarkItem_PropertyChanged; SaveLinkItemRecord(item); UpdateIndex(item, position, "", false); return(item); }
public List <BookmarkItem> TestDatav2() { List <BookmarkItem> itemList = new List <BookmarkItem>(); BookmarkItem item = new BookmarkItem( "http://www.baidu.com", "Baidu", "123456", true, false ); itemList.Add(item); item = new BookmarkItem( "http://www.google.com", "Google", "654321", true, false ); itemList.Add(item); return(itemList); }
public void SetUnFavorite(BookmarkItem item) { ModifyItem(item.hash, "unfavorite"); }
public void UpdateRecentIndex(BookmarkItem item) { GetItemListHolder(Views.IndexPage.PageStatus.RECENT).Insert(item, 0); }