#!/usr/bin/env python

# Copyright 2008-2015 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
import re


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)
        if not any(self.open(x) for x in
                   ('/usr/share/wallpapers/dm-background.png',
                    '/usr/share/apps/ksplash/Themes/CalculateSplashEn/'
                    '400x300/background.png')):
            self.set_background()

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

    def set_background(self):
        self.setStyleSheet("background-color: %s" % self.select_color())

    def open(self, fn):
        image = QtGui.QImage(fn)
        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_())
