Messagelib

imagescalingutils.cpp
1/*
2 SPDX-FileCopyrightText: 2013-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "imagescalingutils.h"
8#include "settings/messagecomposersettings.h"
9
10#include <QFileInfo>
11
12using namespace MessageComposer;
13
14bool Utils::resizeImage(MessageCore::AttachmentPart::Ptr part)
15{
16 const QString filename = part->fileName();
17 const QString pattern = MessageComposer::MessageComposerSettings::self()->filterSourcePattern();
18
19 if (!pattern.isEmpty()) {
20 // TODO use regexp ?
21 const QStringList lstPattern = pattern.split(QLatin1Char(';'));
22 for (const QString &patternStr : lstPattern) {
23 switch (MessageComposer::MessageComposerSettings::self()->filterSourceType()) {
24 case MessageComposer::MessageComposerSettings::EnumFilterSourceType::NoFilter:
25 break;
26 case MessageComposer::MessageComposerSettings::EnumFilterSourceType::IncludeFilesWithPattern:
27 if (!filename.contains(patternStr)) {
28 return false;
29 }
30 break;
31 case MessageComposer::MessageComposerSettings::EnumFilterSourceType::ExcludeFilesWithPattern:
32 if (filename.contains(patternStr)) {
33 return false;
34 }
35 break;
36 }
37 }
38 }
39
40 if (MessageComposer::MessageComposerSettings::self()->resizeImagesWithFormats()) {
41 const QString formatsType = MessageComposer::MessageComposerSettings::self()->resizeImagesWithFormatsType();
42 if (!formatsType.isEmpty()) {
43 const QStringList lstFormat = formatsType.split(QLatin1Char(';'));
44 bool willResizeImage = false;
45 for (const QString &type : lstFormat) {
46 if (QLatin1StringView(part->mimeType()) == type) {
47 willResizeImage = true;
48 break;
49 }
50 }
51 if (!willResizeImage) {
52 return false;
53 }
54 }
55 }
56
57 if (MessageComposer::MessageComposerSettings::self()->skipImageLowerSizeEnabled()) {
58 if (part->size() > MessageComposer::MessageComposerSettings::self()->skipImageLowerSize() * 1024) {
59 if (hasImage(part->mimeType())) {
60 return true;
61 } else {
62 return false;
63 }
64 } else {
65 return false;
66 }
67 }
68 return true;
69}
70
71void Utils::changeFileName(MessageCore::AttachmentPart::Ptr part)
72{
73 if (MessageComposer::MessageComposerSettings::self()->renameResizedImages()) {
74 QString pattern = MessageComposer::MessageComposerSettings::self()->renameResizedImagesPattern();
75 if (!pattern.isEmpty()) {
76 const QString filename = part->fileName();
79 pattern.replace(QLatin1StringView("%n"), filename); // Original name
80 pattern.replace(QLatin1StringView("%e"), QFileInfo(filename).completeSuffix()); // Original extension
81
82 const QString type = MessageComposer::MessageComposerSettings::self()->writeFormat();
83 QString newExtension;
84 if (type == QLatin1StringView("JPG")) {
85 newExtension = QStringLiteral("jpg");
86 } else if (type == QLatin1StringView("PNG")) {
87 newExtension = QStringLiteral("png");
88 }
89 if (!newExtension.isEmpty()) {
90 pattern.replace(QLatin1StringView("%x"), newExtension); // new Extension
91 }
92
93 // Need to define pattern type.
94 part->setFileName(pattern);
95 part->setName(pattern);
96 }
97 }
98}
99
100bool Utils::filterRecipients(const QStringList &recipients)
101{
102 if (recipients.isEmpty()) {
103 return false;
104 }
105
106 if (MessageComposer::MessageComposerSettings::self()->filterRecipientType()
107 == MessageComposer::MessageComposerSettings::EnumFilterRecipientType::NoFilter) {
108 return true;
109 }
110
111 const QString doNotResizeEmailsPattern = MessageComposer::MessageComposerSettings::self()->doNotResizeEmailsPattern();
112 const QString resizeEmailsPattern = MessageComposer::MessageComposerSettings::self()->resizeEmailsPattern();
113 if (doNotResizeEmailsPattern.isEmpty() && resizeEmailsPattern.isEmpty()) {
114 return true;
115 }
116
117 switch (MessageComposer::MessageComposerSettings::self()->filterRecipientType()) {
118 case MessageComposer::MessageComposerSettings::EnumFilterRecipientType::NoFilter:
119 return true;
120 case MessageComposer::MessageComposerSettings::EnumFilterRecipientType::ResizeEachEmailsContainsPattern:
121 if (resizeEmailsPattern.isEmpty()) {
122 return false;
123 }
124 for (const QString &emails : recipients) {
125 if (!emails.contains(resizeEmailsPattern)) {
126 return false;
127 }
128 }
129 return true;
130 case MessageComposer::MessageComposerSettings::EnumFilterRecipientType::ResizeOneEmailContainsPattern:
131 if (resizeEmailsPattern.isEmpty()) {
132 return false;
133 }
134 for (const QString &emails : recipients) {
135 if (emails.contains(resizeEmailsPattern)) {
136 return true;
137 }
138 }
139 return false;
140 case MessageComposer::MessageComposerSettings::EnumFilterRecipientType::DontResizeEachEmailsContainsPattern:
141 if (doNotResizeEmailsPattern.isEmpty()) {
142 return false;
143 }
144 for (const QString &emails : recipients) {
145 if (!emails.contains(doNotResizeEmailsPattern)) {
146 return false;
147 }
148 }
149 return true;
150 case MessageComposer::MessageComposerSettings::EnumFilterRecipientType::DontResizeOneEmailContainsPattern:
151 if (doNotResizeEmailsPattern.isEmpty()) {
152 return false;
153 }
154 for (const QString &emails : recipients) {
155 if (emails.contains(doNotResizeEmailsPattern)) {
156 return true;
157 }
158 }
159 return false;
160 }
161
162 return false;
163}
164
165bool Utils::hasImage(const QByteArray &mimetype)
166{
167 if (mimetype == QByteArrayLiteral("image/gif") || mimetype == QByteArrayLiteral("image/jpeg") || mimetype == QByteArrayLiteral("image/png")) {
168 return true;
169 }
170 return false;
171}
172
173bool Utils::containsImage(const MessageCore::AttachmentPart::List &parts)
174{
175 for (const MessageCore::AttachmentPart::Ptr &part : parts) {
176 if (hasImage(part->mimeType())) {
177 return true;
178 }
179 }
180 return false;
181}
char * toString(const EngineQuery &query)
Simple interface that both EncryptJob and SignEncryptJob implement so the composer can extract some e...
QDate currentDate()
bool isEmpty() const const
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
bool isEmpty() const const
QString & replace(QChar before, QChar after, Qt::CaseSensitivity cs)
QStringList split(QChar sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
QTime currentTime()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:07:24 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.