Beispiel #1
0
 void user_OnDataChanging(object sender, DataChangeEventArgs e)//This method check if the update valid
 {
     try
     {
         PhoneUser user = (PhoneUser)sender;
         if (user == null)
         {
             return;
         }
         if (e.DataName == PhoneUserDataNames.UserName)
         {
             if (!CanAddUserUser(e.NewValue + "", user.UserNumber))
             {
                 e.CancelEdit = true;
             }
         }
         if (e.DataName == PhoneUserDataNames.UserNumber)
         {
             if (!CanAddUserUser(user.UserName, e.NewValue + ""))
             {
                 e.CancelEdit = true;
             }
         }
     }
     catch { }
 }
Beispiel #2
0
        /// <summary>
        /// If the item found return
        /// Return null if the add user failied
        /// </summary>
        /// <param name="user"></param>
        public PhoneUser AddUser(string UserName, string UserNumber, DateTime RegisterTime, string UserAddress, string UserEmail)
        {
            try
            {
                if (IsExistsUser(UserName, UserNumber))
                {
                    LastException = new Exception("Can not add this user becouse of Repeated data");
                    return(null);
                }
                if (UserName == "" || UserNumber == "")
                {
                    LastException = new Exception("Can not add this user becouse UserName or UserNumber empty.");
                    return(null);
                }
                PhoneUser user = new PhoneUser(UserName, UserNumber, RegisterTime, UserAddress, UserEmail);
                user.Id = ++MaxUserId;
                if (SaveUser(user))
                {
                    RegisterUserEvents(user);
                    if (Users != null)
                    {
                        Users.Add(user);
                    }
                    RiseEvent(OnUserAdded, user);

                    XmlFile.SaveDocument();
                    return(user);
                }
                else
                {
                    return(null);
                }
            }
            catch (Exception ex) { LastException = ex; return(null); }
        }
Beispiel #3
0
 public bool DeleteUser(long userId)
 {
     try
     {
         PhoneUser user = GetUserById(userId);
         if (user == null)
         {
             return(false);
         }
         if (Users != null)//remove user from memory
         {
             Users.Remove(user);
         }
         if (!XmlFile.RemoveSection(userId + ""))
         {
             return(false);
         }
         else
         {
             RiseEvent(OnUserRemoved, user);
             XmlFile.SaveDocument();
             return(true);
         }
     }
     catch (Exception ex) { LastException = ex; return(false); }
 }
Beispiel #4
0
 /// <summary>
 /// Get the first user has this userNumber
 /// </summary>
 /// <param name="userNumber"></param>
 /// <returns></returns>
 public PhoneUser GetUser(string userNumber)
 {
     try
     {
         PhoneUser user = PhoneBook.Instance.SearchOneUser(u => u.UserNumber.ToLower() == userNumber.ToLower());
         return(user);
     }
     catch { return(null); }
 }
Beispiel #5
0
 public bool IsExistsUser(string userName, string userNumber)
 {
     try
     {
         PhoneUser user = PhoneBook.Instance.SearchOneUser(u => (u.UserName.ToLower() == userName.ToLower()) &&
                                                           (u.UserNumber.ToLower() == userNumber.ToLower())
                                                           );
         return(user != null);
     }
     catch { return(false); }
 }
Beispiel #6
0
 /// <summary>
 /// Save this user to xml file
 /// </summary>
 /// <param name="user"></param>
 /// <returns></returns>
 private bool SaveUser(PhoneUser user)
 {
     try
     {
         string section = user.Id + "";
         XmlFile.SetValue(section, PhoneUserDataNames.UserName, user.UserName);
         XmlFile.SetValue(section, PhoneUserDataNames.UserNumber, user.UserNumber);
         XmlFile.SetValue(section, PhoneUserDataNames.UserAddress, user.UserAddress);
         XmlFile.SetValue(section, PhoneUserDataNames.UserEmail, user.UserEmail);
         XmlFile.SetValue(section, PhoneUserDataNames.RegisterTime, user.RegisterTime.ToBinary() + "");
         return(true);
     }
     catch (Exception ex) { LastException = ex; return(false); }
 }
Beispiel #7
0
 private PhoneUser GetRecord(string section)
 {
     try
     {
         Dictionary <string, string> data = XmlFile.GetEntryValues(section);
         PhoneUser user = new PhoneUser();
         user.UserName     = data[PhoneUserDataNames.UserName];
         user.UserNumber   = data[PhoneUserDataNames.UserNumber];
         user.UserAddress  = data[PhoneUserDataNames.UserAddress];
         user.UserEmail    = data[PhoneUserDataNames.UserEmail];
         user.RegisterTime = DateTime.FromBinary(long.Parse(data[PhoneUserDataNames.RegisterTime]));
         return(user);
     }
     catch { return(null); }
 }
Beispiel #8
0
 /// <summary>
 /// Search users and get userName if user found or return the user number
 /// </summary>
 /// <param name="userNumber"></param>
 /// <returns></returns>
 public string GetUserNameOrNumber(string userNumber)
 {
     try
     {
         PhoneUser user = GetUser(userNumber);
         if (user == null)
         {
             return(userNumber);
         }
         else
         {
             return(user.UserName);
         }
     }
     catch { return(""); }
 }
Beispiel #9
0
 private void user_OnDataChanged(object sender, DataChangeEventArgs e)
 {
     try
     {
         PhoneUser user = (PhoneUser)sender;
         if (user == null)
         {
             return;
         }
         RiseEvent(OnUserEdited, sender, e);
         string section  = user.Id + "";
         string newValue = e.NewValue is DateTime ? ((DateTime)e.NewValue).ToBinary() + "" : e.NewValue + "";
         XmlFile.SetValue(section, e.DataName, newValue);
         XmlFile.SaveDocument();
     }
     catch { }
 }
Beispiel #10
0
        private void LoadRecords()
        {
            Users = new List <PhoneUser>();
            List <string> sections = XmlFile.GetSectionNames();

            foreach (string section in sections)
            {
                PhoneUser user = GetRecord(section);
                if (user == null)
                {
                    continue;
                }
                user.Id = long.Parse(section);
                if (user.Id > MaxUserId)
                {
                    MaxUserId = user.Id;
                }
                RegisterUserEvents(user);
                Users.Add(user);
            }
            //    Users.Sort(PhoneUserComparer.Instance);
        }
Beispiel #11
0
 public void DeleteUser(long[] userIds)
 {
     try
     {
         foreach (long userId in userIds)
         {
             PhoneUser user = GetUserById(userId);
             if (user == null)
             {
                 continue;
             }
             if (Users != null)//remove user from memory
             {
                 Users.Remove(user);
             }
             if (XmlFile.RemoveSection(userId + ""))
             {
                 RiseEvent(OnUserRemoved, user);
             }
         }
         XmlFile.SaveDocument();
     }
     catch (Exception ex) { LastException = ex; }
 }
Beispiel #12
0
 private void RegisterUserEvents(PhoneUser user)
 {
     user.OnDataChanged  += new SIPProvider.SIPCore.DataChanged(user_OnDataChanged);
     user.OnDataChanging += new DataChanged(user_OnDataChanging);
 }