summaryrefslogtreecommitdiff
path: root/pkg/systray/notificator.go
blob: f3aa9a9549a11444632f718399ced423291a574d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Copyright (C) 2018 LEAP
//
// This program is free software: you can redistribute it and/or modify
// 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.
//
// This program is distributed in the hope that it will be useful,
// 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 this program.  If not, see <http://www.gnu.org/licenses/>.

package systray

import (
	"io/ioutil"
	"os"
	"path"
	"runtime"
	"time"

	"0xacab.org/leap/bitmask-vpn/pkg/config"
	"0xacab.org/leap/go-dialog"
	"github.com/skratchdot/open-golang/open"
)

const (
	donationText = `The %s service is expensive to run. Because we don't want to store personal information about you, there are no accounts or billing for this service. But if you want the service to continue, donate at least $5 each month.
	
Do you want to donate now?`
	aboutText = `%[1]s is an easy, fast, and secure VPN service from %[2]s. %[1]s does not require a user account, keep logs, or track you in any way.
	    
This service is paid for entirely by donations from users like you. Please donate at %[3]s.
		
By using this application, you agree to the Terms of Service available at %[4]s. This service is provide as-is, without any warranty, and is intended for people who work to make the world a better place.


%[1]v version: %[5]s`
	missingAuthAgent = `Could not find a polkit authentication agent. Please run one and try again.`
	errorStartingVPN = `Can't connect to %s: %v`
	svgFileName      = "riseupvpn.svg"
)

type notificator struct {
	conf *Config
}

func newNotificator(conf *Config) *notificator {
	n := notificator{conf}
	go n.donations()
	return &n
}

func (n *notificator) donations() {
	for {
		time.Sleep(time.Hour)
		if n.conf.needsNotification() {
			letsDonate := dialog.Message(n.conf.Printer.Sprintf(donationText, config.ApplicationName)).
				Title(n.conf.Printer.Sprintf("Donate")).
				Icon(getIconPath()).
				YesNo()
			n.conf.setNotification()
			if letsDonate {
				open.Run("https://riseup.net/vpn/donate")
				n.conf.setDonated()
			}
		}
	}
}

func (n *notificator) about(version string) {
	if version == "" && os.Getenv("SNAP") != "" {
		_version, err := ioutil.ReadFile(os.Getenv("SNAP") + "/snap/version.txt")
		if err == nil {
			version = string(_version)
		}
	}
	dialog.Message(n.conf.Printer.Sprintf(aboutText, config.ApplicationName, config.Provider, config.DonateURL, config.TosURL, version)).
		Title(n.conf.Printer.Sprintf("About")).
		Icon(getIconPath()).
		Info()
}

func (n *notificator) initFailure(err error) {
	dialog.Message(err.Error()).
		Title(n.conf.Printer.Sprintf("Initialization error")).
		Icon(getIconPath()).
		Error()
}

func (n *notificator) authAgent() {
	dialog.Message(n.conf.Printer.Sprintf(missingAuthAgent)).
		Title(n.conf.Printer.Sprintf("Missing authentication agent")).
		Icon(getIconPath()).
		Error()
}

func (n *notificator) errorStartingVPN(err error) {
	dialog.Message(n.conf.Printer.Sprintf(errorStartingVPN, config.ApplicationName, err)).
		Title(n.conf.Printer.Sprintf("Error starting VPN")).
		Icon(getIconPath()).
		Error()
}

func getIconPath() string {
	gopath := os.Getenv("GOPATH")
	if gopath == "" {
		gopath = path.Join(os.Getenv("HOME"), "go")
	}

	if runtime.GOOS == "windows" {
		icoPath := `C:\Program Files\` + config.ApplicationName + `\riseupvpn.ico`
		if fileExist(icoPath) {
			return icoPath
		}
		icoPath = path.Join(gopath, "src", "0xacab.org", "leap", "riseup_vpn", "assets", "riseupvpn.ico")
		if fileExist(icoPath) {
			return icoPath
		}
		return ""
	}

	if runtime.GOOS == "darwin" {
		icnsPath := "/Applications/" + config.ApplicationName + ".app/Contents/Resources/app.icns"
		if fileExist(icnsPath) {
			return icnsPath
		}
		icnsPath = path.Join(gopath, "src", "0xacab.org", "leap", "riseup_vpn", "assets", "riseupvpn.icns")
		if fileExist(icnsPath) {
			return icnsPath
		}
		return ""
	}

	snapPath := os.Getenv("SNAP")
	if snapPath != "" {
		return snapPath + "/snap/gui/riseupvpn.svg"
	}

	wd, _ := os.Getwd()
	svgPath := path.Join(wd, svgFileName)
	if fileExist(svgPath) {
		return svgPath
	}

	svgPath = "/usr/share/" + config.BinaryName + "/riseupvpn.svg"
	if fileExist(svgPath) {
		return svgPath
	}

	svgPath = path.Join(gopath, "src", "0xacab.org", "leap", "bitmask-systray", svgFileName)
	if fileExist(svgPath) {
		return svgPath
	}

	return ""
}

func fileExist(filePath string) bool {
	_, err := os.Stat(filePath)
	return err == nil
}