github.com/ethanhsieh/snapd@v0.0.0-20210615102523-3db9b8e4edc5/usersession/userd/ui/kdialog.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2018 Canonical Ltd 5 * 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License version 3 as 8 * published by the Free Software Foundation. 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 <http://www.gnu.org/licenses/>. 17 * 18 */ 19 20 package ui 21 22 import ( 23 "fmt" 24 "html" 25 "os/exec" 26 "time" 27 ) 28 29 // KDialog provides a kdialog based UI interface 30 type KDialog struct{} 31 32 // YesNo asks a yes/no question using kdialog 33 func (*KDialog) YesNo(primary, secondary string, options *DialogOptions) bool { 34 if options == nil { 35 options = &DialogOptions{} 36 } 37 38 txt := fmt.Sprintf(`<p><big><b>%s</b></big></p><p>%s</p>`, html.EscapeString(primary), html.EscapeString(secondary)) 39 if options.Footer != "" { 40 txt += fmt.Sprintf(`<p><small>%s</small></p>`, html.EscapeString(options.Footer)) 41 } 42 cmd := exec.Command("kdialog", "--yesno="+txt) 43 if err := cmd.Start(); err != nil { 44 return false 45 } 46 47 var err error 48 if options.Timeout > 0 { 49 done := make(chan error, 1) 50 go func() { done <- cmd.Wait() }() 51 select { 52 case err = <-done: 53 // normal exit 54 case <-time.After(options.Timeout): 55 // timeout do nothing, the other side will have timed 56 // out as well, no need to send a reply. 57 cmd.Process.Kill() 58 return false 59 } 60 } else { 61 err = cmd.Wait() 62 } 63 if err == nil { 64 return true 65 } 66 67 return false 68 }