#ifndef CARDBOARD_H
#define CARDBOARD_H

#include <QDeclarativeItem>
#include <QGraphicsScene>
#include <QEvent>
#include <QMessageService>
#include <QMessage>
#include <QDeclarativeView>
#include <QFile>

QTM_USE_NAMESPACE

class CardBoard : public QDeclarativeItem {

    Q_OBJECT

    QDeclarativeView *view;

public:
    CardBoard(QDeclarativeView *view) : QDeclarativeItem() {
        this->view = view;
    }

    CardBoard() : QDeclarativeItem() {
        this->view = view;
    }


    Q_INVOKABLE void sendMms() {
        QMessageService *pMessageManager = new QMessageService(this);
        QMessage message;
        message.setType(QMessage::Mms);
        message.setBody("Foo");
        QStringList images;
        images << "qrc:/images/1245944_74222603_854.jpg";
        message.appendAttachments(images);
        pMessageManager->compose(message);
    }

    Q_INVOKABLE void sendEmail() {
        QMessageService *pMessageManager = new QMessageService(this);
        QMessage message;
        message.setType(QMessage::Email);
        //message.setSubject("auto test");
        //message.setTo(QMessageAddress(QMessageAddress::Email, "swinkels@o2.pl"));
        QStringList *images = new QStringList();
        images->append("/home/user/MyDocs/Pictures/eCard_email.jpg");
        message.appendAttachments(*images);
        pMessageManager->compose(message);
        //pMessageManager->send(message);
    }

    Q_INVOKABLE bool fileExists(QString fileName) {
        QString fullName = "/home/user/MyDocs/Pictures/" + fileName;
        return QFile::exists(fullName);
    }


    Q_INVOKABLE bool saveJPG(QString fileName) {
        QPixmap pixmap = QPixmap::grabWidget(view);
        if (pixmap.isNull())
            qDebug() << "save pixmap is null";
        else
            qDebug() << "pixmap size:" << pixmap.width() << " x " << pixmap.height();

        QImage image = pixmap.toImage();
        QImage image32 = image.convertToFormat(QImage::Format_RGB32);
        QString fullName = "/home/user/MyDocs/Pictures/" + fileName;

        bool val = image32.save(fullName, "JPG", 80);
        if (val == false)
            qDebug() << "saving failed: " << fullName;
        else
            qDebug() << "saving OK: " << fullName;
        return val;
    }


};

#endif // CARDBOARD_H
