aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/org/pacien/tincapp/activities/configure/tools/JoinNetworkTool.kt
diff options
context:
space:
mode:
Diffstat (limited to 'app/src/main/java/org/pacien/tincapp/activities/configure/tools/JoinNetworkTool.kt')
-rw-r--r--app/src/main/java/org/pacien/tincapp/activities/configure/tools/JoinNetworkTool.kt83
1 files changed, 83 insertions, 0 deletions
diff --git a/app/src/main/java/org/pacien/tincapp/activities/configure/tools/JoinNetworkTool.kt b/app/src/main/java/org/pacien/tincapp/activities/configure/tools/JoinNetworkTool.kt
new file mode 100644
index 0000000..82a4380
--- /dev/null
+++ b/app/src/main/java/org/pacien/tincapp/activities/configure/tools/JoinNetworkTool.kt
@@ -0,0 +1,83 @@
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
19package org.pacien.tincapp.activities.configure.tools
20
21import android.app.Fragment
22import android.content.Intent
23import android.view.View
24import com.google.zxing.integration.android.IntentIntegrator
25import com.google.zxing.integration.android.IntentResult
26import kotlinx.android.synthetic.main.configure_tools_dialog_network_join.view.*
27import org.pacien.tincapp.R
28import org.pacien.tincapp.activities.BaseActivity
29import org.pacien.tincapp.commands.Tinc
30import org.pacien.tincapp.commands.TincApp
31import org.pacien.tincapp.databinding.ConfigureToolsDialogNetworkJoinBinding
32
33/**
34 * @author pacien
35 */
36class JoinNetworkTool(parentFragment: Fragment, private val parentActivity: BaseActivity) : ConfigurationTool(parentActivity) {
37 private val scanner by lazy { IntentIntegrator(parentFragment) }
38 private var joinDialog: View? = null
39
40 fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
41 IntentIntegrator.parseActivityResult(requestCode, resultCode, data)
42 ?.let(IntentResult::getContents)
43 ?.let(String::trim)
44 ?.let { joinDialog?.invitation_url?.setText(it) }
45 }
46
47 fun openJoinNetworkDialog() =
48 makeJoinDialog().let { newDialog ->
49 joinDialog = newDialog
50 showDialog(
51 newDialog,
52 R.string.configure_tools_join_network_title,
53 R.string.configure_tools_join_network_action
54 ) { dialog ->
55 joinNetwork(
56 dialog.net_name.text.toString(),
57 dialog.invitation_url.text.toString(),
58 dialog.join_passphrase.text.toString()
59 )
60 }
61 }
62
63 private fun makeJoinDialog() =
64 parentActivity.inflate { inflater, parent, attachToRoot ->
65 ConfigureToolsDialogNetworkJoinBinding.inflate(inflater, parent, attachToRoot)
66 .apply { scanAction = this@JoinNetworkTool::scanCode }
67 .root
68 }
69
70 private fun scanCode() {
71 scanner.initiateScan()
72 }
73
74 private fun joinNetwork(netName: String, url: String, passphrase: String? = null) =
75 execAction(
76 R.string.configure_tools_join_network_joining,
77 validateNetName(netName)
78 .thenCompose { Tinc.join(netName, url) }
79 .thenCompose { TincApp.removeScripts(netName) }
80 .thenCompose { TincApp.generateIfaceCfg(netName) }
81 .thenCompose { TincApp.setPassphrase(netName, newPassphrase = passphrase) }
82 )
83}