presentation/ComposeMessage.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "ComposeMessage.h" | ||
| 2 | |||
| 3 | #include "service/Rfc2822Builder.h" | ||
| 4 | |||
| 5 | #include <QFile> | ||
| 6 | #include <QFileInfo> | ||
| 7 | #include <QRegularExpression> | ||
| 8 | |||
| 9 | namespace { | ||
| 10 | constexpr qint64 MaxTotalBytes = 100LL * 1024 * 1024; | ||
| 11 | |||
| 12 | 27 | bool readFile(const QString &path, qint64 &budget, QByteArray &out) { | |
| 13 |
1/1✓ Branch 2 → 3 taken 27 times.
|
27 | QFile file(path); |
| 14 |
3/3✓ Branch 4 → 5 taken 27 times.
✓ Branch 5 → 6 taken 2 times.
✓ Branch 5 → 7 taken 25 times.
|
27 | if (!file.open(QIODevice::ReadOnly)) return false; |
| 15 |
1/1✓ Branch 7 → 8 taken 25 times.
|
25 | const qint64 size = file.size(); |
| 16 |
3/6✓ Branch 8 → 9 taken 25 times.
✗ Branch 8 → 11 not taken.
✓ Branch 9 → 10 taken 25 times.
✗ Branch 9 → 11 not taken.
✗ Branch 10 → 11 not taken.
✓ Branch 10 → 12 taken 25 times.
|
25 | if (size < 0 || size > Rfc2822Builder::MaxAttachmentBytes || size > budget) return false; |
| 17 |
1/1✓ Branch 12 → 13 taken 25 times.
|
25 | out = file.read(size + 1); |
| 18 |
1/2✗ Branch 16 → 17 not taken.
✓ Branch 16 → 18 taken 25 times.
|
25 | if (out.size() != size) return false; |
| 19 | 25 | budget -= size; | |
| 20 | 25 | return true; | |
| 21 | 27 | } | |
| 22 | } // namespace | ||
| 23 | |||
| 24 | 66 | ComposeMessage::Result ComposeMessage::build(Form form) const { | |
| 25 | 66 | Result result; | |
| 26 | 66 | Rfc2822Builder builder; | |
| 27 |
1/1✓ Branch 3 → 4 taken 66 times.
|
66 | builder.setFrom(fromName, fromAddress); |
| 28 |
1/1✓ Branch 4 → 5 taken 66 times.
|
66 | builder.setTo(to); |
| 29 |
1/1✓ Branch 5 → 6 taken 66 times.
|
66 | builder.setCc(cc); |
| 30 |
1/1✓ Branch 6 → 7 taken 66 times.
|
66 | builder.setBcc(bcc); |
| 31 |
2/2✓ Branch 7 → 8 taken 66 times.
✓ Branch 8 → 9 taken 66 times.
|
66 | builder.setSubject(subject.trimmed()); |
| 32 |
4/6✓ Branch 11 → 12 taken 4 times.
✓ Branch 11 → 20 taken 62 times.
✓ Branch 17 → 18 taken 4 times.
✓ Branch 17 → 19 taken 4 times.
✗ Branch 112 → 113 not taken.
✗ Branch 112 → 114 not taken.
|
74 | if (!replyTo.isEmpty()) builder.setReplyTo({replyTo}); |
| 33 | 66 | builder.setPriority(priority); | |
| 34 |
2/2✓ Branch 21 → 22 taken 4 times.
✓ Branch 21 → 23 taken 62 times.
|
66 | if (readReceipt) builder.setDispositionNotificationTo(fromAddress); |
| 35 |
3/3✓ Branch 24 → 25 taken 7 times.
✓ Branch 24 → 26 taken 59 times.
✓ Branch 25 → 26 taken 7 times.
|
66 | if (!inReplyTo.isEmpty()) builder.setInReplyTo(inReplyTo); |
| 36 |
3/3✓ Branch 27 → 28 taken 6 times.
✓ Branch 27 → 29 taken 60 times.
✓ Branch 28 → 29 taken 6 times.
|
66 | if (!references.isEmpty()) builder.setReferences(references); |
| 37 |
1/1✓ Branch 29 → 30 taken 66 times.
|
66 | builder.setBodyText(text); |
| 38 |
2/2✓ Branch 30 → 31 taken 31 times.
✓ Branch 30 → 32 taken 35 times.
|
66 | if (formatted) builder.setBodyHtml(html); |
| 39 |
3/3✓ Branch 33 → 34 taken 7 times.
✓ Branch 33 → 35 taken 59 times.
✓ Branch 34 → 35 taken 7 times.
|
66 | if (!calendar.isEmpty()) builder.setCalendarPart(calendar, calendarMethod); |
| 40 |
5/6✓ Branch 35 → 36 taken 10 times.
✓ Branch 35 → 39 taken 56 times.
✓ Branch 37 → 38 taken 10 times.
✗ Branch 37 → 39 not taken.
✓ Branch 40 → 41 taken 10 times.
✓ Branch 40 → 48 taken 56 times.
|
66 | if (form == Form::Draft && !draftHeader.isEmpty()) |
| 41 |
1/1✓ Branch 44 → 45 taken 10 times.
|
20 | builder.setMailJdHeader(QStringLiteral("X-MailJD-Draft"), draftHeader); |
| 42 | 66 | qint64 budget = MaxTotalBytes; | |
| 43 |
2/2✓ Branch 68 → 50 taken 5 times.
✓ Branch 68 → 69 taken 65 times.
|
70 | for (const File &image : inlineImages) { |
| 44 | 5 | QByteArray data; | |
| 45 |
3/3✓ Branch 51 → 52 taken 5 times.
✓ Branch 52 → 53 taken 1 time.
✓ Branch 52 → 55 taken 4 times.
|
5 | if (!readFile(image.path, budget, data)) { |
| 46 | 1 | result.failedFile = image.name; | |
| 47 | 1 | result.tooLarge = budget <= 0; | |
| 48 | 1 | return result; | |
| 49 | } | ||
| 50 | 4 | builder.addInlineImage({image.contentId, image.name, data, image.mimeType.toLatin1()}); | |
| 51 | 5 | } | |
| 52 |
2/2✓ Branch 96 → 71 taken 22 times.
✓ Branch 96 → 97 taken 64 times.
|
86 | for (const File &file : attachments) { |
| 53 | 22 | QByteArray data; | |
| 54 |
3/3✓ Branch 72 → 73 taken 22 times.
✓ Branch 73 → 74 taken 1 time.
✓ Branch 73 → 76 taken 21 times.
|
22 | if (!readFile(file.path, budget, data)) { |
| 55 | 1 | result.failedFile = file.name; | |
| 56 | 1 | result.tooLarge = budget <= 0; | |
| 57 | 1 | return result; | |
| 58 | } | ||
| 59 |
3/5✓ Branch 78 → 79 taken 1 time.
✓ Branch 81 → 82 taken 1 time.
✓ Branch 81 → 83 taken 20 times.
✗ Branch 144 → 145 not taken.
✗ Branch 144 → 146 not taken.
|
43 | const QByteArray mime = file.mimeType.isEmpty() ? Rfc2822Builder::mimeTypeForFile(file.name, data.left(4096)) |
| 60 |
4/4✓ Branch 77 → 78 taken 1 time.
✓ Branch 77 → 80 taken 20 times.
✓ Branch 79 → 81 taken 1 time.
✓ Branch 80 → 81 taken 20 times.
|
22 | : file.mimeType.toLatin1(); |
| 61 | 21 | builder.addAttachment({file.name, data, mime}); | |
| 62 | 22 | } | |
| 63 |
1/1✓ Branch 97 → 98 taken 64 times.
|
64 | builder.setMessageId(messageId); |
| 64 |
1/1✓ Branch 98 → 99 taken 64 times.
|
64 | result.bytes = builder.build(form == Form::Smtp ? Rfc2822Builder::BccHeader::Omit : Rfc2822Builder::BccHeader::Include); |
| 65 | 64 | result.messageId = builder.messageId(); | |
| 66 | 64 | return result; | |
| 67 | 95 | } | |
| 68 | |||
| 69 | 2 | QString ComposeMessage::wrapPlainText(const QString &text, int width) { | |
| 70 | 2 | QStringList out; | |
| 71 | 2 | bool signature = false; | |
| 72 |
4/7✓ Branch 2 → 3 taken 1 time.
✓ Branch 2 → 15 taken 1 time.
✓ Branch 4 → 5 taken 1 time.
✗ Branch 4 → 15 not taken.
✓ Branch 9 → 10 taken 1 time.
✗ Branch 87 → 88 not taken.
✗ Branch 87 → 89 not taken.
|
3 | static const QRegularExpression url(QStringLiteral("\\b(?:https?|ftp)://\\S+")); |
| 73 |
5/5✓ Branch 18 → 19 taken 2 times.
✓ Branch 19 → 20 taken 2 times.
✓ Branch 20 → 21 taken 2 times.
✓ Branch 71 → 22 taken 7 times.
✓ Branch 71 → 72 taken 2 times.
|
9 | for (const QString &line : text.split(QLatin1Char('\n'))) { |
| 74 |
2/2✓ Branch 25 → 26 taken 1 time.
✓ Branch 25 → 27 taken 6 times.
|
7 | if (line == QLatin1String("-- ")) signature = true; |
| 75 |
14/17✓ Branch 27 → 28 taken 5 times.
✓ Branch 27 → 38 taken 2 times.
✓ Branch 30 → 31 taken 5 times.
✓ Branch 31 → 32 taken 4 times.
✓ Branch 31 → 38 taken 1 time.
✓ Branch 33 → 34 taken 4 times.
✗ Branch 33 → 38 not taken.
✓ Branch 35 → 36 taken 4 times.
✓ Branch 36 → 37 taken 4 times.
✓ Branch 37 → 38 taken 1 time.
✓ Branch 37 → 39 taken 3 times.
✓ Branch 40 → 41 taken 4 times.
✓ Branch 40 → 42 taken 3 times.
✓ Branch 42 → 43 taken 4 times.
✓ Branch 42 → 45 taken 3 times.
✗ Branch 93 → 94 not taken.
✗ Branch 93 → 95 not taken.
|
11 | if (signature || line.startsWith(QLatin1Char('>')) || line.size() <= width || url.match(line).hasMatch()) { |
| 76 |
1/1✓ Branch 43 → 44 taken 4 times.
|
4 | out.append(line); |
| 77 | 4 | continue; | |
| 78 | } | ||
| 79 | 3 | QString rest = line; | |
| 80 |
2/2✓ Branch 65 → 47 taken 6 times.
✓ Branch 65 → 66 taken 2 times.
|
8 | while (rest.size() > width) { |
| 81 | 6 | qsizetype cut = rest.lastIndexOf(QLatin1Char(' '), width); | |
| 82 |
2/2✓ Branch 50 → 51 taken 1 time.
✓ Branch 50 → 55 taken 5 times.
|
6 | if (cut <= 0) cut = rest.indexOf(QLatin1Char(' '), width); |
| 83 |
2/2✓ Branch 55 → 56 taken 1 time.
✓ Branch 55 → 57 taken 5 times.
|
6 | if (cut <= 0) break; |
| 84 |
2/2✓ Branch 57 → 58 taken 5 times.
✓ Branch 58 → 59 taken 5 times.
|
5 | out.append(rest.left(cut)); |
| 85 |
1/1✓ Branch 60 → 61 taken 5 times.
|
5 | rest = rest.mid(cut + 1); |
| 86 | } | ||
| 87 |
1/1✓ Branch 66 → 67 taken 3 times.
|
3 | out.append(rest); |
| 88 | 5 | } | |
| 89 |
1/1✓ Branch 75 → 76 taken 2 times.
|
4 | return out.join(QLatin1Char('\n')); |
| 90 | 2 | } | |
| 91 |