#!/usr/bin/python
# 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/>.
#
# Author: Hanno Vene

import pygtk
pygtk.require('2.0')
import gtk
import os
import gconf
import sys

class BackgroundRotator:
    def __init__(self):
        self.index = 0;
        self.dir = None
        if len(sys.argv)>1:
            self.dir = sys.argv[1]
        
        self.tray = gtk.StatusIcon()
        self.tray.set_tooltip("Uus taust kliki kaugusel ...")
        self.tray.set_from_file("icon.png")
        self.tray.set_visible(True)
        self.tray.connect("activate",self.onTrayClick)
        self.tray.connect("popup-menu",self.openContextMenu)
        
        self.menu = gtk.Menu()

        item_dir = gtk.MenuItem("Vali pilte sisaldav kaust ...")
        item_dir.connect("activate", self.openFileDialog)
        item_dir.show()
        self.menu.append(item_dir)

        item_close = gtk.MenuItem("Sulge")
        item_close.connect("activate", self.destroy)
        item_close.show()
        self.menu.append(item_close)
        self.menu.show()
        
    def openContextMenu(self, status_icon, button, activate_time):
        self.menu.popup(None, None, None, button, activate_time)
        
    
    def openFileDialog(self, widget):
        dialog = gtk.FileChooserDialog("Vali kataloog ...",
                                       None,
                                       gtk.FILE_CHOOSER_ACTION_SELECT_FOLDER,
                                       (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
                                        gtk.STOCK_OPEN, gtk.RESPONSE_OK))
        dialog.set_default_response(gtk.RESPONSE_OK)

        response = dialog.run()

        if response == gtk.RESPONSE_OK:
            self.dir = dialog.get_filename()
        
        dialog.destroy()

    def onTrayClick(self, widget):
        if self.dir:
            pictures =  os.listdir(self.dir)
            if len(pictures)==0:
                self.dir = None
                return False
            else:
                self.update_picture_filename(pictures[self.index])
                self.index = (self.index + 1) % len(pictures)
        else:
            self.openFileDialog(widget)

    def update_picture_filename(self, filename):
        client = gconf.client_get_default()
        client.add_dir('/desktop/gnome/background', gconf.CLIENT_PRELOAD_NONE)
        client.set_string('/desktop/gnome/background/picture_filename', self.dir+"/"+filename)

    def destroy(self, widget, data=None):
        print "Good bye!"
        gtk.main_quit()

    def main(self):
        gtk.main()

if __name__ == "__main__":
    hello = BackgroundRotator()
    hello.main()
