1 /*
2  * %kadu copyright begin%
3  * Copyright 2011 Tomasz Rostanski (rozteck@interia.pl)
4  * Copyright 2012 Piotr Dąbrowski (ultr@ultr.pl)
5  * Copyright 2013, 2014 Bartosz Brachaczek (b.brachaczek@gmail.com)
6  * Copyright 2011, 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 <QtWidgets/QMenu>
24 
25 #include "accounts/account.h"
26 #include "core/core.h"
27 #include "gui/actions/action.h"
28 #include "gui/widgets/chat-edit-box.h"
29 #include "gui/widgets/chat-widget/chat-widget.h"
30 #include "plugin/plugin-injected-factory.h"
31 #include "protocols/protocol.h"
32 
33 #include "configuration/screen-shot-configuration.h"
34 #include "screenshot.h"
35 
36 #include "screenshot-action.h"
37 
ScreenshotAction(ScreenShotConfiguration * screenShotConfiguration,QObject * parent)38 ScreenshotAction::ScreenshotAction(ScreenShotConfiguration *screenShotConfiguration, QObject *parent) :
39 		ActionDescription(parent),
40 		m_screenShotConfiguration{screenShotConfiguration}
41 {
42 	setType(ActionDescription::TypeChat);
43 	setName("ScreenShotAction");
44 	setIcon(KaduIcon("external_modules/screenshot-camera-photo"));
45 	setText(tr("ScreenShot"));
46 }
47 
~ScreenshotAction()48 ScreenshotAction::~ScreenshotAction()
49 {
50 }
51 
setPluginInjectedFactory(PluginInjectedFactory * pluginInjectedFactory)52 void ScreenshotAction::setPluginInjectedFactory(PluginInjectedFactory *pluginInjectedFactory)
53 {
54 	m_pluginInjectedFactory = pluginInjectedFactory;
55 }
56 
actionInstanceCreated(Action * action)57 void ScreenshotAction::actionInstanceCreated(Action *action)
58 {
59 	ChatEditBox *chatEditBox = qobject_cast<ChatEditBox *>(action->parent());
60 	if (!chatEditBox || !chatEditBox->chatWidget())
61 		return;
62 
63 	QVariant chatWidgetData = (qlonglong)chatEditBox->chatWidget();
64 	action->setData(chatWidgetData);
65 
66 	// not a menu
67 	if (action->context()->chat() != chatEditBox->actionContext()->chat())
68 		return;
69 
70 	// no parents for menu as it is destroyed manually by Action class
71 	QMenu *menu = new QMenu();
72 	menu->addAction(tr("Simple Shot"), this, SLOT(takeStandardShotSlot()))->setData(chatWidgetData);
73 	menu->addAction(tr("With Chat Window Hidden"), this, SLOT(takeShotWithChatWindowHiddenSlot()))->setData(chatWidgetData);
74 	menu->addAction(tr("Window Shot"), this, SLOT(takeWindowShotSlot()))->setData(chatWidgetData);
75 	action->setMenu(menu);
76 }
77 
actionTriggered(QAction * sender,bool toggled)78 void ScreenshotAction::actionTriggered(QAction *sender, bool toggled)
79 {
80 	Q_UNUSED(sender)
81 	Q_UNUSED(toggled)
82 
83 	takeStandardShotSlot(findChatWidget(sender));
84 }
85 
updateActionState(Action * action)86 void ScreenshotAction::updateActionState(Action *action)
87 {
88 	action->setEnabled(false);
89 
90 	ChatEditBox *chatEditBox = qobject_cast<ChatEditBox *>(action->parent());
91 	if (!chatEditBox)
92 		return;
93 
94 	const Account &account = action->context()->chat().chatAccount();
95 	if (!account)
96 		return;
97 
98 	Protocol *protocol = account.protocolHandler();
99 	if (!protocol)
100 		return;
101 
102 	action->setEnabled(protocol->chatImageService());
103 }
104 
findChatWidget(QObject * object)105 ChatWidget * ScreenshotAction::findChatWidget(QObject *object)
106 {
107 	QAction *action = qobject_cast<QAction *>(object);
108 	if (!action)
109 		return 0;
110 
111 	return static_cast<ChatWidget *>((void*)(action->data().toLongLong()));
112 }
113 
takeStandardShotSlot(ChatWidget * chatWidget)114 void ScreenshotAction::takeStandardShotSlot(ChatWidget *chatWidget)
115 {
116 	// in case of non-menu call
117 	if (!chatWidget)
118 		chatWidget = findChatWidget(sender());
119 	if (chatWidget)
120 		(m_pluginInjectedFactory->makeInjected<ScreenShot>(m_screenShotConfiguration, chatWidget))->takeStandardShot();
121 }
122 
takeShotWithChatWindowHiddenSlot()123 void ScreenshotAction::takeShotWithChatWindowHiddenSlot()
124 {
125 	ChatWidget *chatWidget = findChatWidget(sender());
126 	if (chatWidget)
127 		(m_pluginInjectedFactory->makeInjected<ScreenShot>(m_screenShotConfiguration, chatWidget))->takeShotWithChatWindowHidden();
128 }
129 
takeWindowShotSlot()130 void ScreenshotAction::takeWindowShotSlot()
131 {
132 	ChatWidget *chatWidget = findChatWidget(sender());
133 	if (chatWidget)
134 		(m_pluginInjectedFactory->makeInjected<ScreenShot>(m_screenShotConfiguration, chatWidget))->takeWindowShot();
135 }
136 
137 #include "moc_screenshot-action.cpp"
138