public override object getExtraInfo(SelectableMediaItem selectableItem)
        {
            String info = null;

            ImageResultItem item = (ImageResultItem)selectableItem.Item;

            SortItem sortItem = (SortItem)SortModes.CurrentItem;

            switch (sortItem.SortMode)
            {
            case SortMode.Width:

            case SortMode.Height:
                info = item.ImageInfo.Width + " x " + item.ImageInfo.Height;
                break;

            case SortMode.Size:
                info = MiscUtils.formatSizeBytes(item.ImageInfo.FileSize.Value);
                break;

            case SortMode.MimeType:
                info = item.ImageInfo.ContentType;
                break;

            case SortMode.Location:
                info = item.ImageInfo.SourceUrl;
                break;

            default:
                break;
            }

            return(info);
        }
        async Task doSearch(ImageSearchQuery state, int imageOffset)
        {
            if (String.IsNullOrEmpty(state.Query) || String.IsNullOrWhiteSpace(state.Query))             
            {
                return;
            }

            var bingContainer = new Bing.BingSearchContainer(new Uri(rootUri));
           
            // Configure bingContainer to use your credentials.

            bingContainer.Credentials = new NetworkCredential(BingAccountKey.accountKey, BingAccountKey.accountKey);           

            // Build the query.
            String imageFilters = null;

            if (!state.Size.Equals(size[0]))
            {
                imageFilters = "Size:" + state.Size;
            }

            if (!state.Layout.Equals(layout[0]))
            {
                if (imageFilters != null) imageFilters += "+";

                imageFilters += "Aspect:" + state.Layout;
            }

            if (!state.Type.Equals(type[0]))
            {
                if (imageFilters != null) imageFilters += "+";

                imageFilters += "Style:" + state.Type;
            }

            if (!state.People.Equals(type[0]))
            {
                if (imageFilters != null) imageFilters += "+";

                imageFilters += "Face:" + state.People;
            }

            if (!state.Color.Equals(type[0]))
            {
                if (imageFilters != null) imageFilters += "+";

                imageFilters += "Color:" + state.Color;
            }
               
            var imageQuery = bingContainer.Image(Query, null, null, state.SafeSearch, state.GeoTag.LatDecimal, state.GeoTag.LonDecimal, imageFilters);
            imageQuery = imageQuery.AddQueryOption("$top", 50);
            imageQuery = imageQuery.AddQueryOption("$skip", imageOffset);
                        
            IEnumerable<Bing.ImageResult> imageResults = 
                await Task.Factory.FromAsync<IEnumerable<Bing.ImageResult>>(imageQuery.BeginExecute(null, null), imageQuery.EndExecute);
                               
            if (imageOffset == 0)
            {
                MediaState.clearUIState(Query, DateTime.Now, MediaStateType.SearchResult);
            }

            List<MediaItem> results = new List<MediaItem>();

            int relevance = imageOffset;

            MediaState.UIMediaCollection.EnterReadLock();

            foreach (var image in imageResults)
            {
                MediaItem item = new ImageResultItem(image, relevance);

                if (MediaState.UIMediaCollection.Contains(item)) continue;

                results.Add(item);

                relevance++;
            }

            MediaState.UIMediaCollection.ExitReadLock();

            MediaState.addUIState(results);
                                                                
        }
Esempio n. 3
0
        public override bool Equals(MediaItem other)
        {
            ImageResultItem item = (ImageResultItem)other;

            return(ImageInfo.Thumbnail.MediaUrl.Equals(item.ImageInfo.Thumbnail.MediaUrl));
        }
