1 /*
2  * %kadu copyright begin%
3  * Copyright 2010, 2011 Piotr Galiszewski (piotr.galiszewski@kadu.im)
4  * Copyright 2010, 2012 Piotr Dąbrowski (ultr@ultr.pl)
5  * Copyright 2010, 2011, 2013, 2014 Bartosz Brachaczek (b.brachaczek@gmail.com)
6  * Copyright 2010, 2011, 2012, 2013, 2014 Rafał Przemysław Malinowski (rafal.przemyslaw.malinowski@gmail.com)
7  * %kadu copyright end%
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License as
11  * published by the Free Software Foundation; either version 2 of
12  * the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <http://www.gnu.org/licenses/>.
21  */
22 
23 #include "buddy-delete-window.h"
24 
25 #include "buddies/buddy-additional-data-delete-handler-manager.h"
26 #include "buddies/buddy-additional-data-delete-handler.h"
27 #include "buddies/buddy-manager.h"
28 #include "icons/icons-manager.h"
29 #include "icons/kadu-icon.h"
30 #include "roster/roster.h"
31 
32 #include <QtWidgets/QApplication>
33 #include <QtWidgets/QDialogButtonBox>
34 #include <QtWidgets/QLabel>
35 #include <QtWidgets/QListWidget>
36 #include <QtWidgets/QPushButton>
37 #include <QtWidgets/QStyle>
38 #include <QtWidgets/QVBoxLayout>
39 
BuddyDeleteWindow(const BuddySet & buddiesToDelete,QWidget * parent)40 BuddyDeleteWindow::BuddyDeleteWindow(const BuddySet &buddiesToDelete, QWidget *parent) :
41 		QDialog(parent), BuddiesToDelete(buddiesToDelete)
42 {
43 	setWindowRole("kadu-buddy-delete");
44 
45 	setAttribute(Qt::WA_DeleteOnClose);
46 	setModal(false);
47 	setMaximumHeight(250);
48 }
49 
~BuddyDeleteWindow()50 BuddyDeleteWindow::~BuddyDeleteWindow()
51 {
52 
53 }
54 
setBuddyAdditionalDataDeleteHandlerManager(BuddyAdditionalDataDeleteHandlerManager * buddyAdditionalDataDeleteHandlerManager)55 void BuddyDeleteWindow::setBuddyAdditionalDataDeleteHandlerManager(BuddyAdditionalDataDeleteHandlerManager *buddyAdditionalDataDeleteHandlerManager)
56 {
57 	m_buddyAdditionalDataDeleteHandlerManager = buddyAdditionalDataDeleteHandlerManager;
58 }
59 
setBuddyManager(BuddyManager * buddyManager)60 void BuddyDeleteWindow::setBuddyManager(BuddyManager *buddyManager)
61 {
62 	m_buddyManager = buddyManager;
63 }
64 
setIconsManager(IconsManager * iconsManager)65 void BuddyDeleteWindow::setIconsManager(IconsManager *iconsManager)
66 {
67 	m_iconsManager = iconsManager;
68 }
69 
setRoster(Roster * roster)70 void BuddyDeleteWindow::setRoster(Roster *roster)
71 {
72 	m_roster = roster;
73 }
74 
init()75 void BuddyDeleteWindow::init()
76 {
77 	createGui();
78 }
79 
createGui()80 void BuddyDeleteWindow::createGui()
81 {
82 	QVBoxLayout *mainLayout = new QVBoxLayout(this);
83 
84 	QWidget *topWidget = new QWidget(this);
85 	mainLayout->addWidget(topWidget);
86 
87 	QHBoxLayout *topLayout = new QHBoxLayout(topWidget);
88 
89 	QLabel *iconLabel = new QLabel(topWidget);
90 	iconLabel->setPixmap(m_iconsManager->iconByPath(KaduIcon("dialog-warning")).pixmap(32, 32));
91 	topLayout->addWidget(iconLabel, 0, Qt::AlignTop);
92 
93 	QWidget *contentWidget = new QWidget(topWidget);
94 	topLayout->addWidget(contentWidget);
95 
96 	QVBoxLayout *contentLayout = new QVBoxLayout(contentWidget);
97 
98 	QLabel *messageLabel = new QLabel(tr("The following buddies will be deleted:<br/>%1.<br/>Are you sure?").arg(getBuddiesNames()), contentWidget);
99 	messageLabel->setTextFormat(Qt::RichText);
100 	messageLabel->setWordWrap(true);
101 	contentLayout->addWidget(messageLabel);
102 
103 	QLabel *additionalDataLabel = new QLabel(tr("Please select additional data that will be removed:"), contentWidget);
104 	contentLayout->addWidget(additionalDataLabel);
105 
106 	AdditionalDataListView = new QListWidget(contentWidget);
107 	contentLayout->addWidget(AdditionalDataListView);
108 	connect(AdditionalDataListView, SIGNAL(itemPressed(QListWidgetItem *)), this, SLOT(additionalDataListViewItemPressed(QListWidgetItem *)));
109 	connect(AdditionalDataListView, SIGNAL(itemClicked(QListWidgetItem *)), this, SLOT(additionalDataListViewItemClicked(QListWidgetItem *)));
110 
111 	fillAdditionalDataListView();
112 
113 	QPushButton *deleteButton = new QPushButton(tr("Delete"));
114 	QPushButton *cancelButton = new QPushButton(qApp->style()->standardIcon(QStyle::SP_DialogCancelButton), tr("Cancel"));
115 	cancelButton->setDefault(true);
116 
117 	QDialogButtonBox *buttons = new QDialogButtonBox(this);
118 	buttons->addButton(deleteButton, QDialogButtonBox::DestructiveRole);
119 	buttons->addButton(cancelButton, QDialogButtonBox::RejectRole);
120 	mainLayout->addWidget(buttons);
121 
122 	connect(deleteButton, SIGNAL(clicked(bool)), this, SLOT(accept()));
123 	connect(cancelButton, SIGNAL(clicked(bool)), this, SLOT(reject()));
124 }
125 
fillAdditionalDataListView()126 void BuddyDeleteWindow::fillAdditionalDataListView()
127 {
128 	for (auto handler : m_buddyAdditionalDataDeleteHandlerManager->items())
129 	{
130 		QListWidgetItem *item = new QListWidgetItem(AdditionalDataListView);
131 		item->setText(handler->displayName());
132 		item->setCheckState(Qt::Unchecked);
133 		item->setData(Qt::UserRole, handler->name());
134 
135 		AdditionalDataListView->addItem(item);
136 	}
137 }
138 
getBuddiesNames()139 QString BuddyDeleteWindow::getBuddiesNames()
140 {
141 	QStringList displays;
142 	foreach (const Buddy &buddy, BuddiesToDelete)
143 		displays.append(QString("<b>%1</b>").arg(buddy.display()));
144 
145 	return displays.join(", ");
146 }
147 
deleteBuddy(Buddy buddy)148 void BuddyDeleteWindow::deleteBuddy(Buddy buddy)
149 {
150 	for (int i = 0; i < AdditionalDataListView->count(); i++)
151 	{
152 		QListWidgetItem *item = AdditionalDataListView->item(i);
153 		if (Qt::Checked == item->checkState())
154 		{
155 			QString deleteHandlerName = item->data(Qt::UserRole).toString();
156 			BuddyAdditionalDataDeleteHandler *handler = m_buddyAdditionalDataDeleteHandlerManager->byName(deleteHandlerName);
157 			if (handler)
158 				handler->deleteBuddyAdditionalData(buddy);
159 		}
160 	}
161 
162 	QList<Contact> contacts = buddy.contacts();
163 
164 	// this set owner buddy on all of the contacts
165 	m_buddyManager->removeItem(buddy);
166 
167 	foreach (const Contact &contact, contacts)
168 		m_roster->removeContact(contact);
169 }
170 
accept()171 void BuddyDeleteWindow::accept()
172 {
173 	QDialog::accept();
174 
175 	foreach (const Buddy &buddy, BuddiesToDelete)
176 		deleteBuddy(buddy);
177 	m_buddyManager->ensureStored();
178 }
179 
reject()180 void BuddyDeleteWindow::reject()
181 {
182 	QDialog::reject();
183 }
184 
additionalDataListViewItemPressed(QListWidgetItem * item)185 void BuddyDeleteWindow::additionalDataListViewItemPressed(QListWidgetItem *item)
186 {
187 	if (!item)
188 		return;
189 
190 	// required for proper handling of mouse double clicks
191 	ItemState = item->checkState();
192 }
193 
additionalDataListViewItemClicked(QListWidgetItem * item)194 void BuddyDeleteWindow::additionalDataListViewItemClicked(QListWidgetItem *item)
195 {
196 	if (!item)
197 		return;
198 
199 	ItemState = (ItemState == Qt::Unchecked ? Qt::Checked : Qt::Unchecked);
200 
201 	item->setCheckState(ItemState);
202 }
203 
204 #include "moc_buddy-delete-window.cpp"
205