public static PrinterContact ParseContactsFromJson(Printer printer, string json) { try { PrinterContact p = new PrinterContact(); List<ContactViewPrinter> list = new List<ContactViewPrinter>(); JToken token = JObject.Parse(json); var ip = token.SelectToken("printer-ip"); var contact_lim = token.SelectToken("contact-limit"); var group_lim = token.SelectToken("group-limit"); int maxContact = MAX_CONTACT; int maxGroup = MAX_GROUP; if(contact_lim != null) { Int32.TryParse(contact_lim.ToString(), out maxContact); } if(group_lim != null) { Int32.TryParse(group_lim.ToString(), out maxGroup); } p.PrinterIP = ip != null ? ip.ToString() : ""; p.ContactLimit = maxContact; p.GroupLimit = maxGroup; JToken contacts = token.SelectToken("contacts"); foreach (JToken data in contacts) { ContactViewPrinter contact = PrinterContact.ParseContactFromJToken(data, p.PrinterIP); list.Add(contact); } p.contacts = list; return p; } catch { return null; } }
public void notifyPrinterContactList(PrinterContact c) { if (!flagSendToPrinter) { listContact_EF.Clear(); listGroupName.Clear(); contactJsons.Clear(); //listContactPrinter.Clear(); if (c != null && c.contacts.Count > 0) { //listContactPrinter = c.contacts; UC_AddressBook.CurrentPrinterContact = c; foreach (ContactViewPrinter cp in c.contacts) { ContactView cv = new ContactView { Name = cp.Name, EmailAddress = cp.EmailAddress, Fax = cp.Fax, Group = cp.Group, Id = cp.Id, ImportedFrom = cp.ImportedFrom }; cv.SplitFirstLastMidFromFullName(ContactDB.NAME_ORDER_FIRST_MID_LAST); listContact_EF.Add(cv); if (!listGroupName.Contains(cp.Group) && !string.IsNullOrEmpty(cp.Group)) { listGroupName.Add(cp.Group); } } } UC_AddressBook.CurrentPrinterContact.contacts = MyUtils.ConvertContactViewToContactViewPrinter(listContact_EF); currentPrinterMaxID = oldPrinterMaxID = GetMaxContactIdPrinter(); printerError = null; } else { contactJsons.Clear(); //listContact_EF.Clear(); flagSendToPrinter = false; if (c != null && c.contacts.Count > 0) { //var temp = new List<ContactView>(); //foreach (ContactView cw in listContact_EF) //{ // if (cw.Id > oldPrinterMaxID) // { // temp.Add(cw); // } //} //foreach (ContactView cc in temp) //{ // listContact_EF.Remove(cc); //} //foreach (ContactViewPrinter cp in c.contacts) //{ // ContactView cv = new ContactView // { // Name = cp.Name, // EmailAddress = cp.EmailAddress, // Fax = cp.Fax, // Group = cp.Group, // Id = cp.Id, // ImportedFrom = cp.ImportedFrom // }; // cv.SplitFirstLastMidFromFullName(ContactDB.NAME_ORDER_FIRST_MID_LAST); // listContact_EF.Add(cv); // //if (cp.ContactAction == MyUtils.ADD) // //{ // // cv.Id = ++currentPrinterMaxID; // // listContact_EF.Add(cv); // //} // //if (cp.ContactAction == MyUtils.DELETE) // //{ // // var found = listContact_EF.Where(x => x.Id == cp.Id).FirstOrDefault(); // // if (found != null) // // { // // listContact_EF.Remove(found); // // } // //} // //if (cp.ContactAction == MyUtils.UPDATE) // //{ // // var found = listContact_EF.Where(x => x.Id == cp.Id).FirstOrDefault(); // // if (found != null) // // { // // listContact_EF.Remove(found); // // } // // listContact_EF.Add(cv); // //} // //if (!listGroupName.Contains(cp.Group) && !string.IsNullOrEmpty(cp.Group)) // //{ // // listGroupName.Add(cp.Group); // //} //} var contact = c.contacts[0]; contact.SplitFirstLastMidFromFullName(ContactDB.NAME_ORDER_FIRST_MID_LAST); contact.Id = ++currentPrinterMaxID; listContact_EF.Add(contact); if (!listGroupName.Contains(contact.Group) && !string.IsNullOrEmpty(contact.Group)) { listGroupName.Add(contact.Group); } UC_AddressBook.CurrentPrinterContact.contacts = MyUtils.ConvertContactViewToContactViewPrinter(listContact_EF); currentPrinterMaxID = oldPrinterMaxID = GetMaxContactIdPrinter(); } } DoRefreshListView(); }