1 /*
2  * %kadu copyright begin%
3  * Copyright 2012 Wojciech Treter (juzefwt@gmail.com)
4  * Copyright 2011, 2013, 2014 Bartosz Brachaczek (b.brachaczek@gmail.com)
5  * Copyright 2011, 2012, 2013, 2014 Rafał Przemysław Malinowski (rafal.przemyslaw.malinowski@gmail.com)
6  * %kadu copyright end%
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 2 of
11  * the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 #include <QtWidgets/QApplication>
23 
24 #include "buddies/buddy-set.h"
25 #include "chat/chat.h"
26 #include "chat/type/chat-type-manager.h"
27 #include "core/injected-factory.h"
28 #include "core/myself.h"
29 #include "gui/actions/action-context.h"
30 #include "gui/actions/action.h"
31 #include "gui/windows/buddy-delete-window.h"
32 #include "gui/windows/kadu-window.h"
33 #include "gui/windows/message-dialog.h"
34 #include "icons/icons-manager.h"
35 
36 #include "delete-talkable-action.h"
37 
DeleteTalkableAction(QObject * parent)38 DeleteTalkableAction::DeleteTalkableAction(QObject *parent) :
39 		ActionDescription(parent)
40 {
41 	// TODO: TypeChat | TypeUser or TypeTalkables
42 	setType(TypeUser);
43 	setName("deleteUsersAction");
44 	setIcon(KaduIcon("edit-delete"));
45 	setShortcut("kadu_deleteuser");
46 	setText(tr("Delete Buddy"));
47 }
48 
~DeleteTalkableAction()49 DeleteTalkableAction::~DeleteTalkableAction()
50 {
51 }
52 
setChatTypeManager(ChatTypeManager * chatTypeManager)53 void DeleteTalkableAction::setChatTypeManager(ChatTypeManager *chatTypeManager)
54 {
55 	m_chatTypeManager = chatTypeManager;
56 }
57 
setIconsManager(IconsManager * iconsManager)58 void DeleteTalkableAction::setIconsManager(IconsManager *iconsManager)
59 {
60 	m_iconsManager = iconsManager;
61 }
62 
setMyself(Myself * myself)63 void DeleteTalkableAction::setMyself(Myself *myself)
64 {
65 	m_myself = myself;
66 }
67 
init()68 void DeleteTalkableAction::init()
69 {
70 	registerAction(actionsRegistry());
71 }
72 
actionRole(ActionContext * context) const73 int DeleteTalkableAction::actionRole(ActionContext *context) const
74 {
75 	if (context->roles().contains(ContactRole)) // we wont allow deleting contacts
76 		return -1;
77 	if (context->roles().contains(ChatRole))
78 		return ChatRole;
79 	if (context->roles().contains(BuddyRole))
80 		return BuddyRole;
81 	return -1;
82 }
83 
actionChat(ActionContext * context) const84 Chat DeleteTalkableAction::actionChat(ActionContext *context) const
85 {
86 	return context->chat();
87 }
88 
actionBuddy(ActionContext * context) const89 Buddy DeleteTalkableAction::actionBuddy(ActionContext *context) const
90 {
91 	if (context->buddies().size())
92 		return context->buddies().toBuddy();
93 	else
94 		return context->contacts().toContact().ownerBuddy();
95 }
96 
setChatActionTitleAndIcon(Action * action)97 void DeleteTalkableAction::setChatActionTitleAndIcon(Action *action)
98 {
99 	action->setText(QCoreApplication::translate("KaduWindowActions", "Delete Chat"));
100 }
101 
setBuddyActionTitleAndIcon(Action * action)102 void DeleteTalkableAction::setBuddyActionTitleAndIcon(Action *action)
103 {
104 	action->setText(QCoreApplication::translate("KaduWindowActions", "Delete Buddy"));
105 }
106 
updateChatActionState(Action * action)107 void DeleteTalkableAction::updateChatActionState(Action *action)
108 {
109 	setChatActionTitleAndIcon(action);
110 
111 	auto const &chat = actionChat(action->context());
112 	auto chatType = m_chatTypeManager->chatType(chat.type());
113 	action->setEnabled(chat && (!chatType || (chatType->name() != "Contact" && !chat.display().isEmpty())));
114 }
115 
updateBuddyActionState(Action * action)116 void DeleteTalkableAction::updateBuddyActionState(Action *action)
117 {
118 	setBuddyActionTitleAndIcon(action);
119 
120 	const BuddySet &buddies = action->context()->buddies();
121 	if (buddies.isEmpty() || buddies.contains(m_myself->buddy()))
122 		return;
123 
124 	action->setEnabled(true);
125 }
126 
actionInstanceCreated(Action * action)127 void DeleteTalkableAction::actionInstanceCreated(Action *action)
128 {
129 	switch (actionRole(action->context()))
130 	{
131 		case ChatRole:
132 			setChatActionTitleAndIcon(action);
133 			break;
134 		case BuddyRole:
135 			setBuddyActionTitleAndIcon(action);
136 			break;
137 	}
138 
139 	updateActionState(action);
140 }
141 
updateActionState(Action * action)142 void DeleteTalkableAction::updateActionState(Action *action)
143 {
144 	action->setEnabled(false);
145 
146 	if (action->context()->buddies().isAnyTemporary())
147 		return;
148 
149 	switch (actionRole(action->context()))
150 	{
151 		case ChatRole:
152 			updateChatActionState(action);
153 			break;
154 		case BuddyRole:
155 			updateBuddyActionState(action);
156 			break;
157 	}
158 }
159 
chatActionTriggered(ActionContext * context)160 void DeleteTalkableAction::chatActionTriggered(ActionContext *context)
161 {
162 	const Chat &chat = actionChat(context);
163 	if (!chat)
164 		return;
165 
166 	MessageDialog *dialog = MessageDialog::create(m_iconsManager->iconByPath(KaduIcon("dialog-warning")), tr("Delete Chat"),
167 	                        tr("<b>%1</b> chat will be deleted.<br/>Are you sure?").arg(chat.display()));
168 	dialog->addButton(QMessageBox::Yes, tr("Delete chat"));
169 	dialog->addButton(QMessageBox::No, tr("Cancel"));
170 
171 	if (dialog->ask())
172 		chat.setDisplay(QString());
173 }
174 
buddyActionTriggered(ActionContext * context)175 void DeleteTalkableAction::buddyActionTriggered(ActionContext *context)
176 {
177 	auto buddySet = context->buddies();
178 	if (buddySet.empty())
179 		return;
180 
181 	auto deleteWindow = injectedFactory()->makeInjected<BuddyDeleteWindow>(buddySet);
182 	deleteWindow->show();
183 }
184 
triggered(QWidget * widget,ActionContext * context,bool toggled)185 void DeleteTalkableAction::triggered(QWidget *widget, ActionContext *context, bool toggled)
186 {
187 	Q_UNUSED(widget)
188 	Q_UNUSED(toggled)
189 
190 	trigger(context);
191 }
192 
trigger(ActionContext * context)193 void DeleteTalkableAction::trigger(ActionContext *context)
194 {
195 	switch (actionRole(context))
196 	{
197 		case ChatRole:
198 			chatActionTriggered(context);
199 			break;
200 		case BuddyRole:
201 			buddyActionTriggered(context);
202 			break;
203 	}
204 }
205 
206 #include "moc_delete-talkable-action.cpp"
207