Plugins: image_timer.py

File image_timer.py, 5.9 kB (added by louizatakk, 8 months ago)
Line 
1# -*- coding: utf-8 -*-
2
3#   This file is part of emesene.
4#
5#    Emesene is free software; you can redistribute it and/or modify
6#    it under the terms of the GNU General Public License as published by
7#    the Free Software Foundation; either version 2 of the License, or
8#    (at your option) any later version.
9#
10#    emesene is distributed in the hope that it will be useful,
11#    but WITHOUT ANY WARRANTY; without even the implied warranty of
12#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13#    GNU General Public License for more details.
14#
15#    You should have received a copy of the GNU General Public License
16#    along with emesene; if not, write to the Free Software
17#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
19import os
20import gtk
21import gobject
22import random
23import string
24import Plugin
25
26class MainClass( Plugin.Plugin ):
27
28    def __init__( self, controller, msn ):
29        Plugin.Plugin.__init__( self, controller, msn )
30
31        self.description = _('Every time interval,picks an image from a selected folder.V 0.1a')
32        self.authors = { 'liuggio' : 'liuggio YAHOO' }
33        self.website = ''
34        self.displayName = _('Image Timer')
35        self.name = 'Image_Timer'
36        self.config = controller.config
37        self.config.readPluginConfig(self.name)
38        self.enabled = False
39        self.controller = controller
40        self.checking = False
41        self.thedir = self.config.getPluginValue( self.name, 'user_folder', self.controller.config.getAvatarsCachePath() )
42        self.random=False
43        self.Indexoflist=0
44        self.recursive =1
45
46
47    def start( self ):
48        self.enabled = True
49        self.source_id = 0
50        ''' list of all file in the directory   not(yet) RECURSIVE  '''
51        #todo for root, dirs, self.imagefilenames in os.walk(self.thedir, topdown=True):
52        if self.recursive>0:
53            self.imagefilenames=os.listdir(self.thedir)
54            self.imagedirlen=len(self.imagefilenames)
55            if self.imagedirlen>0:
56                self.Image_Timer(0)
57                self.interval = 1000*int(self.config.getPluginValue( self.name, 'time', '20' ))
58                self.source_id = gobject.timeout_add(self.interval, self.Image_Timer, 0)
59            else:
60                print self.name + " : ("+self.thedir+") dir is empty "
61
62    def Image_Timer( self ,  data=0):
63        if data is None:
64            data = 0
65
66        self.imagedirlen=len(self.imagefilenames)
67        ''' check if list is not empty '''
68        if self.imagedirlen > 0:
69            if self.random:
70                #random choice
71                element = random.choice(self.imagefilenames)
72                #print "******random"+element
73            else:
74                #sequential choice
75                self.Indexoflist=self.Indexoflist+1
76                if self.imagedirlen<=self.Indexoflist:
77                    self.Indexoflist=0
78                element = self.imagefilenames[self.Indexoflist]
79                #print "*****indexed"+lement
80
81
82            nimg=string.lower(element)
83            '''only if jpg/gif/png/bmp '''
84            if nimg.endswith('.jpg') or nimg.endswith('.gif') or  nimg.endswith('.png') or  nimg.endswith('.bmp'):
85                pa = os.path.join(self.thedir,element)
86                try:
87                    self.controller.changeAvatar(pa)
88                except:
89                    print self.name + " : changeAvatar failure "
90                    self.imagefilenames.remove(element)
91                    #print len(self.imagefilenames)
92                    return self.Image_Timer( data+1 )
93                else:
94                    print self.name + " : Avatar changed "
95                    return True
96            else:
97               print self.name + " : removing element from list is not image "
98               self.imagefilenames.remove(element)
99               #print len(self.imagefilenames)
100               return self.Image_Timer( 0 )
101        else:
102            print self.name + " : Folder doesn't not has images  "
103            self.stop()
104            return False
105
106    def stop( self ):
107        self.enabled = False
108        gobject.source_remove(self.source_id)
109
110    def check( self ):
111        return ( True, 'Ok' )
112
113    def configure( self ):
114        dataM = []
115        dataM.append(Plugin.Option('time', str, _('Change every [sec]:'), '',\
116            self.config.getPluginValue( self.name, 'time', '20' )) )
117        dataM.append( Plugin.Option( 'user_folder', str, 'Image folder:', '',\
118            self.config.getPluginValue( self.name, 'user_folder',self.controller.config.getAvatarsCachePath())) )
119        dataM.append(Plugin.Option("random", bool, "Random ?", \
120            '', bool(int(self.config.getPluginValue(self.name, "random", False)))))
121
122        self.confW = Plugin.ConfigWindow(_('Image Timer config'), dataM)
123
124        r = self.confW.run()
125
126        if r is not None:
127            needtostart=0
128            OLDt=self.config.getPluginValue( self.name, 'time', '20' )
129            OLDuf=self.config.getPluginValue( self.name, 'user_folder',self.controller.config.getAvatarsCachePath())
130            OLDrnd=bool(int(self.config.getPluginValue(self.name, "random", False)))
131
132            if(OLDt != r['time'].value or OLDuf != r['user_folder'].value) or OLDrnd!=r['random'].value:
133                needtostart=1
134
135            self.config.setPluginValue(self.name, 'time', r['time'].value)
136            self.config.setPluginValue(self.name, 'user_folder', r['user_folder'].value)
137            self.config.setPluginValue(self.name, 'random', str(int(r['random'].value)) )
138            if (needtostart>0):
139                print self.name + " : restarting  "
140                self.thedir = self.config.getPluginValue( self.name, 'user_folder',\
141                    self.controller.config.getAvatarsCachePath() )
142                self.random=r['random'].value;
143                if not self.random:
144                    self.Indexoflist=0;
145                self.stop()
146                self.start()
147        return True