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);
            
        }
Example #2
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);
        }
        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);
                                                                
        }
Example #4
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);
        }