1 /*
2  * %kadu copyright begin%
3  * Copyright 2011 Piotr Galiszewski (piotr.galiszewski@kadu.im)
4  * Copyright 2013 Bartosz Brachaczek (b.brachaczek@gmail.com)
5  * Copyright 2011, 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 <QtCore/QDateTime>
23 #include <QtCore/QDir>
24 
25 #include "configuration/screen-shot-configuration.h"
26 #include "gui/windows/message-dialog.h"
27 #include "icons/icons-manager.h"
28 
29 #include "screen-shot-saver.h"
30 
ScreenShotSaver(IconsManager * iconsManager,ScreenShotConfiguration * screenShotConfiguration,QObject * parent)31 ScreenShotSaver::ScreenShotSaver(IconsManager *iconsManager, ScreenShotConfiguration *screenShotConfiguration, QObject *parent) :
32 		QObject{parent},
33 		m_iconsManager{iconsManager},
34 		m_screenShotConfiguration{screenShotConfiguration},
35 		Size{}
36 {
37 }
38 
~ScreenShotSaver()39 ScreenShotSaver::~ScreenShotSaver()
40 {
41 }
42 
createScreenshotPath()43 QString ScreenShotSaver::createScreenshotPath()
44 {
45 	QString dirPath = m_screenShotConfiguration->imagePath();
46 
47 	QDir dir(dirPath);
48 	if (!dir.exists() && !dir.mkpath(dirPath))
49 	{
50 		MessageDialog::show(m_iconsManager->iconByPath(KaduIcon("dialog-warning")), tr("Kadu"), tr("Unable to create direcotry %1 for storing screenshots!").arg(dirPath));
51 		return QString();
52 	}
53 
54 	return QDir::cleanPath(QString("%1/%2%3.%4")
55 			.arg(dir.absolutePath())
56 			.arg(m_screenShotConfiguration->fileNamePrefix())
57 			.arg(QString::number(QDateTime::currentDateTime().toTime_t()))
58 			.arg(m_screenShotConfiguration->screenshotFileNameExtension().toLower()));
59 }
60 
saveScreenShot(QPixmap pixmap)61 QString ScreenShotSaver::saveScreenShot(QPixmap pixmap)
62 {
63 	QString path = createScreenshotPath();
64 	if (path.isEmpty())
65 		return QString();
66 
67 	int quality = m_screenShotConfiguration->quality();
68 
69 	// do not extract qPrintable... to variable
70 	if (!pixmap.save(path, qPrintable(m_screenShotConfiguration->fileFormat()), quality))
71 	{
72 		MessageDialog::show(m_iconsManager->iconByPath(KaduIcon("dialog-warning")), tr("Kadu"), tr("Can't write file %1.\nAccess denied or other problem!").arg(path));
73 		return QString();
74 	}
75 
76 	QFileInfo f(path);
77 	Size = f.size();
78 
79 	if (Size == 0)
80 	{
81 		MessageDialog::show(m_iconsManager->iconByPath(KaduIcon("dialog-warning")), tr("Kadu"), tr("Screenshot %1 has 0 size!\nIt should be bigger.").arg(path));
82 		return QString();
83 	}
84 
85 	return path;
86 }
87 
88 #include "moc_screen-shot-saver.cpp"
89