public GallerySettingsPage()
        {
            InitializeComponent();

            _post = App.MasterViewModel.CurrentPost;
            DataContext = _post;
            _localizedStrings = App.Current.Resources["StringTable"] as StringTable;

            InitializeApplicationBar();
            InitializePickers();
        }
        private void LoadPage()
        {
            Blog currentBlog = App.MasterViewModel.CurrentBlog;

            BlogName.Text = currentBlog.BlogNameUpper;

            if (null != App.MasterViewModel.CurrentPageListItem)
            {
                if (App.MasterViewModel.CurrentPageListItem.DraftIndex > -1)
                {
                    // Page is a local draft
                    this.isEditingLocalDraft = true;
                    DataContext = App.MasterViewModel.CurrentBlog.LocalPageDrafts[App.MasterViewModel.CurrentPageListItem.DraftIndex];
                    App.MasterViewModel.CurrentPost = App.MasterViewModel.CurrentBlog.LocalPageDrafts[App.MasterViewModel.CurrentPageListItem.DraftIndex];
                    setStatus();

                    //update the Media UI
                    List<Media> unavaiblePictures = new List<Media>();
                    foreach (Media currentMedia in App.MasterViewModel.CurrentPost.Media)
                    {
                        using (Stream stream = currentMedia.getImageStream())
                        {
                            if (stream == null)
                            {
                                unavaiblePictures.Add(currentMedia);
                                continue;
                            }

                            try
                            {
                                BitmapImage image = new BitmapImage();
                                image.SetSource(stream);
                                imageWrapPanel.Children.Add(BuildTappableImageElement(image, currentMedia));
                            }
                            catch (Exception)
                            {
                            }

                        }
                    }
                    if (unavaiblePictures.Count > 0)
                    {
                        MessageBoxResult result = MessageBox.Show("Can't read a picture attached to this draft, please try to load the draft later.", "Error", MessageBoxButton.OK);
                        foreach (Media m in unavaiblePictures)
                        {
                            App.MasterViewModel.CurrentPost.Media.Remove(m);
                        }
                    }
                }
                else
                {
                    Post post = App.MasterViewModel.CurrentPost;
                    setStatus();
                    DataContext = post;
                }
            }
            else
            {   //New Page
                Post page = new Post();
                page.PostStatus = "publish";
                App.MasterViewModel.CurrentPost = page;
                page.DateCreated = DateTime.Now;
                page.DateCreatedGMT = DateTime.Now.ToUniversalTime();
                DataContext = page;
                setStatus();
            }

            ToggleGalleryControlsVisibility();
        }
 public EditPostRPC(Blog blog, Post post)
     : base(blog.Xmlrpc, "metaWeblog.editPost", blog.Username, blog.Password)
 {
     _content = XMLRPCTable.metaWeblog_editPost;
     Post = post;
 }
        /// <summary>
        /// Locates a Post object and specifies the result as the page's DataContext
        /// </summary>
        private void LoadBlog()
        {
            Blog currentBlog = App.MasterViewModel.CurrentBlog;

            BlogName.Text = string.IsNullOrEmpty(currentBlog.BlogNameUpper) ? "" : currentBlog.BlogNameUpper;

            bool isSharingPhoto = (App.MasterViewModel.SharingPhotoToken != null);

            if (null != App.MasterViewModel.CurrentPostListItem && !isSharingPhoto)
            {
                string postId = App.MasterViewModel.CurrentPostListItem.PostId;

                if (App.MasterViewModel.CurrentPostListItem.DraftIndex > -1)
                {
                    // Post is a local draft
                    this.isEditingLocalDraft = true;
                    DataContext = App.MasterViewModel.CurrentBlog.LocalPostDrafts[App.MasterViewModel.CurrentPostListItem.DraftIndex];
                    App.MasterViewModel.CurrentPost = App.MasterViewModel.CurrentBlog.LocalPostDrafts[App.MasterViewModel.CurrentPostListItem.DraftIndex];
                    setStatus();
                    initPostFormatUI(App.MasterViewModel.CurrentPost.PostFormat);

                    //update the Media UI
                    List<Media> unavaiblePictures = new List<Media>();
                    foreach (Media currentMedia in App.MasterViewModel.CurrentPost.Media)
                    {
                        using (Stream stream = currentMedia.getImageStream())
                        {
                            if (stream == null)
                            {
                                unavaiblePictures.Add(currentMedia);
                                continue;
                            }
                            try
                            {
                                BitmapImage image = new BitmapImage();
                                image.SetSource(stream);
                                imageWrapPanel.Children.Add(BuildTappableImageElement(image, currentMedia));
                            }
                            catch (Exception)
                            {
                            }
                        }
                    }
                    if (unavaiblePictures.Count > 0 )
                    {
                        MessageBoxResult result = MessageBox.Show("Can't read a picture attached to this draft, please try to load the draft later.", "Error", MessageBoxButton.OK);
                        foreach (Media m in unavaiblePictures)
                        {
                            App.MasterViewModel.CurrentPost.Media.Remove(m);
                        }
                    }
                }
                else
                {
                    Post post = App.MasterViewModel.CurrentPost;
                    DataContext = post;
                    setStatus();
                    initPostFormatUI(post.PostFormat);
                    if (!string.IsNullOrWhiteSpace(post.MtKeyWords))
                    {
                        tagsTextBox.Text = post.MtKeyWords;
                    }
                }
            }
            else
            {   //New post
                Post post = new Post();
                App.MasterViewModel.CurrentPost = post;
                post.DateCreated = DateTime.Now;
                post.DateCreatedGMT = DateTime.Now.ToUniversalTime();
                DataContext = post;
                initPostFormatUI("standard");
                post.PostStatus = "publish";
                setStatus();
                /*postTimePicker.Value = post.DateCreated;
                postDatePicker.Value = post.DateCreated;*/
                if (isSharingPhoto)
                {
                    MediaLibrary library = new MediaLibrary();
                    Picture picture = library.GetPictureFromToken(App.MasterViewModel.SharingPhotoToken);
                    using (Stream pictureStream = picture.GetImage())
                        AddNewMediaStream(pictureStream, picture.Name);;

                    // clear the photo token so we don't try to add it to another post
                    App.MasterViewModel.SharingPhotoToken = null;

                    // blog selection page will be in the backstack, but if the user hits Back they should leave the app
                    // and return to the photo that they were sharing (e.g., so they can share it on another service)
                    NavigationService.RemoveBackEntry();
                }
            }

            this.ToggleGalleryControlsVisibility();
        }
        private void DeletePost()
        {
            PostListItem postItem = postsListBox.SelectedItem as PostListItem;
            if (null == postItem) return;

            string prompt = string.Format(_localizedStrings.Prompts.ConfirmDeletePostFormat, postItem.Title);
            MessageBoxResult result = MessageBox.Show(prompt, _localizedStrings.PageTitles.ConfirmDelete, MessageBoxButton.OKCancel);
            if (MessageBoxResult.Cancel == result) return;

            Post post = new Post();
            post.PostId = postItem.PostId;

            DeletePostRPC rpc = new DeletePostRPC(App.MasterViewModel.CurrentBlog, post);
            rpc.Completed += OnDeletePostRPCCompleted;
            rpc.ExecuteAsync();

            ApplicationBar.IsVisible = false; //hide the application bar
            App.WaitIndicationService.ShowIndicator(_localizedStrings.Messages.DeletingPost);
            currentXMLRPCConnection = rpc;
        }