Exemple #1
0
        private ITask GetTaskFromToodleDo(ToodleDoTask source)
        {
            var task = this.Workbook.CreateTask();

            task.Title           = source.Title;
            task.Note            = source.Note;
            task.Tags            = source.Tags;
            task.Added           = source.Added;
            task.Priority        = source.EnumPriority;
            task.Due             = source.Due;
            task.Start           = source.Start;
            task.Completed       = source.Completed;
            task.SyncId          = source.Id;
            task.Modified        = source.Modified;
            task.UseFixedDate    = (source.RepeatFrom == 0);
            task.CustomFrequency = ToodleDoRecurrencyHelpers.Get2DayRecurrency(source.Repeat);

            if (!string.IsNullOrWhiteSpace(source.ParentId))
            {
                var parent = this.Workbook.Tasks.FirstOrDefault(t => t.SyncId == source.ParentId);
                if (parent != null)
                {
                    parent.AddChild(task);
                }
            }

            return(task);
        }
Exemple #2
0
        public async Task <bool> UpdateTask(string id, ToodleDoTask toodleDoTask, TaskProperties properties)
        {
            // if only the "Modified" property has changed, do nothing
            if (properties == TaskProperties.Modified)
            {
                return(true);
            }

            var request = this.GetTaskChangeRequest(toodleDoTask, properties, true);

            string apiCall = null;

            if (string.IsNullOrEmpty(request.Options))
            {
                apiCall = string.Format("{0}/tasks/edit.php?key={1};tasks=[{2}];f=xml;t={3}",
                                        this.serverUrl, this.Key, request.JsonData, DateTime.UtcNow.Ticks);
            }
            else
            {
                apiCall = string.Format("{0}/tasks/edit.php?key={1};tasks=[{2}];fields={3};f=xml;t={4}",
                                        this.serverUrl, this.Key, request.JsonData, request.Options, DateTime.UtcNow.Ticks);
            }

            var result = await this.DownloadDataAsync(apiCall);

            if (result.HasError)
            {
                return(false);
            }

            return(true);
        }
Exemple #3
0
        private static bool IsEquivalentTo(ToodleDoTask toodleDoTask, ITask task)
        {
            bool match = toodleDoTask.Title.Equals(task.Title, StringComparison.CurrentCultureIgnoreCase) &&
                         (
                (toodleDoTask.Note == null && task.Note == null) ||
                (toodleDoTask.Note != null && task.Note != null && toodleDoTask.Note.Equals(task.Note, StringComparison.CurrentCultureIgnoreCase))
                         ) &&
                         toodleDoTask.EnumPriority == task.Priority &&
                         toodleDoTask.Due == task.Due &&
                         toodleDoTask.Start == task.Start &&
                         toodleDoTask.Tags == task.Tags &&
                         toodleDoTask.Completed == task.Completed &&
                         toodleDoTask.FolderId == task.Folder.SyncId &&
                         ((task.Context == null && (toodleDoTask.ContextId == string.Empty || toodleDoTask.ContextId == "0")) || (task.Context != null && toodleDoTask.ContextId == task.Context.SyncId)) &&
                         toodleDoTask.RepeatFrom == (task.UseFixedDate ? 0 : 1) &&
                         ToodleDoRecurrencyHelpers.Get2DayRecurrency(toodleDoTask.Repeat).Equals(task.CustomFrequency);

            if (!match)
            {
                return(false);
            }

            if (toodleDoTask.ParentId == null && task.ParentId == null)
            {
                return(true);
            }

            if (toodleDoTask.ParentId != null && task.ParentId == null)
            {
                return(false);
            }

            if (toodleDoTask.ParentId == null && task.ParentId != null)
            {
                return(false);
            }

            // check parent
            if (toodleDoTask.ParentId != null && task.ParentId != null)
            {
                var parentTask = task.Folder.Tasks.FirstOrDefault(t => t.Id == task.ParentId);
                return(parentTask != null && toodleDoTask.ParentId == parentTask.SyncId);
            }

            return(true);
        }
Exemple #4
0
        public async Task <string> AddTask(ToodleDoTask toodleDoTask)
        {
            this.LogDebugFormat("Adding tasks {0}", toodleDoTask.Title);

            string apiCall = null;

            TaskChangeRequest request = this.GetTaskChangeRequest(toodleDoTask, TaskProperties.All, false);

            apiCall = string.Format("{0}/tasks/add.php?key={1};tasks=[{2}];fields={3};f=xml;t={4}", this.serverUrl, this.Key, request.JsonData, request.Options, DateTime.UtcNow.Ticks);

            if (apiCall.Length > MaxLengthUri && toodleDoTask.Note != null)
            {
                // ToodleDo sends an error if the whole uri is more than 8 200 characters
                while (apiCall.Length > MaxLengthUri)
                {
                    toodleDoTask.Note = toodleDoTask.Note.Substring(0, toodleDoTask.Note.Length - 100);
                    request           = this.GetTaskChangeRequest(toodleDoTask, TaskProperties.All, false);
                    apiCall           = string.Format("{0}/tasks/add.php?key={1};tasks=[{2}];fields={3};f=xml;t={4}", this.serverUrl, this.Key, request.JsonData, request.Options, DateTime.UtcNow.Ticks);
                }
            }

            var result = await this.DownloadDataAsync(apiCall);

            if (result.HasError || !result.Document.Descendants("tasks").Any())
            {
                return(null);
            }

            string id = null;

            List <string> ids = result.Document.Descendants("task").Select(x => x.Element("id").Value).ToList();

            if (ids.Count > 0 && ids[0] != "-1")
            {
                id = ids[0];
            }
            else
            {
                this.LogDebugFormat("Error while adding task {0}", toodleDoTask.Title);
            }

            return(id);
        }
