public static string GetDescription(this SyncMode mode) { FieldInfo fi = mode.GetType().GetField(mode.ToString()); var desc = fi.GetCustomAttributes(typeof(DescriptionAttribute), false) as DescriptionAttribute[]; if (null != desc && desc.Length > 0) { return(desc[0].Description); } else { return(mode.ToString()); } }
public void SendSync(string[] numbers, SyncMode mode = SyncMode.Delta, SyncContext context = SyncContext.Background, int index = 0, bool last = true) { List <ProtocolTreeNode> users = new List <ProtocolTreeNode>(); foreach (string number in numbers) { string _number = number; if (!_number.StartsWith("+", StringComparison.InvariantCulture)) { _number = string.Format("+{0}", number); } users.Add(new ProtocolTreeNode("user", null, System.Text.Encoding.UTF8.GetBytes(_number))); } ProtocolTreeNode node = new ProtocolTreeNode("iq", new KeyValue[] { new KeyValue("to", GetJID(this.phoneNumber)), new KeyValue("type", "get"), new KeyValue("id", TicketCounter.MakeId()), new KeyValue("xmlns", "urn:xmpp:whatsapp:sync") }, new ProtocolTreeNode("sync", new KeyValue[] { new KeyValue("mode", mode.ToString().ToLowerInvariant()), new KeyValue("context", context.ToString().ToLowerInvariant()), new KeyValue("sid", DateTime.Now.ToFileTimeUtc().ToString()), new KeyValue("index", index.ToString()), new KeyValue("last", last.ToString()) }, users.ToArray() ) ); this.SendNode(node); }
public void SendSync(string[] numbers, SyncMode mode = SyncMode.Delta, SyncContext context = SyncContext.Background, int index = 0, bool last = true) { List<ProtocolTreeNode> users = new List<ProtocolTreeNode>(); foreach (string number in numbers) { string _number = number; if (!_number.StartsWith("+", StringComparison.InvariantCulture)) _number = string.Format("+{0}", number); users.Add(new ProtocolTreeNode("user", null, System.Text.Encoding.UTF8.GetBytes(_number))); } ProtocolTreeNode node = new ProtocolTreeNode("iq", new KeyValue[] { new KeyValue("to", GetJID(this.phoneNumber)), new KeyValue("type", "get"), new KeyValue("id", TicketCounter.MakeId("sendsync_")), new KeyValue("xmlns", "urn:xmpp:whatsapp:sync") }, new ProtocolTreeNode("sync", new KeyValue[] { new KeyValue("mode", mode.ToString().ToLowerInvariant()), new KeyValue("context", context.ToString().ToLowerInvariant()), new KeyValue("sid", DateTime.Now.ToFileTimeUtc().ToString()), new KeyValue("index", index.ToString()), new KeyValue("last", last.ToString()) }, users.ToArray() ) ); this.SendNode(node); }
/// <summary> /// Writes the current context to the specified <see cref="XmlWriter"/>. /// </summary> /// <param name="writer">The <b>XmlWriter</b> to which you want to write the current context.</param> /// <param name="xmlNamespace">The XML namespace used to qualify prefixed syndication extension elements and attributes.</param> /// <exception cref="ArgumentNullException">The <paramref name="writer"/> is a null reference (Nothing in Visual Basic).</exception> /// <exception cref="ArgumentNullException">The <paramref name="xmlNamespace"/> is a null reference (Nothing in Visual Basic).</exception> /// <exception cref="ArgumentNullException">The <paramref name="xmlNamespace"/> is an empty string.</exception> public void WriteTo(XmlWriter writer, string xmlNamespace) { //------------------------------------------------------------ // Validate parameter //------------------------------------------------------------ Guard.ArgumentNotNull(writer, "writer"); Guard.ArgumentNotNullOrEmptyString(xmlNamespace, "xmlNamespace"); //------------------------------------------------------------ // Write current extension details to the writer //------------------------------------------------------------ if (SyncMode != null) { writer.WriteElementString("syncMode", xmlNamespace, SyncMode.ToString().ToUpper()); } if (Digest != null) { Digest.WriteTo(writer, xmlNamespace); } if (SyncState != null) { SyncState.WriteTo(writer, xmlNamespace); } }
public void SyncMode_VerifyCustomPreset() { var syncMode = new SyncMode { ItemsInSourceOnly = SyncItemOperation.Delete, SameMatches = SyncMatchOperation.UpdateOldItem }; syncMode.SyncModePreset.Should().Be(SyncModePreset.Custom); syncMode.ToString().Should().Be($"{nameof(SyncMode.SyncModePreset)}: {syncMode.SyncModePreset}, {nameof(SyncMode.ItemsInSourceOnly)}: {syncMode.ItemsInSourceOnly}, {nameof(SyncMode.ItemsInDestinationOnly)}: {syncMode.ItemsInDestinationOnly}, {nameof(SyncMode.SameMatches)}: {syncMode.SameMatches}, {nameof(SyncMode.NewerMatches)}: {syncMode.NewerMatches}, {nameof(SyncMode.ConflictMatches)}: {syncMode.ConflictMatches}"); }
public void SyncMode_VerifyGetterAndSetter(SyncModePreset syncModePreset) { var syncMode = new SyncMode { SyncModePreset = syncModePreset }; syncMode.SyncModePreset.Should().Be(syncModePreset); syncMode.ToString().Should().Be($"{nameof(SyncMode.SyncModePreset)}: {syncMode.SyncModePreset}"); }
public static string GetSyncMode(this SyncMode value) { DescriptionAttribute[] attributes = (DescriptionAttribute[])value .GetType() .GetField(value.ToString()) .GetCustomAttributes(typeof(DescriptionAttribute), false); return(attributes.Length > 0 ? attributes[0].Description : string.Empty); }
public JObject ToJSON() { JObject json = new JObject { { "ID", new JValue(ID) }, { "Name", new JValue(Name) }, { "Description", new JValue(Description) }, { "SyncMode", new JValue(SyncMode.ToString()) }, { "Provider", new JValue(Provider) }, { "FullPath", new JValue(FullPath) }, { "CloudProviderPath", new JValue(CloudProviderPath) }, { "Unopened", new JValue(Unopened) } }; return(json); }
private async Task <DateTime?> RunInBackground(SyncMode mode, DateTime?lastRun) { var bus = ServiceContainer.Resolve <MessageBus> (); var log = ServiceContainer.Resolve <Logger> (); var syncDuration = Stopwatch.StartNew(); // Resolve automatic sync mode to actual mode if (mode == SyncMode.Auto) { if (lastRun != null && lastRun > Time.UtcNow - TimeSpan.FromMinutes(5)) { mode = SyncMode.Push; } else { mode = SyncMode.Full; } log.Info(Tag, "Starting automatic ({0}) sync.", mode.ToString()); } else { log.Info(Tag, "Starting {0} sync.", mode.ToString()); } bus.Send(new SyncStartedMessage(this, mode)); bool hasErrors = false; Exception ex = null; try { if (mode == SyncMode.Full) { await CollectGarbage().ConfigureAwait(false); } if (mode.HasFlag(SyncMode.Pull)) { lastRun = await PullChanges(lastRun).ConfigureAwait(false); } if (mode.HasFlag(SyncMode.Push)) { hasErrors = await PushChanges().ConfigureAwait(false); } } catch (Exception e) { if (e.IsNetworkFailure() || e is TaskCanceledException) { log.Info(Tag, e, "Sync ({0}) failed.", mode); if (e.IsNetworkFailure()) { ServiceContainer.Resolve <INetworkPresence> ().RegisterSyncWhenNetworkPresent(); } } else { log.Warning(Tag, e, "Sync ({0}) failed.", mode); } hasErrors = true; ex = e; } finally { syncDuration.Stop(); log.Info(Tag, "Sync finished in {0}ms{1}.", syncDuration.ElapsedMilliseconds, hasErrors ? " (with errors)" : String.Empty); bus.Send(new SyncFinishedMessage(this, mode, hasErrors, ex)); } return(lastRun); }