| 1 | /* Copyright (C) 2016 - 2017 Dan Chapman <[email protected]> |
| 2 | |
| 3 | This file is part of Dekko email client for Ubuntu devices |
| 4 | |
| 5 | This program is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU General Public License as |
| 7 | published by the Free Software Foundation; either version 2 of |
| 8 | the License or (at your option) version 3 |
| 9 | |
| 10 | This program is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | GNU General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU General Public License |
| 16 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | #include "Attachments.h" |
| 19 | #include <QDebug> |
| 20 | #include <QtQml> |
| 21 | #include <QFile> |
| 22 | #include <QFileInfo> |
| 23 | #include <QQmlEngine> |
| 24 | #include <QUrlQuery> |
| 25 | #include <QNetworkRequest> |
| 26 | #include <QMimeDatabase> |
| 27 | #include <qmailaccount.h> |
| 28 | #include <qmailnamespace.h> |
| 29 | #include <MailServiceClient.h> |
| 30 | #include <Paths.h> |
| 31 | |
| 32 | Attachments::Attachments(QObject *parent) : QObject(parent), |
| 33 | m_model(0) |
| 34 | { |
| 35 | m_model = new QQmlObjectListModel<Attachment>(this); |
| 36 | emit modelChanged(); |
| 37 | } |
| 38 | |
| 39 | void Attachments::setMessageId(const QMailMessageId &id) |
| 40 | { |
| 41 | m_id = id; |
| 42 | QMailMessage msg(m_id); |
| 43 | |
| 44 | Q_FOREACH(auto pLocation, msg.findAttachmentLocations()) { |
| 45 | Attachment *a = new Attachment(0); |
| 46 | a->init(m_id, pLocation); |
| 47 | qDebug() << "Attachment name: " << a->displayName(); |
| 48 | qDebug() << "Attachment size: " << a->size(); |
| 49 | m_model->append(a); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | Attachment::Attachment(QObject *parent) : QObject(parent), |
| 54 | m_type(Text), m_fetching(false), m_qnam(0), m_reply(0), m_hasRefs(false) |
| 55 | { |
| 56 | } |
| 57 | |
| 58 | Attachment::Attachment(QObject *parent, const QString &attachment, const Attachment::PartType &partType, const Attachment::Disposition &disposition): |
| 59 | QObject(parent), m_fetching(false), m_partType(partType), m_disposition(disposition), m_hasRefs(false) |
| 60 | { |
| 61 | switch(partType) { |
| 62 | case Message: |
| 63 | { |
| 64 | const QMailMessageId mid(attachment.toULongLong()); |
| 65 | const QMailMessage msg(mid); |
| 66 | QMailAccount srcAccount(msg.parentAccountId()); |
| 67 | bool viaReference((srcAccount.status() & QMailAccount::CanReferenceExternalData) && |
| 68 | (srcAccount.status() & QMailAccount::CanTransmitViaReference)); |
| 69 | // QMailMessageContentType cType; |
| 70 | // if (msg.multipartType() == QMailMessage::MultipartNone) { |
| 71 | // cType = msg.contentType(); |
| 72 | // } else { |
| 73 | // // wrap the message in a rfc822 |
| 74 | // cType = |
| 75 | // } |
| 76 | QMailMessageContentType cType(QByteArrayLiteral("message/rfc822")); |
| 77 | QMailMessageContentDisposition d(QMailMessageContentDisposition::Attachment); |
| 78 | d.setFilename(msg.subject().simplified().toUtf8() + QByteArrayLiteral(".eml")); |
| 79 | d.setSize(msg.size()); |
| 80 | if (viaReference) { |
| 81 | m_part = QMailMessagePart::fromMessageReference(msg.id(), d, cType, msg.transferEncoding()); |
| 82 | m_hasRefs = true; |
| 83 | } else { |
| 84 | m_part = QMailMessagePart::fromData(msg.toRfc2822(), d, cType, msg.transferEncoding()); |
| 85 | } |
| 86 | break; |
| 87 | } |
| 88 | case MessagePart: |
| 89 | { |
| 90 | QMailMessagePart::Location location(attachment); |
| 91 | const QMailMessage msg(location.containingMessageId()); |
| 92 | const QMailMessagePart &existingPart(msg.partAt(location)); |
| 93 | QMailMessageContentDisposition existingDisposition(existingPart.contentDisposition()); |
| 94 | |
| 95 | QMailMessageContentDisposition d(disposition == Inline ? QMailMessageContentDisposition::Inline : QMailMessageContentDisposition::Attachment); |
| 96 | d.setFilename(existingDisposition.filename()); |
| 97 | d.setSize(existingDisposition.size()); |
| 98 | m_part = QMailMessagePart::fromPartReference(existingPart.location(), d, existingPart.contentType(), existingPart.transferEncoding()); |
| 99 | break; |
| 100 | } |
| 101 | case File: |
| 102 | QFileInfo fi(attachment); |
| 103 | QString partName(fi.fileName()); |
| 104 | QString filePath(fi.absoluteFilePath()); |
| 105 | |
| 106 | QString mimeType(QMimeDatabase().mimeTypeForFile(attachment).name()); |
| 107 | QMailMessageContentType type(mimeType.toLatin1()); |
| 108 | type.setName(partName.toLatin1()); |
| 109 | |
| 110 | QMailMessageContentDisposition d(disposition == Inline ? QMailMessageContentDisposition::Inline : QMailMessageContentDisposition::Attachment); |
| 111 | d.setFilename(partName.toLatin1()); |
| 112 | d.setSize(fi.size()); |
| 113 | |
| 114 | QMailMessageBodyFwd::TransferEncoding encoding(QMailMessageBody::Base64); |
| 115 | QMailMessageBodyFwd::EncodingStatus encodingStatus(QMailMessageBody::RequiresEncoding); |
| 116 | if (mimeType == "message/rfc822") { |
| 117 | encoding = QMailMessageBody::NoEncoding; |
| 118 | encodingStatus = QMailMessageBody::AlreadyEncoded; |
| 119 | } |
| 120 | m_part = QMailMessagePart::fromFile(filePath, d, type, encoding, encodingStatus); |
| 121 | m_filePath = filePath; |
| 122 | m_url = m_filePath; |
| 123 | break; |
| 124 | } |
| 125 | emit attachmentChanged(); |
| 126 | } |
| 127 | |
| 128 | QString Attachment::displayName() |
| 129 | { |
| 130 | // // if this is an rfc822 message attachment then |
| 131 | // // the displayname is either empty or just not useable. |
| 132 | // // Fall back to the subject of the attached message |
| 133 | // if (isRfc822()) { |
| 134 | // QMailMessage msgPart = QMailMessage::fromRfc2822(m_part.body().data(QMailMessageBody::Decoded)); |
| 135 | // qDebug() << |
| 136 | // if (!msgPart.subject().isEmpty()) { |
| 137 | // return msgPart.subject(); |
| 138 | // } |
| 139 | //// const bool nameIsEmpty = m_part.contentDisposition().parameter(QByteArrayLiteral("name")).isEmpty(); |
| 140 | //// const bool fnameIsEmpty = m_part.contentDisposition().parameter(QByteArrayLiteral("filename")).isEmpty(); |
| 141 | //// const bool ctypeNameIsEmpty = m_part.contentType().name().isEmpty(); |
| 142 | //// if (nameIsEmpty && fnameIsEmpty && ctypeNameIsEmpty) { |
| 143 | |
| 144 | //// } |
| 145 | // } |
| 146 | return m_part.displayName(); |
| 147 | } |
| 148 | |
| 149 | QString Attachment::mimeType() const |
| 150 | { |
| 151 | return QString::fromLatin1(m_part.contentType().content()); |
| 152 | } |
| 153 | |
| 154 | QString Attachment::size() |
| 155 | { |
| 156 | return sizeToReadableString(m_part.contentDisposition().size()); |
| 157 | } |
| 158 | |
| 159 | int Attachment::sizeInBytes() const |
| 160 | { |
| 161 | return m_part.contentDisposition().size(); |
| 162 | } |
| 163 | |
| 164 | Attachment::Type Attachment::type() const |
| 165 | { |
| 166 | return m_type; |
| 167 | } |
| 168 | |
| 169 | bool Attachment::contentAvailable() const |
| 170 | { |
| 171 | return m_part.contentAvailable(); |
| 172 | } |
| 173 | |
| 174 | QString Attachment::location() const |
| 175 | { |
| 176 | return m_part.location().toString(true); |
| 177 | } |
| 178 | |
| 179 | QString Attachment::url() const |
| 180 | { |
| 181 | return m_url; |
| 182 | } |
| 183 | |
| 184 | bool Attachment::fetchInProgress() const |
| 185 | { |
| 186 | return m_fetching; |
| 187 | } |
| 188 | |
| 189 | QString Attachment::mimeTypeIcon() const |
| 190 | { |
| 191 | return QStringLiteral("%1-symbolic").arg(QMimeDatabase().mimeTypeForName(mimeType()).genericIconName()); |
| 192 | } |
| 193 | |
| 194 | typedef QMap<QString, Attachment::Type> CTypes; |
| 195 | static CTypes types() |
| 196 | { |
| 197 | CTypes t; |
| 198 | t["image"] = Attachment::Type::Image; |
| 199 | t["audio"] = Attachment::Type::Media; |
| 200 | t["video"] = Attachment::Type::Media; |
| 201 | t["text"] = Attachment::Type::Text; |
| 202 | t["multipart"] = Attachment::Type::Multipart; |
| 203 | return t; |
| 204 | } |
| 205 | |
| 206 | void Attachment::init(const QMailMessageId &id, const QMailMessagePartContainer::Location &location) |
| 207 | { |
| 208 | static const CTypes typeMap(types()); |
| 209 | m_location = location; |
| 210 | m_id = id; |
| 211 | m_part = QMailMessage(m_id).partAt(m_location); |
| 212 | |
| 213 | QString type = m_part.contentType().type().toLower(); |
| 214 | CTypes::const_iterator it = typeMap.find(type); |
| 215 | if (it != typeMap.end()) { |
| 216 | m_type = it.value(); |
| 217 | } else { |
| 218 | m_type = Type::Other; |
| 219 | } |
| 220 | emit attachmentChanged(); |
| 221 | } |
| 222 | |
| 223 | void Attachment::addToMessage(QMailMessage &msg) |
| 224 | { |
| 225 | switch(m_partType) { |
| 226 | case Message: |
| 227 | case MessagePart: |
| 228 | { |
| 229 | msg.appendPart(m_part); |
| 230 | if (m_hasRefs) { |
| 231 | msg.setStatus(QMailMessage::HasReferences, true); |
| 232 | msg.setStatus(QMailMessage::HasUnresolvedReferences, true); |
| 233 | } |
| 234 | break; |
| 235 | } |
| 236 | case File: |
| 237 | msg.appendPart(m_part); |
| 238 | // Store the location of this file for future reference |
| 239 | const QMailMessagePart &mailPart(msg.partAt(msg.partCount() - 1)); |
| 240 | QString name("qmf-file-location-" + mailPart.location().toString(false)); |
| 241 | msg.setCustomField(name, m_filePath); |
| 242 | break; |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | void Attachment::open(QObject *qmlObject) |
| 247 | { |
| 248 | if (m_partType == File) { |
| 249 | qDebug() << "Fixme: opening attachments of part type File not yet implemented"; |
| 250 | return; |
| 251 | } |
| 252 | |
| 253 | m_fetching = true; |
| 254 | m_url = QString(); |
| 255 | emit progressChanged(); |
| 256 | |
| 257 | if (!contentAvailable()) { |
| 258 | QQmlEngine *engine = qmlEngine(qmlObject); |
| 259 | Q_ASSERT(engine); |
| 260 | m_qnam = engine->networkAccessManagerFactory()->create(this); |
| 261 | fetch(); |
| 262 | } else { |
| 263 | handlePartFetched(); |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | void Attachment::fetch() |
| 268 | { |
| 269 | if (!m_qnam) { |
| 270 | // TODO emit some error |
| 271 | m_fetching = false; |
| 272 | emit progressChanged(); |
| 273 | return; |
| 274 | } |
| 275 | if (m_reply) { |
| 276 | disconnect(m_reply, 0, this, 0); |
| 277 | delete m_reply; |
| 278 | m_reply = 0; |
| 279 | } |
| 280 | m_reply = m_qnam->get(QNetworkRequest(partFetchUrl())); |
| 281 | connect(m_reply, SIGNAL(finished()), this, SLOT(handlePartFetched())); |
| 282 | } |
| 283 | |
| 284 | void Attachment::handlePartFetched() |
| 285 | { |
| 286 | m_part = QMailMessage(m_id).partAt(m_location); |
| 287 | if (contentAvailable()) { |
| 288 | QString filepath = writePartToFile(); |
| 289 | if (!filepath.isEmpty()) { |
| 290 | if (!filepath.startsWith(QStringLiteral("file://"))) { |
| 291 | filepath.prepend(QStringLiteral("file://")); |
| 292 | } |
| 293 | m_url = filepath; |
| 294 | emit urlChanged(); |
| 295 | emit readyToOpen(m_url); |
| 296 | } |
| 297 | } else { |
| 298 | qDebug() << "[Attachments::handlePartFetched] content still not available"; |
| 299 | } |
| 300 | m_fetching = false; |
| 301 | emit progressChanged(); |
| 302 | } |
| 303 | |
| 304 | QString Attachment::writePartToFile() |
| 305 | { |
| 306 | QMailMessage msg(m_id); |
| 307 | QMailAccountId accountId = msg.parentAccountId(); |
| 308 | QString attachmentPath = Paths::cacheLocationForFile( |
| 309 | QStringLiteral("attachments/%1/%2").arg( |
| 310 | QString::number(accountId.toULongLong()), m_location.toString(true))); |
| 311 | QString attachmentFile = attachmentPath + "/" + displayName(); |
| 312 | QFile file(attachmentFile); |
| 313 | QString path; |
| 314 | if (!file.exists()) { |
| 315 | // refresh the part |
| 316 | m_part = QMailMessage(m_id).partAt(m_location); |
| 317 | if (m_part.hasBody()) { |
| 318 | path = m_part.writeBodyTo(attachmentPath); |
| 319 | } else { |
| 320 | return QString(); |
| 321 | } |
| 322 | } else { |
| 323 | path = attachmentFile; |
| 324 | } |
| 325 | qDebug() << "AttachmentPath" << path; |
| 326 | return QFileInfo(path).absoluteFilePath(); |
| 327 | } |
| 328 | |
| 329 | QUrl Attachment::partFetchUrl() |
| 330 | { |
| 331 | QUrl url; |
| 332 | url.setScheme(QStringLiteral("dekko-part")); |
| 333 | url.setHost(QStringLiteral("msg")); |
| 334 | QUrlQuery query; |
| 335 | query.addQueryItem(QStringLiteral("messageId"), QString::number(m_id.toULongLong())); |
| 336 | query.addQueryItem(QStringLiteral("location"), m_location.toString(true)); |
| 337 | url.setQuery(query); |
| 338 | return url; |
| 339 | } |
| 340 | |
| 341 | QString Attachment::sizeToReadableString(const int &size) |
| 342 | { |
| 343 | if(size < 1024) |
| 344 | return QObject::tr("%n byte(s)", "", size); |
| 345 | else if(size < (1024 * 1024)) |
| 346 | return QObject::tr("%1 KB").arg(((float)size)/1024.0, 0, 'f', 1); |
| 347 | else if(size < (1024 * 1024 * 1024)) |
| 348 | return QObject::tr("%1 MB").arg(((float)size)/(1024.0 * 1024.0), 0, 'f', 1); |
| 349 | else |
| 350 | return QObject::tr("%1 GB").arg(((float)size)/(1024.0 * 1024.0 * 1024.0), 0, 'f', 1); |
| 351 | } |
| 352 | |
| 353 | bool Attachment::isRfc822() |
| 354 | { |
| 355 | return (m_part.contentType().type().toLower() == "message") && |
| 356 | (m_part.contentType().subType().toLower() == "rfc822"); |
| 357 | } |