Exemple #5
0
        private TaskChangeRequest GetTaskChangeRequest(ToodleDoTask toodleDoTask, TaskProperties properties, bool includeId)
        {
            var           request = new TaskChangeRequest();
            StringBuilder sb      = new StringBuilder();
            StringWriter  sw      = new StringWriter(sb);

            using (JsonWriter jsonWriter = new JsonTextWriter(sw))
            {
                jsonWriter.Formatting = Formatting.None;
                jsonWriter.WriteStartObject();

                if (includeId)
                {
                    jsonWriter.WritePropertyName("id");
                    jsonWriter.WriteValue(toodleDoTask.Id);
                }

                if ((properties & TaskProperties.Title) == TaskProperties.Title)
                {
                    jsonWriter.WritePropertyName("title");
                    jsonWriter.WriteValue(toodleDoTask.Title);
                }

                if ((properties & TaskProperties.Note) == TaskProperties.Note)
                {
                    string note = string.Empty;
                    if (!string.IsNullOrEmpty(toodleDoTask.Note))
                    {
                        note = toodleDoTask.Note;
                    }

                    jsonWriter.WritePropertyName("note");
                    jsonWriter.WriteValue(note);
                    request.Options += "note,";
                }

                if ((properties & TaskProperties.Tags) == TaskProperties.Tags)
                {
                    if (!string.IsNullOrEmpty(toodleDoTask.Tags))
                    {
                        jsonWriter.WritePropertyName("tag");
                        jsonWriter.WriteValue(toodleDoTask.Tags);
                        request.Options += "tag,";
                    }
                }

                if ((properties & TaskProperties.Folder) == TaskProperties.Folder)
                {
                    jsonWriter.WritePropertyName("folder");
                    jsonWriter.WriteValue(toodleDoTask.FolderId);
                    request.Options += "folder,";
                }

                if ((properties & TaskProperties.Context) == TaskProperties.Context)
                {
                    string contextId = toodleDoTask.ContextId;
                    if (string.IsNullOrEmpty(contextId))
                    {
                        contextId = "0";
                    }

                    jsonWriter.WritePropertyName("context");
                    jsonWriter.WriteValue(contextId);
                    request.Options += "context,";
                }

                if ((properties & TaskProperties.Due) == TaskProperties.Due)
                {
                    int duedate = 0;

                    if (toodleDoTask.Due.HasValue)
                    {
                        duedate = toodleDoTask.Due.Value.DateTimeToTimestamp();
                    }

                    jsonWriter.WritePropertyName("duedate");
                    jsonWriter.WriteValue(duedate);

                    request.Options += "duedate,";
                }

                if ((properties & TaskProperties.Start) == TaskProperties.Start)
                {
                    int startdate = 0;
                    int starttime = 0;

                    if (toodleDoTask.Start.HasValue)
                    {
                        startdate = toodleDoTask.Start.Value.Date.DateTimeToTimestamp();
                        starttime = (int)toodleDoTask.Start.Value.TimeOfDay.TotalSeconds;
                    }

                    jsonWriter.WritePropertyName("startdate");
                    jsonWriter.WriteValue(startdate);

                    request.Options += "starttime,";

                    jsonWriter.WritePropertyName("starttime");
                    jsonWriter.WriteValue(starttime);

                    request.Options += "starttime,";
                }

                if ((properties & TaskProperties.Frequency) == TaskProperties.Frequency)
                {
                    jsonWriter.WritePropertyName("repeat");
                    jsonWriter.WriteValue(toodleDoTask.Repeat);

                    request.Options += "repeat,";
                }

                if ((properties & TaskProperties.RepeatFrom) == TaskProperties.RepeatFrom)
                {
                    jsonWriter.WritePropertyName("repeatfrom");
                    jsonWriter.WriteValue(toodleDoTask.RepeatFrom);

                    request.Options += "repeatfrom,";
                }

                if ((properties & TaskProperties.Completed) == TaskProperties.Completed)
                {
                    if (toodleDoTask.Completed.HasValue)
                    {
                        jsonWriter.WritePropertyName("completed");
                        jsonWriter.WriteValue(toodleDoTask.Completed.Value.DateTimeToTimestamp());
                    }
                    else
                    {
                        jsonWriter.WritePropertyName("completed");
                        jsonWriter.WriteValue(0);
                    }
                }

                if ((properties & TaskProperties.Added) == TaskProperties.Added)
                {
                    jsonWriter.WritePropertyName("added");
                    jsonWriter.WriteValue(toodleDoTask.Added.DateTimeToTimestamp());

                    request.Options += "added,";
                }

                if ((properties & TaskProperties.Priority) == TaskProperties.Priority)
                {
                    jsonWriter.WritePropertyName("priority");
                    jsonWriter.WriteValue(toodleDoTask.Priority);

                    request.Options += "priority,";
                }

                if ((properties & TaskProperties.Parent) == TaskProperties.Parent)
                {
                    jsonWriter.WritePropertyName("parent");
                    jsonWriter.WriteValue(toodleDoTask.ParentId);

                    request.Options += "parent,";
                }

                jsonWriter.WriteEndObject();
            }

            request.JsonData = Escape(sb.ToString());

            return(request);
        }