Esempio n. 4
0
        public ImageSearchViewModel()
        {
            NrColumns = 4;

            SearchCommand = new AsyncCommand <int>(async(imageOffset) =>
            {
                try
                {
                    if (imageOffset == 0)
                    {
                        CurrentQuery = new ImageSearchQuery(this);
                    }

                    SearchCommand.IsExecutable = false;
                    await doSearch(CurrentQuery, imageOffset);
                }
                catch (Exception e)
                {
                    MessageBox.Show("Image search error\n\n" + e.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                }
                finally
                {
                    SearchCommand.IsExecutable = true;
                }
            });

            ViewCommand = new Command <SelectableMediaItem>((selectableItem) =>
            {
                ImageResultItem item = (ImageResultItem)selectableItem.Item;

                if (item.ImageInfo.ContentType.Equals("image/animatedgif"))
                {
                    Shell.ShellViewModel.navigateToVideoView(item);
                }
                else
                {
                    Shell.ShellViewModel.navigateToImageView(item);
                }
            });

            ViewSourceCommand = new Command <SelectableMediaItem>((selectableItem) =>
            {
                ImageResultItem item = (ImageResultItem)selectableItem.Item;

                Process.Start(item.ImageInfo.SourceUrl);
            });

            SelectAllCommand = new Command(() =>
            {
                MediaStateCollectionView.selectAll();
            }, false);

            DeselectAllCommand = new Command(() =>
            {
                MediaStateCollectionView.deselectAll();
            });

            CloseCommand = new Command(() =>
            {
                OnClosingRequest();
            });

            DownloadCommand = new AsyncCommand <SelectableMediaItem>(async(selectableItem) =>
            {
                List <MediaItem> items = MediaStateCollectionView.getSelectedItems();
                if (items.Count == 0)
                {
                    items.Add(selectableItem.Item);
                }

                String outputPath = null;

                switch (ImageSearchPlugin.Properties.Settings.Default.ImageSaveMode)
                {
                case MediaViewer.Infrastructure.Constants.SaveLocation.Current:
                    {
                        outputPath = MediaFileWatcher.Instance.Path;
                        break;
                    }

                case MediaViewer.Infrastructure.Constants.SaveLocation.Ask:
                    {
                        DirectoryPickerView directoryPicker = new DirectoryPickerView();
                        directoryPicker.DirectoryPickerViewModel.InfoString   = "Select Output Directory";
                        directoryPicker.DirectoryPickerViewModel.SelectedPath = MediaFileWatcher.Instance.Path;

                        if (directoryPicker.ShowDialog() == false)
                        {
                            return;
                        }

                        outputPath = directoryPicker.DirectoryPickerViewModel.SelectedPath;

                        break;
                    }

                case MediaViewer.Infrastructure.Constants.SaveLocation.Fixed:
                    {
                        outputPath = ImageSearchPlugin.Properties.Settings.Default.FixedDownloadPath;
                        break;
                    }

                default:
                    break;
                }

                CancellableOperationProgressView progressView = new CancellableOperationProgressView();
                DownloadProgressViewModel vm = new DownloadProgressViewModel();
                progressView.DataContext     = vm;

                progressView.Show();
                vm.OkCommand.IsExecutable     = false;
                vm.CancelCommand.IsExecutable = true;

                try
                {
                    await Task.Factory.StartNew(() =>
                    {
                        vm.startDownload(outputPath, items);
                    }, vm.CancellationToken);
                }
                catch (Exception)
                {
                }

                vm.OkCommand.IsExecutable     = true;
                vm.CancelCommand.IsExecutable = false;
            });

            SettingsViewModel = (ImageSearchSettingsViewModel)ServiceLocator.Current.GetInstance(typeof(ImageSearchSettingsViewModel));

            Size = new ListCollectionView(size);
            Size.MoveCurrentTo(Settings.Default.Size);
            SafeSearch = new ListCollectionView(safeSearch);
            SafeSearch.MoveCurrentTo(Settings.Default.SafeSearch);
            Layout = new ListCollectionView(layout);
            Layout.MoveCurrentTo(Settings.Default.Layout);
            Type = new ListCollectionView(type);
            Type.MoveCurrentTo(Settings.Default.Type);
            People = new ListCollectionView(people);
            People.MoveCurrentTo(Settings.Default.People);
            Color = new ListCollectionView(color);
            Color.MoveCurrentTo(Settings.Default.Color);

            GeoTag = new GeoTagCoordinatePair();

            MediaState = new MediaState();

            MediaStateCollectionView = new ImageResultCollectionView(MediaState);
            MediaStateCollectionView.MediaState.MediaStateType = MediaStateType.SearchResult;

            WeakEventManager <MediaLockedCollection, EventArgs> .AddHandler(MediaStateCollectionView.MediaState.UIMediaCollection, "IsLoadingChanged", mediaCollection_IsLoadingChanged);
        }
Esempio n. 5
0
        async Task doSearch(ImageSearchQuery state, int imageOffset)
        {
            if (String.IsNullOrEmpty(state.Query) || String.IsNullOrWhiteSpace(state.Query))
            {
                return;
            }

            var bingContainer = new Bing.BingSearchContainer(new Uri(rootUri));

            // Configure bingContainer to use your credentials.

            bingContainer.Credentials = new NetworkCredential(BingAccountKey.accountKey, BingAccountKey.accountKey);

            // Build the query.
            String imageFilters = null;

            if (!state.Size.Equals(size[0]))
            {
                imageFilters = "Size:" + state.Size;
            }

            if (!state.Layout.Equals(layout[0]))
            {
                if (imageFilters != null)
                {
                    imageFilters += "+";
                }

                imageFilters += "Aspect:" + state.Layout;
            }

            if (!state.Type.Equals(type[0]))
            {
                if (imageFilters != null)
                {
                    imageFilters += "+";
                }

                imageFilters += "Style:" + state.Type;
            }

            if (!state.People.Equals(type[0]))
            {
                if (imageFilters != null)
                {
                    imageFilters += "+";
                }

                imageFilters += "Face:" + state.People;
            }

            if (!state.Color.Equals(type[0]))
            {
                if (imageFilters != null)
                {
                    imageFilters += "+";
                }

                imageFilters += "Color:" + state.Color;
            }

            var imageQuery = bingContainer.Image(Query, null, null, state.SafeSearch, state.GeoTag.LatDecimal, state.GeoTag.LonDecimal, imageFilters);

            imageQuery = imageQuery.AddQueryOption("$top", 50);
            imageQuery = imageQuery.AddQueryOption("$skip", imageOffset);

            IEnumerable <Bing.ImageResult> imageResults =
                await Task.Factory.FromAsync <IEnumerable <Bing.ImageResult> >(imageQuery.BeginExecute(null, null), imageQuery.EndExecute);

            if (imageOffset == 0)
            {
                MediaState.clearUIState(Query, DateTime.Now, MediaStateType.SearchResult);
            }

            List <MediaItem> results = new List <MediaItem>();

            int relevance = imageOffset;

            MediaState.UIMediaCollection.EnterReadLock();

            foreach (var image in imageResults)
            {
                MediaItem item = new ImageResultItem(image, relevance);

                if (MediaState.UIMediaCollection.Contains(item))
                {
                    continue;
                }

                results.Add(item);

                relevance++;
            }

            MediaState.UIMediaCollection.ExitReadLock();

            MediaState.addUIState(results);
        }
        public ImageResultCollectionView(MediaState mediaState)
            : base(mediaState)
        {
            SortItemCollection <SortItem, SortMode> sortItems = new SortItemCollection <SortItem, SortMode>();

            foreach (SortMode mode in Enum.GetValues(typeof(SortMode)))
            {
                sortItems.Add(new SortItem(mode));
            }

            sortItems.ItemSortDirectionChanged += sortItems_ItemSortDirectionChanged;

            SortModes = new System.Windows.Data.ListCollectionView(sortItems);

            SortModes.CurrentChanged += (s, e) => {
                SortItem sortItem = (SortItem)SortModes.CurrentItem;

                switch (sortItem.SortMode)
                {
                case SortMode.Relevance:
                    SortFunc = new Func <SelectableMediaItem, SelectableMediaItem, int>((a, b) =>
                    {
                        ImageResultItem itemA = (ImageResultItem)a.Item;
                        ImageResultItem itemB = (ImageResultItem)b.Item;

                        return(itemA.Relevance.CompareTo(itemB.Relevance));
                    });
                    break;

                case SortMode.Location:
                    SortFunc = new Func <SelectableMediaItem, SelectableMediaItem, int>((a, b) =>
                    {
                        ImageResultItem itemA = (ImageResultItem)a.Item;
                        ImageResultItem itemB = (ImageResultItem)b.Item;

                        return(itemA.ImageInfo.SourceUrl.CompareTo(itemB.ImageInfo.SourceUrl));
                    });
                    break;

                case SortMode.Width:
                    SortFunc = new Func <SelectableMediaItem, SelectableMediaItem, int>((a, b) =>
                    {
                        ImageResultItem itemA = (ImageResultItem)a.Item;
                        ImageResultItem itemB = (ImageResultItem)b.Item;

                        return(Nullable.Compare(itemA.ImageInfo.Width, itemB.ImageInfo.Width));
                    });
                    break;

                case SortMode.Height:
                    SortFunc = new Func <SelectableMediaItem, SelectableMediaItem, int>((a, b) =>
                    {
                        ImageResultItem itemA = (ImageResultItem)a.Item;
                        ImageResultItem itemB = (ImageResultItem)b.Item;

                        return(Nullable.Compare(itemA.ImageInfo.Height, itemB.ImageInfo.Height));
                    });
                    break;

                case SortMode.Size:
                    SortFunc = new Func <SelectableMediaItem, SelectableMediaItem, int>((a, b) =>
                    {
                        ImageResultItem itemA = (ImageResultItem)a.Item;
                        ImageResultItem itemB = (ImageResultItem)b.Item;

                        return(Nullable.Compare(itemA.ImageInfo.FileSize, itemB.ImageInfo.FileSize));
                    });
                    break;

                case SortMode.MimeType:
                    SortFunc = new Func <SelectableMediaItem, SelectableMediaItem, int>((a, b) =>
                    {
                        ImageResultItem itemA = (ImageResultItem)a.Item;
                        ImageResultItem itemB = (ImageResultItem)b.Item;

                        return(itemA.ImageInfo.ContentType.CompareTo(itemB.ImageInfo.ContentType));
                    });
                    break;

                default:
                    break;
                }

                SortDirection = sortItem.SortDirection;

                refresh();
            };
        }