Wed, 28 Nov 2012 19:00:40 +0100
Continued with IRC management.
--- a/Network/IRC/IrcNetworkEditDialog.py Tue Nov 27 20:05:59 2012 +0100 +++ b/Network/IRC/IrcNetworkEditDialog.py Wed Nov 28 19:00:40 2012 +0100 @@ -42,13 +42,44 @@ self.__okButton = self.buttonBox.button(QDialogButtonBox.Ok) + self.__network = self.__manager.getNetwork(networkName) + + # network name self.networkEdit.setText(networkName) + + # identities identities = list(sorted(self.__manager.getIdentityNames())) identities[identities.index(IrcIdentity.DefaultIdentityName)] = \ IrcIdentity.DefaultIdentityDisplay self.identityCombo.addItems(identities) + identity = self.__network.getIdentityName() + if identity == IrcIdentity.DefaultIdentityName: + identity = IrcIdentity.DefaultIdentityDisplay + index = self.identityCombo.findText(identity) + if index == -1: + index = 0 + self.identityCombo.setCurrentIndex(index) + + # servers + self.serverCombo.addItems(self.__manager.getServerNames()) + server = self.__network.getServerName() + index = self.serverCombo.findText(server) + if index == -1: + index = 0 + self.serverCombo.setCurrentIndex(index) + + # channels + # TODO: change this to use channel objects + for channelName in sorted(self.__network.getChannelNames()): + channel = self.__network.getChannel(channelName) + if channel.autoJoin(): + autoJoin = self.trUtf8("Yes") + else: + autoJoin = self.trUtf8("No") + QTreeWidgetItem(self.channelList, [channelName, autoJoin]) self.__updateOkButton() + self.on_channelList_itemSelectionChanged() def __updateOkButton(self): """ @@ -105,10 +136,11 @@ @pyqtSlot() def on_editChannelButton_clicked(self): """ - Slot documentation goes here. + Private slot to edit the selected channel. """ - # TODO: not implemented yet - raise NotImplementedError + itm = self.channelList.selectedItems()[0] + if itm: + self.__editChannel(itm.text(0)) @pyqtSlot() def on_deleteChannelButton_clicked(self): @@ -121,15 +153,31 @@ @pyqtSlot(QTreeWidgetItem, int) def on_channelList_itemActivated(self, item, column): """ - Slot documentation goes here. + Private slot to handle the activation of a channel entry. + + @param item reference to the activated item (QTreeWidgetItem) + @param column column the activation occurred in (integer) """ - # TODO: not implemented yet - raise NotImplementedError + self.__editChannel(item.text(0)) @pyqtSlot() def on_channelList_itemSelectionChanged(self): """ - Slot documentation goes here. + Private slot to handle changes of the selection of channels. + """ + selectedItems = self.channelList.selectedItems() + if len(selectedItems) == 0: + enable = False + else: + enable = True + self.editChannelButton.setEnabled(enable) + self.deleteChannelButton.setEnabled(enable) + + def __editChannel(self, name): + """ + Private method to edit a channel. + + @param name name of the channel (string) """ # TODO: not implemented yet raise NotImplementedError
--- a/Network/IRC/IrcNetworkListDialog.py Tue Nov 27 20:05:59 2012 +0100 +++ b/Network/IRC/IrcNetworkListDialog.py Wed Nov 28 19:00:40 2012 +0100 @@ -48,13 +48,7 @@ [self.trUtf8("Server"), "{0}:{1}".format( server.getServer(), server.getPort())]) QTreeWidgetItem(topitm, - [self.trUtf8("Channels"), ", ".join(network.getChannels())]) - if network.autoJoinChannels(): - autoJoin = self.trUtf8("Yes") - else: - autoJoin = self.trUtf8("No") - QTreeWidgetItem(topitm, - [self.trUtf8("Auto-join Channels"), autoJoin]) + [self.trUtf8("Channels"), ", ".join(network.getChannelNames())]) topitm.setExpanded(True) self.__resizeColumns()
--- a/Network/IRC/IrcNetworkManager.py Tue Nov 27 20:05:59 2012 +0100 +++ b/Network/IRC/IrcNetworkManager.py Wed Nov 28 19:00:40 2012 +0100 @@ -231,9 +231,88 @@ return pwConvert(self.__password, encode=False) +class IrcChannel(QObject): + """ + Class implementing the IRC channel object. + """ + def __init__(self, name, parent=None): + """ + Constructor + + @param name name of the network (string) + @param parent reference to the parent object (QObject) + """ + super().__init__(parent) + + self.__name = name + self.__key = "" + self.__autoJoin = False + + def save(self, settings): + """ + Public method to save the channel data. + + @param settings reference to the settings object (QSettings) + """ + # no need to save the channel name because that is the group key + settings.setValue("Key", self.__key) + settings.setValue("AutoJoin", self.__autoJoin) + + def load(self, settings): + """ + Public method to load the network data. + + @param settings reference to the settings object (QSettings) + """ + self.__key = settings.value("Key", "") + self.__autoJoin = Preferences.toBool(settings.value("AutoJoin"), False) + + def getName(self): + """ + Public method to get the channel name. + + @return channel name (string) + """ + return self.__name + + def setKey(self, key): + """ + Public method to set a new channel key. + + @param key channel key to set (string) + """ + self.__key = pwConvert(key, encode=True) + + def getKey(self): + """ + Public method to get the channel key. + + @return channel key (string) + """ + return pwConvert(self.__key, encode=False) + + def autoJoin(self): + """ + Public method to check the auto join status. + + @return flag indicating if the channel should be + joined automatically (boolean) + """ + return self.__autoJoin + + def setAutoJoin(self, enable): + """ + Public method to set the auto join status of the channel. + + @param enable flag indicating if the channel should be + joined automatically (boolean) + """ + self.__autoJoin = enable + + class IrcNetwork(QObject): """ - Class implementing the IRC identity object. + Class implementing the IRC network object. """ def __init__(self, name, parent=None): """ @@ -247,8 +326,7 @@ self.__name = name self.__identity = "" self.__server = "" - self.__channels = [] - self.__autoJoinChannels = False + self.__channels = {} def save(self, settings): """ @@ -259,8 +337,12 @@ # no need to save the network name because that is the group key settings.setValue("Identity", self.__identity) settings.setValue("Server", self.__server) - settings.setValue("Channels", self.__channels) - settings.setValue("AutoJoinChannels", self.__autoJoinChannels) + settings.beginGroup("Channels") + for key in self.__channels: + settings.beginGroup(key) + self.__channels[key].save(settings) + settings.endGroup() + settings.endGroup() def load(self, settings): """ @@ -270,9 +352,13 @@ """ self.__identity = settings.value("Identity", "") self.__server = settings.value("Server", "") - self.__channels = Preferences.toList(settings.value("Channels", [])) - self.__autoJoinChannels = Preferences.toBool( - settings.value("AutoJoinChannels", False)) + settings.beginGroup("Channels") + for key in self.__channels: + self.__channels[key] = IrcChannel(key, self) + settings.beginGroup(key) + self.__channels[key].load(settings) + settings.endGroup() + settings.endGroup() def getName(self): """ @@ -318,35 +404,61 @@ """ Public method to set the list of channels. - @param channels list of channels (list of string) + @param channels list of channels for the network (list of IrcChannel) """ - self.__channels = channels[:] + self.__channels = {} + for channel in channels: + self.__channels[channel.getName()] = channel def getChannels(self): """ + Public method to get the channels. + + @return list of channels for the network (list of IrcChannel) + """ + return list(self.__channels.values()) + + def getChannelNames(self): + """ Public method to get the list of channels. - @return list of channels (list of string) + @return list of channel names (list of string) """ - return self.__channels[:] + return list(sorted(self.__channels.keys())) - def setAutoJoinChannels(self, on): + def getChannel(self, channelName): + """ + Public method to get a channel. + + @param channelName name of the channel to retrieve (string) + @return reference to the channel (IrcChannel) """ - Public method to enable channel auto joining. - - @param on flag indicating to join the channels after connecting - to the server (boolean) + if channelName in self.__channels: + return self.__channels[channelName] + else: + return None + + def setChannel(self, channel): """ - self.__autoJoinChannels = on - - def autoJoinChannels(self): + Public method to set a channel. + + @param channel channel object to set (IrcChannel) """ - Public method to check, if channel auto joining is enabled. + channelName = channel.getName() + if channelName in self.__channels: + channel.setParent(self) + self.__channels[channelName] = channel + + def addChannel(self, channel): + """ + Public method to add a channel. - @return flag indicating to join the channels after connecting - to the server (boolean) + @param channel channel object to add (IrcChannel) """ - return self.__autoJoinChannels + channelName = channel.getName() + if channelName not in self.__channels: + channel.setParent(self) + self.__channels[channelName] = channel class IrcNetworkManager(QObject): @@ -500,7 +612,9 @@ network = IrcNetwork(networkName, self) network.setIdentityName(IrcIdentity.DefaultIdentityName) network.setServerName(serverName) - network.setChannels(["#eric-ide"]) + channel = IrcChannel("#eric-ide", network) + channel.setAutoJoin(False) + network.addChannel(channel) self.__networks[networkName] = network self.dataChanged.emit() @@ -616,6 +730,17 @@ """ self.dataChanged.emit() + def getServerNames(self): + """ + Public method to get a list of all known server names. + + @return list of server names (list of string) + """ + if not self.__loaded: + self.__load() + + return list(sorted(self.__servers.keys())) + def getNetwork(self, name): """ Public method to get a network object. @@ -631,8 +756,7 @@ else: return None - def createNetwork(self, name, identity, server, channels=None, - autoJoinChannels=False): + def createNetwork(self, name, identity, server, channels=None): """ Public method to create a new network object. @@ -641,9 +765,7 @@ this network (IrcIdentity) @param server reference to a server object to associate with this network (IrcServer) - @param channels list of channels for the network (list of string) - @param autoJoinChannels flag indicating to join the channels - automatically (boolean) + @param channels list of channels for the network (list of IrcChannel) @return reference to the created network object (IrcNetwork) """ if not self.__loaded: @@ -655,8 +777,9 @@ network = IrcNetwork(name) network.setIdentityName(identity.getName()) network.setServerName(server.getServer()) + # TODO: change this network.setChannels(channels[:]) - network.setAutoJoinChannels(autoJoinChannels) +## network.setAutoJoinChannels(autoJoinChannels) self.__networks[name] = network self.networkChanged() @@ -689,4 +812,4 @@ if not self.__loaded: self.__load() - return sorted(self.__networks.keys()) + return list(sorted(self.__networks.keys()))
--- a/Network/IRC/IrcNetworkWidget.py Tue Nov 27 20:05:59 2012 +0100 +++ b/Network/IRC/IrcNetworkWidget.py Wed Nov 28 19:00:40 2012 +0100 @@ -45,6 +45,7 @@ self.connectButton.setIcon(UI.PixmapCache.getIcon("ircConnect.png")) self.editButton.setIcon(UI.PixmapCache.getIcon("ircConfigure.png")) self.joinButton.setIcon(UI.PixmapCache.getIcon("ircJoinChannel.png")) + self.joinButton.setEnabled(False) self.__manager = None self.__connected = False @@ -101,7 +102,7 @@ @param txt current text of the channel combo (string) """ - on = bool(txt) + on = bool(txt) and self.__connected self.joinButton.setEnabled(on) @pyqtSlot() @@ -124,7 +125,7 @@ self.nickCombo.clear() self.channelCombo.clear() if network: - channels = network.getChannels() + channels = network.getChannelNames() self.channelCombo.addItems(channels) self.channelCombo.setEnabled(True) identity = self.__manager.getIdentity( @@ -136,6 +137,17 @@ self.channelCombo.setEnabled(False) self.nickCombo.setEnabled(False) + def getNetworkChannels(self): + """ + Public method to get the list of channels associated with the + selected network. + + @return associated channels (list of IrcChannel) + """ + networkName = self.networkCombo.currentText() + network = self.__manager.getNetwork(networkName) + return network.getChannels() + @pyqtSlot(str) def on_nickCombo_activated(self, nick): """ @@ -221,3 +233,6 @@ self.connectButton.setIcon(UI.PixmapCache.getIcon("ircDisconnect.png")) else: self.connectButton.setIcon(UI.PixmapCache.getIcon("ircConnect.png")) + + on = bool(self.channelCombo.currentText()) and self.__connected + self.joinButton.setEnabled(on)
--- a/Network/IRC/IrcWidget.py Tue Nov 27 20:05:59 2012 +0100 +++ b/Network/IRC/IrcWidget.py Wed Nov 28 19:00:40 2012 +0100 @@ -10,7 +10,7 @@ import re import logging -from PyQt4.QtCore import pyqtSlot, Qt, QByteArray +from PyQt4.QtCore import pyqtSlot, Qt, QByteArray, QTimer from PyQt4.QtGui import QWidget, QToolButton, QLabel from PyQt4.QtNetwork import QTcpSocket, QAbstractSocket @@ -164,11 +164,12 @@ dlg = IrcNetworkListDialog(self.__ircNetworkManager, self) dlg.exec_() - def __joinChannel(self, name): + def __joinChannel(self, name, key=""): """ Private slot to join a channel. @param name name of the channel (string) + @param key key of the channel (string) """ # step 1: check, if this channel is already joined for channel in self.__channelList: @@ -187,7 +188,7 @@ self.channelsWidget.addTab(channel, name) self.__channelList.append(channel) - self.__send("JOIN " + name) + self.__send("JOIN " + name) # TODO: add channel key self.__send("MODE " + name) emptyIndex = self.channelsWidget.indexOf(self.__emptyLabel) @@ -464,6 +465,7 @@ if code == 1: # register with services after the welcome message self.__registerWithServices() + QTimer.singleShot(1000, self.__autoJoinChannels) elif code == 5: # extract the user privilege prefixes # ... PREFIX=(ov)@+ ... @@ -488,6 +490,16 @@ if service and password: self.__send("PRIVMSG " + service + " :identify " + password) + def __autoJoinChannels(self): + """ + Private slot to join channels automatically once a server got connected. + """ + for channel in self.networkWidget.getNetworkChannels(): + if channel.autoJoin(): + name = channel.getName() + key = channel.getKey() + self.__joinChannel(name, key) + def __tcpError(self, error): """ Private slot to handle errors reported by the TCP socket.