notificationUI.py (12582B)
1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\packages\notifications\client\notificationUI.py 2 from __future__ import print_function 3 import itertoolsext 4 from carbon.common.script.sys.service import CoreService 5 from notifications.client.bountyNotificationAdapter import BountyNotificationAdapter 6 from notifications.client.ccpNotificationAdapter import CCPNotificationAdapter 7 from notifications.client.essNotificationAdapter import ESSNotificationAdapter 8 from notifications.client.contactNotificationAdapter import ContactNotificationAdapter 9 from notifications.client.generator.notificationGenerator import NotificationGenerator 10 from notifications.client.notificationCenter import NotificationCenter 11 from notifications.client.notificationSettings.notificationSettingHandler import NotificationSettingHandler 12 from notifications.client.oreMinedNotificationAdapter import OreMinedNotificationAdapter 13 from notifications.client.rwPirateKilledNotificationAdapter import PirateKilledNotificationAdapter 14 from notifications.client.shutdownNotificationAdapter import ShutdownNotificationAdapter 15 16 class NotificationUIService(CoreService): 17 __guid__ = 'svc.notificationUIService' 18 __notifyevents__ = ['OnNewNotificationReceived', 19 'OnSetDevice', 20 'OnLocalNotificationSettingChanged', 21 'OnNotificationReadOutside'] 22 __startupdependencies__ = ['settings', 23 'mailSvc', 24 'notificationSvc', 25 'logger'] 26 27 def __init__(self): 28 super(NotificationUIService, self).__init__() 29 self.forceAlwaysEnabled = False 30 31 def Run(self, memstream = None): 32 self.notificationCenter = None 33 self.notificationCache = None 34 self.pendingNotificationCache = [] 35 self.cacheFillThread = None 36 self.unreadCounter = 0 37 self.notificationSettings = NotificationSettingHandler() 38 self.notificationGenerator = NotificationGenerator() 39 self.isEnabled = self.ShouldEnableNotifications() 40 self.shouldShowOnEnable = False 41 self.__developerMode = False 42 self.adapterRegistry = [ContactNotificationAdapter(), 43 CCPNotificationAdapter(), 44 ESSNotificationAdapter(), 45 ShutdownNotificationAdapter(), 46 OreMinedNotificationAdapter(sm.GetService('logger')), 47 BountyNotificationAdapter(sm.GetService('logger')), 48 PirateKilledNotificationAdapter(sm.GetService('logger'))] 49 for adapter in self.adapterRegistry: 50 if getattr(adapter, '__notifyevents__', None): 51 sm.RegisterNotify(adapter) 52 53 self.lastSeenMessageTime = self.notificationSettings.GetLastSeenTime() 54 self.lastHistoryTimeCleanTime = self.notificationSettings.GetLastHistoryTimeCleanTime() 55 self.lastSeenNotificationId = self.notificationSettings.GetLastSeenNotificationId() 56 self.lastClearedNotificationId = self.notificationSettings.GetLastClearedNotificationId() 57 58 def SetNotificationsAlwaysEnabled(self, alwaysEnabled): 59 self.forceAlwaysEnabled = alwaysEnabled 60 61 def AreNotificationsAlwaysEnabled(self): 62 return self.forceAlwaysEnabled 63 64 def ShouldEnableNotifications(self): 65 return self.notificationSettings.GetNotificationWidgetEnabled() or self.AreNotificationsAlwaysEnabled() 66 67 def OnLocalNotificationSettingChanged(self): 68 self.UpdateEnabledStatus() 69 70 def Stop(self, memStream = None): 71 CoreService.Stop(self, memStream) 72 for adapter in self.adapterRegistry: 73 if getattr(adapter, '__notifyevents__', None): 74 sm.UnregisterNotify(adapter) 75 if hasattr(adapter, 'teardown'): 76 adapter.teardown() 77 78 def PlaySound(self, eventName): 79 if self.notificationSettings.GetNotificationSoundEnabled(): 80 sm.GetService('audio').SendUIEvent(eventName) 81 82 def ToggleDeveloperMode(self): 83 print('TogglingDeveloperMode') 84 self.__developerMode = not self.__developerMode 85 print('DeveloperMode is ' + str(self.__developerMode)) 86 87 def IsDeveloperMode(self): 88 return self.__developerMode 89 90 def IsEnabled(self): 91 return self.isEnabled 92 93 def IsAvailable(self): 94 return self.notificationCenter is not None 95 96 def OnNewNotificationReceived(self, notification): 97 if self.isEnabled: 98 self._InsertNotification(notification) 99 self._DisplayNotificationIfPossible(notification) 100 101 def SpawnFakeNotifications(self): 102 self.notificationGenerator.Start() 103 104 def ResetUnreadCounter(self): 105 if self.notificationCache and len(self.notificationCache) > 0: 106 self._SetLastReadTime(self.notificationCache[0].created) 107 self.unreadCounter = 0 108 self._UpdateCounter() 109 110 def _UpdateCounter(self): 111 if self.notificationCenter: 112 self.notificationCenter.SetBadgeValue(self.unreadCounter) 113 if self.unreadCounter == 0: 114 self.notificationCenter.hideBadge() 115 else: 116 self.notificationCenter.showBadge() 117 118 def _IncrementCounter(self): 119 self.unreadCounter = self.unreadCounter + 1 120 self._UpdateCounter() 121 122 def _SetLastReadTime(self, lastReadTimeStamp): 123 self.lastSeenMessageTime = lastReadTimeStamp 124 self.notificationSettings.SetLastSeenTime(self.lastSeenMessageTime) 125 126 def _ShouldIncrementCounterForNotification(self, notification): 127 notificationSetting = self.notificationSettings.LoadSettings() 128 specificSetting = notificationSetting.get(notification.typeID) 129 if specificSetting and specificSetting.showAtAll: 130 return True 131 return False 132 133 def _IncrementCounterIfIShould(self, notification): 134 if self._ShouldIncrementCounterForNotification(notification): 135 self._IncrementCounter() 136 137 def _InsertNotification(self, notification): 138 self._IncrementCounterIfIShould(notification) 139 if self.notificationCache is None: 140 self.pendingNotificationCache.append(notification) 141 else: 142 self.notificationCache.insert(0, notification) 143 144 def _DisplayNotificationIfPossible(self, notification): 145 if self.notificationCenter: 146 self.notificationCenter.DisplaySingleNotification(notification) 147 148 def ClearHistory(self): 149 self.lastHistoryTimeCleanTime = self.lastSeenMessageTime 150 self.lastClearedNotificationId = self.lastSeenNotificationId 151 self.notificationSettings.SetLastHistoryCleanTime(self.lastHistoryTimeCleanTime) 152 self.notificationSettings.SetLastClearedNotificationId(self.lastClearedNotificationId) 153 self.ClearCache() 154 155 def UnClearHistory(self): 156 self.lastHistoryTimeCleanTime = 0 157 self.lastClearedNotificationId = 0 158 self.lastSeenNotificationId = 0 159 self.notificationSettings.SetLastHistoryCleanTime(self.lastHistoryTimeCleanTime) 160 self.notificationSettings.SetLastClearedNotificationId(self.lastClearedNotificationId) 161 self.SaveLastSeenNotificationId() 162 self.ClearCache() 163 164 def SaveLastSeenNotificationId(self): 165 self.notificationSettings.SetLastSeenNotificationId(self.lastSeenNotificationId) 166 167 def SetLastSeenNotificationId(self, notificationId): 168 self.lastSeenNotificationId = notificationId 169 self.SaveLastSeenNotificationId() 170 171 def OnSetDevice(self): 172 pass 173 174 def _IsCacheInitialized(self): 175 return self.notificationCache is not None 176 177 def _CheckAndFillCache(self): 178 if self.notificationCache is None: 179 self.notificationCache = self._NotificationProvider(sortThem=False) 180 tempNotifications = self.pendingNotificationCache 181 for pendingNotification in tempNotifications: 182 for notification in self.notificationCache: 183 if pendingNotification.notificationID == notification.notificationID and pendingNotification in self.pendingNotificationCache: 184 self.pendingNotificationCache.remove(pendingNotification) 185 186 self.notificationCache.extend(self.pendingNotificationCache) 187 self.pendingNotificationCache = [] 188 self._SortNotifications(self.notificationCache) 189 counter = 0 190 for notification in self.notificationCache: 191 if notification.created > self.lastSeenMessageTime and self._ShouldIncrementCounterForNotification(notification): 192 counter += 1 193 self.lastSeenNotificationId = max(notification.notificationID, self.lastSeenNotificationId) 194 195 self.SaveLastSeenNotificationId() 196 self.unreadCounter = counter 197 self._UpdateCounter() 198 self._NotifyInitialized() 199 200 def OnNotificationReadOutside(self, notificationID): 201 if not self.notificationCache: 202 return 203 n = itertoolsext.first_or_default(self.notificationCache, lambda x: x.notificationID == notificationID, None) 204 if n: 205 n.processed = True 206 207 def _NotifyInitialized(self): 208 if self.notificationCenter: 209 self.notificationCenter.SetCacheIsInitialized(True) 210 211 def _NotifyUnInitialized(self): 212 if self.notificationCenter: 213 self.notificationCenter.SetCacheIsInitialized(False) 214 215 def UpdateEnabledStatus(self): 216 if self.ShouldEnableNotifications(): 217 self.SetEnabled(True) 218 else: 219 self.SetEnabled(False) 220 221 def SetEnabled(self, value): 222 if value is self.isEnabled: 223 return 224 if self.shouldShowOnEnable and value is True: 225 self.isEnabled = True 226 self.Show() 227 else: 228 self.TearDown() 229 self.isEnabled = value 230 231 def _StartCheckAndFillCacheThread(self): 232 import uthread 233 uthread.new(self._CheckAndFillCache) 234 235 def Show(self): 236 if session.charid is None: 237 return 238 self.shouldShowOnEnable = True 239 if self.isEnabled: 240 self._ConstructNotificationCenter() 241 sm.ScatterEvent('OnNotificationUiReady') 242 self._StartCheckAndFillCacheThread() 243 else: 244 self.UpdateEnabledStatus() 245 246 def ToggleEnabledFlag(self): 247 if self.isEnabled and not self.AreNotificationsAlwaysEnabled(): 248 self.Hide() 249 self.isEnabled = False 250 else: 251 self.isEnabled = True 252 self.Show() 253 254 def Hide(self): 255 self.shouldShowOnEnable = False 256 self.TearDown() 257 258 def TearDown(self): 259 if self.isEnabled: 260 self._TearDownNotificationCenter() 261 self.ClearCache(refillCache=False) 262 263 def _TearDownNotificationCenter(self): 264 if self.notificationCenter: 265 self.notificationCenter.deconstruct() 266 self.notificationCenter = None 267 268 def _OnNotificationCenterReconstructed(self): 269 self._UpdateCounter() 270 271 def _ConstructNotificationCenter(self): 272 if self.isEnabled: 273 self._TearDownNotificationCenter() 274 self.notificationCenter = NotificationCenter(onReconstructCallBack=self._OnNotificationCenterReconstructed, developerMode=self.IsDeveloperMode(), audioCallback=self.PlaySound) 275 self.notificationCenter.Construct(notificationProviderFunction=self._PersonalCachedProvider) 276 self.notificationCenter.SetCacheIsInitialized(self._IsCacheInitialized()) 277 self._UpdateCounter() 278 279 def _PersonalCachedProvider(self): 280 if self.notificationCache is None: 281 self._CheckAndFillCache() 282 else: 283 self.ResetUnreadCounter() 284 return self.notificationCache 285 286 def _SortNotifications(self, notificationList): 287 notificationList.sort(key=lambda notification: notification.created, reverse=True) 288 289 def _NotificationProvider(self, sortThem = True): 290 from notifications.client.development.skillHistoryProvider import SkillHistoryProvider 291 skillNotifications = SkillHistoryProvider(onlyShowAfterDate=self.lastHistoryTimeCleanTime).provide() 292 restOfNotifications = sm.GetService('notificationSvc').GetAllFormattedNotifications(fromID=self.lastClearedNotificationId) 293 sortedList = skillNotifications + restOfNotifications 294 if sortThem: 295 self._SortNotifications(sortedList) 296 return sortedList 297 298 def ClearCache(self, refillCache = True): 299 self.notificationCache = None 300 sm.GetService('notificationSvc').ClearAllNotificationsCache() 301 self._NotifyUnInitialized() 302 if refillCache: 303 self._CheckAndFillCache()