2 * Copyright (C) 2011, Jamie Thompson
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License as published by the Free Software Foundation; either
7 * version 3 of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public
15 * License along with this program; If not, see
16 * <http://www.gnu.org/licenses/>.
19 #include "Attachment.h"
21 #include <QCryptographicHash>
24 #include <QTextStream>
31 #include <rtcom-eventlogger/eventlogger-attach-iter.h>
33 Attachment::Attachment(QString path, QString description) :
35 m_Description(description),
37 m_DeleteFileOnDestruction(true)
39 if(m_FileHandle.open(QFile::WriteOnly | QFile::Unbuffered))
41 m_Stream = new QTextStream(&m_FileHandle);
44 m_Stream->setCodec("UTF8");
47 throw std::runtime_error(QString("Unable to create attachment stream: '%1'").arg(m_Path).toStdString());
50 throw std::runtime_error(QString("Unable to open attachment file: '%1'. The reason was: %2").arg(m_Path).arg(m_FileHandle.errorString()).toStdString());
53 Attachment::~Attachment()
60 if(m_DeleteFileOnDestruction && m_Path.length() > 0)
61 QFile::remove(m_Path);
64 Attachment::Attachment(const _RTComElAttachment& attachment) :
65 m_Path(attachment.path),
66 m_Description(attachment.desc),
68 m_DeleteFileOnDestruction(attachment.id < 1) // Don't delete the file if we have a valid attachment ID
70 if(m_FileHandle.open(QFile::ReadWrite))
72 m_Stream = new QTextStream(&m_FileHandle);
75 m_Stream->setCodec("UTF8");
78 throw std::runtime_error(QString("Unable to create attachment stream: '%1'").arg(m_Path).toStdString());
81 throw std::runtime_error(QString("Unable to open attachment file: '%1'. The reason was: %2").arg(m_Path).arg(m_FileHandle.errorString()).toStdString());
84 RTComElAttachment * Attachment::toRTComAttachment() const
86 RTComElAttachment *attachment(
87 rtcom_el_attachment_new(
88 g_strdup(Path().toUtf8()),
89 g_strdup(Description().toUtf8())));
93 void Attachment::freeRTComContents(RTComElAttachment &attachment)
95 g_free(attachment.path);
96 attachment.path = NULL;
98 g_free(attachment.desc);
99 attachment.desc = NULL;
102 const uint Attachment::HashCode() const
104 qDebug() << "Hashing attachment: " << Path();
107 if(file.open(QFile::ReadOnly))
109 uchar *memory(file.map(0, file.size()));
113 data.fromRawData((char*)memory, file.size());
114 uint hashcode = QCryptographicHash::hash(data, QCryptographicHash::Md5).toUInt();
121 throw std::runtime_error(QString("Unable to map attachment file: %1").arg(Path()).toStdString());
124 throw std::runtime_error(QString("Unable to open attachment file: '%1'. The reason was: %2").arg(m_Path).arg(m_FileHandle.errorString()).toStdString());
127 QDebug operator<<(QDebug dbg, Attachment& attachment)
129 dbg.nospace() << "Path:\t" << attachment.Path() << "\n";
130 dbg.nospace() << "Desc:\t" << attachment.Description() << "\n";