1# -*- coding: utf-8 -*-
2#
3# PhotoFilmStrip - Creates movies out of your pictures.
4#
5# Copyright (C) 2018 Jens Goepfert
6#
7
8import logging
9
10from photofilmstrip.lib.DestructionManager import Destroyable
11import importlib
12
13
14class UxService(Destroyable):
15
16    __instance = None
17
18    @classmethod
19    def GetInstance(cls):
20        if cls.__instance is None:
21            cls.__instance = UxService()
22        return cls.__instance
23
24    def __init__(self):
25        Destroyable.__init__(self)
26
27        self.uxAdapters = []
28
29        for modname in ("gnome", "uwp"):
30            try:
31                ux_module = importlib.import_module("photofilmstrip.ux." + modname)
32            except:
33                logging.getLogger('UxService').debug(
34                    "importing UxAdapter '%s' failed", modname, exc_info=1)
35                continue
36
37            if hasattr(ux_module, "ux_init"):
38                try:
39                    uxAdapter = getattr(ux_module, "ux_init")()
40                    if isinstance(uxAdapter, UxAdapter):
41                        self.uxAdapters.append(uxAdapter)
42                except:
43                    logging.getLogger('UxService').error(
44                        "initializing UxAdapter '%s' failed", ux_module, exc_info=1)
45
46    def Initialize(self):
47        for uxAdapter in self.uxAdapters:
48            uxAdapter.OnInit()
49
50    def Start(self):
51        for uxAdapter in self.uxAdapters:
52            uxAdapter.OnStart()
53
54    def Destroy(self):
55        for uxAdapter in self.uxAdapters:
56            uxAdapter.OnDestroy()
57
58
59class UxAdapter:
60
61    def OnInit(self):
62        '''
63        Triggers on right before GUI is started.
64        '''
65        raise NotImplementedError()
66
67    def OnStart(self):
68        '''
69        Triggers right after the process has started. Raise
70        PreventStartupSignal if startup may be prevented
71        '''
72        pass
73
74    def OnDestroy(self):
75        '''
76        Triggers on shut down to clean up stuff.
77        '''
78        pass
79
80
81class Ux:
82    '''
83    Entity for user experience information
84    '''
85
86    def __init__(self):
87        self.uxEvents = []
88
89    def AddUxEvent(self, uxEvent):
90        self.uxEvents.append(uxEvent)
91
92    def GetUxEvents(self):
93        return self.uxEvents
94
95
96class UxPreventStartupSignal(Exception):
97    pass
98
99