From 84dcfb6263b46b62504706d69198675690f759be Mon Sep 17 00:00:00 2001
From: Tobias Markmann <tm@ayena.de>
Date: Sun, 21 Jun 2015 00:23:59 +0200
Subject: Add 'Open file' button for successfully transferred files

The button is shown after the 'Transfer completed successful.'
message and asks the desktop environment to open the file with the
default program.

Test-Information:

Send a file to another Swift on OS X 10.9.5 and verified that it opens
the file on button click.

Change-Id: I602e534ef07a119247cbf979e13551be7771880c

diff --git a/Swift/QtUI/QtWebKitChatView.cpp b/Swift/QtUI/QtWebKitChatView.cpp
index 60e5440..d6dc6f5 100644
--- a/Swift/QtUI/QtWebKitChatView.cpp
+++ b/Swift/QtUI/QtWebKitChatView.cpp
@@ -4,36 +4,36 @@
  * See the COPYING file for more information.
  */
 
-#include "QtWebKitChatView.h"
+#include <Swift/QtUI/QtWebKitChatView.h>
 
-#include <QtDebug>
+#include <QApplication>
+#include <QDesktopServices>
 #include <QEventLoop>
 #include <QFile>
-#include <QDesktopServices>
-#include <QVBoxLayout>
-#include <QWebFrame>
+#include <QFileDialog>
+#include <QInputDialog>
 #include <QKeyEvent>
+#include <QMessageBox>
 #include <QStackedWidget>
 #include <QTimer>
-#include <QMessageBox>
-#include <QApplication>
-#include <QInputDialog>
-#include <QFileDialog>
+#include <QVBoxLayout>
+#include <QWebFrame>
+#include <QtDebug>
 
-#include <Swiften/Base/Log.h>
 #include <Swiften/Base/FileSize.h>
+#include <Swiften/Base/Log.h>
 #include <Swiften/StringCodecs/Base64.h>
 
-#include <Swift/Controllers/UIEvents/UIEventStream.h>
 #include <Swift/Controllers/UIEvents/JoinMUCUIEvent.h>
+#include <Swift/Controllers/UIEvents/UIEventStream.h>
 
-#include <Swift/QtUI/QtWebView.h>
+#include <Swift/QtUI/MessageSnippet.h>
 #include <Swift/QtUI/QtChatWindow.h>
 #include <Swift/QtUI/QtChatWindowJSBridge.h>
 #include <Swift/QtUI/QtScaledAvatarCache.h>
 #include <Swift/QtUI/QtSwiftUtil.h>
 #include <Swift/QtUI/QtUtilities.h>
