#!/usr/bin/env python

# Copyright 2008-2012 Calculate Ltd. http://www.calculate-linux.org
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.

from PySide import QtCore, QtGui
import os,sys

class ImageViewer(QtGui.QMainWindow):
    def __init__(self):
        super(ImageViewer, self).__init__()

        self.setWindowFlags(QtCore.Qt.X11BypassWindowManagerHint)
        self.imageLabel = QtGui.QLabel()
        self.imageLabel.setScaledContents(True)
        self.setCentralWidget(self.imageLabel)

        screen = QtGui.QDesktopWidget().screenGeometry()
        self.setGeometry(screen)
        self.open('/usr/share/wallpapers/dm-background.png') or \
            self.open('/usr/share/apps/ksplash/Themes/CalculateSplashEn/'
                      '400x300/background.png') or \
            self.setBackground()

    def selectColor(self):
        try:
            if filter(re.compile(r"(cld|cldx|cldg|cmc|cls)-themes-12").search,
               os.listdir('/var/db/pkg/media-gfx')):
                return "#73a363"
        except:
            pass
        return '#30648b'

    def setBackground(self):
        self.setStyleSheet("background-color: %s"%self.selectColor())

    def open(self,fileName):
        image = QtGui.QImage(fileName)
        if image.isNull():
            return False

        self.imageLabel.setPixmap(QtGui.QPixmap.fromImage(image))

        self.imageLabel.adjustSize()
        return True

if __name__ == '__main__':
    import sys
    app = QtGui.QApplication(sys.argv)
    imageViewer = ImageViewer()
    imageViewer.show()
    sys.exit(app.exec_())
