| 
									
										
										
										
											2017-04-27 20:26:05 +02:00
										 |  |  | // SPDX-License-Identifier: GPL-2.0
 | 
					
						
							| 
									
										
										
										
											2015-11-08 10:33:29 -02:00
										 |  |  | #include "facebookconnectwidget.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <QJsonDocument>
 | 
					
						
							|  |  |  | #include <QJsonArray>
 | 
					
						
							|  |  |  | #include <QJsonObject>
 | 
					
						
							|  |  |  | #include <QNetworkReply>
 | 
					
						
							|  |  |  | #include <QNetworkRequest>
 | 
					
						
							|  |  |  | #include <QNetworkAccessManager>
 | 
					
						
							|  |  |  | #include <QNetworkCookieJar>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <QUrlQuery>
 | 
					
						
							|  |  |  | #include <QHttpMultiPart>
 | 
					
						
							|  |  |  | #include <QFile>
 | 
					
						
							|  |  |  | #include <QBuffer>
 | 
					
						
							|  |  |  | #include <QDebug>
 | 
					
						
							|  |  |  | #include <QMessageBox>
 | 
					
						
							|  |  |  | #include <QInputDialog>
 | 
					
						
							| 
									
										
										
										
											2017-10-02 14:48:34 +02:00
										 |  |  | #include <QLoggingCategory>
 | 
					
						
							| 
									
										
										
										
											2016-08-09 00:12:12 +02:00
										 |  |  | #ifdef USE_WEBENGINE
 | 
					
						
							|  |  |  | #include <QWebEngineView>
 | 
					
						
							|  |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2015-11-08 10:33:29 -02:00
										 |  |  | #include <QWebView>
 | 
					
						
							| 
									
										
										
										
											2016-08-09 00:12:12 +02:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2015-11-08 10:33:29 -02:00
										 |  |  | #include "mainwindow.h"
 | 
					
						
							|  |  |  | #include "profile-widget/profilewidget2.h"
 | 
					
						
							| 
									
										
										
										
											2016-04-05 14:22:00 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include "core/pref.h"
 | 
					
						
							| 
									
										
										
										
											2018-06-03 22:15:19 +02:00
										 |  |  | #include "core/qthelper.h"
 | 
					
						
							| 
									
										
										
										
											2018-08-15 11:55:27 +02:00
										 |  |  | #include "core/settings/qPrefFacebook.h"
 | 
					
						
							| 
									
										
										
										
											2016-04-05 14:22:00 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-08 10:33:29 -02:00
										 |  |  | #include "ui_socialnetworksdialog.h"
 | 
					
						
							|  |  |  | #include "ui_facebookconnectwidget.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-02 14:48:34 +02:00
										 |  |  | Q_LOGGING_CATEGORY(lcFacebook, "subsurface.facebook") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-08 10:33:29 -02:00
										 |  |  | FacebookManager *FacebookManager::instance() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	static FacebookManager *self = new FacebookManager(); | 
					
						
							|  |  |  | 	return self; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-29 11:54:12 +02:00
										 |  |  | FacebookManager::FacebookManager(QObject *parent) : | 
					
						
							|  |  |  | 	QObject(parent), | 
					
						
							|  |  |  | 	manager(new QNetworkAccessManager(this)) | 
					
						
							| 
									
										
										
										
											2015-11-08 10:33:29 -02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-10-20 06:51:06 -04:00
										 |  |  | 	// log only in verbose mode
 | 
					
						
							|  |  |  | 	QLoggingCategory::setFilterRules(QStringLiteral("subsurface.facebook=%1").arg(verbose ? "true" : "false")); | 
					
						
							| 
									
										
										
										
											2017-10-02 14:35:36 +02:00
										 |  |  | 	connect(this, &FacebookManager::albumIdReceived, this, &FacebookManager::sendDiveToAlbum); | 
					
						
							| 
									
										
										
										
											2015-11-08 10:33:29 -02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-13 11:56:41 +02:00
										 |  |  | static QString graphApi = QStringLiteral("https://graph.facebook.com/v2.10/"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-29 11:54:12 +02:00
										 |  |  | QUrl FacebookManager::albumListUrl() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return QUrl("https://graph.facebook.com/me/albums?access_token=" + QString(prefs.facebook.access_token)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-08 10:33:29 -02:00
										 |  |  | QUrl FacebookManager::connectUrl() { | 
					
						
							|  |  |  | 	return QUrl("https://www.facebook.com/dialog/oauth?" | 
					
						
							| 
									
										
										
										
											2017-09-29 12:52:49 -07:00
										 |  |  | 		    "client_id=427722490709000" | 
					
						
							|  |  |  | 		    "&redirect_uri=http://www.facebook.com/connect/login_success.html" | 
					
						
							|  |  |  | 		    "&response_type=token,granted_scopes" | 
					
						
							|  |  |  | 		    "&display=popup" | 
					
						
							|  |  |  | 		    "&scope=publish_actions,user_photos" | 
					
						
							|  |  |  | 		    ); | 
					
						
							| 
									
										
										
										
											2015-11-08 10:33:29 -02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool FacebookManager::loggedIn() { | 
					
						
							|  |  |  | 	return prefs.facebook.access_token != NULL; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void FacebookManager::tryLogin(const QUrl& loginResponse) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2017-10-02 15:08:03 +02:00
										 |  |  | 	qCDebug(lcFacebook) << "Current url call" << loginResponse; | 
					
						
							| 
									
										
										
										
											2015-11-08 10:33:29 -02:00
										 |  |  | 	QString result = loginResponse.toString(); | 
					
						
							| 
									
										
										
										
											2017-10-02 14:48:34 +02:00
										 |  |  | 	if (!result.contains("access_token")) { | 
					
						
							|  |  |  | 		qCDebug(lcFacebook) << "Response without access token!"; | 
					
						
							| 
									
										
										
										
											2015-11-08 10:33:29 -02:00
										 |  |  | 		return; | 
					
						
							| 
									
										
										
										
											2017-10-02 14:48:34 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-11-08 10:33:29 -02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (result.contains("denied_scopes=publish_actions") || result.contains("denied_scopes=user_photos")) { | 
					
						
							| 
									
										
										
										
											2017-10-02 14:48:34 +02:00
										 |  |  | 		qCDebug(lcFacebook) << "user did not allow us access" << result; | 
					
						
							| 
									
										
										
										
											2015-11-08 10:33:29 -02:00
										 |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-10-02 14:48:34 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-08 10:33:29 -02:00
										 |  |  | 	int from = result.indexOf("access_token=") + strlen("access_token="); | 
					
						
							|  |  |  | 	int to = result.indexOf("&expires_in"); | 
					
						
							|  |  |  | 	QString securityToken = result.mid(from, to-from); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-15 11:55:27 +02:00
										 |  |  | 	qPrefFacebook::set_access_token(securityToken); | 
					
						
							| 
									
										
										
										
											2017-10-02 14:48:34 +02:00
										 |  |  | 	qCDebug(lcFacebook) << "Got securityToken" << securityToken; | 
					
						
							| 
									
										
										
										
											2015-11-08 10:33:29 -02:00
										 |  |  | 	requestUserId(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void FacebookManager::logout() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-08-15 11:55:27 +02:00
										 |  |  | 	qPrefFacebook::set_access_token(QString()); | 
					
						
							|  |  |  | 	qPrefFacebook::set_user_id(QString()); | 
					
						
							|  |  |  | 	qPrefFacebook::set_album_id(QString()); | 
					
						
							| 
									
										
										
										
											2015-11-08 10:33:29 -02:00
										 |  |  | 	emit justLoggedOut(true); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void FacebookManager::requestAlbumId() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2017-10-02 14:48:34 +02:00
										 |  |  | 	qCDebug(lcFacebook) << "Starting to request the album id" << albumListUrl(); | 
					
						
							| 
									
										
										
										
											2017-09-29 11:54:12 +02:00
										 |  |  | 	QNetworkReply *reply = manager->get(QNetworkRequest(albumListUrl())); | 
					
						
							| 
									
										
										
										
											2017-09-29 12:45:28 -07:00
										 |  |  | 	connect(reply, &QNetworkReply::finished, this, &FacebookManager::albumListReceived); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2015-11-08 10:33:29 -02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-29 12:45:28 -07:00
										 |  |  | void FacebookManager::albumListReceived() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2017-10-02 14:48:34 +02:00
										 |  |  | 	qCDebug(lcFacebook) << "Reply for the album id"; | 
					
						
							| 
									
										
										
										
											2017-09-29 12:45:28 -07:00
										 |  |  | 	QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender()); | 
					
						
							| 
									
										
										
										
											2015-11-08 10:33:29 -02:00
										 |  |  | 	QJsonDocument albumsDoc = QJsonDocument::fromJson(reply->readAll()); | 
					
						
							|  |  |  | 	QJsonArray albumObj = albumsDoc.object().value("data").toArray(); | 
					
						
							| 
									
										
										
										
											2016-08-26 15:17:49 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-29 12:00:31 +02:00
										 |  |  | 	reply->deleteLater(); | 
					
						
							| 
									
										
										
										
											2015-11-08 10:33:29 -02:00
										 |  |  | 	foreach(const QJsonValue &v, albumObj){ | 
					
						
							|  |  |  | 		QJsonObject obj = v.toObject(); | 
					
						
							| 
									
										
										
										
											2017-10-02 14:54:00 +02:00
										 |  |  | 		if (obj.value("name").toString() == fbInfo.albumName) { | 
					
						
							| 
									
										
										
										
											2018-08-15 11:55:27 +02:00
										 |  |  | 			qPrefFacebook::set_album_id(obj.value("id").toString()); | 
					
						
							| 
									
										
										
										
											2017-10-02 14:54:00 +02:00
										 |  |  | 			qCDebug(lcFacebook) << "Album" << fbInfo.albumName << "already exists, using id" << obj.value("id").toString(); | 
					
						
							| 
									
										
										
										
											2018-08-15 11:55:27 +02:00
										 |  |  | 			emit albumIdReceived(qPrefFacebook::album_id()); | 
					
						
							| 
									
										
										
										
											2015-11-08 10:33:29 -02:00
										 |  |  | 			return; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-10-02 14:48:34 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-29 12:45:28 -07:00
										 |  |  | 	// No album with the name we requested, create a new one.
 | 
					
						
							|  |  |  | 	createFacebookAlbum(); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2015-11-08 10:33:29 -02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-29 12:45:28 -07:00
										 |  |  | void FacebookManager::createFacebookAlbum() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2017-10-02 14:54:00 +02:00
										 |  |  | 	qCDebug(lcFacebook) << "Album with name" << fbInfo.albumName << "doesn't exists, creating it."; | 
					
						
							| 
									
										
										
										
											2015-11-08 10:33:29 -02:00
										 |  |  | 	QUrlQuery params; | 
					
						
							| 
									
										
										
										
											2017-10-02 14:54:00 +02:00
										 |  |  | 	params.addQueryItem("name", fbInfo.albumName ); | 
					
						
							| 
									
										
										
										
											2015-11-08 10:33:29 -02:00
										 |  |  | 	params.addQueryItem("description", "Subsurface Album"); | 
					
						
							|  |  |  | 	params.addQueryItem("privacy", "{'value': 'SELF'}"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-29 11:54:12 +02:00
										 |  |  | 	QNetworkRequest request(albumListUrl()); | 
					
						
							| 
									
										
										
										
											2015-11-08 10:33:29 -02:00
										 |  |  | 	request.setHeader(QNetworkRequest::ContentTypeHeader, "application/octet-stream"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-02 20:29:50 +01:00
										 |  |  | 	QNetworkReply *reply = manager->post(request, params.query().toUtf8()); | 
					
						
							| 
									
										
										
										
											2017-09-29 12:45:28 -07:00
										 |  |  | 	connect(reply, &QNetworkReply::finished, this, &FacebookManager::facebookAlbumCreated); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void FacebookManager::facebookAlbumCreated() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender()); | 
					
						
							|  |  |  | 	QJsonDocument albumsDoc = QJsonDocument::fromJson(reply->readAll()); | 
					
						
							| 
									
										
										
										
											2015-11-08 10:33:29 -02:00
										 |  |  | 	QJsonObject album = albumsDoc.object(); | 
					
						
							| 
									
										
										
										
											2017-09-29 12:00:31 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	reply->deleteLater(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-08 10:33:29 -02:00
										 |  |  | 	if (album.contains("id")) { | 
					
						
							| 
									
										
										
										
											2017-10-02 14:54:00 +02:00
										 |  |  | 		qCDebug(lcFacebook) << "Album" << fbInfo.albumName << "created successfully with id" << album.value("id").toString(); | 
					
						
							| 
									
										
										
										
											2018-08-15 11:55:27 +02:00
										 |  |  | 		qPrefFacebook::set_album_id(album.value("id").toString()); | 
					
						
							|  |  |  | 		emit albumIdReceived(qPrefFacebook::album_id()); | 
					
						
							| 
									
										
										
										
											2015-11-08 10:33:29 -02:00
										 |  |  | 		return; | 
					
						
							| 
									
										
										
										
											2017-10-02 14:48:34 +02:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2017-10-02 14:54:00 +02:00
										 |  |  | 		qCDebug(lcFacebook) << "It was not possible to create the album with name" << fbInfo.albumName; | 
					
						
							| 
									
										
										
										
											2017-10-23 15:12:15 +02:00
										 |  |  | 		qCDebug(lcFacebook).noquote() << "Reply was: " << QString(albumsDoc.toJson(QJsonDocument::Indented)); | 
					
						
							| 
									
										
										
										
											2017-10-23 15:37:38 +02:00
										 |  |  | 		// FIXME: we are lacking 'user_photos' facebook permission to create an album, 
 | 
					
						
							|  |  |  | 		// but we are able to upload the image to Facebook (album will be named 'Subsurface Photos')
 | 
					
						
							|  |  |  | 		qCDebug(lcFacebook) << "But we are still able to upload data. Album name will be 'Subsurface Photos'"; | 
					
						
							| 
									
										
										
										
											2018-08-15 11:55:27 +02:00
										 |  |  | 		emit albumIdReceived(qPrefFacebook::album_id()); | 
					
						
							| 
									
										
										
										
											2015-11-08 10:33:29 -02:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void FacebookManager::requestUserId() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2017-10-02 14:48:34 +02:00
										 |  |  | 	qCDebug(lcFacebook) << "Requesting user id"; | 
					
						
							| 
									
										
										
										
											2015-11-08 10:33:29 -02:00
										 |  |  | 	QUrl userIdRequest("https://graph.facebook.com/me?fields=id&access_token=" + QString(prefs.facebook.access_token)); | 
					
						
							| 
									
										
										
										
											2017-09-29 11:54:12 +02:00
										 |  |  | 	QNetworkReply *reply = manager->get(QNetworkRequest(userIdRequest)); | 
					
						
							| 
									
										
										
										
											2015-11-08 10:33:29 -02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-29 12:21:35 +02:00
										 |  |  | 	connect(reply, &QNetworkReply::finished, this, &FacebookManager::userIdReceived); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2015-11-08 10:33:29 -02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-29 12:21:35 +02:00
										 |  |  | void FacebookManager::userIdReceived() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender()); | 
					
						
							| 
									
										
										
										
											2015-11-08 10:33:29 -02:00
										 |  |  | 	QJsonDocument jsonDoc = QJsonDocument::fromJson(reply->readAll()); | 
					
						
							|  |  |  | 	QJsonObject obj = jsonDoc.object(); | 
					
						
							| 
									
										
										
										
											2017-09-29 12:22:33 +02:00
										 |  |  | 	if (obj.keys().contains("id")) { | 
					
						
							| 
									
										
										
										
											2017-10-02 14:48:34 +02:00
										 |  |  | 		qCDebug(lcFacebook) << "User id requested successfully:" << obj.value("id").toString(); | 
					
						
							| 
									
										
										
										
											2018-08-15 11:55:27 +02:00
										 |  |  | 		qPrefFacebook::set_user_id(obj.value("id").toString()); | 
					
						
							| 
									
										
										
										
											2017-10-02 15:25:44 +02:00
										 |  |  | 		emit sendMessage(tr("Facebook logged in successfully")); | 
					
						
							| 
									
										
										
										
											2017-09-29 12:22:33 +02:00
										 |  |  | 		emit justLoggedIn(true); | 
					
						
							| 
									
										
										
										
											2017-10-02 14:48:34 +02:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2017-10-02 15:25:44 +02:00
										 |  |  | 		emit sendMessage(tr("Error, unknown user id, cannot login.")); | 
					
						
							|  |  |  | 		qCDebug(lcFacebook) << "Error, unknown user id, cannot login."; | 
					
						
							| 
									
										
										
										
											2015-11-08 10:33:29 -02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-09-29 12:21:35 +02:00
										 |  |  | 	reply->deleteLater(); | 
					
						
							| 
									
										
										
										
											2015-11-08 10:33:29 -02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-02 12:55:13 +02:00
										 |  |  | QPixmap FacebookManager::grabProfilePixmap() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2017-10-02 14:48:34 +02:00
										 |  |  | 	qCDebug(lcFacebook) << "Grabbing Dive Profile pixmap"; | 
					
						
							| 
									
										
										
										
											2017-10-02 12:55:13 +02:00
										 |  |  | 	ProfileWidget2 *profile = MainWindow::instance()->graphics(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	QSize size = fbInfo.profileSize == FacebookInfo::SMALL  ? QSize(800,600) : | 
					
						
							|  |  |  | 		     fbInfo.profileSize == FacebookInfo::MEDIUM ? QSize(1024,760) : | 
					
						
							|  |  |  | 		     fbInfo.profileSize == FacebookInfo::BIG ? QSize(1280,1024) : QSize(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	auto currSize = profile->size(); | 
					
						
							|  |  |  | 	profile->resize(size); | 
					
						
							|  |  |  | 	profile->setToolTipVisibile(false); | 
					
						
							|  |  |  | 	QPixmap pix = profile->grab(); | 
					
						
							|  |  |  | 	profile->setToolTipVisibile(true); | 
					
						
							|  |  |  | 	profile->resize(currSize); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return pix; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-08 10:33:29 -02:00
										 |  |  | /* to be changed to export the currently selected dive as shown on the profile.
 | 
					
						
							|  |  |  |  * Much much easier, and its also good to people do not select all the dives | 
					
						
							|  |  |  |  * and send erroniously *all* of them to facebook. */ | 
					
						
							| 
									
										
										
										
											2017-10-02 14:35:36 +02:00
										 |  |  | void FacebookManager::sendDiveInit() | 
					
						
							| 
									
										
										
										
											2015-11-08 10:33:29 -02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-10-02 14:48:34 +02:00
										 |  |  | 	qCDebug(lcFacebook) << "Starting to upload the dive to facebook"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-08 10:33:29 -02:00
										 |  |  | 	SocialNetworkDialog dialog(qApp->activeWindow()); | 
					
						
							| 
									
										
										
										
											2017-10-02 14:48:34 +02:00
										 |  |  | 	if (dialog.exec() != QDialog::Accepted) { | 
					
						
							|  |  |  | 		qCDebug(lcFacebook) << "User cancelled."; | 
					
						
							| 
									
										
										
										
											2015-11-08 10:33:29 -02:00
										 |  |  | 		return; | 
					
						
							| 
									
										
										
										
											2017-10-02 14:48:34 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-11-08 10:33:29 -02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-02 14:22:02 +02:00
										 |  |  | 	fbInfo.bodyText = dialog.text(); | 
					
						
							|  |  |  | 	fbInfo.profileSize = dialog.profileSize(); | 
					
						
							|  |  |  | 	fbInfo.profileData = grabProfilePixmap(); | 
					
						
							| 
									
										
										
										
											2017-10-02 14:54:00 +02:00
										 |  |  | 	fbInfo.albumName = dialog.album(); | 
					
						
							| 
									
										
										
										
											2017-10-02 14:22:02 +02:00
										 |  |  | 	fbInfo.albumId = QString(); // request Album Id wil handle that.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-02 14:35:36 +02:00
										 |  |  | 	// will emit albumIdReceived, that's connected to sendDiveToAlbum
 | 
					
						
							| 
									
										
										
										
											2015-11-08 10:33:29 -02:00
										 |  |  | 	requestAlbumId(); | 
					
						
							| 
									
										
										
										
											2017-10-02 14:35:36 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2015-11-08 10:33:29 -02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-02 14:48:34 +02:00
										 |  |  | void FacebookManager::sendDiveToAlbum(const QString& albumId) | 
					
						
							| 
									
										
										
										
											2017-10-02 14:35:36 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-10-02 14:54:00 +02:00
										 |  |  | 	qCDebug(lcFacebook) << "Starting to upload the dive to album" << fbInfo.albumName << "id" << albumId; | 
					
						
							| 
									
										
										
										
											2017-10-02 14:48:34 +02:00
										 |  |  | 	QUrl url(graphApi + albumId + "/photos?" + | 
					
						
							| 
									
										
										
										
											2015-11-08 10:33:29 -02:00
										 |  |  | 		 "&access_token=" + QString(prefs.facebook.access_token) + | 
					
						
							|  |  |  | 		 "&source=image" + | 
					
						
							| 
									
										
										
										
											2017-10-02 14:35:36 +02:00
										 |  |  | 		 "&message=" + fbInfo.bodyText.replace(""", "%22")); | 
					
						
							| 
									
										
										
										
											2015-11-08 10:33:29 -02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	QNetworkRequest request(url); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	QString bound="margin"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	//according to rfc 1867 we need to put this string here:
 | 
					
						
							| 
									
										
										
										
											2018-03-02 20:29:50 +01:00
										 |  |  | 	QByteArray data(QString("--" + bound + "\r\n").toUtf8()); | 
					
						
							| 
									
										
										
										
											2015-11-08 10:33:29 -02:00
										 |  |  | 	data.append("Content-Disposition: form-data; name=\"action\"\r\n\r\n"); | 
					
						
							| 
									
										
										
										
											2017-09-13 11:56:41 +02:00
										 |  |  | 	data.append(graphApi + "\r\n"); | 
					
						
							| 
									
										
										
										
											2015-11-08 10:33:29 -02:00
										 |  |  | 	data.append("--" + bound + "\r\n");   //according to rfc 1867
 | 
					
						
							| 
									
										
										
										
											2017-09-29 12:45:28 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	//name of the input is "uploaded" in my form, next one is a file name.
 | 
					
						
							|  |  |  | 	data.append("Content-Disposition: form-data; name=\"uploaded\"; filename=\"" + QString::number(qrand()) + ".png\"\r\n"); | 
					
						
							| 
									
										
										
										
											2015-11-08 10:33:29 -02:00
										 |  |  | 	data.append("Content-Type: image/jpeg\r\n\r\n"); //data type
 | 
					
						
							| 
									
										
										
										
											2017-10-02 14:20:53 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	QByteArray bytes; | 
					
						
							|  |  |  | 	QBuffer buffer(&bytes); | 
					
						
							|  |  |  | 	buffer.open(QIODevice::WriteOnly); | 
					
						
							| 
									
										
										
										
											2017-10-02 14:35:36 +02:00
										 |  |  | 	fbInfo.profileData.save(&buffer, "PNG"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-08 10:33:29 -02:00
										 |  |  | 	data.append(bytes);   //let's read the file
 | 
					
						
							|  |  |  | 	data.append("\r\n"); | 
					
						
							|  |  |  | 	data.append("--" + bound + "--\r\n");  //closing boundary according to rfc 1867
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-02 20:29:50 +01:00
										 |  |  | 	request.setRawHeader(QByteArray("Content-Type"),QString("multipart/form-data; boundary=" + bound).toUtf8()); | 
					
						
							|  |  |  | 	request.setRawHeader(QByteArray("Content-Length"), QString::number(data.length()).toUtf8()); | 
					
						
							| 
									
										
										
										
											2017-09-29 11:54:12 +02:00
										 |  |  | 	QNetworkReply *reply = manager->post(request,data); | 
					
						
							| 
									
										
										
										
											2015-11-08 10:33:29 -02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-29 11:14:40 +02:00
										 |  |  | 	connect(reply, &QNetworkReply::finished, this, &FacebookManager::uploadFinished); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2015-11-08 10:33:29 -02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-29 11:14:40 +02:00
										 |  |  | void FacebookManager::uploadFinished() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2017-10-02 14:48:34 +02:00
										 |  |  | 	qCDebug(lcFacebook) << "Upload finish"; | 
					
						
							| 
									
										
										
										
											2017-09-29 11:14:40 +02:00
										 |  |  | 	auto reply = qobject_cast<QNetworkReply*>(sender()); | 
					
						
							| 
									
										
										
										
											2015-11-08 10:33:29 -02:00
										 |  |  | 	QByteArray response = reply->readAll(); | 
					
						
							|  |  |  | 	QJsonDocument jsonDoc = QJsonDocument::fromJson(response); | 
					
						
							|  |  |  | 	QJsonObject obj = jsonDoc.object(); | 
					
						
							| 
									
										
										
										
											2017-09-29 12:00:31 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	reply->deleteLater(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-08 10:33:29 -02:00
										 |  |  | 	if (obj.keys().contains("id")){ | 
					
						
							| 
									
										
										
										
											2017-10-02 15:25:44 +02:00
										 |  |  | 		emit sendMessage(tr("Dive uploaded successfully to Facebook")); | 
					
						
							| 
									
										
										
										
											2015-11-08 10:33:29 -02:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2017-10-02 15:25:44 +02:00
										 |  |  | 		emit sendMessage(tr("Dive upload failed. Please see debug output and send to Subsurface mailing list")); | 
					
						
							|  |  |  | 		qCDebug(lcFacebook) << "Dive upload failed" << response; | 
					
						
							| 
									
										
										
										
											2015-11-08 10:33:29 -02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-10-02 14:35:36 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	emit sendDiveFinished(); | 
					
						
							| 
									
										
										
										
											2015-11-08 10:33:29 -02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-10 10:30:26 +01:00
										 |  |  | void FacebookConnectWidget::showEvent(QShowEvent *event) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	if (FacebookManager::instance()->loggedIn()) { | 
					
						
							|  |  |  | 		facebookLoggedIn(); | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		facebookDisconnect(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return QDialog::showEvent(event); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-08 10:33:29 -02:00
										 |  |  | FacebookConnectWidget::FacebookConnectWidget(QWidget *parent) : QDialog(parent), ui(new Ui::FacebookConnectWidget) { | 
					
						
							| 
									
										
										
										
											2015-11-08 10:51:50 -02:00
										 |  |  | 	ui->setupUi(this); | 
					
						
							| 
									
										
										
										
											2015-11-08 10:33:29 -02:00
										 |  |  | 	FacebookManager *fb = FacebookManager::instance(); | 
					
						
							| 
									
										
										
										
											2016-08-09 00:12:12 +02:00
										 |  |  | #ifdef USE_WEBENGINE
 | 
					
						
							|  |  |  | 	facebookWebView = new QWebEngineView(this); | 
					
						
							|  |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2015-11-08 10:33:29 -02:00
										 |  |  | 	facebookWebView = new QWebView(this); | 
					
						
							| 
									
										
										
										
											2016-08-09 00:12:12 +02:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2015-11-08 10:33:29 -02:00
										 |  |  | 	ui->fbWebviewContainer->layout()->addWidget(facebookWebView); | 
					
						
							| 
									
										
										
										
											2016-08-09 00:12:12 +02:00
										 |  |  | #ifdef USE_WEBENGINE
 | 
					
						
							|  |  |  | 	connect(facebookWebView, &QWebEngineView::urlChanged, fb, &FacebookManager::tryLogin); | 
					
						
							|  |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2015-11-08 10:33:29 -02:00
										 |  |  | 	connect(facebookWebView, &QWebView::urlChanged, fb, &FacebookManager::tryLogin); | 
					
						
							| 
									
										
										
										
											2016-08-09 00:12:12 +02:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2015-11-08 10:33:29 -02:00
										 |  |  | 	connect(fb, &FacebookManager::justLoggedIn, this, &FacebookConnectWidget::facebookLoggedIn); | 
					
						
							| 
									
										
										
										
											2017-01-01 22:21:47 -08:00
										 |  |  | 	connect(fb, &FacebookManager::justLoggedOut, this, &FacebookConnectWidget::facebookDisconnect); | 
					
						
							| 
									
										
										
										
											2015-11-08 10:33:29 -02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void FacebookConnectWidget::facebookLoggedIn() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	ui->fbWebviewContainer->hide(); | 
					
						
							|  |  |  | 	ui->fbWebviewContainer->setEnabled(false); | 
					
						
							| 
									
										
										
										
											2017-01-01 22:23:39 -08:00
										 |  |  | 	ui->FBLabel->setText(tr("To disconnect Subsurface from your Facebook account, use the 'Share on' menu entry.")); | 
					
						
							| 
									
										
										
										
											2017-10-02 15:25:44 +02:00
										 |  |  | 	close(); | 
					
						
							| 
									
										
										
										
											2015-11-08 10:33:29 -02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void FacebookConnectWidget::facebookDisconnect() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2017-10-02 15:08:03 +02:00
										 |  |  | 	qCDebug(lcFacebook) << "Disconnecting from facebook"; | 
					
						
							| 
									
										
										
										
											2015-11-08 10:33:29 -02:00
										 |  |  | 	// remove the connect/disconnect button
 | 
					
						
							|  |  |  | 	// and instead add the login view
 | 
					
						
							|  |  |  | 	ui->fbWebviewContainer->show(); | 
					
						
							|  |  |  | 	ui->fbWebviewContainer->setEnabled(true); | 
					
						
							|  |  |  | 	ui->FBLabel->setText(tr("To connect to Facebook, please log in. This enables Subsurface to publish dives to your timeline")); | 
					
						
							|  |  |  | 	if (facebookWebView) { | 
					
						
							| 
									
										
										
										
											2016-08-09 00:12:12 +02:00
										 |  |  | #ifdef USE_WEBENGINE
 | 
					
						
							|  |  |  | 	//FIX ME
 | 
					
						
							|  |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2015-11-08 10:33:29 -02:00
										 |  |  | 		facebookWebView->page()->networkAccessManager()->setCookieJar(new QNetworkCookieJar()); | 
					
						
							| 
									
										
										
										
											2016-08-09 00:12:12 +02:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2015-11-08 10:33:29 -02:00
										 |  |  | 		facebookWebView->setUrl(FacebookManager::instance()->connectUrl()); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-08 12:10:13 -02:00
										 |  |  | SocialNetworkDialog::SocialNetworkDialog(QWidget *parent) : | 
					
						
							|  |  |  | 	QDialog(parent), | 
					
						
							|  |  |  | 	ui( new Ui::SocialnetworksDialog()) | 
					
						
							| 
									
										
										
										
											2015-11-08 10:33:29 -02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2015-11-08 12:10:13 -02:00
										 |  |  | 	ui->setupUi(this); | 
					
						
							|  |  |  | 	ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false); | 
					
						
							| 
									
										
										
										
											2017-09-29 12:44:39 -07:00
										 |  |  | 	connect(ui->date, &QCheckBox::clicked, this, &SocialNetworkDialog::selectionChanged); | 
					
						
							|  |  |  | 	connect(ui->duration, &QCheckBox::clicked, this, &SocialNetworkDialog::selectionChanged); | 
					
						
							|  |  |  | 	connect(ui->Buddy, &QCheckBox::clicked, this, &SocialNetworkDialog::selectionChanged); | 
					
						
							|  |  |  | 	connect(ui->Divemaster, &QCheckBox::clicked, this, &SocialNetworkDialog::selectionChanged); | 
					
						
							|  |  |  | 	connect(ui->Location, &QCheckBox::clicked, this, &SocialNetworkDialog::selectionChanged); | 
					
						
							|  |  |  | 	connect(ui->Notes, &QCheckBox::clicked, this, &SocialNetworkDialog::selectionChanged); | 
					
						
							| 
									
										
										
										
											2017-10-02 16:12:15 +02:00
										 |  |  | 	connect(ui->album, &QLineEdit::textChanged, this, &SocialNetworkDialog::albumChanged); | 
					
						
							| 
									
										
										
										
											2015-11-08 10:33:29 -02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-02 12:50:40 +02:00
										 |  |  | FacebookInfo::Size SocialNetworkDialog::profileSize() const | 
					
						
							| 
									
										
										
										
											2017-09-29 14:33:20 +02:00
										 |  |  | { | 
					
						
							|  |  |  | 	QString currText = ui->profileSize->currentText(); | 
					
						
							| 
									
										
										
										
											2017-10-02 12:50:40 +02:00
										 |  |  | 	return currText.startsWith(tr("Small")) ? FacebookInfo::SMALL : | 
					
						
							|  |  |  | 	       currText.startsWith(tr("Medium")) ?  FacebookInfo::MEDIUM : | 
					
						
							|  |  |  | 	       /* currText.startsWith(tr("Big")) ? */ FacebookInfo::BIG; | 
					
						
							| 
									
										
										
										
											2017-09-29 14:33:20 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-08 12:10:13 -02:00
										 |  |  | void SocialNetworkDialog::albumChanged() | 
					
						
							| 
									
										
										
										
											2015-11-08 10:33:29 -02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2015-11-08 12:10:13 -02:00
										 |  |  | 	QAbstractButton *button = ui->buttonBox->button(QDialogButtonBox::Ok); | 
					
						
							|  |  |  | 	button->setEnabled(!ui->album->text().isEmpty()); | 
					
						
							| 
									
										
										
										
											2015-11-08 10:33:29 -02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-08 12:10:13 -02:00
										 |  |  | void SocialNetworkDialog::selectionChanged() | 
					
						
							| 
									
										
										
										
											2015-11-08 10:33:29 -02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2015-11-08 12:10:13 -02:00
										 |  |  | 	struct dive *d = current_dive; | 
					
						
							|  |  |  | 	QString fullText; | 
					
						
							| 
									
										
										
										
											2017-03-25 11:51:37 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (!d) | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-08 12:10:13 -02:00
										 |  |  | 	if (ui->date->isChecked()) { | 
					
						
							|  |  |  | 		fullText += tr("Dive date: %1 \n").arg(get_short_dive_date_string(d->when)); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if (ui->duration->isChecked()) { | 
					
						
							|  |  |  | 		fullText += tr("Duration: %1 \n").arg(get_dive_duration_string(d->duration.seconds, | 
					
						
							| 
									
										
										
										
											2017-05-11 22:43:36 +02:00
										 |  |  | 									       tr("h", "abbreviation for hours"), | 
					
						
							| 
									
										
										
										
											2015-11-08 12:10:13 -02:00
										 |  |  | 									       tr("min", "abbreviation for minutes"))); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if (ui->Location->isChecked()) { | 
					
						
							|  |  |  | 		fullText += tr("Dive location: %1 \n").arg(get_dive_location(d)); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if (ui->Buddy->isChecked()) { | 
					
						
							|  |  |  | 		fullText += tr("Buddy: %1 \n").arg(d->buddy); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if (ui->Divemaster->isChecked()) { | 
					
						
							|  |  |  | 		fullText += tr("Divemaster: %1 \n").arg(d->divemaster); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if (ui->Notes->isChecked()) { | 
					
						
							|  |  |  | 		fullText += tr("\n%1").arg(d->notes); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	ui->text->setPlainText(fullText); | 
					
						
							| 
									
										
										
										
											2015-11-08 10:33:29 -02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-08 12:10:13 -02:00
										 |  |  | QString SocialNetworkDialog::text() const { | 
					
						
							|  |  |  | 	return ui->text->toPlainText().toHtmlEscaped(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | QString SocialNetworkDialog::album() const { | 
					
						
							|  |  |  | 	return ui->album->text().toHtmlEscaped(); | 
					
						
							| 
									
										
										
										
											2015-11-08 10:33:29 -02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2015-11-08 12:10:13 -02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 |