diff options
Diffstat (limited to 'app/src/main/java/org/pacien/tincapp/activities/configure/tools/JoinNetworkToolDialogFragment.kt')
-rw-r--r-- | app/src/main/java/org/pacien/tincapp/activities/configure/tools/JoinNetworkToolDialogFragment.kt | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/app/src/main/java/org/pacien/tincapp/activities/configure/tools/JoinNetworkToolDialogFragment.kt b/app/src/main/java/org/pacien/tincapp/activities/configure/tools/JoinNetworkToolDialogFragment.kt new file mode 100644 index 0000000..25bdb15 --- /dev/null +++ b/app/src/main/java/org/pacien/tincapp/activities/configure/tools/JoinNetworkToolDialogFragment.kt | |||
@@ -0,0 +1,82 @@ | |||
1 | /* | ||
2 | * Tinc App, an Android binding and user interface for the tinc mesh VPN daemon | ||
3 | * Copyright (C) 2017-2018 Pacien TRAN-GIRARD | ||
4 | * | ||
5 | * This program is free software: you can redistribute it and/or modify | ||
6 | * it under the terms of the GNU General Public License as published by | ||
7 | * the Free Software Foundation, either version 3 of the License, or | ||
8 | * (at your option) any later version. | ||
9 | * | ||
10 | * This program is distributed in the hope that it will be useful, | ||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
13 | * GNU General Public License for more details. | ||
14 | * | ||
15 | * You should have received a copy of the GNU General Public License | ||
16 | * along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
17 | */ | ||
18 | |||
19 | package org.pacien.tincapp.activities.configure.tools | ||
20 | |||
21 | import android.content.Intent | ||
22 | import android.os.Bundle | ||
23 | import android.view.View | ||
24 | import com.google.zxing.integration.android.IntentIntegrator | ||
25 | import com.google.zxing.integration.android.IntentResult | ||
26 | import kotlinx.android.synthetic.main.configure_tools_dialog_network_join.view.* | ||
27 | import org.pacien.tincapp.R | ||
28 | import org.pacien.tincapp.commands.Tinc | ||
29 | import org.pacien.tincapp.commands.TincApp | ||
30 | import org.pacien.tincapp.databinding.ConfigureToolsDialogNetworkJoinBinding | ||
31 | |||
32 | /** | ||
33 | * @author pacien | ||
34 | */ | ||
35 | class JoinNetworkToolDialogFragment : ConfigurationToolDialogFragment() { | ||
36 | private val scanner by lazy { IntentIntegrator.forSupportFragment(this) } | ||
37 | private var joinDialog: View? = null | ||
38 | |||
39 | override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { | ||
40 | IntentIntegrator.parseActivityResult(requestCode, resultCode, data) | ||
41 | ?.let(IntentResult::getContents) | ||
42 | ?.let(String::trim) | ||
43 | ?.let { joinDialog?.invitation_url?.setText(it) } | ||
44 | } | ||
45 | |||
46 | override fun onCreateDialog(savedInstanceState: Bundle?) = | ||
47 | makeJoinDialog().let { newDialog -> | ||
48 | joinDialog = newDialog | ||
49 | makeDialog( | ||
50 | newDialog, | ||
51 | R.string.configure_tools_join_network_title, | ||
52 | R.string.configure_tools_join_network_action | ||
53 | ) { dialog -> | ||
54 | joinNetwork( | ||
55 | dialog.net_name.text.toString(), | ||
56 | dialog.invitation_url.text.toString(), | ||
57 | dialog.join_passphrase.text.toString() | ||
58 | ) | ||
59 | } | ||
60 | } | ||
61 | |||
62 | private fun makeJoinDialog() = | ||
63 | parentActivity.inflate { inflater, parent, attachToRoot -> | ||
64 | ConfigureToolsDialogNetworkJoinBinding.inflate(inflater, parent, attachToRoot) | ||
65 | .apply { scanAction = this@JoinNetworkToolDialogFragment::scanCode } | ||
66 | .root | ||
67 | } | ||
68 | |||
69 | private fun scanCode() { | ||
70 | scanner.initiateScan() | ||
71 | } | ||
72 | |||
73 | private fun joinNetwork(netName: String, url: String, passphrase: String? = null) = | ||
74 | execAction( | ||
75 | R.string.configure_tools_join_network_joining, | ||
76 | validateNetName(netName) | ||
77 | .thenCompose { Tinc.join(netName, url) } | ||
78 | .thenCompose { TincApp.removeScripts(netName) } | ||
79 | .thenCompose { TincApp.generateIfaceCfg(netName) } | ||
80 | .thenCompose { TincApp.setPassphrase(netName, newPassphrase = passphrase) } | ||
81 | ) | ||
82 | } | ||