Network/IRC/IrcNetworkManager.py

changeset 2234
1e33501a0d33
parent 2232
47290dad6d0b
child 2235
266800cbe7cc
equal deleted inserted replaced
2233:26b34180a943 2234:1e33501a0d33
229 @return password (string) 229 @return password (string)
230 """ 230 """
231 return pwConvert(self.__password, encode=False) 231 return pwConvert(self.__password, encode=False)
232 232
233 233
234 class IrcChannel(QObject):
235 """
236 Class implementing the IRC channel object.
237 """
238 def __init__(self, name, parent=None):
239 """
240 Constructor
241
242 @param name name of the network (string)
243 @param parent reference to the parent object (QObject)
244 """
245 super().__init__(parent)
246
247 self.__name = name
248 self.__key = ""
249 self.__autoJoin = False
250
251 def save(self, settings):
252 """
253 Public method to save the channel data.
254
255 @param settings reference to the settings object (QSettings)
256 """
257 # no need to save the channel name because that is the group key
258 settings.setValue("Key", self.__key)
259 settings.setValue("AutoJoin", self.__autoJoin)
260
261 def load(self, settings):
262 """
263 Public method to load the network data.
264
265 @param settings reference to the settings object (QSettings)
266 """
267 self.__key = settings.value("Key", "")
268 self.__autoJoin = Preferences.toBool(settings.value("AutoJoin"), False)
269
270 def getName(self):
271 """
272 Public method to get the channel name.
273
274 @return channel name (string)
275 """
276 return self.__name
277
278 def setKey(self, key):
279 """
280 Public method to set a new channel key.
281
282 @param key channel key to set (string)
283 """
284 self.__key = pwConvert(key, encode=True)
285
286 def getKey(self):
287 """
288 Public method to get the channel key.
289
290 @return channel key (string)
291 """
292 return pwConvert(self.__key, encode=False)
293
294 def autoJoin(self):
295 """
296 Public method to check the auto join status.
297
298 @return flag indicating if the channel should be
299 joined automatically (boolean)
300 """
301 return self.__autoJoin
302
303 def setAutoJoin(self, enable):
304 """
305 Public method to set the auto join status of the channel.
306
307 @param enable flag indicating if the channel should be
308 joined automatically (boolean)
309 """
310 self.__autoJoin = enable
311
312
234 class IrcNetwork(QObject): 313 class IrcNetwork(QObject):
235 """ 314 """
236 Class implementing the IRC identity object. 315 Class implementing the IRC network object.
237 """ 316 """
238 def __init__(self, name, parent=None): 317 def __init__(self, name, parent=None):
239 """ 318 """
240 Constructor 319 Constructor
241 320
245 super().__init__(parent) 324 super().__init__(parent)
246 325
247 self.__name = name 326 self.__name = name
248 self.__identity = "" 327 self.__identity = ""
249 self.__server = "" 328 self.__server = ""
250 self.__channels = [] 329 self.__channels = {}
251 self.__autoJoinChannels = False
252 330
253 def save(self, settings): 331 def save(self, settings):
254 """ 332 """
255 Public method to save the network data. 333 Public method to save the network data.
256 334
257 @param settings reference to the settings object (QSettings) 335 @param settings reference to the settings object (QSettings)
258 """ 336 """
259 # no need to save the network name because that is the group key 337 # no need to save the network name because that is the group key
260 settings.setValue("Identity", self.__identity) 338 settings.setValue("Identity", self.__identity)
261 settings.setValue("Server", self.__server) 339 settings.setValue("Server", self.__server)
262 settings.setValue("Channels", self.__channels) 340 settings.beginGroup("Channels")
263 settings.setValue("AutoJoinChannels", self.__autoJoinChannels) 341 for key in self.__channels:
342 settings.beginGroup(key)
343 self.__channels[key].save(settings)
344 settings.endGroup()
345 settings.endGroup()
264 346
265 def load(self, settings): 347 def load(self, settings):
266 """ 348 """
267 Public method to load the network data. 349 Public method to load the network data.
268 350
269 @param settings reference to the settings object (QSettings) 351 @param settings reference to the settings object (QSettings)
270 """ 352 """
271 self.__identity = settings.value("Identity", "") 353 self.__identity = settings.value("Identity", "")
272 self.__server = settings.value("Server", "") 354 self.__server = settings.value("Server", "")
273 self.__channels = Preferences.toList(settings.value("Channels", [])) 355 settings.beginGroup("Channels")
274 self.__autoJoinChannels = Preferences.toBool( 356 for key in self.__channels:
275 settings.value("AutoJoinChannels", False)) 357 self.__channels[key] = IrcChannel(key, self)
358 settings.beginGroup(key)
359 self.__channels[key].load(settings)
360 settings.endGroup()
361 settings.endGroup()
276 362
277 def getName(self): 363 def getName(self):
278 """ 364 """
279 Public method to get the network name. 365 Public method to get the network name.
280 366
316 402
317 def setChannels(self, channels): 403 def setChannels(self, channels):
318 """ 404 """
319 Public method to set the list of channels. 405 Public method to set the list of channels.
320 406
321 @param channels list of channels (list of string) 407 @param channels list of channels for the network (list of IrcChannel)
322 """ 408 """
323 self.__channels = channels[:] 409 self.__channels = {}
410 for channel in channels:
411 self.__channels[channel.getName()] = channel
324 412
325 def getChannels(self): 413 def getChannels(self):
326 """ 414 """
415 Public method to get the channels.
416
417 @return list of channels for the network (list of IrcChannel)
418 """
419 return list(self.__channels.values())
420
421 def getChannelNames(self):
422 """
327 Public method to get the list of channels. 423 Public method to get the list of channels.
328 424
329 @return list of channels (list of string) 425 @return list of channel names (list of string)
330 """ 426 """
331 return self.__channels[:] 427 return list(sorted(self.__channels.keys()))
332 428
333 def setAutoJoinChannels(self, on): 429 def getChannel(self, channelName):
334 """ 430 """
335 Public method to enable channel auto joining. 431 Public method to get a channel.
336 432
337 @param on flag indicating to join the channels after connecting 433 @param channelName name of the channel to retrieve (string)
338 to the server (boolean) 434 @return reference to the channel (IrcChannel)
339 """ 435 """
340 self.__autoJoinChannels = on 436 if channelName in self.__channels:
341 437 return self.__channels[channelName]
342 def autoJoinChannels(self): 438 else:
343 """ 439 return None
344 Public method to check, if channel auto joining is enabled. 440
345 441 def setChannel(self, channel):
346 @return flag indicating to join the channels after connecting 442 """
347 to the server (boolean) 443 Public method to set a channel.
348 """ 444
349 return self.__autoJoinChannels 445 @param channel channel object to set (IrcChannel)
446 """
447 channelName = channel.getName()
448 if channelName in self.__channels:
449 channel.setParent(self)
450 self.__channels[channelName] = channel
451
452 def addChannel(self, channel):
453 """
454 Public method to add a channel.
455
456 @param channel channel object to add (IrcChannel)
457 """
458 channelName = channel.getName()
459 if channelName not in self.__channels:
460 channel.setParent(self)
461 self.__channels[channelName] = channel
350 462
351 463
352 class IrcNetworkManager(QObject): 464 class IrcNetworkManager(QObject):
353 """ 465 """
354 Class implementing the IRC identity object. 466 Class implementing the IRC identity object.
498 # network 610 # network
499 networkName = "Freenode" 611 networkName = "Freenode"
500 network = IrcNetwork(networkName, self) 612 network = IrcNetwork(networkName, self)
501 network.setIdentityName(IrcIdentity.DefaultIdentityName) 613 network.setIdentityName(IrcIdentity.DefaultIdentityName)
502 network.setServerName(serverName) 614 network.setServerName(serverName)
503 network.setChannels(["#eric-ide"]) 615 channel = IrcChannel("#eric-ide", network)
616 channel.setAutoJoin(False)
617 network.addChannel(channel)
504 self.__networks[networkName] = network 618 self.__networks[networkName] = network
505 619
506 self.dataChanged.emit() 620 self.dataChanged.emit()
507 621
508 def getIdentity(self, name, create=False): 622 def getIdentity(self, name, create=False):
614 """ 728 """
615 Public method to indicate a change of a server object. 729 Public method to indicate a change of a server object.
616 """ 730 """
617 self.dataChanged.emit() 731 self.dataChanged.emit()
618 732
733 def getServerNames(self):
734 """
735 Public method to get a list of all known server names.
736
737 @return list of server names (list of string)
738 """
739 if not self.__loaded:
740 self.__load()
741
742 return list(sorted(self.__servers.keys()))
743
619 def getNetwork(self, name): 744 def getNetwork(self, name):
620 """ 745 """
621 Public method to get a network object. 746 Public method to get a network object.
622 747
623 @param name name of the network (string) 748 @param name name of the network (string)
629 if name in self.__networks: 754 if name in self.__networks:
630 return self.__networks[name] 755 return self.__networks[name]
631 else: 756 else:
632 return None 757 return None
633 758
634 def createNetwork(self, name, identity, server, channels=None, 759 def createNetwork(self, name, identity, server, channels=None):
635 autoJoinChannels=False):
636 """ 760 """
637 Public method to create a new network object. 761 Public method to create a new network object.
638 762
639 @param name name of the network (string) 763 @param name name of the network (string)
640 @param identity reference to an identity object to associate with 764 @param identity reference to an identity object to associate with
641 this network (IrcIdentity) 765 this network (IrcIdentity)
642 @param server reference to a server object to associate with this 766 @param server reference to a server object to associate with this
643 network (IrcServer) 767 network (IrcServer)
644 @param channels list of channels for the network (list of string) 768 @param channels list of channels for the network (list of IrcChannel)
645 @param autoJoinChannels flag indicating to join the channels
646 automatically (boolean)
647 @return reference to the created network object (IrcNetwork) 769 @return reference to the created network object (IrcNetwork)
648 """ 770 """
649 if not self.__loaded: 771 if not self.__loaded:
650 self.__load() 772 self.__load()
651 773
653 return None 775 return None
654 776
655 network = IrcNetwork(name) 777 network = IrcNetwork(name)
656 network.setIdentityName(identity.getName()) 778 network.setIdentityName(identity.getName())
657 network.setServerName(server.getServer()) 779 network.setServerName(server.getServer())
780 # TODO: change this
658 network.setChannels(channels[:]) 781 network.setChannels(channels[:])
659 network.setAutoJoinChannels(autoJoinChannels) 782 ## network.setAutoJoinChannels(autoJoinChannels)
660 self.__networks[name] = network 783 self.__networks[name] = network
661 784
662 self.networkChanged() 785 self.networkChanged()
663 786
664 return network 787 return network
687 @return list of network names (list of string) 810 @return list of network names (list of string)
688 """ 811 """
689 if not self.__loaded: 812 if not self.__loaded:
690 self.__load() 813 self.__load()
691 814
692 return sorted(self.__networks.keys()) 815 return list(sorted(self.__networks.keys()))

eric ide

mercurial