1
/***************************************************************************
2
* Copyright (C) 2007 by Artur Duque de Souza <morpheuz@gmail.com> *
3
* Vardhman Jain <vardhman@gmail.com> *
4
* Gilles Caulier <caulier.gilles@gmail.com> *
6
* This program is free software; you can redistribute it and/or modify *
7
* it under the terms of the GNU General Public License as published by *
8
* the Free Software Foundation; either version 2 of the License, or *
9
* (at your option) any later version. *
11
* This program is distributed in the hope that it will be useful, *
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14
* GNU General Public License for more details. *
16
* You should have received a copy of the GNU General Public License *
17
* along with this program; if not, write to the *
18
* Free Software Foundation, Inc., *
19
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . *
20
***************************************************************************/
22
#include "imagebinca.h"
27
#include <kmimetype.h>
30
#include <kapplication.h>
33
ImagebinCAServer::ImagebinCAServer(const QString &server)
35
if (server.isEmpty()) {
36
m_server = IMAGEBINCA_SERVER;
41
m_privacy = 0; //config.readEntry("imagebinPrivacy", 0);
43
m_boundary = "----------";
44
m_boundary += KRandom::randomString(42 + 13).toAscii();
47
ImagebinCAServer::~ImagebinCAServer()
51
void ImagebinCAServer::finished(KJob *job)
55
if (_data.length() == 0) {
56
kDebug() << "Error!!! " << _data;
62
QRegExp re("<p>You can find this at <a href='([^<]+)'>([^<]+)</a></p>");
63
if (!re.exactMatch(url)) {
67
QString pasteUrl = re.cap(1);
69
emit postFinished(pasteUrl);
73
void ImagebinCAServer::readKIOData(KIO::Job *job, const QByteArray &data)
77
if (data.length() == 0) {
84
// taken from flickr KIPI Plugin
85
void ImagebinCAServer::finish()
95
// taken from flickr KIPI Plugin
96
bool ImagebinCAServer::addPair(const QString& name, const QString& value)
103
str += "Content-Disposition: form-data; name=\"";
104
str += name.toAscii();
107
str += value.toUtf8();
110
m_buffer.append(str);
114
// taken from flickr KIPI Plugin
115
bool ImagebinCAServer::addFile(const QString& name,const QString& path)
117
KMimeType::Ptr ptr = KMimeType::findByUrl(path);
118
QString mime = ptr->name();
119
if (mime.isEmpty()) {
120
// if we ourselves can't determine the mime of the local file,
121
// very unlikely the remote site will be able to identify it
125
QFile imageFile(path);
126
if (!imageFile.open(QIODevice::ReadOnly)) {
130
QByteArray imageData = imageFile.readAll();
138
str += "Content-Disposition: form-data; name=\"";
139
str += name.toAscii();
141
str += "filename=\"";
142
str += QFile::encodeName(KUrl(path).fileName()).replace(".tmp", ".jpg");
145
str += "Content-Type: ";
146
str += mime.toAscii();
149
m_buffer.append(str);
151
int oldSize = m_buffer.size();
152
m_buffer.resize(oldSize + imageData.size() + 2);
153
memcpy(m_buffer.data() + oldSize, imageData.data(), imageData.size());
154
m_buffer[m_buffer.size()-2] = '\r';
155
m_buffer[m_buffer.size()-1] = '\n';
161
void ImagebinCAServer::post(const QString& content)
163
KUrl url(QString("%1/upload.php").arg(m_server));
164
addPair("t", "file");
165
addPair("name", QFile::encodeName(KUrl(content).fileName()));
166
addPair("tags", "plasma");
175
addPair("adult", adult);
176
addFile("f", content);
181
KIO::TransferJob *tf = KIO::http_post(url, m_buffer, KIO::HideProgressInfo);
183
tf->addMetaData("content-type","Content-Type: multipart/form-data; boundary=" + m_boundary);
185
connect(tf, SIGNAL(data(KIO::Job*, const QByteArray&)),
186
this, SLOT(readKIOData(KIO::Job*, const QByteArray&)));
188
connect(tf, SIGNAL(result(KJob *)), this, SLOT(finished(KJob *)));