Example #1
0
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    connect(&screenshot, SIGNAL(newImageCaptured(QPixmapSharedPtr)), this, SLOT(screenshotCaptured(QPixmapSharedPtr)));
    connect(&areaCapture, SIGNAL(newImageCaptured(QPixmapSharedPtr)), this, SLOT(screenshotCaptured(QPixmapSharedPtr)));
    connect(&imgurUploader, SIGNAL(error(QString)), this, SLOT(uploadError(QString)));
    connect(&imgurUploader, SIGNAL(imageUploaded(QUrl)), this, SLOT(fileUploaded(QUrl)));
    connect(ui->buttonScreenshot, SIGNAL(clicked()), this, SLOT(captureScreen()));
    connect(ui->buttonCaptureArea, SIGNAL(clicked()), this, SLOT(captureArea()));
    connect(&tray, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(show()));
    tray.show();
}
/**
*	Upload an image. This method is called by
*	userUploadImage and anonymousUploadImage
*	methods
*
*	@access public
*/
void ImageShackUploader::sendImage(ImageShackObject * imageToUpload ,
                                   QHash<QString, QString>   headers)
{
    QString   imagePath = imageToUpload->getObjectPath();
    QFile     image(imagePath);
    QFileInfo imageInfos(imagePath);

    QNetworkAccessManager * manager = new QNetworkAccessManager;
    QNetworkRequest         request(this->imageUploadUrl);

    QByteArray boundary   = "IMAGESHACKUPLOADER";
    QByteArray cr         = "\r\n";

    QByteArray data;

    if(!image.open(QIODevice::ReadOnly))
    {
        if(uploadAborted==false)
        {
            emit uploadError(ImageShackError::FailedOpeningTheFile);
            this->abortUploads();
        }

        return;
    }

    // build of the header
    data.append("--" + boundary + cr);
    data.append("Content-Disposition: form-data; ");
    data.append( "name=\"fileupload\"; filename=\"" + imageInfos.absoluteFilePath() + "\";" + cr);
    data.append("Content-Type: " + this->mimeType(imagePath) + cr + cr);

    // insertion of the image
    data.append(image.readAll() + cr);

    image.close();

    // build the footer
    QHashIterator<QString, QString> h(headers);
    while (h.hasNext())
    {
        h.next();

        data.append("--" + boundary + cr);
        data.append("Content-Disposition: form-data; ");
        data.append("name=\"" + h.key() + "\"" + cr + cr);
        data.append(h.value() + cr);
    }

    data.append("--" + boundary + "--" + cr);

    request.setHeader(QNetworkRequest::ContentTypeHeader, "multipart/form-data; boundary=" + boundary);
    request.setHeader(QNetworkRequest::ContentLengthHeader, QString::number(data.size()).toAscii());

    // manage proxy
    if(this->proxy != NULL)
        manager->setProxy(*this->proxy);

    this->fileBeingUploaded = imageToUpload;

    //this->uploadsProcessing	   = true;

    timeoutTimer->start(TIMEOUT);

    this->networkReply = manager->post(request, data);

    connect(this->networkReply, SIGNAL(finished())      ,
            this        , SLOT  (imageUploaded()));

    connect(this->networkReply, SIGNAL(error(QNetworkReply::NetworkError))       ,
            this        , SLOT  (manageUploadError(QNetworkReply::NetworkError)));

    connect(this->networkReply, SIGNAL(uploadProgress(qint64,qint64)),
            this	    , SLOT  (manageUploadProgress(qint64,qint64)));

    connect(manager, SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)),
            this   , SLOT  (manageAuthentificationRequired(QNetworkReply*,QAuthenticator*)));

    connect(manager, SIGNAL(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)),
            this   , SLOT  (manageProxyAuthentificationRequired(QNetworkProxy,QAuthenticator*)));
}