Ejemplo n.º 1
0
        public async void ExportTasks(IRibbonControl control)
        {
            Window context            = control.Context as Window;
            CWin32WindowWrapper owner = new CWin32WindowWrapper((IntPtr)context.WindowHandle);

            Microsoft.Office.Interop.OneNote.Application onenote = new Microsoft.Office.Interop.OneNote.Application();
            string thisNoteBook = onenote.Windows.CurrentWindow.CurrentNotebookId;
            string thisSection  = onenote.Windows.CurrentWindow.CurrentSectionId;
            string thisPage     = onenote.Windows.CurrentWindow.CurrentPageId;

            String link;

            onenote.GetHyperlinkToObject(thisPage, System.String.Empty, out link);

            String xmlNotebooks;

            onenote.GetHierarchy(null,
                                 Microsoft.Office.Interop.OneNote.HierarchyScope.hsPages, out xmlNotebooks);

            var notebooks = XDocument.Parse(xmlNotebooks);

            var currentbook = from item in notebooks.Descendants(notebooks.Root.Name.Namespace + "Notebook")
                              where item.Attribute("ID").Value == thisNoteBook
                              select item;

            var notebook = currentbook.First().Attribute("name").Value;

            String xmlPage;

            onenote.GetPageContent(thisPage, out xmlPage);
            doc = XDocument.Parse(xmlPage);
            ns  = doc.Root.Name.Namespace;

            var pageTitle = RemoveHtmlTags(doc.Descendants(ns + "Title").First().Value);
            var title     = notebook + ": " + pageTitle;

            var tags = from tagDef in doc.Descendants(ns + "TagDef")
                       where tagDef.Attribute("symbol").Value == "3"
                       select tagDef;

            if (tags.Count() == 0)

            {
                MessageBox.Show(owner, "No tasks found on this page!", pageTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }

            else

            {
                var      index   = tags.First().Attribute("index").Value;
                string[] allTags = new string[2];
                int      i       = 0;
                foreach (var tag in tags)
                {
                    allTags[i] = tag.Attribute("index").Value;
                    i++;
                }
                var tasks = from oe in doc.Descendants(ns + "OE")
                            from item in oe.Elements(ns + "Tag")
                            //where item.Attribute("index").Value == index
                            where allTags.Contains(item.Attribute("index").Value)
                            where item.Attribute("completed").Value == "false"
                            select oe;
                if (tasks.Count() == 0)
                {
                    MessageBox.Show(owner, "No tasks found on this page!", pageTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }


                string allTasks  = "";
                string prefix    = title;
                string formTitle = "Tasks found: " + tasks.Count().ToString();
                if (prefix.Length > 60)
                {
                    prefix = prefix.Substring(0, 60);
                }
                int counter = 0;
                foreach (var task in tasks)
                {
                    counter++;
                    allTasks += "o    " + prefix + "\n       " + RemoveHtmlTags(task.Element(ns + "T").Value) + "\n\n";
                    if (counter > 4)
                    {
                        break;
                    }
                }
                string taskas = RemoveHtmlTags(tasks.ElementAt(0).Value);

                LoginForm login = new LoginForm();
                login.ShowDialog(owner);

                if (login.DialogResult == DialogResult.OK)
                {
                    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
                    ITodoistTokenlessClient tokenlessClient = new TodoistTokenlessClient();

                    try
                    {
                        if (login.email.Contains("@") == false)
                        {
                            login.email += "@ardi.lt";
                        }
                        ITodoistClient client = await tokenlessClient.LoginAsync(login.email, login.password);

                        var projects = await client.Projects.GetAsync();

                        TasksForm confirm = new TasksForm(formTitle, title, prefix, taskas, projects);
                        confirm.ShowDialog(owner);

                        if (confirm.DialogResult == DialogResult.OK)
                        {
                            var transaction = client.CreateTransaction();

                            if (confirm.id > 0)
                            {
                                foreach (var item in projects)
                                {
                                    if (item.Name == confirm.project)
                                    {
                                        foreach (var task in tasks)
                                        {
                                            var content = "[" + confirm.prefix + RemoveHtmlTags(task.Value) + "](" + link + ")";
                                            var todo    = new Todoist.Net.Models.Item(content);
                                            todo.ProjectId = item.Id;
                                            var taskId = await transaction.Items.AddAsync(todo);
                                        }
                                    }
                                }
                            }
                            else
                            {
                                var projectId = await transaction.Project.AddAsync(new Todoist.Net.Models.Project(confirm.project));

                                foreach (var task in tasks)
                                {
                                    var content = "[" + confirm.prefix + RemoveHtmlTags(task.Value) + "](" + link + ")";
                                    var taskId  = await transaction.Items.AddAsync(new Todoist.Net.Models.Item(content, projectId));

                                    //await transaction.Notes.AddToItemAsync(new Todoist.Net.Models.Note("Task description"), taskId);
                                }
                            }

                            await transaction.CommitAsync();

                            System.Diagnostics.Process.Start("https://todoist.com");
                        }

                        confirm.Dispose();
                        confirm = null;
                    }
                    catch
                    {
                        MessageBox.Show(owner, "Bad user mail or password...", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }

                login.Dispose();
                login = null;
            }
        }