#! /usr/bin/env python2
# -*- coding=utf-8 -*-

from gi.repository import Gio, Gtk
import os
import pwd
import sys
import subprocess
import syslog
import time

def preload_theme(theme_name):
    start = time.time()
    theme = Gtk.IconTheme()
    theme.set_custom_theme(theme_name)
    for i in Gio.AppInfo.get_all():
        icon = i.get_icon()
        if type(icon) == Gio.ThemedIcon:
            try:
                theme.load_icon(i.get_icon().to_string(), 22, 0)
            except:
                pass
    syslog.syslog('Loading %s took %0.2f seconds' % (theme_name, (time.time()-start)))

if __name__ == "__main__":
    if os.getuid() == 0:
        start = time.time()
        has_encrypted_home = False
        for p in pwd.getpwall():
            if p.pw_uid >= 1000 and p.pw_shell and p.pw_shell != '/bin/false' and p.pw_shell != '/usr/sbin/nologin':
                if not os.path.exists("/home/.ecryptfs/%s" % p.pw_name):
                    subprocess.call(["su", p.pw_name, "-c", os.path.realpath(sys.argv[0])])
                else:
                    has_encrypted_home = True
        if has_encrypted_home:
            syslog.syslog("Encrypted home directories detected, loading default themes")
            theme_list = []
            if os.path.exists("/etc/cinnamon/preload/iconthemes.d/") and os.path.isdir("/etc/cinnamon/preload/iconthemes.d/"):
                for i in os.listdir("/etc/cinnamon/preload/iconthemes.d/"):
                    f = open(os.path.join("/etc/cinnamon/preload/iconthemes.d/", i))
                    print f
                    theme_list += f.read().splitlines()
                    f.close()
            for theme in theme_list:
                if theme:
                    syslog.syslog("Loading default theme %s" % theme)
                    preload_theme(theme)

        syslog.syslog("Loading applets")
        os.system("find /usr/share/cinnamon/applets -type f -exec cat {} \\; > /dev/null")
        syslog.syslog('Took %0.2f seconds' % ((time.time()-start)))
    else:
        theme_name = Gio.Settings("org.cinnamon.desktop.interface").get_string("icon-theme")
        syslog.syslog("Loading theme %s for user ID %d" % (theme_name, os.getuid()))
        preload_theme(theme_name)
