// Creates a new note with the given title and guid with body based on
        // the template note.
        private Note CreateNoteFromTemplate(string title, Note template_note, string guid)
        {
            Tag template_save_title = TagManager.GetOrCreateSystemTag(TagManager.TemplateNoteSaveTitleSystemTag);

            if (template_note.ContainsTag(template_save_title))
            {
                title = GetUniqueName(template_note.Title, notes.Count);
            }

            // Use the body from the template note
            string xml_content =
                template_note.XmlContent.Replace(XmlEncoder.Encode(template_note.Title),
                                                 XmlEncoder.Encode(title));

            xml_content = SanitizeXmlContent(xml_content);

            Note new_note = CreateNewNote(title, xml_content, guid);

            // Copy template note's properties
            Tag template_save_size = TagManager.GetOrCreateSystemTag(TagManager.TemplateNoteSaveSizeSystemTag);

            if (template_note.Data.HasExtent() && template_note.ContainsTag(template_save_size))
            {
                new_note.Data.Height = template_note.Data.Height;
                new_note.Data.Width  = template_note.Data.Width;
            }

            Tag template_save_selection = TagManager.GetOrCreateSystemTag(TagManager.TemplateNoteSaveSelectionSystemTag);

            if (template_note.Data.CursorPosition > 0 && template_note.ContainsTag(template_save_selection))
            {
                Gtk.TextBuffer buffer = new_note.Buffer;
                Gtk.TextIter   iter;

                // Because the titles will be different between template and
                // new note, we can't just drop the cursor at template's
                // CursorPosition. Whitespace after the title makes this more
                // complicated so let's just start counting from the line after the title.
                int title_offset_difference = buffer.GetIterAtLine(1).Offset - template_note.Buffer.GetIterAtLine(1).Offset;

                iter = buffer.GetIterAtOffset(template_note.Data.CursorPosition + title_offset_difference);
                buffer.PlaceCursor(iter);

                iter = buffer.GetIterAtOffset(template_note.Data.SelectionBoundPosition + title_offset_difference);
                buffer.MoveMark(buffer.SelectionBound.Name, iter);
            }

            return(new_note);
        }