Working screenshot upload

This commit is contained in:
Jan Dalheimer 2014-02-24 10:34:51 +01:00
parent 226c1bdae5
commit a8811a27f7
7 changed files with 84 additions and 69 deletions

View File

@ -1522,8 +1522,8 @@ void MainWindow::on_actionScreenshots_triggered()
QStringList urls; QStringList urls;
for (ScreenShot *shot : dialog.uploaded()) for (ScreenShot *shot : dialog.uploaded())
{ {
urls << QString("<a href=\"" + shot->url + "\">Image %s</a>") urls << QString("<a href=\"" + shot->url + "\">Image %1</a>")
.arg(QString::number(shot->imgurIndex)); .arg(shot->timestamp.toString());
} }
CustomMessageBox::selectable(this, tr("Done uploading!"), urls.join("\n"), CustomMessageBox::selectable(this, tr("Done uploading!"), urls.join("\n"),
QMessageBox::Information)->exec(); QMessageBox::Information)->exec();

View File

@ -39,7 +39,7 @@ QList<ScreenShot*> ScreenshotDialog::selected() const
return list; return list;
} }
void ScreenshotDialog::on_buttonBox_accepted() void ScreenshotDialog::on_uploadBtn_clicked()
{ {
QList<ScreenShot *> screenshots = selected(); QList<ScreenShot *> screenshots = selected();
if (screenshots.isEmpty()) if (screenshots.isEmpty())
@ -50,17 +50,18 @@ void ScreenshotDialog::on_buttonBox_accepted()
NetJob *job = new NetJob("Screenshot Upload"); NetJob *job = new NetJob("Screenshot Upload");
for (ScreenShot *shot : screenshots) for (ScreenShot *shot : screenshots)
{ {
qDebug() << shot->file;
job->addNetAction(ScreenShotUpload::make(shot)); job->addNetAction(ScreenShotUpload::make(shot));
} }
m_uploaded = screenshots;
ProgressDialog prog(this); ProgressDialog prog(this);
prog.exec(job); if (prog.exec(job) == QDialog::Accepted)
connect(job, &NetJob::failed, [this] {
accept();
}
else
{ {
CustomMessageBox::selectable(this, tr("Failed to upload screenshots!"), CustomMessageBox::selectable(this, tr("Failed to upload screenshots!"),
tr("Unknown error"), QMessageBox::Warning)->exec(); tr("Unknown error"), QMessageBox::Warning)->exec();
reject(); reject();
}); }
m_uploaded = screenshots;
connect(job, &NetJob::succeeded, this, &ScreenshotDialog::accept);
} }

View File

@ -27,7 +27,7 @@ public:
private private
slots: slots:
void on_buttonBox_accepted(); void on_uploadBtn_clicked();
private: private:
Ui::ScreenshotDialog *ui; Ui::ScreenshotDialog *ui;

View File

@ -17,68 +17,82 @@
<iconset resource="../../resources/multimc/multimc.qrc"> <iconset resource="../../resources/multimc/multimc.qrc">
<normaloff>:/icons/multimc/scalable/apps/multimc.svg</normaloff>:/icons/multimc/scalable/apps/multimc.svg</iconset> <normaloff>:/icons/multimc/scalable/apps/multimc.svg</normaloff>:/icons/multimc/scalable/apps/multimc.svg</iconset>
</property> </property>
<widget class="QDialogButtonBox" name="buttonBox"> <layout class="QVBoxLayout" name="verticalLayout">
<property name="geometry"> <item>
<rect> <widget class="QListView" name="listView">
<x>10</x> <property name="selectionMode">
<y>260</y> <enum>QAbstractItemView::ExtendedSelection</enum>
<width>441</width> </property>
<height>31</height> <property name="selectionBehavior">
</rect> <enum>QAbstractItemView::SelectItems</enum>
</property> </property>
<property name="standardButtons"> <property name="iconSize">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set> <size>
</property> <width>120</width>
</widget> <height>90</height>
<widget class="QListView" name="listView"> </size>
<property name="geometry"> </property>
<rect> <property name="flow">
<x>10</x> <enum>QListView::LeftToRight</enum>
<y>10</y> </property>
<width>439</width> <property name="isWrapping" stdset="0">
<height>241</height> <bool>true</bool>
</rect> </property>
</property> <property name="viewMode">
<property name="selectionMode"> <enum>QListView::IconMode</enum>
<enum>QAbstractItemView::ExtendedSelection</enum> </property>
</property> </widget>
<property name="selectionBehavior"> </item>
<enum>QAbstractItemView::SelectItems</enum> <item>
</property> <layout class="QHBoxLayout" name="horizontalLayout">
<property name="iconSize"> <item>
<size> <spacer name="horizontalSpacer">
<width>120</width> <property name="orientation">
<height>90</height> <enum>Qt::Horizontal</enum>
</size> </property>
</property> <property name="sizeHint" stdset="0">
<property name="flow"> <size>
<enum>QListView::LeftToRight</enum> <width>40</width>
</property> <height>20</height>
<property name="isWrapping" stdset="0"> </size>
<bool>true</bool> </property>
</property> </spacer>
<property name="viewMode"> </item>
<enum>QListView::IconMode</enum> <item>
</property> <widget class="QPushButton" name="closeBtn">
</widget> <property name="text">
<string>Close</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="uploadBtn">
<property name="text">
<string>Upload</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget> </widget>
<resources> <resources>
<include location="../../resources/multimc/multimc.qrc"/> <include location="../../resources/multimc/multimc.qrc"/>
</resources> </resources>
<connections> <connections>
<connection> <connection>
<sender>buttonBox</sender> <sender>closeBtn</sender>
<signal>rejected()</signal> <signal>clicked()</signal>
<receiver>ScreenshotDialog</receiver> <receiver>ScreenshotDialog</receiver>
<slot>reject()</slot> <slot>reject()</slot>
<hints> <hints>
<hint type="sourcelabel"> <hint type="sourcelabel">
<x>316</x> <x>315</x>
<y>260</y> <y>272</y>
</hint> </hint>
<hint type="destinationlabel"> <hint type="destinationlabel">
<x>286</x> <x>271</x>
<y>274</y> <y>258</y>
</hint> </hint>
</hints> </hints>
</connection> </connection>

