Warning, file /graphics/kipi-plugins/facebook/mpform.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /* ============================================================
0002  *
0003  * This file is a part of KDE project
0004  *
0005  *
0006  * Date        : 2008-12-28
0007  * Description : a kipi plugin to export images to Facebook web service
0008  *
0009  * Copyright (C) 2005-2008 by Vardhman Jain <vardhman at gmail dot com>
0010  * Copyright (C) 2008-2018 by Gilles Caulier <caulier dot gilles at gmail dot com>
0011  * Copyright (C) 2008-2009 by Luka Renko <lure at kubuntu dot org>
0012  *
0013  * This program is free software; you can redistribute it
0014  * and/or modify it under the terms of the GNU General
0015  * Public License as published by the Free Software Foundation;
0016  * either version 2, or (at your option) any later version.
0017  *
0018  * This program is distributed in the hope that it will be useful,
0019  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0020  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0021  * GNU General Public License for more details.
0022  *
0023  * ============================================================ */
0024 
0025 #include "mpform.h"
0026 
0027 // Qt includes
0028 
0029 #include <QFile>
0030 #include <QMimeDatabase>
0031 #include <QMimeType>
0032 #include <QUrl>
0033 #include <QString>
0034 
0035 // Local includes
0036 
0037 #include "kipiplugins_debug.h"
0038 #include "kputil.h"
0039 
0040 namespace KIPIFacebookPlugin
0041 {
0042 
0043 MPForm::MPForm()
0044     : m_boundary(KIPIPlugins::KPRandomGenerator::randomString(42 + 13).toLatin1())
0045 {
0046     reset();
0047 }
0048 
0049 MPForm::~MPForm()
0050 {
0051 }
0052 
0053 void MPForm::reset()
0054 {
0055     m_buffer.resize(0);
0056     QByteArray str(contentType().toLatin1());
0057     str += "\r\n";
0058     str += "MIME-version: 1.0";
0059     str += "\r\n\r\n";
0060     m_buffer.append(str);
0061 }
0062 
0063 void MPForm::finish()
0064 {
0065     QByteArray str;
0066     str += "--";
0067     str += m_boundary;
0068     str += "--";
0069     m_buffer.append(str);
0070 }
0071 
0072 void MPForm::addPair(const QString& name, const QString& value)
0073 {
0074     QByteArray str;
0075     QString content_length = QString::number(value.length());
0076 
0077     str += "--";
0078     str += m_boundary;
0079     str += "\r\n";
0080 
0081     if (!name.isEmpty())
0082     {
0083         str += "Content-Disposition: form-data; name=\"";
0084         str += name.toLatin1();
0085         str += "\"\r\n";
0086     }
0087 
0088     str += "\r\n";
0089     str += value.toUtf8();
0090     str += "\r\n";
0091 
0092     m_buffer.append(str);
0093 }
0094 
0095 bool MPForm::addFile(const QString& name, const QString& path)
0096 {
0097     QMimeDatabase db;
0098     QMimeType ptr = db.mimeTypeForUrl(QUrl::fromLocalFile(path));
0099     QString mime  = ptr.name();
0100 
0101     if (mime.isEmpty())
0102         return false;
0103 
0104     QFile imageFile(path);
0105 
0106     if (!imageFile.open(QIODevice::ReadOnly))
0107         return false;
0108 
0109     QByteArray imageData = imageFile.readAll();
0110 
0111     //QString file_size = QString::number(imageFile.size());
0112     imageFile.close();
0113 
0114     QByteArray str;
0115     str += "--";
0116     str += m_boundary;
0117     str += "\r\n";
0118     str += "Content-Disposition: form-data; filename=\"";
0119     str += QFile::encodeName(name);
0120     str += "\"\r\n";
0121     //str += "Content-Length: ";
0122     //str += file_size.toAscii();
0123     //str += "\r\n";
0124     str += "Content-Type: ";
0125     str += mime.toLatin1();
0126     str += "\r\n\r\n";
0127 
0128     m_buffer.append(str);
0129     m_buffer.append(imageData);
0130     m_buffer.append("\r\n");
0131 
0132     return true;
0133 }
0134 
0135 QString MPForm::contentType() const
0136 {
0137     return QString::fromLatin1("multipart/form-data; boundary=") + QString::fromLatin1(m_boundary);
0138 }
0139 
0140 QString MPForm::boundary() const
0141 {
0142     return QString::fromLatin1(m_boundary);
0143 }
0144 
0145 QByteArray MPForm::formData() const
0146 {
0147     return m_buffer;
0148 }
0149 
0150 } // namespace KIPIFacebookPlugin