Commits

Jason R. Coombs committed 7cfc6de

Added support for Windows

Comments (0)

Files changed (1)

jaraco/util/wallpaper.py

 Day and set it as your desktop background. Won't run if you are low on
 space, easily configurable below.
 
-NOTE: Only works on Linux! (Written on Ubuntu)
+NOTE: Only works on Linux or Windows! (Written on Ubuntu)
 """
 
 import os
 import re
 import sys
+import platform
 import urllib2
+import ctypes
 from urllib2 import Request, urlopen, URLError, HTTPError
 from BeautifulSoup import BeautifulSoup, Tag
 from HTMLParser import HTMLParser, HTMLParseError
 
 #------------------------------------------------------------------------------
 
+def _get_free_bytes(folder):
+	"""
+	Return folder/drive free space and total space (in bytes)
+	"""
+	if platform.system() == 'Windows':
+		free_bytes = ctypes.c_ulonglong()
+		p_free_bytes = ctypes.pointer(free_bytes)
+		total_bytes = ctypes.c_ulonglong()
+		p_total_bytes = ctypes.pointer(total_bytes)
+		GetDiskFreeSpaceEx = ctypes.windll.kernel32.GetDiskFreeSpaceExW
+		res = GetDiskFreeSpaceEx(unicode(folder), None, p_total_bytes, p_free_bytes)
+		if not res:
+			raise WindowsError("GetDiskFreeSpace failed")
+		free_bytes = free_bytes.value
+		total_bytes = total_bytes.value
+	else:
+		stat = os.statvfs(folder)
+		free_bytes = stat.f_bsize * stat.f_bfree
+		total_bytes = stat.f_bsize * stat.f_blocks
+	return free_bytes, total_bytes
+
 def free_space(dir):
 	"""
 	Returns percentage of free space.
 	"""
 	try:
-		fs = os.statvfs(dir)
+		free, total = _get_free_bytes(dir)
 	except OSError:
 		return False
-	gb_total = float(float(fs.f_bsize * fs.f_blocks) / 1024 / 1024 / 1024)
-	gb_free = float(float(fs.f_bsize * fs.f_bavail) / 1024 / 1024 / 1024)
-	percen_free = (gb_total - gb_free) / gb_total * 100
+	percen_free = float(free) / total * 100
 	return int(round(percen_free))
 
 def get_wallpaper_details(base_url):
 
 	return outpath
 
-def set_wallpaper(filename):
+def _set_wallpaper_linux2(filename):
 	"""
 	Sets the passed file as wallpaper.
 	"""
 	os.system("gconftool-2 -t str --set /desktop/gnome/background/picture_filename " + filename)
 	print "BG set!"
 
+def _set_wallpaper_win32(filename):
+	SPI_SETDESKWALLPAPER = 0x14
+	SPIF_UPDATEINIFILE = 0x1
+	SPIF_SENDWININICHANGE = 0x2
+	SystemParametersInfo = ctypes.windll.user32.SystemParametersInfoW
+	SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, filename,
+		SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE)
+
+set_wallpaper = globals()['_set_wallpaper_' + sys.platform]
+
 #------------------------------------------------------------------------------
 fs = free_space(picture_dir)
 if not fs: