Use list.sort's keyword parameters
[joel/kofoto.git] / src / packages / kofoto / gkofoto / clipboard.py
1 from kofoto.shelf import Album, Image
2 from kofoto.gkofoto.categories import ClipboardCategories
3
4 class Clipboard:
5
6     # TYPES
7     OBJECTS    = 0 # shelf.Album and shelf.Image
8     CATEGORIES = 1 # shelf.Category
9
10     def __init__(self):
11         self.__objects = None
12         self.__types = None
13         self.__changedCallbacks = set()
14         self.clear()
15
16     def addChangedCallback(self, callback):
17         self.__changedCallbacks.add(callback)
18
19     def removeChangedCallback(self, callback):
20         self.__changedCallbacks.remove(callback)
21
22     def setObjects(self, iterable):
23         self.__objects = []
24         self.__types = Clipboard.OBJECTS
25         for obj in iterable:
26             if (isinstance(obj, Image) or isinstance(obj, Album)):
27                 self.__objects.append(obj)
28             else:
29                 self.clear()
30                 raise Exception("Object is not an Image nor an Album") # TODO
31         self.__invokeChangedCallbacks()
32
33     def setCategories(self, clipboardCategories):
34         self.__objects = []
35         if isinstance(clipboardCategories, ClipboardCategories):
36             self.__objects.append(clipboardCategories)
37         else:
38             self.clear()
39             raise Exception("Object is not a ClipboardCategories") # TODO
40         self.__types = Clipboard.CATEGORIES
41         self.__invokeChangedCallbacks()
42
43     def clear(self):
44         self.__objects = []
45         self.__types = None
46         self.__invokeChangedCallbacks()
47
48     def removeObjects(self, obj):
49         self.__objects = [x for x in self.__objects if x != obj]
50         self.__invokeChangedCallbacks()
51
52     def hasCategories(self):
53         return (self.__types == Clipboard.CATEGORIES and len(self.__objects) > 0)
54
55     def hasObjects(self):
56         return (self.__types == Clipboard.OBJECTS and len(self.__objects) > 0)
57
58     def __len__(self):
59         return len(self.__objects)
60
61     def __iter__(self):
62         return self.__objects.__iter__()
63
64     def __getitem__(self, index):
65         return self.__objects.__getitem__(index)
66
67     def __invokeChangedCallbacks(self):
68         for callback in self.__changedCallbacks:
69             callback(self)