Browse Source

Update to support wallpapers from web

Ruben 5 years ago
parent
commit
683b88af15

+ 26 - 0
PicView/FileHandling/Open_Save.cs

@@ -141,5 +141,31 @@ namespace PicView.FileHandling
             Close_UserControls();
             IsDialogOpen = false;
         }
+
+        /// <summary>
+        /// Sends the file to Windows print system
+        /// </summary>
+        /// <param name="path">The file path</param>
+        internal static bool Print(string path)
+        {
+            if (string.IsNullOrWhiteSpace(path))
+            {
+                return false;
+            }
+
+            if (!File.Exists(path))
+            {
+                return false;
+            }
+
+            using (var p = new Process())
+            {
+                p.StartInfo.FileName = path;
+                p.StartInfo.Verb = "print";
+                p.StartInfo.UseShellExecute = true;
+                p.Start();
+            }
+            return true;
+        }
     }
 }

+ 0 - 24
PicView/Library/Utilities.cs

@@ -32,31 +32,7 @@ namespace PicView.Library
 
         #endregion static helpers
 
-        /// <summary>
-        /// Sends the file to Windows print system
-        /// </summary>
-        /// <param name="path">The file path</param>
-        internal static bool Print(string path)
-        {
-            if (string.IsNullOrWhiteSpace(path))
-            {
-                return false;
-            }
 
-            if (!File.Exists(path))
-            {
-                return false;
-            }
-
-            using (var p = new Process())
-            {
-                p.StartInfo.FileName = path;
-                p.StartInfo.Verb = "print";
-                p.StartInfo.UseShellExecute = true;
-                p.Start();
-            }
-            return true;
-        }
 
         //internal static byte[] BitmapSourceToBytes(BitmapSource bitmapSource)
         //{

+ 48 - 37
PicView/SystemIntegration/Wallpaper.cs

@@ -5,6 +5,7 @@ using System.ComponentModel;
 using System.Diagnostics;
 using System.Globalization;
 using System.IO;
+using System.Text.RegularExpressions;
 using System.Threading.Tasks;
 using System.Timers;
 using static PicView.Library.Fields;
@@ -22,55 +23,65 @@ namespace PicView.SystemIntegration
             Fill
         }
 
-        internal static void SetWallpaper(string path, WallpaperStyle style)
+        /// <summary>
+        /// NOT thread safe!
+        /// </summary>
+        /// <param name="style"></param>
+        internal static void SetWallpaper(WallpaperStyle style)
         {
-            if (CanNavigate)
+            string wallpaper;
+
+            try
+            {
+                var linkParser = new Regex(@"\b(?:https?://|www\.)\S+\b", RegexOptions.Compiled | RegexOptions.IgnoreCase);
+                wallpaper = linkParser.Match(mainWindow.Bar.Text).ToString();
+            }
+            catch (Exception e)
             {
-                if (File.Exists(path))
-                {
-                    try
-                    {
-                        Task.Run(() => SetDesktopWallpaper(path, style));
-                    }
 #if DEBUG
-                    catch (Exception e)
-                    {
-                        Trace.WriteLine("Wallpaper exception \n" + e.Message);
-                        return;
-                    }
-#else
-                    catch (Exception) { return; }
+                Trace.WriteLine(e.Message);
 #endif
-                }
+                return;
             }
-            else
-            {
-                Task.Run(() =>
+
+            if (Uri.IsWellFormedUriString(wallpaper, UriKind.Absolute)) // Check if from web
                 {
-                    try
-                    {
-                        //Handle if file from web, need clipboard image solution
+                    Task.Run(() => {
+                        // Create temp directory
                         var tempPath = Path.GetTempPath();
                         var randomName = Path.GetRandomFileName();
-                        var webClient = new System.Net.WebClient();
+
+                        // Download to it
+                        using var webClient = new System.Net.WebClient();
                         Directory.CreateDirectory(tempPath);
-                        webClient.DownloadFile(path, tempPath + randomName);
+                        webClient.DownloadFile(wallpaper, tempPath + randomName);
+
+                        // Use it
                         SetDesktopWallpaper(tempPath + randomName, style);
+
+                        // Clean up
                         File.Delete(tempPath + randomName);
-                        var timer = new Timer(2000);
+                        using var timer = new Timer(2000);
                         timer.Elapsed += (s, x) => Directory.Delete(tempPath);
-                    }
-#if DEBUG
-                    catch (Exception e)
-                    {
-                        Trace.WriteLine("Wallpaper download exception \n" + e.Message);
                         return;
-                    }
-#else
-                    catch (Exception) { return; }
-#endif
-                });
+                    });
+                }
+                // TODO add Base64 support
+                //if (Base64.IsBase64String(s))
+                //{
+                //}
+
+
+            if (Pics.Count > 0)
+            {
+                if (FolderIndex < Pics.Count)
+                {
+                    Task.Run(() => {
+                        SetDesktopWallpaper(Pics[FolderIndex], style);
+                    });
+                }
             }
+            // TODO add clipboard image support
         }
 
         /// <summary>
