8#include "attachmentmodel.h"
13#include "messagecomposer_debug.h"
15#include <KLocalizedString>
17#include <QTemporaryDir>
19#include <KMime/Headers>
23using namespace MessageCore;
34class MessageComposer::AttachmentModel::AttachmentModelPrivate
38 ~AttachmentModelPrivate();
43 bool modified =
false;
44 bool encryptEnabled =
false;
45 bool signEnabled =
false;
46 bool encryptSelected =
false;
47 bool signSelected =
false;
48 bool autoDisplayEnabled =
false;
51AttachmentModel::AttachmentModelPrivate::AttachmentModelPrivate(
AttachmentModel *qq)
56AttachmentModel::AttachmentModelPrivate::~AttachmentModelPrivate()
64 , d(new AttachmentModelPrivate(this))
68AttachmentModel::~AttachmentModel() =
default;
76 qCDebug(MESSAGECOMPOSER_LOG) <<
"data has formats" << data->
formats() <<
"urls" << data->
urls() <<
"action" << int(action);
87 for (
const QUrl &url : urls) {
94 Q_EMIT attachUrlsRequested(urls);
96 Q_EMIT attachItemsRequester(items);
104QMimeData *AttachmentModel::mimeData(
const QModelIndexList &indexes)
const
106 qCDebug(MESSAGECOMPOSER_LOG);
109 if (index.
column() != 0) {
112 qCWarning(MESSAGECOMPOSER_LOG) <<
"column != 0. Possibly duplicate rows passed to mimeData().";
117 QString attachmentName = part->fileName();
118 if (attachmentName.
isEmpty()) {
119 attachmentName = part->name();
121 if (attachmentName.
isEmpty()) {
122 attachmentName =
i18n(
"unnamed attachment");
126 d->tempDirs.append(tempDir);
130 qCWarning(MESSAGECOMPOSER_LOG) <<
"Cannot write attachment:" << f.errorString();
134 if (f.write(data) != data.
length()) {
135 qCWarning(MESSAGECOMPOSER_LOG) <<
"Failed to write all data to file!";
142 qCDebug(MESSAGECOMPOSER_LOG) <<
" temporary file " << url;
153 const QStringList types = {QStringLiteral(
"text/uri-list")};
167void AttachmentModel::setModified(
bool modified)
169 d->modified = modified;
172bool AttachmentModel::isEncryptEnabled()
const
174 return d->encryptEnabled;
177void AttachmentModel::setEncryptEnabled(
bool enabled)
179 d->encryptEnabled = enabled;
180 Q_EMIT encryptEnabled(enabled);
183bool AttachmentModel::isAutoDisplayEnabled()
const
185 return d->autoDisplayEnabled;
188void AttachmentModel::setAutoDisplayEnabled(
bool enabled)
190 d->autoDisplayEnabled = enabled;
191 Q_EMIT autoDisplayEnabled(enabled);
194bool AttachmentModel::isSignEnabled()
const
196 return d->signEnabled;
199void AttachmentModel::setSignEnabled(
bool enabled)
201 d->signEnabled = enabled;
202 Q_EMIT signEnabled(enabled);
205bool AttachmentModel::isEncryptSelected()
const
207 return d->encryptSelected;
212 d->encryptSelected = selected;
214 part->setEncrypted(selected);
216 Q_EMIT dataChanged(index(0, EncryptColumn), index(rowCount() - 1, EncryptColumn));
219bool AttachmentModel::isSignSelected()
const
221 return d->signSelected;
226 d->signSelected = selected;
228 part->setSigned(selected);
244 return part->name().isEmpty() ? part->fileName() : part->name();
248 return KMime::nameForEncoding(part->encoding());
250 return part->mimeType();
255 return i18nc(
"@info:tooltip",
256 "Name: %1<br>Size: %2<br>Encoding: %3<br>MimeType=%4",
257 part->name().isEmpty() ? part->fileName() : part->name(),
259 KMime::nameForEncoding(part->encoding()),
264 return int(boolToCheckState(part->isCompressed()));
266 return int(boolToCheckState(part->isEncrypted()));
268 return int(boolToCheckState(part->isSigned()));
269 case AutoDisplayColumn:
270 return int(boolToCheckState(part->isInline()));
274 }
else if (role == AttachmentPartRole) {
275 if (index.
column() == 0) {
278 qCWarning(MESSAGECOMPOSER_LOG) <<
"AttachmentPartRole and column != 0.";
281 }
else if (role == NameRole) {
282 return part->fileName().isEmpty() ? part->name() : part->fileName();
283 }
else if (role == SizeRole) {
285 }
else if (role == EncodingRole) {
286 return KMime::nameForEncoding(part->encoding());
287 }
else if (role == MimeTypeRole) {
288 return part->mimeType();
289 }
else if (role == CompressRole) {
290 return part->isCompressed();
291 }
else if (role == EncryptRole) {
292 return part->isEncrypted();
293 }
else if (role == SignRole) {
294 return part->isSigned();
295 }
else if (role == AutoDisplayRole) {
296 return part->isInline();
304 bool emitDataChanged =
true;
321 case CompressColumn: {
322 bool toZip = value.
toBool();
323 if (toZip != part->isCompressed()) {
324 Q_EMIT attachmentCompressRequested(part, toZip);
325 emitDataChanged =
false;
330 part->setEncrypted(value.
toBool());
333 part->setSigned(value.
toBool());
335 case AutoDisplayColumn:
336 part->setInline(value.
toBool());
345 if (emitDataChanged) {
353 Q_ASSERT(!d->parts.contains(part));
354 if (!part->url().isEmpty()) {
356 if (partElement->url() == part->url()) {
363 d->parts.append(part);
369 const int idx = d->parts.indexOf(part);
371 qCWarning(MESSAGECOMPOSER_LOG) <<
"Tried to update non-existent part.";
381 Q_ASSERT(oldPart != newPart);
383 const int idx = d->parts.indexOf(oldPart);
385 qCWarning(MESSAGECOMPOSER_LOG) <<
"Tried to replace non-existent part.";
388 d->parts[idx] = newPart;
396 const int idx = d->parts.indexOf(part);
398 qCWarning(MESSAGECOMPOSER_LOG) <<
"Attachment not found.";
403 d->parts.removeAt(idx);
405 Q_EMIT attachmentRemoved(part);
422 if (index.
column() == CompressColumn || index.
column() == EncryptColumn || index.
column() == SignColumn || index.
column() == AutoDisplayColumn) {
424 }
else if (index.
column() == NameColumn) {
439 return i18nc(
"@title column attachment name.",
"Name");
441 return i18nc(
"@title column attachment size.",
"Size");
443 return i18nc(
"@title column attachment encoding.",
"Encoding");
445 return i18nc(
"@title column attachment type.",
"Type");
447 return i18nc(
"@title column attachment compression checkbox.",
"Compress");
449 return i18nc(
"@title column attachment encryption checkbox.",
"Encrypt");
451 return i18nc(
"@title column attachment signed checkbox.",
"Sign");
452 case AutoDisplayColumn:
453 return i18nc(
"@title column attachment inlined checkbox.",
"Suggest Automatic Display");
455 qCWarning(MESSAGECOMPOSER_LOG) <<
"Bad column" << section;
465 Q_ASSERT(row >= 0 && row < rowCount());
468 qCWarning(MESSAGECOMPOSER_LOG) <<
"Called with weird parent.";
481int AttachmentModel::rowCount(
const QModelIndex &parent)
const
486 return d->parts.count();
489int AttachmentModel::columnCount(
const QModelIndex &parent)
const
495#include "moc_attachmentmodel.cpp"
static Item fromUrl(const QUrl &url)
The AttachmentModel class.
void setEncryptSelected(bool selected)
sets for all
bool isModified() const
for the save/discard warning
void setSignSelected(bool selected)
sets for all
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
KIOCORE_EXPORT QString convertSize(KIO::filesize_t size)
Simple interface that both EncryptJob and SignEncryptJob implement so the composer can extract some e...
void beginInsertRows(const QModelIndex &parent, int first, int last)
void beginRemoveRows(const QModelIndex &parent, int first, int last)
QModelIndex createIndex(int row, int column, const void *ptr) const const
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QList< int > &roles)
virtual Qt::ItemFlags flags(const QModelIndex &index) const const
bool hasIndex(int row, int column, const QModelIndex &parent) const const
qsizetype length() const const
void append(QList< T > &&value)
bool isEmpty() const const
void setUrls(const QList< QUrl > &urls)
QList< QUrl > urls() const const
bool isValid() const const
QObject * parent() const const
QString fromLatin1(QByteArrayView str)
bool isEmpty() const const
QUrl fromLocalFile(const QString &localFile)
QVariant fromValue(T &&value)
bool toBool() const const
QString toString() const const