[prev in list] [next in list] [prev in thread] [next in thread]
List: kde-devel
Subject: upload-to-imageshack for ksnapshot
From: Benjamin Adler <benadler () gmx ! net>
Date: 2008-08-18 0:18:56
Message-ID: 200808180218.56573.benadler () gmx ! net
[Download RAW message or body]
Hello KDE!
I've created a patch for KSnapshot, adding a "Upload to Imageshack"-button.
Its my first patch for KDE, so I have no idea how this is usually handled...
greetings,
Benjamin Adler
["ksnapshot_upload_to_imageshack.diff" (text/x-diff)]
Index: ksnapshotwidget.ui
===================================================================
--- ksnapshotwidget.ui (Revision 848441)
+++ ksnapshotwidget.ui (Arbeitskopie)
@@ -219,6 +219,13 @@
</property>
</widget>
</item>
+ <item>
+ <widget class="KPushButton" name="btnUpload" >
+ <property name="text" >
+ <string>Upload to imageshack</string>
+ </property>
+ </widget>
+ </item>
</layout>
</item>
</layout>
Index: ksnapshot.cpp
===================================================================
--- ksnapshot.cpp (Revision 848441)
+++ ksnapshot.cpp (Arbeitskopie)
@@ -70,12 +70,12 @@
btnSave->setIcon(KIcon("document-save"));
btnOpen->setIcon(KIcon("document-open"));
btnCopy->setIcon(KIcon("edit-copy"));
-
+ btnUpload->setIcon(KIcon("document-open-remote"));
}
};
KSnapshot::KSnapshot(QWidget *parent, KSnapshotObject::CaptureMode mode )
- : KDialog(parent), KSnapshotObject(), modified(false)
+ : KDialog(parent), KSnapshotObject(), http(0), modified(false)
{
setCaption( "" );
setModal( true );
@@ -99,6 +99,7 @@
connect( mainWidget->btnNew, SIGNAL( clicked() ), SLOT( slotGrab() ) );
connect( mainWidget->btnSave, SIGNAL( clicked() ), SLOT( slotSaveAs() ) );
connect( mainWidget->btnCopy, SIGNAL( clicked() ), SLOT( slotCopy() ) );
+ connect( mainWidget->btnUpload, SIGNAL( clicked() ), SLOT( \
slotUploadToImageshack() ) ); // connect( mainWidget->btnOpen, SIGNAL( clicked() \
), SLOT( slotOpen() ) );
connect( mainWidget->comboMode, SIGNAL( activated(int) ), SLOT( \
slotModeChanged(int) ) );
@@ -206,6 +207,8 @@
KSnapshot::~KSnapshot()
{
delete mainWidget;
+ delete progressDialog;
+ delete http;
}
void KSnapshot::resizeEvent( QResizeEvent * )
@@ -298,6 +301,163 @@
KRun::run(application, list, this);
}
+void KSnapshot::slotUploadToImageshack()
+{
+ if(KMessageBox::Yes != KMessageBox::questionYesNo(this,
+ i18n("Are you sure you want to upload this image to imageshack?"),
+ i18n("Upload to imageshack"),
+ KStandardGuiItem::yes(),
+ KStandardGuiItem::no(),
+ "doNotConfirmUploading")
+ )
+ return;
+
+ if(!http)
+ {
+ progressDialog = new KProgressDialog(this, i18n("Uploading to imageshack"), \
i18n("Please wait while the image is being uploaded to imageshack.")); + \
progressDialog->setAutoClose(false); + progressDialog->setMinimumDuration(0);
+ http = new QHttp(this);
+ connect(http, SIGNAL(requestFinished(int, bool)), \
SLOT(slotUploadFinished(int, bool))); + connect(http, SIGNAL(dataSendProgress \
(int, int)), SLOT(slotUploadProgress(int, int))); +// connect(http, \
SIGNAL(stateChanged (int)), SLOT(slotHttpStateChanged(int))); + \
connect(progressDialog, SIGNAL(cancelClicked()), SLOT(slotCancelUpload())); + }
+
+ progressDialog->setLabelText(i18n("Please wait while the image is being uploaded \
to imageshack.")); +
+ QByteArray image;
+ QBuffer buffer(&image);
+ buffer.open(QIODevice::WriteOnly);
+ snapshot.save(&buffer, "PNG");
+ buffer.close();
+
+ const QString host("www.imageshack.us");
+ QHttpRequestHeader header("POST", "/index.php");
+ header.setValue("Host", host);
+ header.setValue("User-Agent", "KSnapshot from KDE");
+ header.setValue("Accept-Charset", "ISO-8859-1,utf-8");
+ header.setContentLength(1234); // We somehow NEED a dummy value here, it won't \
work without it! + header.setValue("Content-Type", "multipart/form-data; \
boundary=---------------------------123456789012345\r\n"); +
+ QByteArray bytes;
+ bytes.append("-----------------------------123456789012345\r\n");
+ bytes.append("Content-Disposition: form-data; name=\"xml\"\r\n\r\nyes\r\n");
+ bytes.append("-----------------------------123456789012345\r\n");
+ bytes.append("Content-Disposition: form-data; name=\"fileupload\"; \
filename=\"snapshot.png\"\r\n"); + bytes.append("Content-Type: \
image/png\r\n\r\n"); + bytes.append(image);
+ bytes.append("\r\n-----------------------------123456789012345--\r\n\r\n");
+
+ http->setHost(host, QHttp::ConnectionModeHttp, 80);
+ http->request(header, bytes);
+
+ progressDialog->exec();
+}
+
+void KSnapshot::slotUploadProgress(int done, int total)
+{
+// qDebug() << "KSnapshot::slotUploadProgress()" << done << total;
+ progressDialog->progressBar()->setRange(0, total);
+ progressDialog->progressBar()->setValue(done);
+
+ if(done == total)
+ progressDialog->setLabelText(i18n("Upload complete, waiting for reply..."));
+}
+
+void KSnapshot::slotCancelUpload()
+{
+// qDebug() << "KSnapshot::slotCancelUpload()";
+ http->abort();
+ slotUploadProgress(0,1);
+}
+
+void KSnapshot::slotUploadFinished(int /*id*/, bool error)
+{
+
+// qDebug() << "KSnapshot::slotUploadFinished()";
+
+ // If the user pressed cancel while uploading, we called http->abort() which \
emits a + // requestFinished()-signal with error=true; Catch this condition here.
+ if(http->error() == QHttp::Aborted)
+ return;
+
+ QHttpRequestHeader header = http->currentRequest();
+
+ if(http->error() != QHttp::NoError || error)
+ {
+ KMessageBox::error(this, i18n("KSnapshot was unable to upload the \
image:\n\n%1", http->errorString()), i18n("Unable to Upload Image")); + // \
flush uninteresting data. + http->readAll();
+ return;
+ }
+
+ if(header.path().endsWith("/index.php"))
+ {
+ // reset the progressbar
+ slotUploadProgress(0,1);
+ progressDialog->accept();
+ QByteArray xml = http->readAll();
+
+ QDomDocument doc;
+ QString errorMessage;
+ int line, column;
+ if(!doc.setContent(xml, &errorMessage, &line, &column))
+ {
+ KMessageBox::error(this, i18n("KSnapshot was unable to upload the image, \
because Imageshack answered with invalid XML:\n\n%1\n\nxml was:\n%2", errorMessage, \
QString(xml)), i18n("Unable to Upload Image")); + //qDebug() << \
"KSnapshot::slotUploadFinished(): The XML obtained from the server is invalid: " << \
errorMessage << ", line" << QString::number(line) << ":" << QString::number(column) \
<< "\n\n" << doc.toString(2) << "\nraw:" << xml; + return;
+ }
+
+ if(doc.documentElement().tagName() != "links")
+ {
+ KMessageBox::error(this, i18n("KSnapshot was unable to upload the image: \
Imageshack answered with invalid XML:\n\n%1", QString(xml)), i18n("Unable to Upload \
Image")); + return;
+ }
+
+ const QDomNodeList list = doc.documentElement().childNodes();
+
+ for(int i=0; i < list.size(); i++)
+ {
+ QDomNode currentNode = list.at(i);
+ if(currentNode.isElement())
+ {
+ if(currentNode.toElement().tagName() == "ad_link")
+ {
+ //qDebug() << "opening webpage:" << \
currentNode.toElement().text(); + \
KRun::runUrl(KUrl(currentNode.toElement().text()), "text/html", this, false, false, \
QString(), ""); + return;
+ }
+ }
+ }
+ }
+}
+
+void KSnapshot::slotHttpStateChanged(int state)
+{
+ switch(state)
+ {
+ case QHttp::Unconnected:
+ qDebug() << "KSnapshot::slotHttpStateChanged(): QHttp::Unconnected";
+ break;
+ case QHttp::Connecting:
+ qDebug() << "KSnapshot::slotHttpStateChanged(): QHttp::Connecting";
+ break;
+ case QHttp::Sending:
+ qDebug() << "KSnapshot::slotHttpStateChanged(): QHttp::Sending";
+ break;
+ case QHttp::Reading:
+ qDebug() << "KSnapshot::slotHttpStateChanged(): QHttp::Reading";
+ break;
+ case QHttp::Connected:
+ qDebug() << "KSnapshot::slotHttpStateChanged(): QHttp::Connected";
+ break;
+ case QHttp::Closing:
+ qDebug() << "KSnapshot::slotHttpStateChanged(): QHttp::Closing";
+ break;
+ }
+}
+
void KSnapshot::slotOpen(QAction* action)
{
KSnapshotServiceAction* serviceAction =
Index: ksnapshot.h
===================================================================
--- ksnapshot.h (Revision 848441)
+++ ksnapshot.h (Arbeitskopie)
@@ -28,6 +28,9 @@
#include <QAction>
#include <QBitmap>
#include <QLabel>
+#include <QHttp>
+#include <QDomNode>
+#include <QBuffer>
#include <QPainter>
#include <QStyleOption>
#include <QTimer>
@@ -36,6 +39,7 @@
#include <kglobalsettings.h>
#include <kdialog.h>
+#include <kprogressdialog.h>
#include <kservice.h>
#include <kurl.h>
#include "ksnapshotobject.h"
@@ -153,6 +157,7 @@
void slotSaveAs();
void slotCopy();
void slotOpen(const QString& application);
+ void slotUploadToImageshack();
void slotMovePointer( int x, int y );
void setTime( int newTime );
void setURL( const QString &newURL );
@@ -180,6 +185,10 @@
void setDelay( int i );
void setIncludeDecorations( bool b );
void setMode( int mode );
+ void slotUploadFinished(int id, bool error);
+ void slotUploadProgress(int done, int total);
+ void slotHttpStateChanged(int state);
+ void slotCancelUpload();
int delay() const;
bool includeDecorations() const;
int mode() const;
@@ -198,6 +207,8 @@
QTimer updateTimer;
QMenu* openMenu;
KSnapshotWidget *mainWidget;
+ QHttp* http;
+ KProgressDialog* progressDialog;
bool modified;
};
>> Visit http://mail.kde.org/mailman/listinfo/kde-devel#unsub to unsubscribe <<
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic