From 71dfdf25df27af0744e5d5ad2251397fa2bf48e5 Mon Sep 17 00:00:00 2001
From: Tobias Markmann <tm@ayena.de>
Date: Mon, 25 Nov 2013 15:46:38 +0100
Subject: Enable domain-wise (un-)blocking.

Change-Id: I88611bead558ccb58bec8d55372f847479e745ff
License: This patch is BSD-licensed, see Documentation/Licenses/BSD-simplified.txt for details.

diff --git a/Swift/Controllers/BlockListController.cpp b/Swift/Controllers/BlockListController.cpp
index e7bc45d..6bdb513 100644
--- a/Swift/Controllers/BlockListController.cpp
+++ b/Swift/Controllers/BlockListController.cpp
@@ -145,11 +145,13 @@ void BlockListController::handleBlockListChanged() {
 		blockListBeforeEdit = blockListManager_->getBlockList()->getItems();
 
 		foreach (const JID& jid, jidsToBlock) {
-			blockListBeforeEdit.push_back(jid);
+			if (std::find(blockListBeforeEdit.begin(), blockListBeforeEdit.end(), jid) == blockListBeforeEdit.end()) {
+				blockListBeforeEdit.push_back(jid);
+			}
 		}
 
 		foreach (const JID& jid, jidsToUnblock) {
-			blockListBeforeEdit.erase(std::remove(blockListBeforeEdit.begin(), blockListBeforeEdit.end(), jid), blockListBeforeEdit.end());;
+			blockListBeforeEdit.erase(std::remove(blockListBeforeEdit.begin(), blockListBeforeEdit.end(), jid), blockListBeforeEdit.end());
 		}
 
 		blockListEditorWidget_->setCurrentBlockList(blockListBeforeEdit);
diff --git a/Swift/Controllers/Roster/ContactRosterItem.h b/Swift/Controllers/Roster/ContactRosterItem.h
index d9ca8af..6de7909 100644
--- a/Swift/Controllers/Roster/ContactRosterItem.h
+++ b/Swift/Controllers/Roster/ContactRosterItem.h
@@ -36,7 +36,8 @@ class ContactRosterItem : public RosterItem {
 		enum BlockState {
 			BlockingNotSupported,
 			IsBlocked,
-			IsUnblocked
+			IsUnblocked,
+			IsDomainBlocked
 		};
 		
 	public:
diff --git a/Swift/Controllers/Roster/ItemOperations/SetBlockingState.h b/Swift/Controllers/Roster/ItemOperations/SetBlockingState.h
index a8eea09..ddb2c7a 100644
--- a/Swift/Controllers/Roster/ItemOperations/SetBlockingState.h
+++ b/Swift/Controllers/Roster/ItemOperations/SetBlockingState.h
@@ -17,13 +17,22 @@ class RosterItem;
 
 class SetBlockingState : public RosterItemOperation {
 	public:
-		SetBlockingState(const JID& jid, ContactRosterItem::BlockState state, JID::CompareType compareType = JID::WithoutResource) : RosterItemOperation(true, jid), jid_(jid), state_(state), compareType_(compareType) {
+		SetBlockingState(const JID& jid, ContactRosterItem::BlockState state, JID::CompareType compareType = JID::WithoutResource) : RosterItemOperation(!jid.getNode().empty(), jid), jid_(jid), state_(state), compareType_(compareType) {
+			if (state_ == ContactRosterItem::IsBlocked && jid.getNode().empty()) {
+				state_ = ContactRosterItem::IsDomainBlocked;
+			}
 		}
 
 		virtual void operator() (RosterItem* item) const {
 			ContactRosterItem* contact = dynamic_cast<ContactRosterItem*>(item);
-			if (contact && contact->getJID().equals(jid_, compareType_)) {
-				contact->setBlockState(state_);
+			if (jid_.getNode().empty()) {
+				if (contact && contact->getJID().getDomain() == jid_.getDomain()) {
+					contact->setBlockState(state_);
+				}
+			} else {
+				if (contact && contact->getJID().equals(jid_, compareType_)) {
+					contact->setBlockState(state_);
+				}
 			}
 		}
 
diff --git a/Swift/QtUI/Roster/QtRosterWidget.cpp b/Swift/QtUI/Roster/QtRosterWidget.cpp
index 6bf3989..b9bc751 100644
--- a/Swift/QtUI/Roster/QtRosterWidget.cpp
+++ b/Swift/QtUI/Roster/QtRosterWidget.cpp
@@ -8,8 +8,10 @@
 
 #include <QContextMenuEvent>
 #include <QMenu>
+#include <QMessageBox>
 #include <QInputDialog>
 #include <QFileDialog>
+#include <QPushButton>
 
 #include <Swift/Controllers/UIEvents/RequestContactEditorUIEvent.h>
 #include <Swift/Controllers/UIEvents/RemoveRosterItemUIEvent.h>
@@ -62,7 +64,8 @@ void QtRosterWidget::contextMenuEvent(QContextMenuEvent* event) {
 		QAction* showProfileForContact = contextMenu.addAction(tr("Show Profile"));
 
 		QAction* unblockContact = NULL;
-		if (contact->blockState() == ContactRosterItem::IsBlocked) {
+		if (contact->blockState() == ContactRosterItem::IsBlocked ||
+			contact->blockState() == ContactRosterItem::IsDomainBlocked) {
 			unblockContact = contextMenu.addAction(tr("Unblock"));
 		}
 
@@ -97,7 +100,18 @@ void QtRosterWidget::contextMenuEvent(QContextMenuEvent* event) {
 			eventStream_->send(boost::make_shared<ShowProfileForRosterItemUIEvent>(contact->getJID()));
 		}
 		else if (unblockContact && result == unblockContact) {
-			eventStream_->send(boost::make_shared<RequestChangeBlockStateUIEvent>(RequestChangeBlockStateUIEvent::Unblocked, contact->getJID()));
+			if (contact->blockState() == ContactRosterItem::IsDomainBlocked) {
+				QMessageBox messageBox(QMessageBox::Question, tr("Swift"), tr("You've blocked everyone on the %1 domain, including %2.\nYou can't just unblock %2. Do you want to unblock the complete %1 domain?.").arg(P2QSTRING(contact->getJID().getDomain()), P2QSTRING(contact->getJID().toString())), QMessageBox::NoButton, this);
+				QPushButton *unblockDomainButton = messageBox.addButton(tr("Unblock %1 domain").arg(P2QSTRING(contact->getJID().getDomain())), QMessageBox::AcceptRole);
+				messageBox.addButton(QMessageBox::Abort);
+
+				messageBox.exec();
+				if (messageBox.clickedButton() == unblockDomainButton)  {
+					eventStream_->send(boost::make_shared<RequestChangeBlockStateUIEvent>(RequestChangeBlockStateUIEvent::Unblocked, contact->getJID().getDomain()));
+				}
+			} else {
+				eventStream_->send(boost::make_shared<RequestChangeBlockStateUIEvent>(RequestChangeBlockStateUIEvent::Unblocked, contact->getJID()));
+			}
 		}
 		else if (blockContact && result == blockContact) {
 			eventStream_->send(boost::make_shared<RequestChangeBlockStateUIEvent>(RequestChangeBlockStateUIEvent::Blocked, contact->getJID()));
diff --git a/Swift/QtUI/Roster/RosterModel.cpp b/Swift/QtUI/Roster/RosterModel.cpp
index a5341fe..24387e0 100644
--- a/Swift/QtUI/Roster/RosterModel.cpp
+++ b/Swift/QtUI/Roster/RosterModel.cpp
@@ -184,7 +184,8 @@ QString RosterModel::getStatusText(RosterItem* item) const {
 QIcon RosterModel::getPresenceIcon(RosterItem* item) const {
 	ContactRosterItem* contact = dynamic_cast<ContactRosterItem*>(item);
 	if (!contact) return QIcon();
-	if (contact->blockState() == ContactRosterItem::IsBlocked) {
+	if (contact->blockState() == ContactRosterItem::IsBlocked ||
+		contact->blockState() == ContactRosterItem::IsDomainBlocked) {
 		return QIcon(":/icons/stop.png");
 	}
 
-- 
cgit v0.10.2-6-g49f6