#
# <one line to give the library's name and an idea of what it does.>
# Copyright (C) 2017  Bob <email>
# 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
# 
#

import threading, time
import logging



class Sleeper:
    def __init__(self):
        self.starttimemillis = time.time() * 1000
        
    def sleep(self, seconds):
        elapsed = time.time() * 1000 - self.starttimemillis
        waittime = (seconds * 1000 - elapsed) / 1000                    ## wait secs
        ## print "Waiting:", waittime
        if waittime <= 0:
            return
        time.sleep(waittime)
    

class ImageGrabberThread(threading.Thread):
    def __init__(self, imageWindow):
        threading.Thread.__init__(self)
        self.daemon = True                              ## automatically killed when needed
        
        self.playEvent = threading.Event()
        self.playEvent.set()                            ## play immediatelly
        
        self.display = imageWindow
        
        self.display.connect('play-pause', self.playPause)
        
        self.urlProvider = None
        self.waitTime = 7
        ##self.start()                                    # invoke the run method

    def playPause(self, window):
        if self.playEvent.is_set() is True:
            logging.info("pause")
            self.playEvent.clear();
        else:
            logging.info("resume")
            self.playEvent.set()

    def run(self):
        newUrl = self.getNextUrl()
        while True:
            sleeper = Sleeper()
            
            loaded = self.display.openUrl( newUrl )
            
            newUrl = self.getNextUrl()

            if loaded == False:
	      ## could not load image
	      sleeper.sleep(1)
	      continue
            
            if self.playEvent.is_set() is False:
                self.playEvent.wait()
            
            sleeper.sleep(self.waitTime)
        
        
    def getNextUrl(self):
        while True:
            if self.urlProvider is None:
                logging.warning("No url provider")
                time.sleep(1)
                continue
            
            sleeper = Sleeper()
            newUrl = self.urlProvider.provideUrl()
            if newUrl == "":
                logging.warning("Url not provided")
                sleeper.sleep(1)
                continue
            
            return newUrl
        

    def setDisplayTime(self, dTimeS):
        self.waitTime = float(dTimeS)

#
#

@@ -2020,32 +2020,245 @@
  def run(self):%0A
+        while True:%0A            self.iterateImages()%0A            ## iteration stopped due to exception -- wait some time to restore%0A            time.sleep(5)%0A        %0A    def iterateImages(self):%0A        try:%0A    
         newUrl =
@@ -2272,24 +2272,28 @@
 etNextUrl()%0A
+    
         whil
@@ -2304,32 +2304,36 @@
 ue:%0A            
+    
 sleeper = Sleepe
@@ -2340,33 +2340,41 @@
 r()%0A            
+  
-%0A
+  %0A    
             load
@@ -2413,33 +2413,40 @@
 l )%0A            
-%0A
+    %0A   
             newU
@@ -2433,32 +2433,33 @@
 %0A               
+ 
 newUrl = self.ge
@@ -2482,16 +2482,20 @@
         
+    
 if loade
@@ -2506,17 +2506,30 @@
  False:%0A
-%09
+              
       ##
@@ -2550,17 +2550,30 @@
 d image%0A
-%09
+              
       sl
@@ -2591,15 +2591,28 @@
 (1)%0A
-%09
+             
       
+ 
 cont
@@ -2620,33 +2620,41 @@
 nue%0A            
-%0A
+    %0A    
             if s
@@ -2694,32 +2694,36 @@
                 
+   
+ 
 self.playEvent.w
@@ -2732,33 +2732,39 @@
 t()%0A            
-%0A
+    %0A  
             slee
@@ -2751,32 +2751,34 @@
  %0A              
+  
 sleeper.sleep(se
@@ -2785,24 +2785,114 @@
 lf.waitTime)
+        %0A        except:%0A            logging.exception(%22Error while providing next image%22)
 %0A        %0A  

Handle provider exceptions to prevent thread from terminating