annotate sat_frontends/quick_frontend/quick_widgets.py @ 2645:f2cf1daa42cb

core: added async TriggerManager Async trigger manager add an asyncPoint method which can be used with Deferred (or sync method). This allow a triggers do to some long operations like doing network requests. MessageReceived and sendMessageData now use asyncPoint.
author Goffi <goffi@goffi.org>
date Sun, 29 Jul 2018 19:22:51 +0200
parents 56f94936df1e
children 003b8b4b56a7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1934
2daf7b4c6756 use of /usr/bin/env instead of /usr/bin/python in shebang
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
1 #!/usr/bin/env python2
0
goffi@necton2
parents:
diff changeset
2 # -*- coding: utf-8 -*-
goffi@necton2
parents:
diff changeset
3
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 587
diff changeset
4 # helper class for making a SAT frontend
2483
0046283a285d dates update
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
5 # Copyright (C) 2009-2018 Jérôme Poisson (goffi@goffi.org)
0
goffi@necton2
parents:
diff changeset
6
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 587
diff changeset
7 # This program is free software: you can redistribute it and/or modify
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 587
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 587
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 587
diff changeset
10 # (at your option) any later version.
0
goffi@necton2
parents:
diff changeset
11
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 587
diff changeset
12 # This program is distributed in the hope that it will be useful,
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 587
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 587
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 587
diff changeset
15 # GNU Affero General Public License for more details.
0
goffi@necton2
parents:
diff changeset
16
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 587
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 587
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
0
goffi@necton2
parents:
diff changeset
19
1265
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
20 from sat.core.log import getLogger
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
21
1265
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
22 log = getLogger(__name__)
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
23 from sat.core import exceptions
1301
afc57b34c0a3 Quick Frontend (quick_widgets): added on_existing_widget in getOrCreateWidget (see docstring for more infos) + use of constants
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
24 from sat_frontends.quick_frontend.constants import Const as C
1265
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
25
2039
6353deb1bd73 quick frontend (quick_widget): getWidgets can now filter on hash (using target), handling recreated widgets too
Goffi <goffi@goffi.org>
parents: 2027
diff changeset
26
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
27 NEW_INSTANCE_SUFF = "_new_instance_"
1265
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
28 classes_map = {}
0
goffi@necton2
parents:
diff changeset
29
goffi@necton2
parents:
diff changeset
30
1290
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
31 try:
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
32 # FIXME: to be removed when an acceptable solution is here
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
33 unicode("") # XXX: unicode doesn't exist in pyjamas
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
34 except (
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
35 TypeError,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
36 AttributeError,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
37 ): # Error raised is not the same depending on pyjsbuild options
1290
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
38 unicode = str
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
39
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
40
1265
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
41 def register(base_cls, child_cls=None):
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
42 """Register a child class to use by default when a base class is needed
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
43
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
44 @param base_cls: "Quick..." base class (like QuickChat or QuickContact), must inherit from QuickWidget
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
45 @param child_cls: inherited class to use when Quick... class is requested, must inherit from base_cls.
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
46 Can be None if it's the base_cls itself which register
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
47 """
1304
1a61b18703c4 quick frontend (quick widgets): class' __name__ method is used for classes_map hash because the use of class directly was causing bugs with pyjamas (difficult to find, several MicroblogPanel instances were added only once in Libervia's TabPanel, hash method seemed buggy)
Goffi <goffi@goffi.org>
parents: 1303
diff changeset
48 # FIXME: we use base_cls.__name__ instead of base_cls directly because pyjamas because
1a61b18703c4 quick frontend (quick widgets): class' __name__ method is used for classes_map hash because the use of class directly was causing bugs with pyjamas (difficult to find, several MicroblogPanel instances were added only once in Libervia's TabPanel, hash method seemed buggy)
Goffi <goffi@goffi.org>
parents: 1303
diff changeset
49 # in the second case
1a61b18703c4 quick frontend (quick widgets): class' __name__ method is used for classes_map hash because the use of class directly was causing bugs with pyjamas (difficult to find, several MicroblogPanel instances were added only once in Libervia's TabPanel, hash method seemed buggy)
Goffi <goffi@goffi.org>
parents: 1303
diff changeset
50 classes_map[base_cls.__name__] = child_cls
1a61b18703c4 quick frontend (quick widgets): class' __name__ method is used for classes_map hash because the use of class directly was causing bugs with pyjamas (difficult to find, several MicroblogPanel instances were added only once in Libervia's TabPanel, hash method seemed buggy)
Goffi <goffi@goffi.org>
parents: 1303
diff changeset
51
1a61b18703c4 quick frontend (quick widgets): class' __name__ method is used for classes_map hash because the use of class directly was causing bugs with pyjamas (difficult to find, several MicroblogPanel instances were added only once in Libervia's TabPanel, hash method seemed buggy)
Goffi <goffi@goffi.org>
parents: 1303
diff changeset
52
1301
afc57b34c0a3 Quick Frontend (quick_widgets): added on_existing_widget in getOrCreateWidget (see docstring for more infos) + use of constants
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
53 class WidgetAlreadyExistsError(Exception):
afc57b34c0a3 Quick Frontend (quick_widgets): added on_existing_widget in getOrCreateWidget (see docstring for more infos) + use of constants
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
54 pass
1265
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
55
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
56
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
57 class QuickWidgetsManager(object):
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
58 """This class is used to manage all the widgets of a frontend
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
59 A widget can be a window, a graphical thing, or someting else depending of the frontend"""
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
60
0
goffi@necton2
parents:
diff changeset
61 def __init__(self, host):
goffi@necton2
parents:
diff changeset
62 self.host = host
1265
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
63 self._widgets = {}
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
64
1290
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
65 def __iter__(self):
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
66 """Iterate throught all widgets"""
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
67 for widget_map in self._widgets.itervalues():
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
68 for widget in widget_map.itervalues():
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
69 yield widget
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
70
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
71 def getRealClass(self, class_):
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
72 """Return class registered for given class_
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
73
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
74 @param class_: subclass of QuickWidget
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
75 @return: class actually used to create widget
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
76 """
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
77 try:
1304
1a61b18703c4 quick frontend (quick widgets): class' __name__ method is used for classes_map hash because the use of class directly was causing bugs with pyjamas (difficult to find, several MicroblogPanel instances were added only once in Libervia's TabPanel, hash method seemed buggy)
Goffi <goffi@goffi.org>
parents: 1303
diff changeset
78 # FIXME: we use base_cls.__name__ instead of base_cls directly because pyjamas bugs
1a61b18703c4 quick frontend (quick widgets): class' __name__ method is used for classes_map hash because the use of class directly was causing bugs with pyjamas (difficult to find, several MicroblogPanel instances were added only once in Libervia's TabPanel, hash method seemed buggy)
Goffi <goffi@goffi.org>
parents: 1303
diff changeset
79 # in the second case
1a61b18703c4 quick frontend (quick widgets): class' __name__ method is used for classes_map hash because the use of class directly was causing bugs with pyjamas (difficult to find, several MicroblogPanel instances were added only once in Libervia's TabPanel, hash method seemed buggy)
Goffi <goffi@goffi.org>
parents: 1303
diff changeset
80 cls = classes_map[class_.__name__]
1290
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
81 except KeyError:
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
82 cls = class_
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
83 if cls is None:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
84 raise exceptions.InternalError(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
85 "There is not class registered for {}".format(class_)
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
86 )
1290
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
87 return cls
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
88
2039
6353deb1bd73 quick frontend (quick_widget): getWidgets can now filter on hash (using target), handling recreated widgets too
Goffi <goffi@goffi.org>
parents: 2027
diff changeset
89 def getRootHash(self, hash_):
6353deb1bd73 quick frontend (quick_widget): getWidgets can now filter on hash (using target), handling recreated widgets too
Goffi <goffi@goffi.org>
parents: 2027
diff changeset
90 """Return root hash (i.e. hash without new instance suffix for recreated widgets
6353deb1bd73 quick frontend (quick_widget): getWidgets can now filter on hash (using target), handling recreated widgets too
Goffi <goffi@goffi.org>
parents: 2027
diff changeset
91
6353deb1bd73 quick frontend (quick_widget): getWidgets can now filter on hash (using target), handling recreated widgets too
Goffi <goffi@goffi.org>
parents: 2027
diff changeset
92 @param hash_(immutable): hash of a widget
6353deb1bd73 quick frontend (quick_widget): getWidgets can now filter on hash (using target), handling recreated widgets too
Goffi <goffi@goffi.org>
parents: 2027
diff changeset
93 @return (unicode): root hash (transtyped to unicode)
6353deb1bd73 quick frontend (quick_widget): getWidgets can now filter on hash (using target), handling recreated widgets too
Goffi <goffi@goffi.org>
parents: 2027
diff changeset
94 """
6353deb1bd73 quick frontend (quick_widget): getWidgets can now filter on hash (using target), handling recreated widgets too
Goffi <goffi@goffi.org>
parents: 2027
diff changeset
95 return unicode(hash_).split(NEW_INSTANCE_SUFF)[0]
6353deb1bd73 quick frontend (quick_widget): getWidgets can now filter on hash (using target), handling recreated widgets too
Goffi <goffi@goffi.org>
parents: 2027
diff changeset
96
6353deb1bd73 quick frontend (quick_widget): getWidgets can now filter on hash (using target), handling recreated widgets too
Goffi <goffi@goffi.org>
parents: 2027
diff changeset
97 def getWidgets(self, class_, target=None, profiles=None):
2007
19b9d3f8a6c7 plugin XEP-0085, quick_frontends, primitivus: chat states are working again
Goffi <goffi@goffi.org>
parents: 1995
diff changeset
98 """Get all subclassed widgets instances
1290
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
99
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
100 @param class_: subclass of QuickWidget, same parameter as used in [getOrCreateWidget]
2039
6353deb1bd73 quick frontend (quick_widget): getWidgets can now filter on hash (using target), handling recreated widgets too
Goffi <goffi@goffi.org>
parents: 2027
diff changeset
101 @param target: if not None, construct a hash with this target and filter corresponding widgets
6353deb1bd73 quick frontend (quick_widget): getWidgets can now filter on hash (using target), handling recreated widgets too
Goffi <goffi@goffi.org>
parents: 2027
diff changeset
102 recreated widgets (with new instance suffix) are handled
2007
19b9d3f8a6c7 plugin XEP-0085, quick_frontends, primitivus: chat states are working again
Goffi <goffi@goffi.org>
parents: 1995
diff changeset
103 @param profiles(iterable, None): if not None, filter on instances linked to these profiles
1290
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
104 @return: iterator on widgets
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
105 """
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
106 class_ = self.getRealClass(class_)
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
107 try:
1304
1a61b18703c4 quick frontend (quick widgets): class' __name__ method is used for classes_map hash because the use of class directly was causing bugs with pyjamas (difficult to find, several MicroblogPanel instances were added only once in Libervia's TabPanel, hash method seemed buggy)
Goffi <goffi@goffi.org>
parents: 1303
diff changeset
108 widgets_map = self._widgets[class_.__name__]
1290
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
109 except KeyError:
2007
19b9d3f8a6c7 plugin XEP-0085, quick_frontends, primitivus: chat states are working again
Goffi <goffi@goffi.org>
parents: 1995
diff changeset
110 return
1290
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
111 else:
2039
6353deb1bd73 quick frontend (quick_widget): getWidgets can now filter on hash (using target), handling recreated widgets too
Goffi <goffi@goffi.org>
parents: 2027
diff changeset
112 if target is not None:
6353deb1bd73 quick frontend (quick_widget): getWidgets can now filter on hash (using target), handling recreated widgets too
Goffi <goffi@goffi.org>
parents: 2027
diff changeset
113 filter_hash = unicode(class_.getWidgetHash(target, profiles))
6353deb1bd73 quick frontend (quick_widget): getWidgets can now filter on hash (using target), handling recreated widgets too
Goffi <goffi@goffi.org>
parents: 2027
diff changeset
114 else:
6353deb1bd73 quick frontend (quick_widget): getWidgets can now filter on hash (using target), handling recreated widgets too
Goffi <goffi@goffi.org>
parents: 2027
diff changeset
115 filter_hash = None
6353deb1bd73 quick frontend (quick_widget): getWidgets can now filter on hash (using target), handling recreated widgets too
Goffi <goffi@goffi.org>
parents: 2027
diff changeset
116 for w_hash, w in widgets_map.iteritems():
2007
19b9d3f8a6c7 plugin XEP-0085, quick_frontends, primitivus: chat states are working again
Goffi <goffi@goffi.org>
parents: 1995
diff changeset
117 if profiles is None or w.profiles.intersection(profiles):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
118 if (
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
119 filter_hash is not None
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
120 and self.getRootHash(w_hash) != filter_hash
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
121 ):
2039
6353deb1bd73 quick frontend (quick_widget): getWidgets can now filter on hash (using target), handling recreated widgets too
Goffi <goffi@goffi.org>
parents: 2027
diff changeset
122 continue
2007
19b9d3f8a6c7 plugin XEP-0085, quick_frontends, primitivus: chat states are working again
Goffi <goffi@goffi.org>
parents: 1995
diff changeset
123 yield w
1290
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
124
1938
011eff37e21d quick frontend, primitivus: quickContactList refactored to handle several profiles at once
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
125 def getWidget(self, class_, target=None, profiles=None):
1349
273b044fde6d quick_frontend: add a method getWidget to return a widget without creating it if it doesn't exist
souliane <souliane@mailoo.org>
parents: 1322
diff changeset
126 """Get a widget without creating it if it doesn't exist.
273b044fde6d quick_frontend: add a method getWidget to return a widget without creating it if it doesn't exist
souliane <souliane@mailoo.org>
parents: 1322
diff changeset
127
273b044fde6d quick_frontend: add a method getWidget to return a widget without creating it if it doesn't exist
souliane <souliane@mailoo.org>
parents: 1322
diff changeset
128 @param class_(class): class of the widget to create
273b044fde6d quick_frontend: add a method getWidget to return a widget without creating it if it doesn't exist
souliane <souliane@mailoo.org>
parents: 1322
diff changeset
129 @param target: target depending of the widget, usually a JID instance
1938
011eff37e21d quick frontend, primitivus: quickContactList refactored to handle several profiles at once
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
130 @param profiles (unicode, iterable[unicode], None): profile(s) to use (may or may not be
011eff37e21d quick frontend, primitivus: quickContactList refactored to handle several profiles at once
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
131 used, depending of the widget class)
1349
273b044fde6d quick_frontend: add a method getWidget to return a widget without creating it if it doesn't exist
souliane <souliane@mailoo.org>
parents: 1322
diff changeset
132 @return: a class_ instance or None if the widget doesn't exist
273b044fde6d quick_frontend: add a method getWidget to return a widget without creating it if it doesn't exist
souliane <souliane@mailoo.org>
parents: 1322
diff changeset
133 """
1938
011eff37e21d quick frontend, primitivus: quickContactList refactored to handle several profiles at once
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
134 assert (target is not None) or (profiles is not None)
011eff37e21d quick frontend, primitivus: quickContactList refactored to handle several profiles at once
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
135 if profiles is not None and isinstance(profiles, unicode):
011eff37e21d quick frontend, primitivus: quickContactList refactored to handle several profiles at once
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
136 profiles = [profiles]
1349
273b044fde6d quick_frontend: add a method getWidget to return a widget without creating it if it doesn't exist
souliane <souliane@mailoo.org>
parents: 1322
diff changeset
137 class_ = self.getRealClass(class_)
1938
011eff37e21d quick frontend, primitivus: quickContactList refactored to handle several profiles at once
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
138 hash_ = class_.getWidgetHash(target, profiles)
1349
273b044fde6d quick_frontend: add a method getWidget to return a widget without creating it if it doesn't exist
souliane <souliane@mailoo.org>
parents: 1322
diff changeset
139 try:
273b044fde6d quick_frontend: add a method getWidget to return a widget without creating it if it doesn't exist
souliane <souliane@mailoo.org>
parents: 1322
diff changeset
140 return self._widgets[class_.__name__][hash_]
273b044fde6d quick_frontend: add a method getWidget to return a widget without creating it if it doesn't exist
souliane <souliane@mailoo.org>
parents: 1322
diff changeset
141 except KeyError:
273b044fde6d quick_frontend: add a method getWidget to return a widget without creating it if it doesn't exist
souliane <souliane@mailoo.org>
parents: 1322
diff changeset
142 return None
273b044fde6d quick_frontend: add a method getWidget to return a widget without creating it if it doesn't exist
souliane <souliane@mailoo.org>
parents: 1322
diff changeset
143
1265
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
144 def getOrCreateWidget(self, class_, target, *args, **kwargs):
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
145 """Get an existing widget or create a new one when necessary
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
146
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
147 If the widget is new, self.host.newWidget will be called with it.
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
148 @param class_(class): class of the widget to create
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
149 @param target: target depending of the widget, usually a JID instance
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
150 @param args(list): optional args to create a new instance of class_
1312
9e904f8a094e quick_frontend: getOrCreateWidget callbacks can return another widget
souliane <souliane@mailoo.org>
parents: 1310
diff changeset
151 @param kwargs(dict): optional kwargs to create a new instance of class_
1265
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
152 if 'profile' key is present, it will be popped and put in 'profiles'
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
153 if there is neither 'profile' nor 'profiles', None will be used for 'profiles'
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
154 if 'on_new_widget' is present it can have the following values:
1301
afc57b34c0a3 Quick Frontend (quick_widgets): added on_existing_widget in getOrCreateWidget (see docstring for more infos) + use of constants
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
155 C.WIDGET_NEW [default]: self.host.newWidget will be called on widget creation
1322
1f13a837e4b2 quick_frontend (quick_widgets): revert commit 1319
souliane <souliane@mailoo.org>
parents: 1321
diff changeset
156 [callable]: this method will be called instead of self.host.newWidget
1265
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
157 None: do nothing
1301
afc57b34c0a3 Quick Frontend (quick_widgets): added on_existing_widget in getOrCreateWidget (see docstring for more infos) + use of constants
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
158 if 'on_existing_widget' is present it can have the following values:
afc57b34c0a3 Quick Frontend (quick_widgets): added on_existing_widget in getOrCreateWidget (see docstring for more infos) + use of constants
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
159 C.WIDGET_KEEP [default]: return the existing widget
afc57b34c0a3 Quick Frontend (quick_widgets): added on_existing_widget in getOrCreateWidget (see docstring for more infos) + use of constants
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
160 C.WIDGET_RAISE: raise WidgetAlreadyExistsError
afc57b34c0a3 Quick Frontend (quick_widgets): added on_existing_widget in getOrCreateWidget (see docstring for more infos) + use of constants
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
161 C.WIDGET_RECREATE: create a new widget *WITH A NEW HASH*
2062
e22ab34c68f6 quick fronten (widgets): if a widget is recreated, "recreateArgs" method can be implemented to specify arguments to clone it correctly
Goffi <goffi@goffi.org>
parents: 2047
diff changeset
162 if the existing widget has a "recreateArgs" method, it will be called with args list and kwargs dict
e22ab34c68f6 quick fronten (widgets): if a widget is recreated, "recreateArgs" method can be implemented to specify arguments to clone it correctly
Goffi <goffi@goffi.org>
parents: 2047
diff changeset
163 so the values can be completed to create correctly the new instance
1322
1f13a837e4b2 quick_frontend (quick_widgets): revert commit 1319
souliane <souliane@mailoo.org>
parents: 1321
diff changeset
164 [callable]: this method will be called with existing widget as argument
1265
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
165 if 'force_hash' is present, the hash given in value will be used instead of the one returned by class_.getWidgetHash
1307
9512590dc3d7 quick_ frontend (quick widgets): added explanation in docstring for non special keywords in getOrCreateWidget
Goffi <goffi@goffi.org>
parents: 1304
diff changeset
166 other keys will be used to instanciate class_ if the case happen (e.g. if type_ is present and class_ is a QuickChat subclass,
9512590dc3d7 quick_ frontend (quick widgets): added explanation in docstring for non special keywords in getOrCreateWidget
Goffi <goffi@goffi.org>
parents: 1304
diff changeset
167 it will be used to create a new QuickChat instance).
1265
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
168 @return: a class_ instance, either new or already existing
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
169 """
1290
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
170 cls = self.getRealClass(class_)
1265
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
171
1301
afc57b34c0a3 Quick Frontend (quick_widgets): added on_existing_widget in getOrCreateWidget (see docstring for more infos) + use of constants
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
172 ## arguments management ##
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
173 _args = [self.host, target] + list(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
174 args
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
175 ) or [] # FIXME: check if it's really necessary to use optional args
1265
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
176 _kwargs = kwargs or {}
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
177 if "profiles" in _kwargs and "profile" in _kwargs:
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
178 raise ValueError(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
179 "You can't have 'profile' and 'profiles' keys at the same time"
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
180 )
1265
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
181 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
182 _kwargs["profiles"] = [_kwargs.pop("profile")]
1265
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
183 except KeyError:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
184 if not "profiles" in _kwargs:
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
185 _kwargs["profiles"] = None
1265
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
186
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
187 # on_new_widget tell what to do for the new widget creation
1301
afc57b34c0a3 Quick Frontend (quick_widgets): added on_existing_widget in getOrCreateWidget (see docstring for more infos) + use of constants
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
188 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
189 on_new_widget = _kwargs.pop("on_new_widget")
1301
afc57b34c0a3 Quick Frontend (quick_widgets): added on_existing_widget in getOrCreateWidget (see docstring for more infos) + use of constants
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
190 except KeyError:
afc57b34c0a3 Quick Frontend (quick_widgets): added on_existing_widget in getOrCreateWidget (see docstring for more infos) + use of constants
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
191 on_new_widget = C.WIDGET_NEW
afc57b34c0a3 Quick Frontend (quick_widgets): added on_existing_widget in getOrCreateWidget (see docstring for more infos) + use of constants
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
192
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
193 # on_existing_widget tell what to do when the widget already exists
1301
afc57b34c0a3 Quick Frontend (quick_widgets): added on_existing_widget in getOrCreateWidget (see docstring for more infos) + use of constants
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
194 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
195 on_existing_widget = _kwargs.pop("on_existing_widget")
1301
afc57b34c0a3 Quick Frontend (quick_widgets): added on_existing_widget in getOrCreateWidget (see docstring for more infos) + use of constants
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
196 except KeyError:
afc57b34c0a3 Quick Frontend (quick_widgets): added on_existing_widget in getOrCreateWidget (see docstring for more infos) + use of constants
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
197 on_existing_widget = C.WIDGET_KEEP
afc57b34c0a3 Quick Frontend (quick_widgets): added on_existing_widget in getOrCreateWidget (see docstring for more infos) + use of constants
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
198
afc57b34c0a3 Quick Frontend (quick_widgets): added on_existing_widget in getOrCreateWidget (see docstring for more infos) + use of constants
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
199 ## we get the hash ##
1265
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
200 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
201 hash_ = _kwargs.pop("force_hash")
1265
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
202 except KeyError:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
203 hash_ = cls.getWidgetHash(target, _kwargs["profiles"])
1265
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
204
1304
1a61b18703c4 quick frontend (quick widgets): class' __name__ method is used for classes_map hash because the use of class directly was causing bugs with pyjamas (difficult to find, several MicroblogPanel instances were added only once in Libervia's TabPanel, hash method seemed buggy)
Goffi <goffi@goffi.org>
parents: 1303
diff changeset
205 ## widget creation or retrieval ##
1a61b18703c4 quick frontend (quick widgets): class' __name__ method is used for classes_map hash because the use of class directly was causing bugs with pyjamas (difficult to find, several MicroblogPanel instances were added only once in Libervia's TabPanel, hash method seemed buggy)
Goffi <goffi@goffi.org>
parents: 1303
diff changeset
206
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
207 widgets_map = self._widgets.setdefault(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
208 cls.__name__, {}
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
209 ) # we sorts widgets by classes
1265
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
210 if not cls.SINGLE:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
211 widget = None # if the class is not SINGLE, we always create a new widget
1265
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
212 else:
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
213 try:
1290
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
214 widget = widgets_map[hash_]
1265
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
215 widget.addTarget(target)
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
216 except KeyError:
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
217 widget = None
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
218
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
219 if widget is None:
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
220 # we need to create a new widget
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
221 log.debug(u"Creating new widget for target {} {}".format(target, cls))
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
222 widget = cls(*_args, **_kwargs)
1290
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
223 widgets_map[hash_] = widget
0
goffi@necton2
parents:
diff changeset
224
1301
afc57b34c0a3 Quick Frontend (quick_widgets): added on_existing_widget in getOrCreateWidget (see docstring for more infos) + use of constants
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
225 if on_new_widget == C.WIDGET_NEW:
1265
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
226 self.host.newWidget(widget)
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
227 elif callable(on_new_widget):
1322
1f13a837e4b2 quick_frontend (quick_widgets): revert commit 1319
souliane <souliane@mailoo.org>
parents: 1321
diff changeset
228 on_new_widget(widget)
1265
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
229 else:
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
230 assert on_new_widget is None
1301
afc57b34c0a3 Quick Frontend (quick_widgets): added on_existing_widget in getOrCreateWidget (see docstring for more infos) + use of constants
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
231 else:
afc57b34c0a3 Quick Frontend (quick_widgets): added on_existing_widget in getOrCreateWidget (see docstring for more infos) + use of constants
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
232 # the widget already exists
1308
f079e6ed1e69 quick frontend(quick widgets): added the ability to use a callable with getOrCreateWidget's on_existing_widget
Goffi <goffi@goffi.org>
parents: 1307
diff changeset
233 if on_existing_widget == C.WIDGET_KEEP:
f079e6ed1e69 quick frontend(quick widgets): added the ability to use a callable with getOrCreateWidget's on_existing_widget
Goffi <goffi@goffi.org>
parents: 1307
diff changeset
234 pass
1309
d0d5ba3b4d64 quick frontend(quick widgets): fixed bad condition test in previous commit
Goffi <goffi@goffi.org>
parents: 1308
diff changeset
235 elif on_existing_widget == C.WIDGET_RAISE:
1301
afc57b34c0a3 Quick Frontend (quick_widgets): added on_existing_widget in getOrCreateWidget (see docstring for more infos) + use of constants
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
236 raise WidgetAlreadyExistsError(hash_)
afc57b34c0a3 Quick Frontend (quick_widgets): added on_existing_widget in getOrCreateWidget (see docstring for more infos) + use of constants
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
237 elif on_existing_widget == C.WIDGET_RECREATE:
afc57b34c0a3 Quick Frontend (quick_widgets): added on_existing_widget in getOrCreateWidget (see docstring for more infos) + use of constants
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
238 # we use getOrCreateWidget to recreate the new widget
afc57b34c0a3 Quick Frontend (quick_widgets): added on_existing_widget in getOrCreateWidget (see docstring for more infos) + use of constants
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
239 # /!\ we use args and kwargs and not _args and _kwargs because we need the original args
afc57b34c0a3 Quick Frontend (quick_widgets): added on_existing_widget in getOrCreateWidget (see docstring for more infos) + use of constants
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
240 # we need to get rid of kwargs special options
afc57b34c0a3 Quick Frontend (quick_widgets): added on_existing_widget in getOrCreateWidget (see docstring for more infos) + use of constants
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
241 new_kwargs = kwargs.copy()
afc57b34c0a3 Quick Frontend (quick_widgets): added on_existing_widget in getOrCreateWidget (see docstring for more infos) + use of constants
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
242 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
243 new_kwargs.pop(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
244 "force_hash"
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
245 ) # FIXME: we use pop instead of del here because pyjamas doesn't raise error on del
1301
afc57b34c0a3 Quick Frontend (quick_widgets): added on_existing_widget in getOrCreateWidget (see docstring for more infos) + use of constants
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
246 except KeyError:
afc57b34c0a3 Quick Frontend (quick_widgets): added on_existing_widget in getOrCreateWidget (see docstring for more infos) + use of constants
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
247 pass
afc57b34c0a3 Quick Frontend (quick_widgets): added on_existing_widget in getOrCreateWidget (see docstring for more infos) + use of constants
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
248 else:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
249 raise ValueError(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
250 "force_hash option can't be used with on_existing_widget=RECREATE"
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
251 )
1265
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
252
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
253 new_kwargs["on_new_widget"] = on_new_widget
1995
b5ef9b2b995e quick_frontend (QuickWidget): keep on_new_widget argument when recreating a widget
Goffi <goffi@goffi.org>
parents: 1955
diff changeset
254
1301
afc57b34c0a3 Quick Frontend (quick_widgets): added on_existing_widget in getOrCreateWidget (see docstring for more infos) + use of constants
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
255 # XXX: keep up-to-date if new special kwargs are added (i.e.: delete these keys here)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
256 new_kwargs["on_existing_widget"] = C.WIDGET_RAISE
2062
e22ab34c68f6 quick fronten (widgets): if a widget is recreated, "recreateArgs" method can be implemented to specify arguments to clone it correctly
Goffi <goffi@goffi.org>
parents: 2047
diff changeset
257 try:
e22ab34c68f6 quick fronten (widgets): if a widget is recreated, "recreateArgs" method can be implemented to specify arguments to clone it correctly
Goffi <goffi@goffi.org>
parents: 2047
diff changeset
258 recreateArgs = widget.recreateArgs
e22ab34c68f6 quick fronten (widgets): if a widget is recreated, "recreateArgs" method can be implemented to specify arguments to clone it correctly
Goffi <goffi@goffi.org>
parents: 2047
diff changeset
259 except AttributeError:
e22ab34c68f6 quick fronten (widgets): if a widget is recreated, "recreateArgs" method can be implemented to specify arguments to clone it correctly
Goffi <goffi@goffi.org>
parents: 2047
diff changeset
260 pass
e22ab34c68f6 quick fronten (widgets): if a widget is recreated, "recreateArgs" method can be implemented to specify arguments to clone it correctly
Goffi <goffi@goffi.org>
parents: 2047
diff changeset
261 else:
e22ab34c68f6 quick fronten (widgets): if a widget is recreated, "recreateArgs" method can be implemented to specify arguments to clone it correctly
Goffi <goffi@goffi.org>
parents: 2047
diff changeset
262 recreateArgs(args, new_kwargs)
1301
afc57b34c0a3 Quick Frontend (quick_widgets): added on_existing_widget in getOrCreateWidget (see docstring for more infos) + use of constants
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
263 hash_idx = 1
afc57b34c0a3 Quick Frontend (quick_widgets): added on_existing_widget in getOrCreateWidget (see docstring for more infos) + use of constants
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
264 while True:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
265 new_kwargs["force_hash"] = "{}{}{}".format(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
266 hash_, NEW_INSTANCE_SUFF, hash_idx
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
267 )
1301
afc57b34c0a3 Quick Frontend (quick_widgets): added on_existing_widget in getOrCreateWidget (see docstring for more infos) + use of constants
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
268 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
269 widget = self.getOrCreateWidget(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
270 class_, target, *args, **new_kwargs
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
271 )
1301
afc57b34c0a3 Quick Frontend (quick_widgets): added on_existing_widget in getOrCreateWidget (see docstring for more infos) + use of constants
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
272 except WidgetAlreadyExistsError:
afc57b34c0a3 Quick Frontend (quick_widgets): added on_existing_widget in getOrCreateWidget (see docstring for more infos) + use of constants
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
273 hash_idx += 1
afc57b34c0a3 Quick Frontend (quick_widgets): added on_existing_widget in getOrCreateWidget (see docstring for more infos) + use of constants
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
274 else:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
275 log.debug(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
276 u"Widget already exists, a new one has been recreated with hash {}".format(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
277 new_kwargs["force_hash"]
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
278 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
279 )
1301
afc57b34c0a3 Quick Frontend (quick_widgets): added on_existing_widget in getOrCreateWidget (see docstring for more infos) + use of constants
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
280 break
1308
f079e6ed1e69 quick frontend(quick widgets): added the ability to use a callable with getOrCreateWidget's on_existing_widget
Goffi <goffi@goffi.org>
parents: 1307
diff changeset
281 elif callable(on_existing_widget):
1322
1f13a837e4b2 quick_frontend (quick_widgets): revert commit 1319
souliane <souliane@mailoo.org>
parents: 1321
diff changeset
282 on_existing_widget(widget)
1308
f079e6ed1e69 quick frontend(quick widgets): added the ability to use a callable with getOrCreateWidget's on_existing_widget
Goffi <goffi@goffi.org>
parents: 1307
diff changeset
283 else:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
284 raise exceptions.InternalError(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
285 "Unexpected on_existing_widget value ({})".format(on_existing_widget)
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
286 )
1308
f079e6ed1e69 quick frontend(quick widgets): added the ability to use a callable with getOrCreateWidget's on_existing_widget
Goffi <goffi@goffi.org>
parents: 1307
diff changeset
287
1265
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
288 return widget
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
289
2047
7f5e344938c1 quick_frontend(quick_widgets): deleteWidget can use additional arguments which will be passed to onDelete
Goffi <goffi@goffi.org>
parents: 2043
diff changeset
290 def deleteWidget(self, widget_to_delete, *args, **kwargs):
1303
d3ef3894254d quick frontend (quick_widgets): add deleteWidget method
Goffi <goffi@goffi.org>
parents: 1301
diff changeset
291 """Delete a widget
d3ef3894254d quick frontend (quick_widgets): add deleteWidget method
Goffi <goffi@goffi.org>
parents: 1301
diff changeset
292
2043
f3940f6a3222 quick frontend (widgets): a deletion with onDelete can now be cancelled if onDelete return False
Goffi <goffi@goffi.org>
parents: 2039
diff changeset
293 this method must be called by frontends when a widget is deleted
1303
d3ef3894254d quick frontend (quick_widgets): add deleteWidget method
Goffi <goffi@goffi.org>
parents: 1301
diff changeset
294 widget's onDelete method will be called before deletion
2047
7f5e344938c1 quick_frontend(quick_widgets): deleteWidget can use additional arguments which will be passed to onDelete
Goffi <goffi@goffi.org>
parents: 2043
diff changeset
295 @param widget_to_delete(QuickWidget): widget which need to deleted
7f5e344938c1 quick_frontend(quick_widgets): deleteWidget can use additional arguments which will be passed to onDelete
Goffi <goffi@goffi.org>
parents: 2043
diff changeset
296 @param *args: extra arguments to pass to onDelete
7f5e344938c1 quick_frontend(quick_widgets): deleteWidget can use additional arguments which will be passed to onDelete
Goffi <goffi@goffi.org>
parents: 2043
diff changeset
297 @param *kwargs: extra keywords arguments to pass to onDelete
7f5e344938c1 quick_frontend(quick_widgets): deleteWidget can use additional arguments which will be passed to onDelete
Goffi <goffi@goffi.org>
parents: 2043
diff changeset
298 the extra arguments are not use by QuickFrontend, it's is up to
7f5e344938c1 quick_frontend(quick_widgets): deleteWidget can use additional arguments which will be passed to onDelete
Goffi <goffi@goffi.org>
parents: 2043
diff changeset
299 the frontend to use them or not
1303
d3ef3894254d quick frontend (quick_widgets): add deleteWidget method
Goffi <goffi@goffi.org>
parents: 1301
diff changeset
300 """
2047
7f5e344938c1 quick_frontend(quick_widgets): deleteWidget can use additional arguments which will be passed to onDelete
Goffi <goffi@goffi.org>
parents: 2043
diff changeset
301 if widget_to_delete.onDelete(*args, **kwargs) == False:
2043
f3940f6a3222 quick frontend (widgets): a deletion with onDelete can now be cancelled if onDelete return False
Goffi <goffi@goffi.org>
parents: 2039
diff changeset
302 return
f3940f6a3222 quick frontend (widgets): a deletion with onDelete can now be cancelled if onDelete return False
Goffi <goffi@goffi.org>
parents: 2039
diff changeset
303
f3940f6a3222 quick frontend (widgets): a deletion with onDelete can now be cancelled if onDelete return False
Goffi <goffi@goffi.org>
parents: 2039
diff changeset
304 if self.host.selected_widget == widget_to_delete:
f3940f6a3222 quick frontend (widgets): a deletion with onDelete can now be cancelled if onDelete return False
Goffi <goffi@goffi.org>
parents: 2039
diff changeset
305 self.host.selected_widget = None
1303
d3ef3894254d quick frontend (quick_widgets): add deleteWidget method
Goffi <goffi@goffi.org>
parents: 1301
diff changeset
306
d3ef3894254d quick frontend (quick_widgets): add deleteWidget method
Goffi <goffi@goffi.org>
parents: 1301
diff changeset
307 for widget_map in self._widgets.itervalues():
1410
e2e75c3c7c7b quick_frontend, primitivus: fixes a couple of issues:
souliane <souliane@mailoo.org>
parents: 1396
diff changeset
308 to_delete = set()
1303
d3ef3894254d quick frontend (quick_widgets): add deleteWidget method
Goffi <goffi@goffi.org>
parents: 1301
diff changeset
309 for hash_, widget in widget_map.iteritems():
d3ef3894254d quick frontend (quick_widgets): add deleteWidget method
Goffi <goffi@goffi.org>
parents: 1301
diff changeset
310 if widget_to_delete is widget:
1410
e2e75c3c7c7b quick_frontend, primitivus: fixes a couple of issues:
souliane <souliane@mailoo.org>
parents: 1396
diff changeset
311 to_delete.add(hash_)
e2e75c3c7c7b quick_frontend, primitivus: fixes a couple of issues:
souliane <souliane@mailoo.org>
parents: 1396
diff changeset
312 for hash_ in to_delete:
e2e75c3c7c7b quick_frontend, primitivus: fixes a couple of issues:
souliane <souliane@mailoo.org>
parents: 1396
diff changeset
313 del widget_map[hash_]
1303
d3ef3894254d quick frontend (quick_widgets): add deleteWidget method
Goffi <goffi@goffi.org>
parents: 1301
diff changeset
314
1265
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
315
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
316 class QuickWidget(object):
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
317 """generic widget base"""
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
318
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
319 SINGLE = True # if True, there can be only one widget per target(s)
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
320 PROFILES_MULTIPLE = False # If True, this widget can handle several profiles at once
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
321 PROFILES_ALLOW_NONE = False # If True, this widget can be used without profile
0
goffi@necton2
parents:
diff changeset
322
1265
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
323 def __init__(self, host, target, profiles=None):
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
324 """
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
325 @param host: %(doc_host)s
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
326 @param target: target specific for this widget class
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
327 @param profiles: can be either:
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
328 - (unicode): used when widget class manage a unique profile
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
329 - (iterable): some widget class can manage several profiles, several at once can be specified here
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
330 - None: no profile is managed by this widget class (rare)
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
331 @raise: ValueError when (iterable) or None is given to profiles for a widget class which manage one unique profile.
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
332 """
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
333 self.host = host
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
334 self.targets = set()
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
335 self.addTarget(target)
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
336 self.profiles = set()
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
337 if isinstance(profiles, basestring):
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
338 self.addProfile(profiles)
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
339 elif profiles is None:
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
340 if not self.PROFILES_ALLOW_NONE:
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
341 raise ValueError("profiles can't have a value of None")
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
342 else:
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
343 for profile in profiles:
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
344 self.addProfile(profile)
2027
666b42c957b5 quick frontend (widget): minor profiles check on widget creation
Goffi <goffi@goffi.org>
parents: 2007
diff changeset
345 if not self.profiles:
666b42c957b5 quick frontend (widget): minor profiles check on widget creation
Goffi <goffi@goffi.org>
parents: 2007
diff changeset
346 raise ValueError("no profile found, use None for no profile classes")
1265
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
347
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
348 @property
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
349 def profile(self):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
350 assert (
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
351 len(self.profiles) == 1
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
352 and not self.PROFILES_MULTIPLE
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
353 and not self.PROFILES_ALLOW_NONE
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
354 )
1265
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
355 return list(self.profiles)[0]
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
356
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
357 def addTarget(self, target):
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
358 """Add a target if it doesn't already exists
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
359
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
360 @param target: target to add
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
361 """
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
362 self.targets.add(target)
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
363
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
364 def addProfile(self, profile):
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
365 """Add a profile is if doesn't already exists
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
366
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
367 @param profile: profile to add
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
368 """
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
369 if self.profiles and not self.PROFILES_MULTIPLE:
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
370 raise ValueError("multiple profiles are not allowed")
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
371 self.profiles.add(profile)
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
372
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
373 @staticmethod
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
374 def getWidgetHash(target, profiles):
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
375 """Return the hash associated with this target for this widget class
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
376
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
377 some widget classes can manage several target on the same instance
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
378 (e.g.: a chat widget with multiple resources on the same bare jid),
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
379 this method allow to return a hash associated to one or several targets
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
380 to retrieve the good instance. For example, a widget managing JID targets,
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
381 and all resource of the same bare jid would return the bare jid as hash.
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
382
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
383 @param target: target to check
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
384 @param profiles: profile(s) associated to target, see __init__ docstring
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
385 @return: a hash (can correspond to one or many targets or profiles, depending of widget class)
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
386 """
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
387 return unicode(target) # by defaut, there is one hash for one target
1303
d3ef3894254d quick frontend (quick_widgets): add deleteWidget method
Goffi <goffi@goffi.org>
parents: 1301
diff changeset
388
2047
7f5e344938c1 quick_frontend(quick_widgets): deleteWidget can use additional arguments which will be passed to onDelete
Goffi <goffi@goffi.org>
parents: 2043
diff changeset
389 def onDelete(self, *args, **kwargs):
2043
f3940f6a3222 quick frontend (widgets): a deletion with onDelete can now be cancelled if onDelete return False
Goffi <goffi@goffi.org>
parents: 2039
diff changeset
390 """Called when a widget is being deleted
1410
e2e75c3c7c7b quick_frontend, primitivus: fixes a couple of issues:
souliane <souliane@mailoo.org>
parents: 1396
diff changeset
391
2043
f3940f6a3222 quick frontend (widgets): a deletion with onDelete can now be cancelled if onDelete return False
Goffi <goffi@goffi.org>
parents: 2039
diff changeset
392 @return (boot, None): False to cancel deletion
f3940f6a3222 quick frontend (widgets): a deletion with onDelete can now be cancelled if onDelete return False
Goffi <goffi@goffi.org>
parents: 2039
diff changeset
393 all other value continue deletion
f3940f6a3222 quick frontend (widgets): a deletion with onDelete can now be cancelled if onDelete return False
Goffi <goffi@goffi.org>
parents: 2039
diff changeset
394 """
f3940f6a3222 quick frontend (widgets): a deletion with onDelete can now be cancelled if onDelete return False
Goffi <goffi@goffi.org>
parents: 2039
diff changeset
395 log.debug(u"widget {} deleted".format(self))
f3940f6a3222 quick frontend (widgets): a deletion with onDelete can now be cancelled if onDelete return False
Goffi <goffi@goffi.org>
parents: 2039
diff changeset
396 return True