2020-08-10 15:17:29 +03:00
/ *
2020-10-19 20:34:47 +03:00
Copyright ( C ) 2020 Sebastian J . Wolf and other contributors
2020-08-10 15:17:29 +03:00
This file is part of Fernschreiber .
2020-08-13 18:08:14 +03:00
Fernschreiber is free software: you can redistribute it and / or modify
2020-08-10 15:17:29 +03:00
it under the terms of the GNU General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
2020-08-13 18:08:14 +03:00
Fernschreiber is distributed in the hope that it will be useful ,
2020-08-10 15:17:29 +03:00
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU General Public License for more details .
You should have received a copy of the GNU General Public License
along with Fernschreiber . If not , see < http: //www.gnu.org/licenses/>.
* /
2020-10-31 22:49:03 +03:00
import QtQuick 2.6
2020-08-10 15:17:29 +03:00
import Sailfish . Silica 1.0
import Nemo . Notifications 1.0
2020-08-13 11:15:26 +03:00
import WerkWolf . Fernschreiber 1.0
2020-08-19 10:55:13 +03:00
import "../components"
import "../js/twemoji.js" as Emoji
2020-08-19 17:47:59 +03:00
import "../js/functions.js" as Functions
2020-11-20 23:58:26 +03:00
import "../js/debug.js" as Debug
2020-08-10 15:17:29 +03:00
Page {
id: overviewPage
allowedOrientations: Orientation . All
2020-08-18 12:11:03 +03:00
property bool initializationCompleted: false ;
2020-08-13 11:15:26 +03:00
property bool loading: true ;
2020-08-13 16:35:43 +03:00
property int authorizationState: TelegramAPI . Closed
property int connectionState: TelegramAPI . WaitingForNetwork
2020-08-20 01:24:24 +03:00
property int ownUserId ;
2020-08-20 19:45:56 +03:00
property bool chatListCreated: false ;
2020-08-13 11:15:26 +03:00
2020-08-18 12:11:03 +03:00
onStatusChanged: {
2020-08-21 19:03:51 +03:00
if ( status === PageStatus . Active && initializationCompleted && ! chatListCreated ) {
2020-08-18 12:11:03 +03:00
updateContent ( ) ;
}
}
2020-09-15 22:17:44 +03:00
Connections {
target: dBusAdaptor
onPleaseOpenMessage: {
2020-11-20 23:58:26 +03:00
Debug . log ( "[OverviewPage] Opening chat from external call..." )
2020-09-15 22:17:44 +03:00
if ( chatListCreated ) {
2020-11-01 06:13:55 +03:00
pageStack . pop ( overviewPage , PageStackAction . Immediate )
2020-11-20 21:08:05 +03:00
pageStack . push ( Qt . resolvedUrl ( "../pages/ChatPage.qml" ) , { "chatInformation" : chatListModel . getById ( chatId ) } , PageStackAction . Immediate )
2020-09-15 22:17:44 +03:00
}
}
2020-11-08 23:13:04 +03:00
onPleaseOpenUrl: {
2020-11-20 23:58:26 +03:00
Debug . log ( "[OverviewPage] Opening URL requested: " , url ) ;
2020-11-08 23:13:04 +03:00
Functions . handleLink ( url ) ;
}
2020-09-15 22:17:44 +03:00
}
2020-08-20 19:45:56 +03:00
Timer {
id: chatListCreatedTimer
2020-09-30 22:41:49 +03:00
interval: 300
2020-08-20 19:45:56 +03:00
running: false
repeat: false
onTriggered: {
overviewPage . chatListCreated = true ;
2020-09-30 22:41:49 +03:00
chatListModel . redrawModel ( ) ;
2020-08-20 19:45:56 +03:00
}
}
2020-11-27 12:25:23 +03:00
Timer {
id: openInitializationPageTimer
interval: 0
onTriggered: {
pageStack . push ( Qt . resolvedUrl ( "../pages/InitializationPage.qml" ) ) ;
}
}
2020-08-13 18:08:14 +03:00
function setPageStatus ( ) {
switch ( overviewPage . connectionState ) {
case TelegramAPI.WaitingForNetwork:
pageStatus . color = "red" ;
2020-08-13 23:32:35 +03:00
pageHeader . title = qsTr ( "Waiting for network..." ) ;
2020-08-13 18:08:14 +03:00
break ;
case TelegramAPI.Connecting:
pageStatus . color = "gold" ;
2020-08-13 23:32:35 +03:00
pageHeader . title = qsTr ( "Connecting to network..." ) ;
2020-08-13 18:08:14 +03:00
break ;
case TelegramAPI.ConnectingToProxy:
pageStatus . color = "gold" ;
2020-08-13 23:32:35 +03:00
pageHeader . title = qsTr ( "Connecting to proxy..." ) ;
2020-08-13 18:08:14 +03:00
break ;
case TelegramAPI.ConnectionReady:
pageStatus . color = "green" ;
pageHeader . title = qsTr ( "Fernschreiber" ) ;
break ;
case TelegramAPI.Updating:
pageStatus . color = "lightblue" ;
2020-08-13 23:32:35 +03:00
pageHeader . title = qsTr ( "Updating content..." ) ;
2020-08-13 18:08:14 +03:00
break ;
}
}
2020-08-13 23:32:35 +03:00
function updateContent ( ) {
2020-08-16 18:38:51 +03:00
tdLibWrapper . getChats ( ) ;
2020-10-12 23:44:21 +03:00
tdLibWrapper . getRecentStickers ( ) ;
tdLibWrapper . getInstalledStickerSets ( ) ;
2020-11-20 20:30:33 +03:00
tdLibWrapper . getContacts ( ) ;
2020-08-18 00:44:37 +03:00
}
function initializePage ( ) {
overviewPage . authorizationState = tdLibWrapper . getAuthorizationState ( ) ;
2020-11-27 12:25:23 +03:00
overviewPage . handleAuthorizationState ( true ) ;
2020-08-18 12:11:03 +03:00
overviewPage . connectionState = tdLibWrapper . getConnectionState ( ) ;
overviewPage . setPageStatus ( ) ;
}
2020-11-27 12:25:23 +03:00
function handleAuthorizationState ( isOnInitialization ) {
2020-08-18 12:11:03 +03:00
switch ( overviewPage . authorizationState ) {
case TelegramAPI.WaitPhoneNumber:
case TelegramAPI.WaitCode:
2020-09-16 23:36:43 +03:00
case TelegramAPI.WaitPassword:
2020-10-01 01:55:26 +03:00
case TelegramAPI.WaitRegistration:
2020-09-16 23:36:43 +03:00
overviewPage . loading = false ;
2020-11-27 12:25:23 +03:00
if ( isOnInitialization ) { // pageStack isn't ready on Component.onCompleted
openInitializationPageTimer . start ( )
} else {
pageStack . push ( Qt . resolvedUrl ( "../pages/InitializationPage.qml" ) ) ;
}
2020-09-16 23:36:43 +03:00
break ;
2020-08-18 12:11:03 +03:00
case TelegramAPI.AuthorizationReady:
overviewPage . loading = false ;
overviewPage . initializationCompleted = true ;
2020-08-18 00:44:37 +03:00
overviewPage . updateContent ( ) ;
2020-08-18 12:11:03 +03:00
break ;
default:
// Nothing ;)
2020-08-18 00:44:37 +03:00
}
2020-08-13 23:32:35 +03:00
}
2020-08-13 11:15:26 +03:00
Connections {
target: tdLibWrapper
onAuthorizationStateChanged: {
2020-08-13 16:35:43 +03:00
overviewPage . authorizationState = authorizationState ;
2020-08-18 12:11:03 +03:00
handleAuthorizationState ( ) ;
2020-08-13 11:15:26 +03:00
}
2020-08-13 16:35:43 +03:00
onConnectionStateChanged: {
overviewPage . connectionState = connectionState ;
2020-08-13 18:08:14 +03:00
setPageStatus ( ) ;
2020-08-13 16:35:43 +03:00
}
2020-08-20 01:24:24 +03:00
onOwnUserIdFound: {
overviewPage . ownUserId = ownUserId ;
}
2020-08-20 19:45:56 +03:00
onChatLastMessageUpdated: {
if ( ! overviewPage . chatListCreated ) {
2020-10-04 14:36:30 +03:00
chatListCreatedTimer . restart ( ) ;
2020-08-20 19:45:56 +03:00
}
}
2020-08-20 11:50:47 +03:00
onChatOrderUpdated: {
2020-08-20 19:45:56 +03:00
if ( ! overviewPage . chatListCreated ) {
2020-10-04 14:36:30 +03:00
chatListCreatedTimer . restart ( ) ;
2020-08-20 19:45:56 +03:00
}
2020-08-20 11:50:47 +03:00
}
2020-10-01 13:50:29 +03:00
onChatsReceived: {
if ( chats && chats . chat_ids && chats . chat_ids . length === 0 ) {
2020-10-04 14:36:30 +03:00
chatListCreatedTimer . restart ( ) ;
2020-10-01 13:50:29 +03:00
}
}
2020-10-19 13:20:02 +03:00
onChatReceived: {
if ( chat [ "@extra" ] === "openDirectly" ) {
2020-11-01 06:13:55 +03:00
pageStack . pop ( overviewPage , PageStackAction . Immediate )
2020-10-19 13:20:02 +03:00
// if we get a new chat (no messages?), we can not use the provided data
pageStack . push ( Qt . resolvedUrl ( "../pages/ChatPage.qml" ) , { "chatInformation" : tdLibWrapper . getChat ( chat . id ) } ) ;
}
}
2020-11-10 01:22:24 +03:00
onErrorReceived: {
2020-11-16 16:37:14 +03:00
Functions . handleErrorMessage ( code , message ) ;
2020-11-10 01:22:24 +03:00
}
2020-08-13 16:35:43 +03:00
}
Component.onCompleted: {
2020-08-18 00:44:37 +03:00
initializePage ( ) ;
2020-08-13 11:15:26 +03:00
}
2020-08-10 15:17:29 +03:00
SilicaFlickable {
2020-08-21 15:26:56 +03:00
id: overviewContainer
2020-08-19 10:55:13 +03:00
contentHeight: parent . height
contentWidth: parent . width
2020-08-10 15:17:29 +03:00
anchors.fill: parent
2020-08-13 11:15:26 +03:00
visible: ! overviewPage . loading
2020-08-10 15:17:29 +03:00
PullDownMenu {
MenuItem {
text: qsTr ( "About Fernschreiber" )
onClicked: pageStack . push ( Qt . resolvedUrl ( "../pages/AboutPage.qml" ) )
}
2020-09-16 23:04:02 +03:00
MenuItem {
text: qsTr ( "Settings" )
onClicked: pageStack . push ( Qt . resolvedUrl ( "../pages/SettingsPage.qml" ) )
}
2020-11-20 20:30:33 +03:00
MenuItem {
text: qsTr ( "New Chat" )
onClicked: pageStack . push ( Qt . resolvedUrl ( "../pages/NewChatPage.qml" ) )
}
2020-08-10 15:17:29 +03:00
}
Column {
id: column
width: parent . width
2020-08-19 10:55:13 +03:00
height: parent . height
spacing: Theme . paddingMedium
2020-08-10 15:17:29 +03:00
2020-08-13 18:08:14 +03:00
Row {
2020-08-19 10:55:13 +03:00
id: headerRow
2020-08-13 18:08:14 +03:00
width: parent . width
GlassItem {
id: pageStatus
width: Theme . itemSizeMedium
height: Theme . itemSizeMedium
color: "red"
falloffRadius: 0.1
radius: 0.2
cache: false
}
PageHeader {
id: pageHeader
title: qsTr ( "Fernschreiber" )
width: parent . width - pageStatus . width
}
2020-08-10 15:17:29 +03:00
}
2020-08-30 14:42:22 +03:00
Item {
id: chatListItem
2020-08-19 10:55:13 +03:00
width: parent . width
height: parent . height - Theme . paddingMedium - headerRow . height
2020-08-30 14:42:22 +03:00
SilicaListView {
2020-08-19 10:55:13 +03:00
2020-08-30 14:42:22 +03:00
id: chatListView
2020-08-19 10:55:13 +03:00
2020-08-30 14:42:22 +03:00
anchors.fill: parent
2020-08-19 10:55:13 +03:00
2020-08-30 14:42:22 +03:00
clip: true
opacity: overviewPage . chatListCreated ? 1 : 0
Behavior on opacity { NumberAnimation { } }
2020-08-19 10:55:13 +03:00
2020-08-30 14:42:22 +03:00
model: chatListModel
2020-10-19 13:20:02 +03:00
delegate: ChatListViewItem {
ownUserId: overviewPage . ownUserId
2020-08-30 14:42:22 +03:00
onClicked: {
2020-11-20 22:39:23 +03:00
pageStack . push ( Qt . resolvedUrl ( "../pages/ChatPage.qml" ) , {
chatInformation : display ,
chatPicture: photo_small
} )
2020-09-16 01:15:43 +03:00
}
2020-10-04 17:06:20 +03:00
Connections {
target: chatListModel
onChatChanged: {
if ( overviewPage . chatListCreated && changedChatId === chat_id ) {
// Force update of some list item elements (currently only last message text seems to create problems). dataChanged() doesn't seem to trigger them all :(
2020-11-19 13:41:32 +03:00
secondaryText . text = last_message_text ? Emoji . emojify ( Functions . enhanceHtmlEntities ( last_message_text ) , Theme . fontSizeExtraSmall ) : qsTr ( "Unknown" )
2020-08-19 10:55:13 +03:00
}
2020-08-30 14:42:22 +03:00
}
2020-08-19 10:55:13 +03:00
}
}
2020-10-01 13:50:29 +03:00
ViewPlaceholder {
enabled: chatListView . count === 0
text: qsTr ( "You don't have any chats yet." )
}
2020-08-30 14:42:22 +03:00
VerticalScrollDecorator { }
2020-08-19 10:55:13 +03:00
}
2020-08-30 14:42:22 +03:00
Column {
width: parent . width
height: loadingLabel . height + loadingBusyIndicator . height + Theme . paddingMedium
spacing: Theme . paddingMedium
anchors.verticalCenter: parent . verticalCenter
opacity: overviewPage . chatListCreated ? 0 : 1
Behavior on opacity { NumberAnimation { } }
visible: ! overviewPage . chatListCreated
InfoLabel {
id: loadingLabel
text: qsTr ( "Loading chat list..." )
}
BusyIndicator {
id: loadingBusyIndicator
anchors.horizontalCenter: parent . horizontalCenter
running: ! overviewPage . chatListCreated
size: BusyIndicatorSize . Large
}
}
2020-08-19 10:55:13 +03:00
}
2020-08-10 15:17:29 +03:00
}
}
}