Skip to content

Commit

Permalink
gui: Add partial sync configuration button (#2174)
Browse files Browse the repository at this point in the history
We add a button in the `Preferences` tab of the main window to open
the partial synchronization configuration for the Desktop's OAuth
client within the remote Cozy Settings app.

It will be opened in the user's own Web browser for 2 reasons:
1. it clearly shows the configuration can be done from the Settings
   Web app and thus any computer
2. the user is more likely to be logged into their Cozy in their own
   browser and thus not be required to log in again to configure the
   Desktop's partial synchronization

The button will only be shown if the
`settings.partial-desktop-sync.show-synced-folders-selection` flag is
enabled (either in the local configuration or on the remote Cozy).
  • Loading branch information
taratatach authored Nov 16, 2021
2 parents f02223e + a5e7e59 commit ac1fd54
Show file tree
Hide file tree
Showing 19 changed files with 245 additions and 38 deletions.
106 changes: 106 additions & 0 deletions gui/elm/Data/SyncConfig.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
port module Data.SyncConfig exposing (SyncConfig, buildAppUrl, gotSyncConfig, init)

import Url exposing (Url)


type alias SyncConfig =
{ address : Maybe Url
, capabilities :
{ flatSubdomains : Bool
}
, deviceId : String
, deviceName : String
, flags :
{ partialSyncEnabled : Bool
}
}


init : SyncConfig
init =
{ address = Nothing
, capabilities =
{ flatSubdomains = True
}
, deviceId = ""
, deviceName = ""
, flags =
{ partialSyncEnabled = False
}
}


type alias AppSlug =
String


buildAppUrl : SyncConfig -> AppSlug -> Maybe Url
buildAppUrl { address, capabilities } slug =
let
cozyName =
case address of
Just url ->
String.split "." url.host
|> List.head
|> Maybe.withDefault ""

_ ->
""

host =
case ( address, capabilities.flatSubdomains ) of
( Just url, True ) ->
String.replace cozyName (cozyName ++ "-" ++ slug) url.host

( Just url, False ) ->
String.join "." [ slug, url.host ]

( _, _ ) ->
""
in
Maybe.map
(\url ->
{ protocol = url.protocol
, host = host
, port_ = url.port_
, path = ""
, query = Nothing
, fragment = Nothing
}
)
address



-- Communicate through ports


port syncConfig : (EncodedSyncConfig -> msg) -> Sub msg


gotSyncConfig : (SyncConfig -> msg) -> Sub msg
gotSyncConfig msg =
syncConfig (msg << decode)


type alias EncodedSyncConfig =
{ address : String
, capabilities :
{ flatSubdomains : Bool
}
, deviceId : String
, deviceName : String
, flags :
{ partialSyncEnabled : Bool
}
}


decode : EncodedSyncConfig -> SyncConfig
decode { address, capabilities, deviceId, deviceName, flags } =
{ address = Url.fromString address
, capabilities = capabilities
, deviceId = deviceId
, deviceName = deviceName
, flags = flags
}
4 changes: 0 additions & 4 deletions gui/elm/Ports.elm
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ port module Ports exposing
, showHelp
, showInParent
, startSync
, synchonization
, transfer
, unlinkCozy
, updateDownloading
Expand Down Expand Up @@ -116,9 +115,6 @@ port showHelp : () -> Cmd msg
port startSync : String -> Cmd msg


port synchonization : (( String, String ) -> msg) -> Sub msg


port transfer : (EncodedFile -> msg) -> Sub msg


Expand Down
12 changes: 12 additions & 0 deletions gui/elm/Util/Conditional.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module Util.Conditional exposing (viewIf)

import Html exposing (Html, text)


viewIf : Bool -> Html msg -> Html msg
viewIf condition content =
if condition then
content

else
text ""
9 changes: 5 additions & 4 deletions gui/elm/Window/Tray.elm
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module Window.Tray exposing

import Data.Platform exposing (Platform)
import Data.Status as Status exposing (Status)
import Data.SyncConfig as SyncConfig exposing (SyncConfig)
import Data.SyncState as SyncState exposing (SyncState)
import Html exposing (..)
import Html.Attributes exposing (..)
Expand Down Expand Up @@ -59,7 +60,7 @@ init version platform =

type Msg
= GotSyncState SyncState
| SyncStart ( String, String )
| GotSyncConfig SyncConfig
| GoToCozy
| GoToFolder
| GoToTab Page
Expand Down Expand Up @@ -96,10 +97,10 @@ update msg model =
, Cmd.none
)

SyncStart info ->
GotSyncConfig config ->
let
( settings, _ ) =
Settings.update (Settings.FillAddressAndDevice info) model.settings
Settings.update (Settings.GotSyncConfig config) model.settings
in
( { model | page = DashboardPage, settings = settings }, Cmd.none )

