8 #include "attachmentmodel.h"
13 #include "messagecomposer_debug.h"
15 #include <KLocalizedString>
16 #include <QFileDevice>
17 #include <QTemporaryDir>
19 #include <KMime/Headers>
23 using namespace MessageCore;
34 class 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;
51 AttachmentModel::AttachmentModelPrivate::AttachmentModelPrivate(
AttachmentModel *qq)
56 AttachmentModel::AttachmentModelPrivate::~AttachmentModelPrivate()
62 AttachmentModel::AttachmentModel(
QObject *parent)
64 , d(new AttachmentModelPrivate(this))
68 AttachmentModel::~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);
104 QMimeData *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;
162 bool AttachmentModel::isModified()
const
167 void AttachmentModel::setModified(
bool modified)
169 d->modified = modified;
172 bool AttachmentModel::isEncryptEnabled()
const
174 return d->encryptEnabled;
177 void AttachmentModel::setEncryptEnabled(
bool enabled)
179 d->encryptEnabled = enabled;
180 Q_EMIT encryptEnabled(enabled);
183 bool AttachmentModel::isAutoDisplayEnabled()
const
185 return d->autoDisplayEnabled;
188 void AttachmentModel::setAutoDisplayEnabled(
bool enabled)
190 d->autoDisplayEnabled = enabled;
191 Q_EMIT autoDisplayEnabled(enabled);
194 bool AttachmentModel::isSignEnabled()
const
196 return d->signEnabled;
199 void AttachmentModel::setSignEnabled(
bool enabled)
201 d->signEnabled = enabled;
202 Q_EMIT signEnabled(enabled);
205 bool AttachmentModel::isEncryptSelected()
const
207 return d->encryptSelected;
210 void AttachmentModel::setEncryptSelected(
bool selected)
212 d->encryptSelected = selected;
214 part->setEncrypted(selected);
216 Q_EMIT dataChanged(index(0, EncryptColumn), index(rowCount() - 1, EncryptColumn));
219 bool AttachmentModel::isSignSelected()
const
221 return d->signSelected;
224 void AttachmentModel::setSignSelected(
bool selected)
226 d->signSelected = selected;
228 part->setSigned(selected);
230 Q_EMIT dataChanged(index(0, SignColumn), index(rowCount() - 1, SignColumn));
256 "Name: %1<br>Size: %2<br>Encoding: %3<br>MimeType=%4",
257 part->name().isEmpty() ? part->fileName() : part->name(),
258 KIO::convertSize(part->size()),
259 KMime::nameForEncoding(part->encoding()),
269 case AutoDisplayColumn:
274 }
else if (role == AttachmentPartRole) {
275 if (index.
column() == 0) {
278 qCWarning(MESSAGECOMPOSER_LOG) <<
"AttachmentPartRole and column != 0.";
281 }
else if (role == NameRole) {
283 }
else if (role == SizeRole) {
285 }
else if (role == EncodingRole) {
287 }
else if (role == MimeTypeRole) {
289 }
else if (role == CompressRole) {
291 }
else if (role == EncryptRole) {
293 }
else if (role == SignRole) {
295 }
else if (role == AutoDisplayRole) {
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) {
346 Q_EMIT dataChanged(index, index);
353 Q_ASSERT(!d->parts.contains(part));
354 if (!part->url().isEmpty()) {
356 if (partElement->url() == part->url()) {
362 beginInsertRows(
QModelIndex(), rowCount(), rowCount());
363 d->parts.append(part);
369 const int idx = d->parts.indexOf(part);
371 qCWarning(MESSAGECOMPOSER_LOG) <<
"Tried to update non-existent part.";
375 Q_EMIT dataChanged(index(idx, 0), index(idx, LastColumn - 1));
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;
390 Q_EMIT dataChanged(index(idx, 0), index(idx, LastColumn - 1));
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;
462 if (!hasIndex(row, column, parent)) {
465 Q_ASSERT(row >= 0 && row < rowCount());
468 qCWarning(MESSAGECOMPOSER_LOG) <<
"Called with weird parent.";
472 return createIndex(row, column);
481 int AttachmentModel::rowCount(
const QModelIndex &parent)
const
486 return d->parts.count();
489 int AttachmentModel::columnCount(
const QModelIndex &parent)
const