diff --git a/set_gdm_backgroud.py b/set_gdm_backgroud.py index 21561a0..3b5a589 100755 --- a/set_gdm_backgroud.py +++ b/set_gdm_backgroud.py @@ -23,6 +23,7 @@ pygtk.require('2.0') import gtk import commands +import os # Check for new pygtk: this is new class in PyGtk 2.4 if gtk.pygtk_version < (2,3,90): @@ -30,7 +31,7 @@ if gtk.pygtk_version < (2,3,90): raise SystemExit class App: - path = "/usr/share/images/desktop-base/moreblue-orbit-gdm.svg" + path = None disp = "stretched" conffile = "/etc/gdm3/greeter.gconf-defaults" image = gtk.Image() @@ -41,112 +42,115 @@ class App: def redraw(self): # new pixbuf from current path - readimg = gtk.gdk.pixbuf_new_from_file(self.path) - imgw = readimg.get_width() - imgh = readimg.get_height() + try: + readimg = gtk.gdk.pixbuf_new_from_file(self.path) + except: + readimg = None + print "WARNING: Unable to load image." - # calculate selected image and screen proportions (width-to-height ratio) - disp_ratio = float(self.screenw)/float(self.screenh) - img_ratio = float(imgw)/float(imgh) - - # actual width of the image on the screen thumbnail - thumbw = -1 - thumbh = -1 - - # create an pixbuf for the thumbn, and fill it with a "background" pattern + # create an pixbuf for the thumbnail and fill it with a background pattern thumbpixbuf = gtk.gdk.Pixbuf( gtk.gdk.COLORSPACE_RGB, True, 8, int(self.screenw/4.0), int(self.screenh/4.0) ) thumbpixbuf.fill(0x88888888) thumbpixbuf.saturate_and_pixelate(thumbpixbuf, 0.0, True) - - # switch between the different disposition styles - if self.disp == "zoom": - # if display wider than image, scale image to fit screen width - if disp_ratio > img_ratio: - thumbw = int(self.screenw/4.0) - thumbh = int(imgh*((self.screenw/4.0)/imgw)) + if readimg is not None: + imgw = readimg.get_width() + imgh = readimg.get_height() - # else scale image to fit screen height - else: - thumbh = int(self.screenh/4.0) - thumbw = int(imgw*((self.screenh/4.0)/imgh)) + # calculate selected image and screen proportions (width-to-height ratio) + disp_ratio = float(self.screenw)/float(self.screenh) + img_ratio = float(imgw)/float(imgh) - # scale previously loaded image and copy to the screen thumbnail pixbuf - readimg = readimg.scale_simple(thumbw,thumbh,gtk.gdk.INTERP_BILINEAR) - readimg.copy_area( int((thumbw-self.screenw/4.0)/2), + # switch between the different disposition styles + if self.disp == "zoom": + + # if display wider than image, scale image to fit screen width + if disp_ratio > img_ratio: + thumbw = int(self.screenw/4.0) + thumbh = int(imgh*((self.screenw/4.0)/imgw)) + + # else scale image to fit screen height + else: + thumbh = int(self.screenh/4.0) + thumbw = int(imgw*((self.screenh/4.0)/imgh)) + + # scale previously loaded image and copy to the screen thumbnail pixbuf + readimg = readimg.scale_simple(thumbw,thumbh,gtk.gdk.INTERP_BILINEAR) + readimg.copy_area( int((thumbw-self.screenw/4.0)/2), int((thumbh-self.screenh/4.0)/2), int(self.screenw/4), int(self.screenh/4), thumbpixbuf, 0, 0) - elif self.disp == "scaled": + elif self.disp == "scaled": - # with the "scaled" disposition, image is scaled - # so that it fits entirely onscreen - xscale = (self.screenw/4.0)/imgw - yscale = (self.screenh/4.0)/imgh + # with the "scaled" disposition, image is scaled + # so that it fits entirely onscreen + xscale = (self.screenw/4.0)/imgw + yscale = (self.screenh/4.0)/imgh - thumbw = int(imgw*min(xscale, yscale)) - thumbh = int(imgh*min(yscale, yscale)) + thumbw = int(imgw*min(xscale, yscale)) + thumbh = int(imgh*min(yscale, yscale)) - readimg = readimg.scale_simple(thumbw,thumbh,gtk.gdk.INTERP_BILINEAR) - readimg.copy_area( 0, 0, thumbw, thumbh, thumbpixbuf, + readimg = readimg.scale_simple(thumbw,thumbh,gtk.gdk.INTERP_BILINEAR) + readimg.copy_area( 0, 0, thumbw, thumbh, thumbpixbuf, int((self.screenw/4-thumbw)/2), int((self.screenh/4-thumbh)/2)) - elif self.disp == "centered": + elif self.disp == "centered": - # scale image to 1/4 its original size - # (the screen thumbnail is 1/4 of the real screen size) - readimg = readimg.scale_simple( int(imgw/4), int(imgh/4), + # scale image to 1/4 its original size + # (the screen thumbnail is 1/4 of the real screen size) + readimg = readimg.scale_simple( int(imgw/4), int(imgh/4), gtk.gdk.INTERP_BILINEAR) - # the visible area coordinates of the image - # (if negative, the image is bigger than the screen) - x0 = int((self.screenw-imgw)/8) - y0 = int((self.screenh-imgh)/8) + # the visible area coordinates of the image + # (if negative, the image is bigger than the screen) + x0 = int((self.screenw-imgw)/8) + y0 = int((self.screenh-imgh)/8) - readimg.copy_area( max(-x0,0), max(-y0,0), + readimg.copy_area( max(-x0,0), max(-y0,0), int(min(self.screenw/4,imgw/4)), int(min(self.screenh/4,imgh/4)), thumbpixbuf, max(x0,0), max(y0,0)) - elif self.disp == "stretched": + elif self.disp == "stretched": - # in this case, simply stretch image so that both width and height - # are the same as the screen's - readimg = readimg.scale_simple( int(self.screenw/4), + # in this case, simply stretch image so that both width and height + # are the same as the screen's + readimg = readimg.scale_simple( int(self.screenw/4), int(self.screenh/4), gtk.gdk.INTERP_BILINEAR) - readimg.copy_area( 0, 0, int(self.screenw/4), int(self.screenh/4), + readimg.copy_area( 0, 0, int(self.screenw/4), int(self.screenh/4), thumbpixbuf, 0, 0) - - elif self.disp == "wallpaper": + + elif self.disp == "wallpaper": - # scale image to 1/4 as in centered - readimg = readimg.scale_simple( int(imgw/4), + # scale image to 1/4 as in centered + readimg = readimg.scale_simple( int(imgw/4), int(imgh/4), gtk.gdk.INTERP_BILINEAR) - # copy image as tiles until we fill the entire screen - x0=0 - while( x0 < self.screenw ): - y0=0 - while( y0 < self.screenh): - readimg.copy_area( 0, 0, + # copy image as tiles until we fill the entire screen + x0=0 + while( x0 < self.screenw ): + y0=0 + while( y0 < self.screenh): + readimg.copy_area( 0, 0, int(min(self.screenw/4-x0,imgw/4)), int(min(self.screenh/4-y0,imgh/4)), thumbpixbuf, x0, y0) - y0 += imgh/4 - x0 += imgw/4 + y0 += imgh/4 + x0 += imgw/4 # finally draw the gtk.Image from the pixbuf - self.image.set_from_pixbuf(thumbpixbuf) + self.image.set_from_pixbuf(thumbpixbuf) + # read GDM3 configuration file, and set correspondig variables def read_config( self ): @@ -154,12 +158,16 @@ class App: with open( self.conffile, 'r') as conf: for line in conf: - m1 = re.match(r"\s*/desktop/gnome/background/picture_filename\s+((?:\S*(?:(?<=\\)\s)*)+)", line) - m2 = re.match(r"\s*/desktop/gnome/background/picture_options\s+(\w+)", line) + m1 = re.match(r"^\s*/desktop/gnome/background/picture_filename\s+(\S.*)$", line) + m2 = re.match(r"^\s*/desktop/gnome/background/picture_options\s+(\w+)", line) if m1: - print "Image file option read: " + m1.group(1) - self.path = m1.group(1) + if os.path.isfile( m1.group(1)): + print "Image file option read: " + m1.group(1) + self.path = m1.group(1) + else: + print "Image file not found" + self.path = None elif m2: print "Image disposition option read: " + m2.group(1) @@ -172,19 +180,19 @@ class App: out = "" for line in conf: - m1 = re.match(r"\s*/desktop/gnome/background/picture_filename\s+((?:\S*(?:(?<=\\)\s)*)+)", line) - m2 = re.match(r"\s*/desktop/gnome/background/picture_options\s+(\w+)", line) + m1 = re.match(r"^\s*/desktop/gnome/background/picture_filename", line) + m2 = re.match(r"^\s*/desktop/gnome/background/picture_options", line) # if the current line matchs the picture_filename option, replace # it with a new one containing the current value of path if m1: - out += "/desktop/gnome/background/picture_filename " \ + out += "/desktop/gnome/background/picture_filename " \ + self.path + "\n" # else if it macthes the picture_options option, put the # value of disp instead elif m2: - out += "/desktop/gnome/background/picture_options "\ + out += "/desktop/gnome/background/picture_options "\ + self.disp + "\n" # else copy the line as-is @@ -273,8 +281,9 @@ class App: while self.combo.get_active_text() != self.disp: i+=1 self.combo.set_active( i ) - - self.filedialog.set_filename(self.path) + + if self.path is not None: + self.filedialog.set_filename(self.path) #self.window.set_border_width(4)