Properly clean up after disconnecting
[quassel.git] / src / uisupport / toolbaractionprovider.cpp
index 72d9e5e..a560ea6 100644 (file)
 ToolBarActionProvider::ToolBarActionProvider(QObject *parent)
 : NetworkModelController(parent)
 {
-  registerAction(NetworkConnectAll, MainBarIcon("network-connect"), tr("Connect"))->setToolTip(tr("Connect to IRC"));
-  registerAction(NetworkDisconnectAll, MainBarIcon("network-disconnect"), tr("Disconnect"))->setToolTip(tr("Disconnect from IRC"));
+  registerAction(NetworkConnectAll, DesktopIcon("network-connect"), tr("Connect"))->setToolTip(tr("Connect to IRC"));
+  registerAction(NetworkDisconnectAll, DesktopIcon("network-disconnect"), tr("Disconnect"))->setToolTip(tr("Disconnect from IRC"));
 
-  registerAction(BufferPart, MainBarIcon("irc-close-channel"), tr("Part"))->setToolTip(tr("Leave currently selected channel"));
-  registerAction(JoinChannel, MainBarIcon("irc-join-channel"), tr("Join"))->setToolTip(tr("Join a channel"));
+  registerAction(BufferPart, DesktopIcon("irc-close-channel"), tr("Part"))->setToolTip(tr("Leave currently selected channel"));
+  registerAction(JoinChannel, DesktopIcon("irc-join-channel"), tr("Join"))->setToolTip(tr("Join a channel"));
 
-  registerAction(NickQuery, MainBarIcon("mail-message-new"), tr("Query"))->setToolTip(tr("Start a private conversation")); // fix icon
-  registerAction(NickWhois, MainBarIcon("im-user"), tr("Whois"))->setToolTip(tr("Request user information")); // fix icon
+  registerAction(NickQuery, DesktopIcon("mail-message-new"), tr("Query"))->setToolTip(tr("Start a private conversation")); // fix icon
+  registerAction(NickWhois, DesktopIcon("im-user"), tr("Whois"))->setToolTip(tr("Request user information")); // fix icon
 
-  registerAction(NickOp, MainBarIcon("irc-operator"), tr("Op"))->setToolTip(tr("Give operator privileges to user"));
-  registerAction(NickDeop, MainBarIcon("irc-remove-operator"), tr("Deop"))->setToolTip(tr("Take operator privileges from user"));
-  registerAction(NickVoice, MainBarIcon("irc-voice"), tr("Voice"))->setToolTip(tr("Give voice to user"));
-  registerAction(NickDevoice, MainBarIcon("irc-unvoice"), tr("Devoice"))->setToolTip(tr("Take voice from user"));
-  registerAction(NickKick, MainBarIcon("im-kick-user"), tr("Kick"))->setToolTip(tr("Remove user from channel"));
-  registerAction(NickBan, MainBarIcon("im-ban-user"), tr("Ban"))->setToolTip(tr("Ban user from channel"));
-  registerAction(NickKickBan, MainBarIcon("im-ban-kick-user"), tr("Kick/Ban"))->setToolTip(tr("Remove and ban user from channel"));
-
-  //registerAction(ShowChannelList, SmallIcon("format-list-unordered"), tr("Show Channel List"));
-  //registerAction(ShowIgnoreList, tr("Show Ignore List"));
+  registerAction(NickOp, DesktopIcon("irc-operator"), tr("Op"))->setToolTip(tr("Give operator privileges to user"));
+  registerAction(NickDeop, DesktopIcon("irc-remove-operator"), tr("Deop"))->setToolTip(tr("Take operator privileges from user"));
+  registerAction(NickVoice, DesktopIcon("irc-voice"), tr("Voice"))->setToolTip(tr("Give voice to user"));
+  registerAction(NickDevoice, DesktopIcon("irc-unvoice"), tr("Devoice"))->setToolTip(tr("Take voice from user"));
+  registerAction(NickKick, DesktopIcon("im-kick-user"), tr("Kick"))->setToolTip(tr("Remove user from channel"));
+  registerAction(NickBan, DesktopIcon("im-ban-user"), tr("Ban"))->setToolTip(tr("Ban user from channel"));
+  registerAction(NickKickBan, DesktopIcon("im-ban-kick-user"), tr("Kick/Ban"))->setToolTip(tr("Remove and ban user from channel"));
 
   _networksConnectMenu = new QMenu();
   _networksConnectMenu->setSeparatorsCollapsible(false);
@@ -64,15 +61,30 @@ ToolBarActionProvider::ToolBarActionProvider(QObject *parent)
 
   connect(Client::instance(), SIGNAL(networkCreated(NetworkId)), SLOT(networkCreated(NetworkId)));
   connect(Client::instance(), SIGNAL(networkRemoved(NetworkId)), SLOT(networkRemoved(NetworkId)));
+
+  updateStates();
 }
 
 ToolBarActionProvider::~ToolBarActionProvider() {
 
 }
 
+void ToolBarActionProvider::disconnectedFromCore() {
+  _currentBuffer = QModelIndex();
+  updateStates();
+  NetworkModelController::disconnectedFromCore();
+}
+
+void ToolBarActionProvider::updateStates() {
+  action(BufferPart)->setEnabled(_currentBuffer.isValid()
+                              && _currentBuffer.data(NetworkModel::BufferTypeRole) == BufferInfo::ChannelBuffer
+                              && _currentBuffer.data(NetworkModel::ItemActiveRole).toBool());
+
+}
+
 void ToolBarActionProvider::addActions(QToolBar *bar, ToolBarType type) {
   switch(type) {
-    case NetworkToolBar:
+    case MainToolBar:
       bar->addAction(action(NetworkConnectAll));
       bar->addAction(action(NetworkDisconnectAll));
       bar->addAction(action(JoinChannel));
@@ -95,6 +107,37 @@ void ToolBarActionProvider::addActions(QToolBar *bar, ToolBarType type) {
   }
 }
 
+void ToolBarActionProvider::currentBufferChanged(const QModelIndex &index) {
+  _currentBuffer = index;
+  updateStates();
+}
+
+void ToolBarActionProvider::nickSelectionChanged(const QModelIndexList &indexList) {
+  _selectedNicks = indexList;
+  updateStates();
+}
+
+// override those to set indexes right
+void ToolBarActionProvider::handleNetworkAction(ActionType type, QAction *action) {
+  setIndexList(_currentBuffer);
+  NetworkModelController::handleNetworkAction(type, action);
+}
+
+void ToolBarActionProvider::handleBufferAction(ActionType type, QAction *action) {
+  setIndexList(_currentBuffer);
+  NetworkModelController::handleBufferAction(type, action);
+}
+
+void ToolBarActionProvider::handleNickAction(ActionType type, QAction *action) {
+  setIndexList(_selectedNicks);
+  NetworkModelController::handleNickAction(type, action);
+}
+
+void ToolBarActionProvider::handleGeneralAction(ActionType type, QAction *action) {
+  setIndexList(_currentBuffer);
+  NetworkModelController::handleGeneralAction(type, action);
+}
+
 void ToolBarActionProvider::networkCreated(NetworkId id) {
   const Network *net = Client::network(id);
   Action *act = new Action(net->networkName(), this);