~ubuntu-branches/ubuntu/oneiric/kdeplasma-addons/oneiric

« back to all changes in this revision

Viewing changes to dataengines/pastebin/backends/imgur.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Thomas
  • Date: 2010-05-25 09:50:14 UTC
  • mfrom: (1.1.28 upstream)
  • Revision ID: james.westby@ubuntu.com-20100525095014-6mlrm9z9bkws0zkt
Tags: 4:4.4.80-0ubuntu1
* New upstream beta release:
  - Bump kde-sc-dev-latest build-dep version to 4.4.80
  - Refresh kubuntu_04_kimpanel_disable_scim.diff
  - Update various .install files
  - Drop liblancelot0a and liblancelot-dev packages; Upstream has broken ABI
    without an .so version bump, and after discussion with Debian it was
    decided it was not worth it to ship an unstable library.
  - Add liblancelot files to plasma-widget-lancelot, adding appropriate
    Replaces: entries
* Switch to source format 3.0 (quilt):
  - Bump debhelper build-depend version to 7.3.16 or greater

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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>             *
 
6
 *                                                                         *
 
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.                                   *
 
11
 *                                                                         *
 
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.                          *
 
16
 *                                                                         *
 
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
 ***************************************************************************/
 
22
 
 
23
#include "imgur.h"
 
24
 
 
25
#include <QFile>
 
26
#include <QString>
 
27
 
 
28
#include <kmimetype.h>
 
29
#include <kurl.h>
 
30
#include <krandom.h>
 
31
#include <kapplication.h>
 
32
#include <KDebug>
 
33
 
 
34
ImgurServer::ImgurServer(const QString &server)
 
35
{
 
36
    if (server.isEmpty()) {
 
37
        m_server = IMGUR_SERVER;
 
38
    } else {
 
39
        m_server = server;
 
40
    }
 
41
 
 
42
    m_boundary  = "----------";
 
43
    m_boundary += KRandom::randomString(42 + 13).toAscii();
 
44
}
 
45
 
 
46
ImgurServer::~ImgurServer()
 
47
{
 
48
}
 
49
 
 
50
void ImgurServer::readKIOData(KIO::Job *job, const QByteArray &data)
 
51
{
 
52
    Q_UNUSED(job);
 
53
 
 
54
    if (data.length() == 0) {
 
55
        return;
 
56
    }
 
57
 
 
58
    m_data.append(data);
 
59
}
 
60
 
 
61
void ImgurServer::finished(KJob *job)
 
62
{
 
63
    Q_UNUSED(job);
 
64
 
 
65
    if (m_data.length() == 0) {
 
66
        emit postError();
 
67
        return;
 
68
    }
 
69
 
 
70
    QString reply(m_data);
 
71
    QRegExp re(".*<imgur_page>([^<]+)</imgur_page>.*");
 
72
 
 
73
    if( !re.exactMatch(reply) ) {
 
74
        emit postError();
 
75
        return;
 
76
    }
 
77
 
 
78
    QString pasteUrl = re.cap(1).replace("&amp;", "&");
 
79
    emit postFinished(pasteUrl);
 
80
}
 
81
 
 
82
// taken from flickr KIPI Plugin
 
83
void ImgurServer::finish()
 
84
{
 
85
    QByteArray str;
 
86
    str += "--";
 
87
    str += m_boundary;
 
88
    str += "--";
 
89
 
 
90
    m_buffer.append(str);
 
91
}
 
92
 
 
93
// taken from flickr KIPI Plugin
 
94
bool ImgurServer::addPair(const QString& name, const QString& value)
 
95
{
 
96
     QByteArray str;
 
97
 
 
98
     str += "--";
 
99
     str += m_boundary;
 
100
     str += "\r\n";
 
101
     str += "Content-Disposition: form-data; name=\"";
 
102
     str += name.toAscii();
 
103
     str += "\"";
 
104
     str += "\r\n\r\n";
 
105
     str += value.toUtf8();
 
106
     str += "\r\n";
 
107
 
 
108
     m_buffer.append(str);
 
109
     return true;
 
110
}
 
111
 
 
112
// taken from flickr KIPI Plugin
 
113
bool ImgurServer::addFile(const QString& name,const QString& path)
 
114
{
 
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
 
120
        return false;
 
121
    }
 
122
 
 
123
    QFile imageFile(path);
 
124
    if (!imageFile.open(QIODevice::ReadOnly)) {
 
125
        return false;
 
126
    }
 
127
 
 
128
    QByteArray imageData = imageFile.readAll();
 
129
    imageFile.close();
 
130
 
 
131
    QByteArray str;
 
132
 
 
133
    str += "--";
 
134
    str += m_boundary;
 
135
    str += "\r\n";
 
136
    str += "Content-Disposition: form-data; name=\"";
 
137
    str += name.toAscii();
 
138
    str += "\"; ";
 
139
    str += "filename=\"";
 
140
    str += QFile::encodeName(KUrl(path).fileName()).replace(".tmp", ".jpg");
 
141
    str += "\"";
 
142
    str += "\r\n";
 
143
    str += "Content-Type: ";
 
144
    str +=  mime.toAscii();
 
145
    str += "\r\n\r\n";
 
146
 
 
147
    m_buffer.append(str);
 
148
 
 
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';
 
154
 
 
155
    return true;
 
156
}
 
157
 
 
158
void ImgurServer::post(const QString& content)
 
159
{
 
160
    m_data.clear();
 
161
    KUrl url(QString("%1").arg(m_server));
 
162
    addFile( "image", content );
 
163
 
 
164
    // key associated with plasma-devel@kde.org
 
165
    // thanks to Alan Schaaf of Imgur ( alan@imgur.com )
 
166
    addPair( "key", "d0757bc2e94a0d4652f28079a0be9379" );
 
167
 
 
168
    KIO::TransferJob *tf = KIO::http_post(url, m_buffer, KIO::HideProgressInfo);
 
169
 
 
170
    tf->addMetaData("content-type","Content-Type: multipart/form-data; boundary=" + m_boundary);
 
171
 
 
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 *)));
 
175
}