Exemplo n.º 1
0
        /// <summary>
        /// Add content to a AddCommand or a ReplaceCommand, with currentNode of SifCChangeLogXML.
        /// </summary>
        /// <param name="command">Command object to be manipulated.</param>
        /// <param name="currentNode">A C element under either New or Update element.</param>
        protected virtual void AddContentToCommand(SyncMLUpdateBase command, XElement currentNode)
        {
            if (command == null)
            {
                Trace.TraceWarning("Hey, r u crazy? giving me null command.");
                return;
            }

            if (currentNode == null)
            {
                Trace.TraceWarning("Hey, r u crazy? giving me null currentNode.");
                return;
            }

            command.Meta.Xml.Add(FormatOfBase64);
            command.Meta.Xml.Add(TypeOfText);

            SyncMLItem item = SyncMLItem.Create();

            item.Source.LocURI.Content = currentNode.Attribute("ID").Value;

            UTF8Encoding byteConverter = new UTF8Encoding();

            byte[] buffer = byteConverter.GetBytes("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + currentNode.Element("contact").ToString(SaveOptions.DisableFormatting));
            item.Data.Content = Convert.ToBase64String(buffer);

            command.ItemCollection.Add(item);
        }
Exemplo n.º 2
0
        protected override void AddContentToCommand(SyncMLUpdateBase command, AppointmentItem contact)
        {
            command.Meta.Xml.Add(TypeOfText);
            SyncMLItem item = SyncMLItem.Create();

            item.Source.LocURI.Content = contact.EntryID;
            item.Data.Content          = GetItemData(contact);
            command.ItemCollection.Add(item);
        }
Exemplo n.º 3
0
        protected override void AddContentToCommand(SyncMLUpdateBase command, TaskItem task)
        {
            command.Meta.Xml.Add(TypeOfText);
            SyncMLItem item = SyncMLItem.Create();

            item.Source.LocURI.Content = task.EntryID;
            item.Data.Content          = GetItemData(task);
            command.ItemCollection.Add(item);
        }
Exemplo n.º 4
0
        protected override void AddContentToCommand(SyncMLUpdateBase command, AppointmentItem content)
        {
            command.Meta.Xml.Add(FormatOfBase64);
            command.Meta.Xml.Add(TypeOfText);

            SyncMLItem item = SyncMLItem.Create();

            item.Source.LocURI.Content = content.EntryID;

            UTF8Encoding byteConverter = new UTF8Encoding();

            byte[] buffer = byteConverter.GetBytes(GetItemData(content));
            item.Data.Content = Convert.ToBase64String(buffer);

            command.ItemCollection.Add(item);
        }
Exemplo n.º 5
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="command"></param>
        /// <param name="currentNode">A C element under either New or Update element.</param>
        protected override void AddContentToCommand(SyncMLUpdateBase command, XElement currentNode)
        {
            if (command == null)
            {
                Trace.TraceWarning("Hey, you bad. Null command.");
                return;
            }

            if (currentNode == null)
            {
                Trace.TraceWarning("Hey, r u crazy? giving me null currentNode.");
                return;
            }

            if (useBase64)
            {
                SyncMLItem item = SyncMLItem.Create();
                item.Source.LocURI.Content = currentNode.Attribute("ID").Value;
                item.Meta.Xml.Add(FormatOfBase64);
                item.Meta.Xml.Add(TypeOfText);

                UTF8Encoding byteConverter = new UTF8Encoding();
                byte[]       buffer        = byteConverter.GetBytes(VCardWriter.WriteToString(VCardSIFC.ConvertSifCToVCard(currentNode.Element("contact"))));
                item.Data.Content = Convert.ToBase64String(buffer);

                command.ItemCollection.Add(item);
            }
            else
            {
                SyncMLItem item = SyncMLItem.Create();
                item.Source.LocURI.Content = currentNode.Attribute("ID").Value;
                item.Meta.Xml.Add(xVcardType);

                item.Data.Content = VCardWriter.WriteToString(VCardSIFC.ConvertSifCToVCard(currentNode.Element("contact")));

                command.ItemCollection.Add(item);
            }
        }
Exemplo n.º 6
0
 /// <summary>
 /// Add content to a AddCommand or a ReplaceCommand, with currentNode of SifCChangeLogXML.
 /// </summary>
 /// <param name="command">Command object to be manipulated.</param>
 /// <param name="currentNode">A C element under either New or Update element.</param>
 protected abstract void AddContentToCommand(SyncMLUpdateBase command, T content);
Exemplo n.º 7
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="command"></param>
        /// <returns>localId=serverId pair</returns>
        private string ApplyAddOrReplaceCommand(SyncMLUpdateBase command)
        {
            if (command == null)
            {
                return(null);
            }

            bool isBase64     = false;
            bool isAddCommand = false;

            SyncMLMeta commandMeta = command.Meta;
            SyncMLItem commandItem = command.ItemCollection[0]; //assuming there is always one item.

            if (String.IsNullOrEmpty(commandMeta.Content) && (!commandMeta.Xml.HasElements))
            {
                commandMeta = commandItem.Meta;
            }

            MetaParser metaParser = new MetaParser(commandMeta.Xml);
            MetaFormat metaFormat = metaParser.GetMetaFormat();

            if (metaFormat != null)
            {
                isBase64 = metaFormat.Content == "b64";
            }

            MetaType metaType = metaParser.GetMetaType();

            if (metaType == null)
            {
                return(null); //the meta element may be empty, so no need to proceed.
            }

            //    if (contentType == ContactExchangeType.Unknown)
            //todo: add some exception        throw new LocalDataSourceException("expected data is Base64 encoded SIF-C or vCard");

            isAddCommand = command is SyncMLAdd;

            //Assuming there will be only one item.
            string serverId;
            string localId = null;

            serverId = commandItem.Source.LocURI.Content; //id of remote one

            if (!isAddCommand)
            {
                localId = commandItem.Target.LocURI.Content; // entryId of existing contact
            }
            string text = GetTextFromContent(isBase64, metaType.Content, commandItem.Data.Content);

            if (text != null)
            {
                localId = isAddCommand ? sifAgent.AddItem(text) :
                          sifAgent.ReplaceItem(text, localId);

                return(isAddCommand ? localId + "=" + serverId : null);
            }
            else
            {
                return(null);
            }
        }
        }  // using xmlWriter

        /// <summary>
        ///
        /// </summary>
        /// <param name="topElement">the 'Changes' element, to be manipulated</param>
        /// <param name="command"></param>
        private static void WriteAddOrReplaceCommandToXml(XElement topElement, SyncMLUpdateBase command)
        {
            if (command == null)
            {
                return;
            }

            bool isBase64 = false;
            ContactExchangeType contentType = ContactExchangeType.Unknown;
            bool isAddCommand = false;

            string commandTypeStr;

            SyncMLMeta commandMeta = command.Meta;
            SyncMLItem commandItem = command.ItemCollection[0]; //assuming there is always one item.

            if (String.IsNullOrEmpty(commandMeta.Content) && (!commandMeta.Xml.HasElements))
            {
                commandMeta = commandItem.Meta;
            }

            MetaParser metaParser = new MetaParser(commandMeta.Xml);
            MetaFormat metaFormat = metaParser.GetMetaFormat();

            if (metaFormat != null)
            {
                isBase64 = metaFormat.Content == "b64";
            }

            MetaType metaType = metaParser.GetMetaType();

            if (metaType == null)
            {
                return; //the meta element may be empty, so no need to proceed.
            }

            if (metaType.Content == "text/x-s4j-sifc")
            {
                contentType = ContactExchangeType.Sifc;
            }
            else if ((metaType.Content == "text/x-vcard") || (metaType.Content == "text/vcard"))
            {
                contentType = ContactExchangeType.Vcard21;
            }

            if (contentType == ContactExchangeType.Unknown)
            {
                throw new LocalDataSourceException("expected data is Base64 encoded SIF-C or vCard");
            }

            isAddCommand   = command is SyncMLAdd;
            commandTypeStr = isAddCommand ? "New" : "Update";

            //Assuming there will be only one item.
            string id;

            if (isAddCommand)
            {
                id = commandItem.Source.LocURI.Content;
            }
            else
            {
                id = commandItem.Target.LocURI.Content;
            }

            XElement cItem     = new XElement("C", new XAttribute("ID", id));
            XElement changItem = new XElement(commandTypeStr, cItem);

            topElement.Add(changItem);

            switch (contentType)
            {
            case ContactExchangeType.Sifc:     // do not need to check isBase64, because it must be
                cItem.Add(XElement.Parse(Utility.ConvertFromBase64(commandItem.Data.Content)));
                break;

            case ContactExchangeType.Vcard21:
                XElement sifcXml;
                if (isBase64)
                {
                    sifcXml = VCardSIFC.ConvertVCardToSifCXml(VCardReader.ParseText(
                                                                  Utility.ConvertFromBase64(commandItem.Data.Content)));
                }
                else
                {
                    sifcXml = VCardSIFC.ConvertVCardToSifCXml(VCardReader.ParseText(commandItem.Data.Content));
                }

                cItem.Add(sifcXml);
                break;

            default:
                throw new LocalDataSourceException("Can not create stream from command Data.");
            }
        }