Expand Down Expand Up @@ -148,7 +149,7 @@ update msg model =
subscriptions : Model -> Sub Msg
subscriptions model =
Sub.batch
[ Ports.synchonization SyncStart
[ SyncConfig.gotSyncConfig GotSyncConfig
, Ports.gototab GoToStrTab
, SyncState.gotNewState GotSyncState

Expand Down
67 changes: 56 additions & 11 deletions gui/elm/Window/Tray/Settings.elm
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@ module Window.Tray.Settings exposing

import Data.DiskSpace exposing (DiskSpace)
import Data.Status exposing (Status(..))
import Data.SyncConfig as SyncConfig exposing (SyncConfig)
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (..)
import Locale exposing (Helpers)
import Ports
import Url exposing (Url)
import Util.Conditional exposing (viewIf)
import View.ProgressBar as ProgressBar


Expand All @@ -27,8 +30,7 @@ type alias Model =
{ version : String
, newRelease : Maybe ( String, String )
, autoLaunch : Bool
, address : String
, deviceName : String
, syncConfig : SyncConfig
, disk : DiskSpace
, busyUnlinking : Bool
, busyQuitting : Bool
Expand All @@ -41,8 +43,7 @@ init version =
{ version = version
, newRelease = Nothing
, autoLaunch = True
, address = ""
, deviceName = ""
, syncConfig = SyncConfig.init
, disk =
{ used = 0
, quota = 0
Expand All @@ -58,11 +59,11 @@ init version =


type Msg
= SetAutoLaunch Bool
= GotSyncConfig SyncConfig
| SetAutoLaunch Bool
| AutoLaunchSet Bool
| QuitAndInstall
| NewRelease ( String, String )
| FillAddressAndDevice ( String, String )
| UpdateDiskSpace DiskSpace
| UnlinkCozy
| CancelUnlink
Expand All @@ -77,6 +78,9 @@ update msg model =
case
msg
of
GotSyncConfig syncConfig ->
( { model | syncConfig = syncConfig }, Cmd.none )

SetAutoLaunch autoLaunch ->
( { model | autoLaunch = autoLaunch }, Ports.autoLauncher autoLaunch )

Expand All @@ -89,9 +93,6 @@ update msg model =
NewRelease ( notes, name ) ->
( { model | newRelease = Just ( notes, name ) }, Cmd.none )

FillAddressAndDevice ( address, deviceName ) ->
( { model | address = address, deviceName = deviceName }, Cmd.none )

UpdateDiskSpace disk ->
( { model | disk = disk }, Cmd.none )

Expand Down Expand Up @@ -156,6 +157,10 @@ versionLine helpers model =

view : Helpers -> Status -> Model -> Html Msg
view helpers status model =
let
{ partialSyncEnabled } =
model.syncConfig.flags
in
section [ class "two-panes__content two-panes__content--settings" ]
[ h2 [] [ text (helpers.t "Account Cozy disk space") ]
, diskQuotaLine helpers model
Expand All @@ -178,14 +183,18 @@ view helpers status model =
]
, h2 [] [ text (helpers.t "Settings Synchronize manually") ]
, syncButton helpers status model
, viewIf partialSyncEnabled <|
h2 [] [ text (helpers.t "Settings Selective synchronization") ]
, viewIf partialSyncEnabled <|
selectiveSyncButton helpers model
, h2 [] [ text (helpers.t "Account About") ]
, p []
[ strong [] [ text (helpers.t "Account Account" ++ " ") ]
, a [ href model.address ] [ text model.address ]
, cozyLink model
]
, p []
[ strong [] [ text (helpers.t "Account Device name" ++ " ") ]
, text model.deviceName
, text model.syncConfig.deviceName
]
, p []
[ strong [] [ text (helpers.t "Settings Version" ++ " ") ]
Expand Down Expand Up @@ -228,6 +237,18 @@ view helpers status model =
]


cozyLink : Model -> Html Msg
cozyLink model =
let
{ address } =
model.syncConfig

url =
Maybe.withDefault "" <| Maybe.map Url.toString address
in
a [ href url ] [ text url ]


syncButton : Helpers -> Status -> Model -> Html Msg
syncButton helpers status model =
let
Expand All @@ -244,3 +265,27 @@ syncButton helpers status model =
attribute "disabled" "true"
]
[ span [] [ text (helpers.t "Settings Sync") ] ]


selectiveSyncButton : Helpers -> Model -> Html Msg
selectiveSyncButton helpers model =
let
{ deviceId } =
model.syncConfig

settingsUrl =
SyncConfig.buildAppUrl model.syncConfig "settings"

configurationUrl =
case settingsUrl of
Just url ->
String.join "/" [ Url.toString url, "#/connectedDevices", deviceId ]

Nothing ->
""
in
a
[ class "btn"
, href configurationUrl
]
[ span [] [ text (helpers.t "Settings Configure") ] ]
4 changes: 3 additions & 1 deletion gui/locales/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,10 @@
"Settings Start Cozy Drive on system startup": "Cozy Drive mit dem System starten",
"Settings Startup": "Ihre Cozy wird automatisch mit ihrem Computer synchronisiert.",
"Settings Sync": "Synchronisieren",
"Settings Synchronize manually": "Manuell synchronisieren",
"Settings Version": "Version",
"Settings Synchronize manually": "Manuell synchronisieren",
"Settings Configure": "Configure",
"Settings Selective synchronization": "Selective synchronization",

"SyncDirEmpty Detail": "Um Datenverlust zu vermeiden, wird dies nicht synchronisiert. Wenn du die Synchronisation wirklich zurücksetzen möchtest, kannst du einfach den Cozy Ordner entfernen.",
"SyncDirEmpty Message": "Es sieht so aus, als sei dein Cozy Ordner geleert worden. Vielleicht ist er auf einer Festplatte, die gerade nicht eingesteckt ist?",
Expand Down
4 changes: 3 additions & 1 deletion gui/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,10 @@
"Settings Start Cozy Drive on system startup": "Start Cozy Drive on system startup",
"Settings Startup": "Your Cozy will be automatically synchronized with your computer",
"Settings Sync": "Synchronize",
"Settings Synchronize manually": "Synchronize manually",
"Settings Version": "Version",
"Settings Synchronize manually": "Synchronize manually",
"Settings Configure": "Configure",
"Settings Selective synchronization": "Selective synchronization",

"SyncDirEmpty Detail": "To avoid losing your data, this will not be synchronized. If you really want to reset the synchronization, you can just remove your Cozy folder.",
"SyncDirEmpty Message": "Look like you Cozy folder got emptied. May be it is on a hard-drive which is not plugged at the moment?",
Expand Down
4 changes: 3 additions & 1 deletion gui/locales/eo.json
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,10 @@
"Settings Start Cozy Drive on system startup": "Start Cozy Drive on system startup",
"Settings Startup": "Your Cozy will be automatically synchronized with your computer",
"Settings Sync": "Synchronize",
"Settings Synchronize manually": "Synchronize manually",
"Settings Version": "Version",
"Settings Synchronize manually": "Synchronize manually",
"Settings Configure": "Configure",
"Settings Selective synchronization": "Selective synchronization",

"SyncDirEmpty Detail": "To avoid losing your data, this will not be synchronized. If you really want to reset the synchronization, you can just remove your Cozy folder.",
"SyncDirEmpty Message": "Look like you Cozy folder got emptied. May be it is on a hard-drive which is not plugged at the moment?",
Expand Down
4 changes: 3 additions & 1 deletion gui/locales/es.json
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,10 @@
"Settings Start Cozy Drive on system startup": "Activar Cozy Drive al iniciar el sistema.",
"Settings Startup": "Su Cozy se sincronizará automáticamente con su ordenador",
"Settings Sync": "Sincronizar",
"Settings Synchronize manually": "Sincronizar manualmente",
"Settings Version": "Versión",
"Settings Synchronize manually": "Sincronizar manualmente",
"Settings Configure": "Configure",
"Settings Selective synchronization": "Selective synchronization",

"SyncDirEmpty Detail": "Para evitar la pérdida de datos, estos no se sincronizarán. Si realmente desea restablecer la sincronización, sólo tiene que eliminar la carpeta Cozy.",
"SyncDirEmpty Message": "Mire si su carpeta Cozy está llena. Quizás su disco duro no está conectado en este momento.",
Expand Down
2 changes: 2 additions & 0 deletions gui/locales/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@
"Settings Sync": "Synchroniser",
"Settings Synchronize manually": "Synchroniser manuellement",
"Settings Version": "Version",
"Settings Configure": "Configurer",
"Settings Selective synchronization": "Synchronisation sélective",

"SyncDirEmpty Detail": "Afin d'éviter toute perte de données, ces changements ne seront pas synchronisés. Si vous souhaitez vraiment réinitialiser la synchronisation, vous pouvez simplement supprimer votre dossier Cozy.",
"SyncDirEmpty Message": "Il semblerait que votre dossier Cozy ait été vidé. Peut-être se trouve-t-il sur un disque externe qui n'est pas connecté actuellement ?",
Expand Down
4 changes: 3 additions & 1 deletion gui/locales/it_IT.json
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,10 @@
"Settings Start Cozy Drive on system startup": "Esegui Cozy Drive all'avvio del sistema",
"Settings Startup": "Il tuo Cozy verrà sincronizzato automaticamente con il tuo computer",
"Settings Sync": "Synchronize",
"Settings Synchronize manually": "Synchronize manually",
"Settings Version": "Versione",
"Settings Synchronize manually": "Synchronize manually",
"Settings Configure": "Configure",
"Settings Selective synchronization": "Selective synchronization",

"SyncDirEmpty Detail": "To avoid losing your data, this will not be synchronized. If you really want to reset the synchronization, you can just remove your Cozy folder.",
"SyncDirEmpty Message": "Look like you Cozy folder got emptied. May be it is on a hard-drive which is not plugged at the moment?",
Expand Down
Loading

0 comments on commit ac1fd54

Please sign in to comment.