1
/***************************************************************************
2
* Copyright (C) 2010 by Nikhil Marathe <nsm.nikhil@gmail.com> *
3
* Vardhman Jain <vardhman@gmail.com> *
4
* Gilles Caulier <caulier.gilles@gmail.com> *
5
* Michał Ziąbkowski <mziab@o2.pl> *
7
* This program is free software; you can redistribute it and/or modify *
8
* it under the terms of the GNU General Public License as published by *
9
* the Free Software Foundation; either version 2 of the License, or *
10
* (at your option) any later version. *
12
* This program is distributed in the hope that it will be useful, *
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15
* GNU General Public License for more details. *
17
* You should have received a copy of the GNU General Public License *
18
* along with this program; if not, write to the *
19
* Free Software Foundation, Inc., *
20
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . *
21
***************************************************************************/
28
#include <kmimetype.h>
31
#include <kapplication.h>
34
ImgurServer::ImgurServer(const QString &server)
36
if (server.isEmpty()) {
37
m_server = IMGUR_SERVER;
42
m_boundary = "----------";
43
m_boundary += KRandom::randomString(42 + 13).toAscii();
46
ImgurServer::~ImgurServer()
50
void ImgurServer::readKIOData(KIO::Job *job, const QByteArray &data)
54
if (data.length() == 0) {
61
void ImgurServer::finished(KJob *job)
65
if (m_data.length() == 0) {
70
QString reply(m_data);
71
QRegExp re(".*<imgur_page>([^<]+)</imgur_page>.*");
73
if( !re.exactMatch(reply) ) {
78
QString pasteUrl = re.cap(1).replace("&", "&");
79
emit postFinished(pasteUrl);
82
// taken from flickr KIPI Plugin
83
void ImgurServer::finish()
93
// taken from flickr KIPI Plugin
94
bool ImgurServer::addPair(const QString& name, const QString& value)
101
str += "Content-Disposition: form-data; name=\"";
102
str += name.toAscii();
105
str += value.toUtf8();
108
m_buffer.append(str);
112
// taken from flickr KIPI Plugin
113
bool ImgurServer::addFile(const QString& name,const QString& path)
115
KMimeType::Ptr ptr = KMimeType::findByUrl(path);
116
QString mime = ptr->name();
117
if (mime.isEmpty()) {
118
// if we ourselves can't determine the mime of the local file,
119
// very unlikely the remote site will be able to identify it
123
QFile imageFile(path);
124
if (!imageFile.open(QIODevice::ReadOnly)) {
128
QByteArray imageData = imageFile.readAll();
136
str += "Content-Disposition: form-data; name=\"";
137
str += name.toAscii();
139
str += "filename=\"";
140
str += QFile::encodeName(KUrl(path).fileName()).replace(".tmp", ".jpg");
143
str += "Content-Type: ";
144
str += mime.toAscii();
147
m_buffer.append(str);
149
int oldSize = m_buffer.size();
150
m_buffer.resize(oldSize + imageData.size() + 2);
151
memcpy(m_buffer.data() + oldSize, imageData.data(), imageData.size());
152
m_buffer[m_buffer.size()-2] = '\r';
153
m_buffer[m_buffer.size()-1] = '\n';
158
void ImgurServer::post(const QString& content)
161
KUrl url(QString("%1").arg(m_server));
162
addFile( "image", content );
164
// key associated with plasma-devel@kde.org
165
// thanks to Alan Schaaf of Imgur ( alan@imgur.com )
166
addPair( "key", "d0757bc2e94a0d4652f28079a0be9379" );
168
KIO::TransferJob *tf = KIO::http_post(url, m_buffer, KIO::HideProgressInfo);
170
tf->addMetaData("content-type","Content-Type: multipart/form-data; boundary=" + m_boundary);
172
connect(tf, SIGNAL(data(KIO::Job*, const QByteArray&)),
173
this, SLOT(readKIOData(KIO::Job*, const QByteArray&)));
174
connect(tf, SIGNAL(result(KJob *)), this, SLOT(finished(KJob *)));