//======================================================================
        // Set the wallpaper
        //======================================================================
        public async Task <bool> SetWallpaperAsync(RedditLink redditLink)
        {
            Logger.Instance.LogMessageToFile("Setting wallpaper.", LogLevel.Information);

            if (!await WallpaperLinkValidAsync(redditLink).ConfigureAwait(false))
            {
                return(false);
            }

            HelperMethods.ResetManualOverride();

            _uiMarshaller.UpdateStatus("Setting Wallpaper");

            if (!string.IsNullOrEmpty(redditLink.Url))
            {
                redditLink.Url = await ConvertRedditLinkToImageLink(redditLink.Url, _random, ImageExtensions).ConfigureAwait(false);

                var uri           = new Uri(redditLink.Url);
                var extension     = Path.GetExtension(uri.LocalPath);
                var fileName      = $"{redditLink.ThreadId}{extension}";
                var wallpaperFile = Path.Combine(Path.GetTempPath(), fileName);

                redditLink.SaveAsCurrentWallpaper(extension, wallpaperFile);
                redditLink.LogDetails();

                if (ImageExtensions.Contains(extension.ToUpper()))
                {
                    await DownloadWallpaperAsync(uri.AbsoluteUri, wallpaperFile);

                    if (!await SetWallpaperAsync(redditLink, wallpaperFile))
                    {
                        return(false);
                    }
                }
                else
                {
                    Logger.Instance.LogMessageToFile($"Wallpaper URL failed validation: {extension.ToUpper()}", LogLevel.Warning);

                    _uiMarshaller.RestartChangeWallpaperTimer();
                }

                using (var wc = HelperMethods.CreateWebClient())
                {
                    var bytes = await wc.DownloadDataTaskAsync(uri).ConfigureAwait(false);

                    if (!bytes.Any())
                    {
                        _uiMarshaller.RestartChangeWallpaperTimer();
                    }
                }
            }
            else
            {
                _uiMarshaller.RestartChangeWallpaperTimer();
            }

            return(true);
        }
 public void OpenPopupInfoWindow(RedditLink redditLink)
 {
     if (SynchronizationContext.Current == _uiContext)
     {
         _mainForm.OpenPopupInfoWindow(redditLink);
     }
     else
     {
         _uiContext.Post(x => OpenPopupInfoWindow(redditLink), null);
     }
 }
예제 #3
0
        //======================================================================
        // Add wallpaper to history
        //======================================================================
        public async Task <RedditImage> AddWallpaperToHistoryAsync(RedditLink redditLink)
        {
            try
            {
                return(await InsertWallpaperIntoDatabaseAsync("history", redditLink).ConfigureAwait(false));
            }
            catch (Exception ex)
            {
                Logger.Instance.LogMessageToFile($"Unexpected error adding wallpaper to history: {ex.Message}", LogLevel.Warning);

                throw;
            }
        }
