PDA

View Full Version : Nice Solution to push Class on QVariant and take back...



patrik08
6th October 2013, 02:17
You like the Long or fast way to transport self made class on QObject?

Here a short way..





class Qmailf;

/// 6 okt. 2013 save on svn
/// great this to push class on qvariant
//// http://qt-gmail-access.googlecode.com/svn/trunk/GmailHand/mime_standard.h

template <class T> class VPtr {
public:

static T* asPtr(QVariant v) {
return (T *) v.value<void *>();
}

static QVariant asQVariant(T* ptr) {
return qVariantFromValue((void *) ptr);
}
};

/*
http://blog.bigpixel.ro/2010/04/storing-pointer-in-qvariant/
MyClass *p;
QVariant v = VPtr<MyClass>::asQVariant(p);

MyClass *p1 = VPtr<MyClass>::asPtr(v);
* MMime::MimeTypes help;
*/





Or the long long way...





class Qmailf {
public:
Qmailf(int id) : chunk(""), mime("txt"), file("file.txt"), ext("txt"), uuid(id) {

}
void SetChunk(const QByteArray& x) {
chunk = x;
///// qDebug() << "incomming <" << uuid << "> ";
}
/// set mime automatic
/// todo filename resolver from header && id from inline image
void SetMeta(const QString x) {
meta_header = x;
MMime::MimeTypes help;
mime = help.Contenttype_Resolver(x);
ext = help.MimeFromContent(mime);
QString def(QString("mail_header_%1.%2").arg(uuid).arg(ext));
file = def;
}
/// no make file or db take the chunk from mail and send at the right place
void SetFile(const QString dfile) {
MMime::MimeTypes fmime;
QFileInfo fi(dfile);
file = fi.fileName();
ext = fi.suffix();
mime = fmime.value(ext);
}
QByteArray Chunk() {
return chunk;
}
QString Mime() {
return mime;
}
int Uid() {
return uuid;
}
QString Filename() {
return file;
}
void Info() {
QByteArray text = QByteArray::fromBase64(chunk);
qDebug() << "Content-Type:" << Mime() << " - " << Filename();
qDebug() << "Meta:" << meta_header;
qDebug() << "Body first 28 chunk:" << text.mid(0,20).simplified();
text.clear();
}
private:
QByteArray chunk; // base64 encoded or other meta_heade having info
QString mime;
QString meta_header; /// meta info to create a file or o drupal node
QString file;
QString ext;
int uuid;
};

typedef QMap<int, QVariant> Attach_File_Map;
//// http://www.qtcentre.org/wiki/index.php?title=Using_custom_data_types_with_Qt

/// from this can create
/// mail dir format or other
/// i need to convert on drupal node and send on cms

struct ICmail {
quint64 uuid;
QString msgid;
quint64 multipart;
QString sender; //// long chunk history from one mail
QString md5; /// from incomming file server
QString from;
QString to;
QString subject;
QByteArray charcodec;
QString transferencoding;
QString language;
QString date;
QString txt;
QString html; // image inline base64 encodet
QString master;
QString boundary;
QString root_cmd;
QMap<int, QVariant> alist;
operator QVariant() const {
return QVariant::fromValue(*this);
}
};

/// todo a textstream for console to insert chort info from this mail

inline QDataStream& operator<<(QDataStream& out, const ICmail& mail) {
out << mail.from;
out << mail.subject;
out << mail.md5;
out << mail.date;
out << mail.boundary;
return out;
}
inline QDataStream& operator>>(QDataStream& in, ICmail& mail) {
in >> mail.from;
in >> mail.subject;
in >> mail.md5;
in >> mail.date;
in >> mail.boundary;
return in;
}

/*

<< mail.txt << ",\n"
<< mail.html << ",\n"
*/
inline QDebug &operator <<(QDebug debug, const ICmail &mail) {
debug.nospace() << "ICmail{start(\n"
<< mail.from << ",\n"
<< mail.subject << ",\nContent-Transfer-Encoding:"
<< mail.transferencoding << "\n,"
<< mail.date << "\n,"
<< mail.boundary << "\n,"
<< mail.multipart << "\n,\nRoot cmd Content-Type:\n"
<< mail.root_cmd << ")end}\n";

return debug.space();
}
Q_DECLARE_METATYPE(ICmail);

anda_skoa
6th October 2013, 09:57
The long way looks a lot shorter then the short way :)

The long way requires a Q_DECLARE_METATYPE, the short way requires a helper template and C-style casts.

Cheers,
_

wysota
6th October 2013, 14:58
The short way doesn't check validity of the cast.