-#include <Swift/QtUI/MessageSnippet.h>
+#include <Swift/QtUI/QtWebView.h>
 #include <Swift/QtUI/SystemMessageSnippet.h>
 
 namespace Swift {
@@ -45,6 +45,7 @@ const QString QtWebKitChatView::ButtonFileTransferCancel = QString("filetransfer
 const QString QtWebKitChatView::ButtonFileTransferSetDescription = QString("filetransfer-setdescription");
 const QString QtWebKitChatView::ButtonFileTransferSendRequest = QString("filetransfer-sendrequest");
 const QString QtWebKitChatView::ButtonFileTransferAcceptRequest = QString("filetransfer-acceptrequest");
+const QString QtWebKitChatView::ButtonFileTransferOpenFile = QString("filetransfer-openfile");
 const QString QtWebKitChatView::ButtonMUCInvite = QString("mucinvite");
 
 QtWebKitChatView::QtWebKitChatView(QtChatWindow* window, UIEventStream* eventStream, QtChatTheme* theme, QWidget* parent, bool disableAutoScroll) : QtChatView(parent), window_(window), eventStream_(eventStream), fontSizeSteps_(0), disableAutoScroll_(disableAutoScroll), previousMessageKind_(PreviosuMessageWasNone), previousMessageWasSelf_(false), showEmoticons_(false), insertingLastLine_(false), idCounter_(0) {
@@ -462,7 +463,8 @@ void QtWebKitChatView::setFileTransferStatus(QString id, const ChatWindow::FileT
 	}
 	else if (state == ChatWindow::Finished) {
 		// text "Successful transfer"
-		newInnerHTML = tr("Transfer completed successfully.");
+		newInnerHTML = tr("Transfer completed successfully." ) + " " + buildChatWindowButton(tr("Open file"), ButtonFileTransferOpenFile, id, filePaths_[id]);
+		filePaths_.erase(id);
 	}
 	else if (state == ChatWindow::FTFailed) {
 		newInnerHTML = tr("Transfer failed.");
@@ -492,6 +494,13 @@ void QtWebKitChatView::setMUCInvitationJoined(QString id) {
 	}
 }
 
+void QtWebKitChatView::askDesktopToOpenFile(const QString& filename) {
+	QFileInfo fileInfo(filename);
+	if (fileInfo.exists() && fileInfo.isFile()) {
+		QDesktopServices::openUrl(QUrl::fromLocalFile(filename));
+	}
+}
+
 void QtWebKitChatView::handleScrollRequested(int, int dy, const QRect&) {
 	rememberScrolledToBottom();
 
@@ -652,6 +661,7 @@ std::string QtWebKitChatView::addFileTransfer(const std::string& senderName, boo
 	QString formattedFileSize = P2QSTRING(formatSize(sizeInBytes));
 	if (senderIsSelf) {
 		// outgoing
+		filePaths_[ft_id] = P2QSTRING(filename);
 		actionText = tr("Send file");
 		htmlString = actionText + ": " + P2QSTRING(filename) + " ( " + formattedFileSize + ") <br/>" +
 			"<div id='" + ft_id + "'>" +
@@ -719,9 +729,6 @@ void QtWebKitChatView::setWhiteboardSessionStatus(const std::string& id, const C
 	setWhiteboardSessionStatus(P2QSTRING(id), state);
 }
 
-
-
-
 void QtWebKitChatView::handleHTMLButtonClicked(QString id, QString encodedArgument1, QString encodedArgument2, QString encodedArgument3, QString encodedArgument4, QString encodedArgument5) {
 	QString arg1 = decodeButtonArgument(encodedArgument1);
 	QString arg2 = decodeButtonArgument(encodedArgument2);
@@ -753,9 +760,15 @@ void QtWebKitChatView::handleHTMLButtonClicked(QString id, QString encodedArgume
 
 		QString path = QFileDialog::getSaveFileName(this, tr("Save File"), filename);
 		if (!path.isEmpty()) {
+			filePaths_[ft_id] = path;
 			window_->onFileTransferAccept(Q2PSTRING(ft_id), Q2PSTRING(path));
 		}
 	}
+	else if (id.startsWith(ButtonFileTransferOpenFile)) {
+		QString ft_id = arg1;
+		QString filename = arg2;
+		askDesktopToOpenFile(filename);
+	}
 	else if (id.startsWith(ButtonWhiteboardSessionAcceptRequest)) {
 		QString id = arg1;
 		setWhiteboardSessionStatus(id, ChatWindow::WhiteboardAccepted);
diff --git a/Swift/QtUI/QtWebKitChatView.h b/Swift/QtUI/QtWebKitChatView.h
index ea9a57c..af2da01 100644
--- a/Swift/QtUI/QtWebKitChatView.h
+++ b/Swift/QtUI/QtWebKitChatView.h
@@ -6,12 +6,12 @@
 
 #pragma once
 
-#include <QString>
-#include <QWidget>
+#include <boost/shared_ptr.hpp>
+
 #include <QList>
+#include <QString>
 #include <QWebElement>
-
-#include <boost/shared_ptr.hpp>
+#include <QWidget>
 
 #include <Swiften/Base/Override.h>
 
@@ -41,6 +41,7 @@ namespace Swift {
 			static const QString ButtonFileTransferSetDescription;
 			static const QString ButtonFileTransferSendRequest;
 			static const QString ButtonFileTransferAcceptRequest;
+			static const QString ButtonFileTransferOpenFile;
 			static const QString ButtonMUCInvite;
 		public:
 			QtWebKitChatView(QtChatWindow* window, UIEventStream* eventStream, QtChatTheme* theme, QWidget* parent, bool disableAutoScroll = false);
@@ -96,6 +97,7 @@ namespace Swift {
 			void setFileTransferStatus(QString id, const ChatWindow::FileTransferState state, const QString& msg);
 			void setWhiteboardSessionStatus(QString id, const ChatWindow::WhiteboardSessionState state);
 			void setMUCInvitationJoined(QString id);
+			void askDesktopToOpenFile(const QString& filename);
 			
 		signals:
 			void gotFocus();
@@ -186,5 +188,6 @@ namespace Swift {
 			int idCounter_;
 			QString previousSenderName_;
 			std::map<QString, QString> descriptions_;
+			std::map<QString, QString> filePaths_;
 	};
 }
-- 
cgit v0.10.2-6-g49f6