1 /*
2  * %kadu copyright begin%
3  * Copyright 2011 Piotr Galiszewski (piotr.galiszewski@kadu.im)
4  * Copyright 2012 Piotr Dąbrowski (ultr@ultr.pl)
5  * Copyright 2013, 2014 Bartosz Brachaczek (b.brachaczek@gmail.com)
6  * Copyright 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 <QtCore/QCoreApplication>
24 #include <QtCore/QDateTime>
25 #include <QtCore/QDir>
26 #include <QtCore/QTimer>
27 
28 #include "contacts/contact-set.h"
29 #include "core/core.h"
30 #include "gui/widgets/chat-widget/chat-widget.h"
31 #include "gui/widgets/custom-input.h"
32 #include "gui/windows/message-dialog.h"
33 #include "icons/icons-manager.h"
34 #include "plugin/plugin-injected-factory.h"
35 #include "activate.h"
36 #include "debug.h"
37 
38 #include "configuration/screen-shot-configuration.h"
39 #include "gui/widgets/screenshot-widget.h"
40 #include "pixmap-grabber.h"
41 #include "screenshot-notification-service.h"
42 #include "screenshot-taker.h"
43 
44 #include "screen-shot-saver.h"
45 #include "screenshot.h"
46 
ScreenShot(ScreenShotConfiguration * screenShotConfiguration,ChatWidget * chatWidget)47 ScreenShot::ScreenShot(ScreenShotConfiguration *screenShotConfiguration, ChatWidget *chatWidget) :
48 		m_screenShotConfiguration{screenShotConfiguration},
49 		Mode{},
50 		MyScreenshotTaker{},
51 		MyChatWidget{chatWidget}
52 {
53 }
54 
~ScreenShot()55 ScreenShot::~ScreenShot()
56 {
57 	if (MyChatWidget)
58 		_activateWindow(m_configuration, MyChatWidget->window());
59 
60 	delete MyScreenshotTaker;
61 	MyScreenshotTaker = 0;
62 }
63 
setConfiguration(Configuration * configuration)64 void ScreenShot::setConfiguration(Configuration *configuration)
65 {
66 	m_configuration = configuration;
67 }
68 
setIconsManager(IconsManager * iconsManager)69 void ScreenShot::setIconsManager(IconsManager *iconsManager)
70 {
71 	m_iconsManager = iconsManager;
72 }
73 
setPluginInjectedFactory(PluginInjectedFactory * pluginInjectedFactory)74 void ScreenShot::setPluginInjectedFactory(PluginInjectedFactory *pluginInjectedFactory)
75 {
76 	m_pluginInjectedFactory = pluginInjectedFactory;
77 }
78 
setScreenshotNotificationService(ScreenshotNotificationService * screenshotNotificationService)79 void ScreenShot::setScreenshotNotificationService(ScreenshotNotificationService *screenshotNotificationService)
80 {
81 	m_screenshotNotificationService = screenshotNotificationService;
82 }
83 
init()84 void ScreenShot::init()
85 {
86 	MyScreenshotTaker = m_pluginInjectedFactory->makeInjected<ScreenshotTaker>(MyChatWidget);
87 	connect(MyScreenshotTaker, SIGNAL(screenshotTaken(QPixmap, bool)), this, SLOT(screenshotTaken(QPixmap, bool)));
88 	connect(MyScreenshotTaker, SIGNAL(screenshotNotTaken()), this, SLOT(screenshotNotTaken()));
89 
90 	// Rest stuff
91 	warnedAboutSize = false;
92 }
93 
takeStandardShot()94 void ScreenShot::takeStandardShot()
95 {
96 	MyScreenshotTaker->takeStandardShot();
97 }
98 
takeShotWithChatWindowHidden()99 void ScreenShot::takeShotWithChatWindowHidden()
100 {
101 	MyScreenshotTaker->takeShotWithChatWindowHidden();
102 }
103 
takeWindowShot()104 void ScreenShot::takeWindowShot()
105 {
106 	MyScreenshotTaker->takeWindowShot();
107 }
108 
screenshotTaken(QPixmap screenshot,bool needsCrop)109 void ScreenShot::screenshotTaken(QPixmap screenshot, bool needsCrop)
110 {
111 	if (!needsCrop)
112 	{
113 		screenshotReady(screenshot);
114 		return;
115 	}
116 
117 	ScreenshotWidget *screenshotWidget = new ScreenshotWidget();
118 	connect(screenshotWidget, SIGNAL(pixmapCaptured(QPixmap)), this, SLOT(screenshotReady(QPixmap)));
119 	connect(screenshotWidget, SIGNAL(canceled()), this, SLOT(screenshotNotTaken()));
120 
121 	screenshotWidget->setPixmap(screenshot);
122 	screenshotWidget->setShotMode(Mode);
123 	screenshotWidget->showFullScreen();
124 	screenshotWidget->show();
125 	QCoreApplication::processEvents(); // ensure window was shown, otherwise it won't be activated
126 	_activateWindow(m_configuration, screenshotWidget);
127 }
128 
screenshotNotTaken()129 void ScreenShot::screenshotNotTaken()
130 {
131 	deleteLater();
132 }
133 
screenshotReady(QPixmap p)134 void ScreenShot::screenshotReady(QPixmap p)
135 {
136 	auto saver = new ScreenShotSaver(m_iconsManager, m_screenShotConfiguration, this);
137 	auto screenShotPath = saver->saveScreenShot(p);
138 
139 	if (m_screenShotConfiguration->pasteImageClauseIntoChatWidget())
140 	{
141 		pasteImageClause(screenShotPath);
142 		if (!checkImageSize(saver->size()))
143 			MessageDialog::show(m_iconsManager->iconByPath(KaduIcon("dialog-warning")), tr("Kadu"), tr("Image size is bigger than maximal image size for this chat."));
144 	}
145 
146 	deleteLater();
147 }
148 
pasteImageClause(const QString & path)149 void ScreenShot::pasteImageClause(const QString &path)
150 {
151 	MyChatWidget->edit()->insertHtml(QString("<img src='%1' />").arg(path));
152 }
153 
checkImageSize(long int size)154 bool ScreenShot::checkImageSize(long int size)
155 {
156 	Q_UNUSED(size)
157 
158 	ContactSet contacts = MyChatWidget->chat().contacts();
159 	foreach (const Contact &contact, contacts)
160 		if (contact.maximumImageSize() * 1024 < size)
161 			return false;
162 
163 	return true;
164 }
165 
checkShotsSize()166 void ScreenShot::checkShotsSize()
167 {
168 	kdebugf();
169 	if (!m_screenShotConfiguration->warnAboutDirectorySize())
170 		return;
171 
172 	long size = 0;
173 
174 	long limit = m_screenShotConfiguration->directorySizeLimit();
175 	QDir dir(m_screenShotConfiguration->imagePath());
176 
177 	QString prefix = m_screenShotConfiguration->fileNamePrefix();
178 	QStringList filters;
179 	filters << prefix + '*';
180 	QFileInfoList list = dir.entryInfoList(filters, QDir::Files);
181 
182 	foreach (const QFileInfo &f, list)
183 		size += f.size();
184 
185 	if (size/1024 >= limit)
186 		m_screenshotNotificationService->notifySizeLimit(size);
187 }
188 
189 #undef Bool
190 
191 #include "moc_screenshot.cpp"
192