View File

@ -1,4 +1,5 @@
#include "ScreenshotList.h" #include "ScreenshotList.h"
#include <QDir> #include <QDir>
#include <QIcon> #include <QIcon>
@ -22,9 +23,9 @@ QVariant ScreenshotList::data(const QModelIndex &index, int role) const
case Qt::DecorationRole: case Qt::DecorationRole:
return QIcon(m_screenshots.at(index.row())->file); return QIcon(m_screenshots.at(index.row())->file);
case Qt::DisplayRole: case Qt::DisplayRole:
return m_screenshots.at(index.row())->timestamp; return m_screenshots.at(index.row())->timestamp.toString("yyyy-MM-dd HH:mm:ss");
case Qt::ToolTipRole: case Qt::ToolTipRole:
return m_screenshots.at(index.row())->timestamp; return m_screenshots.at(index.row())->timestamp.toString("yyyy-MM-dd HH:mm:ss");
case Qt::TextAlignmentRole: case Qt::TextAlignmentRole:
return (int)(Qt::AlignHCenter | Qt::AlignVCenter); return (int)(Qt::AlignHCenter | Qt::AlignVCenter);
default: default:
@ -68,7 +69,7 @@ void ScreenshotLoadTask::executeTask()
for (auto file : dir.entryList()) for (auto file : dir.entryList())
{ {
ScreenShot *shot = new ScreenShot(); ScreenShot *shot = new ScreenShot();
shot->timestamp = file.left(file.length() - 4); shot->timestamp = QDateTime::fromString(file, "yyyy-MM-dd_HH.mm.ss.png");
shot->file = dir.absoluteFilePath(file); shot->file = dir.absoluteFilePath(file);
m_results.append(shot); m_results.append(shot);
} }

View File

@ -7,10 +7,10 @@
class ScreenShot class ScreenShot
{ {
public: public:
QString timestamp; QDateTime timestamp;
QString file; QString file;
QString url; QString url;
int imgurIndex; QString imgurIndex;
}; };
class ScreenshotList : public QAbstractListModel class ScreenshotList : public QAbstractListModel

View File

@ -24,7 +24,6 @@ void ScreenShotUpload::start()
m_status = Job_InProgress; m_status = Job_InProgress;
QNetworkRequest request(m_url); QNetworkRequest request(m_url);
request.setHeader(QNetworkRequest::UserAgentHeader, "MultiMC/5.0 (Uncached)"); request.setHeader(QNetworkRequest::UserAgentHeader, "MultiMC/5.0 (Uncached)");
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
request.setRawHeader("Authorization", "Client-ID 5b97b0713fba4a3"); request.setRawHeader("Authorization", "Client-ID 5b97b0713fba4a3");
request.setRawHeader("Accept", "application/json"); request.setRawHeader("Accept", "application/json");
@ -47,7 +46,7 @@ void ScreenShotUpload::start()
multipart->append(typePart); multipart->append(typePart);
QHttpPart namePart; QHttpPart namePart;
namePart.setHeader(QNetworkRequest::ContentDispositionHeader, "form-data; name=\"name\""); namePart.setHeader(QNetworkRequest::ContentDispositionHeader, "form-data; name=\"name\"");
namePart.setBody(m_shot->timestamp.toUtf8()); namePart.setBody(m_shot->timestamp.toString(Qt::ISODate).toUtf8());
multipart->append(namePart); multipart->append(namePart);
auto worker = MMC->qnam(); auto worker = MMC->qnam();
@ -85,8 +84,8 @@ void ScreenShotUpload::downloadFinished()
emit failed(m_index_within_job); emit failed(m_index_within_job);
return; return;
} }
m_shot->imgurIndex = object.value("data").toObject().value("id").toVariant().toInt(); m_shot->imgurIndex = object.value("data").toObject().value("id").toString();
m_shot->url = "https://imgur.com/gallery/" + QString::number(m_shot->imgurIndex); m_shot->url = object.value("data").toObject().value("link").toString();
m_status = Job_Finished; m_status = Job_Finished;
emit succeeded(m_index_within_job); emit succeeded(m_index_within_job);
return; return;