예제 #4
0
        //======================================================================
        // Add wallpaper to favourites
        //======================================================================
        public async Task <bool> FaveWallpaperAsync(RedditLink redditLink)
        {
            try
            {
                await InsertWallpaperIntoDatabaseAsync("favourites", redditLink).ConfigureAwait(false);

                Logger.Instance.LogMessageToFile($"Wallpaper added to favourites! Title: {redditLink.Title}, Thread ID: {redditLink.ThreadId}, URL: {redditLink.Url}", LogLevel.Information);

                return(true);
            }
            catch (Exception ex)
            {
                Logger.Instance.LogMessageToFile($"Unexpected error favouriting wallpaper: {ex.Message}", LogLevel.Warning);
                return(false);
            }
        }
        private async Task <bool> ChangeWallpaperIfValidImageAsync(JToken token)
        {
            var title = token["data"]["title"].ToString();
            var url   = token["data"]["url"].ToString();
            var id    = token["data"]["id"].ToString();

            Logger.Instance.LogMessageToFile($"Found a wallpaper! Title: {title}, URL: {url}, ThreadID: {id}", LogLevel.Information);

            // Validate URL
            if (await HelperMethods.ValidateImageAsync(url).ConfigureAwait(false))
            {
                if (await HelperMethods.ValidateImgurImageAsync(url).ConfigureAwait(false))
                {
                    var imageDetails = new RedditLink(url, title, id);

                    if (!await SetWallpaperAsync(imageDetails).ConfigureAwait(false))
                    {
                        while (!await SearchForWallpaperAsync().ConfigureAwait(false))
                        {
                        }
                    }
                }
                else
                {
                    _uiMarshaller.LogFailure("Wallpaper has been removed from Imgur.",
                                             "The selected wallpaper was deleted from Imgur, searching again.");

                    _noResultCount++;

                    while (!await SearchForWallpaperAsync().ConfigureAwait(false))
                    {
                    }
                }
            }
            else
            {
                _uiMarshaller.LogFailure("The selected URL is not for an image.",
                                         "Not a direct wallpaper URL, searching again.");

                _noResultCount++;
                return(false);
            }

            return(true);
        }
        private async Task <bool> SetWallpaperAsync(RedditLink redditLink, string wallpaperFile)
        {
            if (!WallpaperSizeValid(wallpaperFile))
            {
                return(false);
            }

            await ActiveDesktop.SetWallpaperAsync(wallpaperFile).ConfigureAwait(false);

            _noResultCount = 0;

            _uiMarshaller.UpdateStatus("Wallpaper Changed!");

            Logger.Instance.LogMessageToFile("Wallpaper changed!", LogLevel.Information);

            _currentSessionHistory.Add(redditLink.ThreadId);

            var redditImage = await _database.AddWallpaperToHistoryAsync(redditLink)
                              .ConfigureAwait(false);

            await _database.BuildThumbnailCacheAsync().ConfigureAwait(false);

            _uiMarshaller.AddImageToHistory(redditImage);

            if (!Settings.Default.disableNotifications && Settings.Default.wallpaperInfoPopup)
            {
                _uiMarshaller.OpenPopupInfoWindow(redditLink);
            }

            if (Settings.Default.autoSave)
            {
                HelperMethods.SaveCurrentWallpaper(Settings.Default.currentWallpaperName);
            }

            _uiMarshaller.UpdateStatus("");

            return(true);
        }
        private async Task <bool> WallpaperLinkValidAsync(RedditLink redditLink)
        {
            if (await _database.IsBlacklistedAsync(redditLink.Url).ConfigureAwait(false))
            {
                _uiMarshaller.UpdateStatus("Wallpaper is blacklisted.");
                Logger.Instance.LogMessageToFile("The selected wallpaper has been blacklisted, searching again.", LogLevel.Warning);
                _uiMarshaller.DisableChangeWallpaperTimer();

                return(false);
            }

            if (!Settings.Default.manualOverride &&
                Settings.Default.suppressDuplicates &&
                _currentSessionHistory.Contains(redditLink.ThreadId))
            {
                _uiMarshaller.UpdateStatus("Wallpaper already used this session.");
                Logger.Instance.LogMessageToFile("The selected wallpaper has already been used this session, searching again.", LogLevel.Warning);
                _uiMarshaller.DisableChangeWallpaperTimer();

                return(false);
            }

            return(true);
        }
예제 #8
0
        public static async Task SaveLinkAsync(ToolStripStatusLabel statusLabel, NotifyIcon taskIcon, RedditLink redditLink)
        {
            string statusText;

            if (await redditLink.SaveImage())
            {
                ShowWallpaperSavedBalloonTip(taskIcon);
                statusText = "Wallpaper saved!";
            }
            else
            {
                ShowWallpaperAlreadyExistsBalloonTip(taskIcon);
                statusText = "Wallpaper already saved!";
            }

            statusLabel.Text = statusText;
        }
예제 #9
0
        private async Task <RedditImage> InsertWallpaperIntoDatabaseAsync(string tableName, RedditLink redditLink)
        {
            var thumbnail = await HelperMethods.GetThumbnailAsync(redditLink.Url).ConfigureAwait(false);

            var dateTime    = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.FFF");
            var redditImage = new RedditImage(thumbnail, redditLink.Title, redditLink.ThreadId, redditLink.Url, dateTime);

            redditLink.Title = redditLink.Title.Replace("'", "''");

            using (var connection = await GetNewOpenConnectionAsync().ConfigureAwait(false))
                using (var command = new SQLiteCommand($"INSERT INTO {tableName} (thumbnail, title, threadid, url, date) " +
                                                       "VALUES (@thumbnail, @title, @threadid, @url, @dateTime)", connection))
                {
                    command.Parameters.AddWithValue("@thumbnail", thumbnail);
                    command.Parameters.AddWithValue("@title", redditLink.Title);
                    command.Parameters.AddWithValue("@threadid", redditLink.ThreadId);
                    command.Parameters.AddWithValue("@url", redditLink.Url);
                    command.Parameters.AddWithValue("@dateTime", dateTime);

                    await command.ExecuteNonQueryAsync().ConfigureAwait(false);
                }

            return(redditImage);
        }