Mini Kabibi Habibi

Current Path : C:/Users/Public/Documents/DXperience 13.1 Demos/Silverlight/Bin/
Upload File :
Current File : C:/Users/Public/Documents/DXperience 13.1 Demos/Silverlight/Bin/MailClientService.svc.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ServiceModel.Activation;
using System.Collections.ObjectModel;
using System.Xml;
using System.Web;
using System.Data;
using System.IO;

namespace DevExpress.MailClient.DataService {
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class MailClientService : IMailClientService {
        public ObservableCollection<ServerContact> GetContacts() {
            XmlDocument doc = new XmlDocument();
            ObservableCollection<ServerContact> contacts = new ObservableCollection<ServerContact>();
            doc.Load(FindFile("VideoRent.xml"));
            XmlNodeList customers = doc.GetElementsByTagName("Customer");
            XmlNodeList names = doc.GetElementsByTagName("Person");
            ServerContact contact = null;
            foreach(XmlNode customer in customers) {
                contact = new ServerContact();
                contact.MiddleName = customer["MiddleName"].InnerText;
                contact.Email = customer["Email"].ChildNodes[0].Value;
                contact.Email = contact.Email.Replace("dxvideorent.com", "dxmail.net");
                contact.Address = customer["Address"].ChildNodes[0].Value;
                contact.Phone = customer["Phone"].ChildNodes[0].Value;
                if(customer["Photo"] != null)
                    contact.Photo = Convert.FromBase64String(customer["Photo"].ChildNodes[0].Value);

                FillPersonInformation(contact, names, customer["Oid"].ChildNodes[0].Value);
                contacts.Add(contact);
            }
            return contacts;
        }

        void FillPersonInformation(ServerContact contact, XmlNodeList names, string id) {
            foreach(XmlNode customer in names) {
                if(customer["Oid"].ChildNodes[0].Value == id) {
                    contact.FirstName = customer["FirstName"].ChildNodes[0].Value;
                    contact.LastName = customer["LastName"].ChildNodes[0].Value;
                    contact.Gender = customer["Gender"].ChildNodes[0].Value;
                    contact.BirthDate = customer["BirthDate"].ChildNodes[0].Value;
                }
            }
        }

        DateTime lastMailDate = DateTime.Now;
        public ObservableCollection<ServerMessage> GetMessages() {
            ObservableCollection<ServerMessage> messages = new ObservableCollection<ServerMessage>();
            DataTable messagesTable = CreateDataTable("Messages");
            if(messagesTable != null && messagesTable.Rows.Count > 0) {
                lastMailDate = (DateTime)messagesTable.Rows[0]["Date"];
                foreach(DataRow row in messagesTable.Rows)
                    messages.Add(CreateMessage(row));
            }
            return messages;
        }

        ServerMessage CreateMessage(DataRow row) {
            ServerMessage message = new ServerMessage();
            message.Date = ((DateTime)row["Date"]).Add(DateTime.Now - lastMailDate);
            message.From = string.Format("{0}", row["From"]);
            message.Subject = string.Format("{0}", row["Subject"]);
            message.IsReply = (bool)row["IsReply"];
            message.HasAttachment = (bool)row["HasAttachment"];
            message.Text = string.Format("{0}", row["Text"]);
            message.Folder = string.Format("{0}", row["Folder"]);
            return message;
        }

        public ObservableCollection<ServerAppointment> GetAppointments() {
            ObservableCollection<ServerAppointment> appointments = new ObservableCollection<ServerAppointment>();
            DataTable appointmentsTable = CreateDataTable("Appointments");
            if(appointmentsTable != null && appointmentsTable.Rows.Count > 0) {
                foreach(DataRow row in appointmentsTable.Rows)
                    appointments.Add(CreateAppointment(row));
            }
            return appointments;
        }

        ServerAppointment CreateAppointment(DataRow row) {
            ServerAppointment appointment = new ServerAppointment();
            appointment.EventType = (int?)row["EventType"];
            appointment.StartDate = (DateTime?)row["StartDate"];
            appointment.EndDate = (DateTime?)row["EndDate"];
            appointment.AllDay = (bool?)row["AllDay"];
            appointment.Subject = Convert.ToString(row["Subject"]);
            appointment.Location = Convert.ToString(row["Location"]);
            appointment.Description = Convert.ToString(row["Description"]);
            appointment.Status = (int?)row["Status"];
            appointment.Label = (int?)row["Label"];
            appointment.RecurrenceInfo = Convert.ToString(row["RecurrenceInfo"]);
            appointment.ReminderInfo = Convert.ToString(row["ReminderInfo"]);
            appointment.ContactInfo = Convert.ToString(row["ContactInfo"]);
            return appointment;
        }

        DataTable CreateDataTable(string table) {
            DataSet dataSet = new DataSet();
            dataSet.ReadXml(FindFile("Mail.xml"));
            return dataSet.Tables[table];
        }

        string FindFile(string fileName) {
            string appPath = AppDomain.CurrentDomain.BaseDirectory;
            if(appPath == null)
                return null;
            string dirName = Path.GetFullPath(appPath);
            for(int n = 0; n < 9; n++) {
                string path = dirName + "\\Data\\" + fileName;
                try {
                    if(File.Exists(path) || Directory.Exists(path)) {
                        FileInfo fileInfo = new FileInfo(path);
                        return fileInfo.FullName;
                    }
                } catch { }
                dirName += @"\..";
            }
            throw new FileNotFoundException(fileName + " not found");
        }
    }
}