From 65ae713aab8fde9c990a01ebff511177958640db Mon Sep 17 00:00:00 2001
From: Edward Yang <edward.yang6771@gmail.com>
Date: Wed, 11 May 2016 08:44:16 -0500
Subject: [PATCH] Add in random image selection

---
 assets/js/app.js | 32 +++++++++++++++++++++++++++-----
 index.html       |  4 +++-
 2 files changed, 30 insertions(+), 6 deletions(-)

diff --git a/assets/js/app.js b/assets/js/app.js
index 8c27e7f..d388485 100644
--- a/assets/js/app.js
+++ b/assets/js/app.js
@@ -114,6 +114,7 @@ angular.module('webkitMaterial', ['ngMaterial', 'angularLoad'])
                 }
 
                 factory.loadEngine(function () {
+                    var imagePath = 'assets/ui/no-mans-sky.jpg';
                     if (settings.backgroundEngine === 'trianglify') {
                         var backgroundHeight = Math.max($(document).height(), $(window).height()) * 0.89;
                         var backgroundWidth = Math.max($(document).width(), $(window).width()) * 0.89;
@@ -205,11 +206,32 @@ angular.module('webkitMaterial', ['ngMaterial', 'angularLoad'])
                             density: settings.particlegroundDensity,
                             proximity: settings.particlegroundDensity / 70
                         });
+                    } else if (settings.backgroundEngine === 'random-image') {
+                        var imageDir = config.get_str('branding', 'background_images');
+                        var imagesLoaded = false;
+                        if (imageDir == null) {imageDir = config.get_str('greeter', 'background_images');}
+                        if (imageDir != null) {
+                            var imagePaths = greeterutil.dirlist(imageDir);
+                            if (imagePaths != null) {
+                                if (imagePaths.length > 0) {
+                                    imagesLoaded = true;
+                                    var imageIndex = Math.floor(Math.random() * imagePaths.length);
+                                    imagePath = imagePaths[imageIndex];
+                                }
+                            }
+                            if (!imagesLoaded) {
+                                console.error('Non-existant or missing background images in', imageDir);
+                            }
+                        } else {
+                            console.error('background_images variable unset');
+                        }
                     } else if (settings.backgroundEngine === 'image') {
-                        var imagePath = '/var/lib/AccountsService/wallpapers/lightdm-webkit.jpg';
-                            $rootScope.$applyAsync(function () {
-                                $rootScope.backgroundStyle = {"background-image": 'url('+imagePath+')', "background-color": 'none'};
-                            });
+                        imagePath = '/var/lib/AccountsService/wallpapers/lightdm-webkit.jpg';
+                    }
+                    if (settings.backgroundEngine === 'random-image' || settings.backgroundEngine === 'image') {
+                        $rootScope.$applyAsync(function () {
+                            $rootScope.backgroundStyle = {"background-image": 'url('+imagePath+')', "background-color": 'none'};
+                        });
                         $http({
                             method: 'GET',
                             url: imagePath
@@ -367,4 +389,4 @@ angular.module('webkitMaterial', ['ngMaterial', 'angularLoad'])
 
             $rootScope.animation = settings.animation;
             $rootScope.animationDuration = {'animationDuration': settings.animationDuration + 'ms'};
-        });
\ No newline at end of file
+        });
diff --git a/index.html b/index.html
index 4f9fd59..b939b82 100644
--- a/index.html
+++ b/index.html
@@ -66,8 +66,10 @@
                                 <md-select id="settings-background" name="backgroundEngine" ng-model="settings.backgroundEngine">
                                     <md-option value="trianglify">Trianglify</md-option>
                                     <md-option value="particleground">Particleground</md-option>
+                                    <md-option value="random-image">Random Image</md-option>
                                     <md-option value="image">Image</md-option>
                                 </md-select>
+                                <div class="hint" ng-if="settings.backgroundEngine == 'random-image'">Background images should be located in "/var/lib/AccountsService/wallpapers" in png or jpeg format.</div>
                                 <div class="hint" ng-if="settings.backgroundEngine == 'image'">Background image should be located at "/var/lib/AccountsService/wallpapers/lightdm-webkit.jpg" in jpeg format.</div>
                             </md-input-container>
                             <md-input-container ng-show="settings.backgroundEngine == 'particleground'">
@@ -79,7 +81,7 @@
                                     </div>
                                 </div>
                             </md-input-container>
-                            <md-input-container ng-if="settings.backgroundEngine != 'image'">
+                            <md-input-container ng-if="settings.backgroundEngine != 'image' || settings.backgroundEngine != 'random-image'">
                                 <label for="settings-background"><i class="fa fa-desktop"></i> Background Color Scheme</label>
                                 <md-select id="settings-background" ng-model="settings.background">
                                     <md-option value="random">Random (default)</md-option>