public async void EditSource(WallpaperSource source) { if (source.Source == Source.Reddit || source.Source == Source.Local) { await _dialog.ShowMessageAsync("Error", "There are no options to edit on Reddit sources!"); return; } source.WallhavenOptions = await _db.GetWallhavenOptions(source); var wallhavenDialog = new WallhavenDialog("Edit Wallhaven Source options") { Query = source.Query, General = Convert.ToBoolean(source.WallhavenOptions.General), Anime = Convert.ToBoolean(source.WallhavenOptions.Anime), People = Convert.ToBoolean(source.WallhavenOptions.People), SFW = Convert.ToBoolean(source.WallhavenOptions.SFW), Sketchy = Convert.ToBoolean(source.WallhavenOptions.Sketchy), }; if (!string.IsNullOrEmpty(source.WallhavenOptions.Resolution)) { source.WallhavenOptions.Resolution.Split(',').AsParallel().ForAll(x => wallhavenDialog.Resolutions.FirstOrDefault(y => y.ObjectData == x).IsSelected = true); } if (!string.IsNullOrEmpty(source.WallhavenOptions.Ratio)) { source.WallhavenOptions.Ratio.Split(',').AsParallel().ForAll(x => wallhavenDialog.Ratios.FirstOrDefault(y => y.ObjectData == x).IsSelected = true); } var editedSource = (WallpaperSource)await _dialog.ShowCustomDialog(wallhavenDialog); if (editedSource == null) { return; } if (string.IsNullOrEmpty(editedSource.Query)) { await _dialog.ShowMessageAsync("Error", "The search query cannot be blank"); return; } source = editedSource; await _db.EditSource(source); }
private async void ChangeWallpaper() { try { //TODO - // Don't need internet access for local folders // while (true) { _cts = new CancellationTokenSource(); var sources = await _db.GetSourcesAsync(); var access = await NetworkTester.HasInternetAccess(); if (access || sources.Any(x => x.Source == Source.Local)) { if (sources.Count > 0) { if (!wasLocal) { foreach (var file in currentImages) { File.Delete(file); } } currentImages.Clear(); OptionEnabled = true; NotifyOfPropertyChange(() => OptionEnabled); WallpaperSource randSource; if (access) { randSource = sources[_rand.Next(sources.Count)]; } else { sources = sources.Where(x => x.Source == Source.Local).ToList(); randSource = sources[_rand.Next(sources.Count)]; } if (randSource.Source == Source.Wallhaven) { randSource.WallhavenOptions = await _db.GetWallhavenOptions(randSource); } Image image = null; var isMultiple = await _db.UseMultiple(); string tempFile = null; wasLocal = (randSource.Source == Source.Local); if (isMultiple) { //testing for multiple monitors var screens = System.Windows.Forms.Screen.AllScreens; //this needs to be modified to allow vertically stacked monitors. //right now only side by side monitors will work using (var bmp = new Bitmap(screens.Sum(x => x.Bounds.Width), screens.Max(x => x.Bounds.Height))) using (var g = Graphics.FromImage(bmp)) { //get a random image for each screen foreach (var s in screens) { string imageUrl = await UrlFetcher.GetRandomImageUrl(randSource, await _db.ImgurClientId(), currentImages); if (String.IsNullOrEmpty(imageUrl)) { if (String.IsNullOrEmpty(tempFile)) { continue; } //do nothing, leave the temp file as the current file } else { if (randSource.Source != Source.Local) { tempFile = await WebImage.DownloadImage(imageUrl); } else { tempFile = imageUrl; } } currentImages.Add(tempFile); image = Image.FromFile(tempFile); //we need to scale the image properly to fit the screen image = WebImage.ScaleImage(image, s.Bounds.Width, s.Bounds.Height); //now figure out the location var x = ((s.Bounds.Width - image.Width) / 2) + s.Bounds.Location.X; var y = ((s.Bounds.Height - image.Height) / 2) + s.Bounds.Location.Y; //draw it onto the screen g.DrawImage(image, new Rectangle(x, y, image.Width, image.Height)); newImages.Add(image); } //now get the file out of it g.Save(); tempFile = Path.Combine(Environment.ExpandEnvironmentVariables("%temp%"), "stitched.bmp"); bmp.Save(tempFile); } } else { string imageUrl = await UrlFetcher.GetRandomImageUrl(randSource, await _db.ImgurClientId(), currentImages); if (!String.IsNullOrEmpty(imageUrl)) { if (randSource.Source != Source.Local) { tempFile = await WebImage.DownloadImage(imageUrl); } else { tempFile = imageUrl; } image = Image.FromFile(tempFile); newImages.Add(image); currentImages.Add(tempFile); } } if (!currentImages.All(x => String.IsNullOrEmpty(x))) { DesktopBackground.SetWallpaper(tempFile, isMultiple); ToolTipInfo = string.Format("Source: {0}\r\nResolution: {1}", randSource.Source == Source.Reddit ? randSource.Query : randSource.Source + " / " + randSource.Query, newImages.Aggregate("", (c, v) => $"{c}\r\n{v.Width}x{v.Height}")); NotifyOfPropertyChange(() => ToolTipInfo); } foreach (var i in newImages) { i.Dispose(); } newImages.Clear(); } else { ToolTipInfo = "Please add sources!"; NotifyOfPropertyChange(() => ToolTipInfo); OptionEnabled = false; NotifyOfPropertyChange(() => OptionEnabled); } await Task.Delay(TimeSpan.FromMinutes(await _db.Interval()), _cts.Token).ContinueWith(tsk => { }); } else { ToolTipInfo = "Waiting for internet access..."; NotifyOfPropertyChange(() => ToolTipInfo); OptionEnabled = false; NotifyOfPropertyChange(() => OptionEnabled); await Task.Delay(TimeSpan.FromSeconds(30)); } } } catch (Exception e) { e = e; } }