2017-06-02 16:51:17 +03:00
import QtQuick 2.0
import Sailfish . Silica 1.0
2022-11-01 22:07:17 +03:00
import Sailfish . WebView 1.0
import Sailfish . WebEngine 1.0
2022-11-04 17:40:35 +03:00
import io . thp . pyotherside 1.5
2017-06-02 16:51:17 +03:00
import "../lib/API.js" as Logic
Page {
2022-11-04 17:40:35 +03:00
// Python connections and signals, callable from QML side
// This is not ideal but keeps the page from erroring out on redirect
Python {
id: py
Component.onCompleted: {
addImportPath ( Qt . resolvedUrl ( '../lib/' ) ) ;
importModule ( 'server' , function ( ) { } ) ;
setHandler ( 'finished' , function ( newvalue ) {
console . debug ( newvalue )
} ) ;
startDownload ( ) ;
}
function startDownload ( ) {
call ( 'server.downloader.serve' , function ( ) { } ) ;
console . debug ( "called" )
}
}
2017-06-02 16:51:17 +03:00
id: loginPage
// The effective value will be restricted by ApplicationWindow.allowedOrientations
allowedOrientations: Orientation . All
SilicaFlickable {
anchors.fill: parent
contentHeight: column . height + Theme . paddingLarge
VerticalScrollDecorator { }
Column {
id: column
width: parent . width
2020-05-25 18:54:02 +03:00
PageHeader {
title: qsTr ( "Login" )
}
2017-06-02 16:51:17 +03:00
SectionHeader {
2018-10-26 23:49:37 +03:00
text: qsTr ( "Instance" )
2017-06-02 16:51:17 +03:00
}
TextField {
id: instance
focus: true
2020-04-28 08:24:51 +03:00
label: qsTr ( "Enter a valid Mastodon instance URL" )
2019-01-27 11:59:43 +03:00
text: "https://"
2017-06-02 16:51:17 +03:00
width: parent . width
2018-12-12 04:00:31 +03:00
validator: RegExpValidator { regExp: /^(ftp|http|https):\/\/[^ "]+$/ }
EnterKey.enabled: instance . acceptableInput ;
2020-04-29 15:08:20 +03:00
EnterKey.highlighted: instance . acceptableInput ;
EnterKey.iconSource: "image://theme/icon-m-accept"
2017-06-02 16:51:17 +03:00
EnterKey.onClicked: {
2020-06-04 20:44:12 +03:00
Logic . api = Logic . mastodonAPI ( { instance: instance . text , api_user_token: "" } ) ;
2017-06-02 16:51:17 +03:00
Logic . api . registerApplication ( "Tooter" ,
2022-11-04 17:40:35 +03:00
'http://localhost:8000/index.html' , // redirect uri, we will need this later on
2020-06-11 15:11:39 +03:00
[ "read" , "write" , "follow" ] , //scopes
2022-11-26 15:16:32 +03:00
"https://github.com/poetaster/harbour-tooter#readme" , //website on the login screen
2020-06-11 15:11:39 +03:00
function ( data ) {
2017-06-02 16:51:17 +03:00
2020-06-11 15:11:39 +03:00
console . log ( data )
var conf = JSON . parse ( data )
conf . instance = instance . text ;
conf . login = false ;
2017-06-02 16:51:17 +03:00
2020-06-11 15:11:39 +03:00
/ * c o n f [ ' l o g i n ' ] = f a l s e ;
2020-06-29 11:48:06 +03:00
conf [ 'mastodon_client_id' ] = data [ 'mastodon_client_id' ] ;
conf [ 'mastodon_client_secret' ] = data [ 'mastodon_client_secret' ] ;
conf [ 'mastodon_client_redirect_uri' ] = data [ 'mastodon_client_redirect_uri' ] ;
delete Logic . conf ; * /
2020-06-11 15:11:39 +03:00
Logic . conf = conf ;
console . log ( JSON . stringify ( conf ) )
console . log ( JSON . stringify ( Logic . conf ) )
// we got our application
// our user to it!
var url = Logic . api . generateAuthLink ( Logic . conf [ "client_id" ] ,
Logic . conf [ "redirect_uri" ] ,
"code" , // oauth method
[ "read" , "write" , "follow" ] //scopes
) ;
console . log ( url )
webView . url = url
webView . visible = true
}
) ;
2017-06-02 16:51:17 +03:00
}
}
2020-06-29 11:48:06 +03:00
2017-06-02 16:51:17 +03:00
Label {
2020-06-29 11:48:06 +03:00
id: serviceDescr
text: qsTr ( "Mastodon is a free, open-source social network. A decentralized alternative to commercial platforms, it avoids the risks of a single company monopolizing your communication. Pick a server that you trust — whichever you choose, you can interact with everyone else. Anyone can run their own Mastodon instance and participate in the social network seamlessly." )
font.pixelSize: Theme . fontSizeExtraSmall
color: Theme . highlightColor
wrapMode: Text . WordWrap
width: parent . width
2017-06-02 16:51:17 +03:00
anchors {
2020-04-29 15:08:20 +03:00
topMargin: Theme . paddingMedium
2020-07-03 11:45:30 +03:00
left: parent . left
2020-04-29 15:08:20 +03:00
leftMargin: Theme . horizontalPageMargin
2020-07-03 11:45:30 +03:00
right: parent . right
2020-04-29 15:08:20 +03:00
rightMargin: Theme . horizontalPageMargin
2017-06-02 16:51:17 +03:00
}
}
}
}
2022-11-01 22:07:17 +03:00
WebView {
2017-06-02 16:51:17 +03:00
id: webView
2022-11-01 22:07:17 +03:00
/* This will probably be required from 4.4 on. */
Component.onCompleted: {
WebEngineSettings . setPreference ( "security.disable_cors_checks" , true , WebEngineSettings . BoolPref )
WebEngineSettings . setPreference ( "security.fileuri.strict_origin_policy" , false , WebEngineSettings . BoolPref )
}
2022-11-04 17:40:35 +03:00
onViewInitialized: {
//webview.loadFrameScript(Qt.resolvedUrl("../html/framescript.js"));
//webview.addMessageListener("webview:action");
//webview.runJavaScript("return latlon('" + lat + "','" + lon + "')");
}
onRecvAsyncMessage: {
console . log ( 'async changed: ' + url )
console . debug ( message )
switch ( message ) {
case "embed:contentOrientationChanged" :
break
case "webview:action" :
if ( data . topic != lon ) {
//webview.runJavaScript("return latlon('" + lat + "','" + lon + "')");
//if (debug) console.debug(data.topic)
//if (debug) console.debug(data.also)
//if (debug) console.debug(data.src)
}
break
}
}
2017-06-02 16:51:17 +03:00
visible: false
2022-11-01 22:07:17 +03:00
//opacity: 0
2017-06-02 16:51:17 +03:00
anchors {
top: parent . top
left: parent . left
right: parent . right
bottom: parent . bottom
}
2022-11-01 22:07:17 +03:00
2017-06-02 16:51:17 +03:00
onLoadingChanged: {
2022-11-04 17:40:35 +03:00
console . log ( 'loading changed: ' + url )
2017-07-04 02:26:49 +03:00
if (
2022-11-04 17:40:35 +03:00
( url + "" ) . substr ( 0 , 38 ) === 'http://localhost:8000/index.html?code=' ||
( url + "" ) . substr ( 0 , 39 ) === 'https://localhost:8000/index.html?code='
2020-06-11 15:11:39 +03:00
) {
2017-06-02 16:51:17 +03:00
visible = false ;
2019-01-27 22:14:08 +03:00
var vars = { } ;
( url + "" ) . replace ( /[?&]+([^=&]+)=([^&]*)/gi , function ( m , key , value ) { vars [ key ] = value ; } ) ; /* found on https://html-online.com/articles/get-url-parameters-javascript/ */
var authCode = vars [ "code" ] ;
2017-06-02 16:51:17 +03:00
console . log ( authCode )
Logic . api . getAccessTokenFromAuthCode (
2020-06-11 15:11:39 +03:00
Logic . conf [ "client_id" ] ,
Logic . conf [ "client_secret" ] ,
Logic . conf [ "redirect_uri" ] ,
authCode ,
function ( data ) {
// AAAND DATA CONTAINS OUR TOKEN!
console . log ( data )
data = JSON . parse ( data )
console . log ( JSON . stringify ( data ) )
console . log ( JSON . stringify ( data . access_token ) )
Logic . conf [ "api_user_token" ] = data . access_token
Logic . conf [ "login" ] = true ;
Logic . api . setConfig ( "api_user_token" , Logic . conf [ "api_user_token" ] )
pageStack . replace ( Qt . resolvedUrl ( "MainPage.qml" ) , { } )
}
)
2017-06-02 16:51:17 +03:00
}
2022-11-01 22:07:17 +03:00
/ * s w i t c h ( l o a d R e q u e s t . s t a t u s )
2017-06-02 16:51:17 +03:00
{
case WebView.LoadSucceededStatus:
opacity = 1
break
case WebView.LoadFailedStatus:
//opacity = 0
break
default:
//opacity = 0
break
2022-11-01 22:07:17 +03:00
} * /
2017-06-02 16:51:17 +03:00
}
FadeAnimation on opacity { }
2020-06-29 11:48:06 +03:00
2017-06-02 16:51:17 +03:00
PullDownMenu {
MenuItem {
2018-10-26 23:49:37 +03:00
text: qsTr ( "Reload" )
2017-06-02 16:51:17 +03:00
onClicked: webView . reload ( )
}
}
}
2020-05-29 21:05:05 +03:00
}