@@ -82,7 +93,7 @@ namespace PicView.SystemIntegration
         {
             get
             {
-                return (Environment.OSVersion.Version >= new Version(6, 0));
+                return Environment.OSVersion.Version >= new Version(6, 0);
             }
         }
 
@@ -95,7 +106,7 @@ namespace PicView.SystemIntegration
         {
             get
             {
-                return (Environment.OSVersion.Version >= new Version(6, 1));
+                return Environment.OSVersion.Version >= new Version(6, 1);
             }
         }
 

+ 1 - 1
PicView/UI/Loading/LoadContextMenus.cs

@@ -416,7 +416,7 @@ namespace PicView.UI.Loading
             {
                 Header = "Set as wallpaper"
             };
-            wallcm.Click += (s, x) => SetWallpaper(Pics[FolderIndex], WallpaperStyle.Fill);
+            wallcm.Click += (s, x) => SetWallpaper(WallpaperStyle.Fill);
             var wallcmIcon = new System.Windows.Shapes.Path
             {
                 Data = Geometry.Parse(SVGiconCamera),

+ 5 - 5
PicView/UI/Windows/SettingsWindow.xaml.cs

@@ -185,7 +185,7 @@ namespace PicView.UI.Windows
                 Fill.MouseLeave += Fill_MouseLeave;
                 Fill.Click += delegate
                 {
-                    SetWallpaper(Pics[FolderIndex], WallpaperStyle.Fill);
+                    SetWallpaper(WallpaperStyle.Fill);
                 };
 
                 // Fit
@@ -194,7 +194,7 @@ namespace PicView.UI.Windows
                 Fit.MouseLeave += Fit_MouseLeave;
                 Fit.Click += delegate
                 {
-                    SetWallpaper(Pics[FolderIndex], WallpaperStyle.Fit);
+                    SetWallpaper(WallpaperStyle.Fit);
                 };
 
                 // Center
@@ -203,7 +203,7 @@ namespace PicView.UI.Windows
                 Center.MouseLeave += Center_MouseLeave;
                 Center.Click += delegate
                 {
-                    SetWallpaper(Pics[FolderIndex], WallpaperStyle.Center);
+                    SetWallpaper(WallpaperStyle.Center);
                 };
 
                 // Tile
@@ -212,7 +212,7 @@ namespace PicView.UI.Windows
                 Tile.MouseLeave += Tile_MouseLeave;
                 Tile.Click += delegate
                 {
-                    SetWallpaper(Pics[FolderIndex], WallpaperStyle.Tile);
+                    SetWallpaper(WallpaperStyle.Tile);
                 };
 
                 // Stretch
@@ -221,7 +221,7 @@ namespace PicView.UI.Windows
                 Stretch.MouseLeave += Stretch_MouseLeave;
                 Stretch.Click += delegate
                 {
-                    SetWallpaper(Pics[FolderIndex], WallpaperStyle.Stretch);
+                    SetWallpaper(WallpaperStyle.Stretch);
                 };
 
                 // Set Center