| 1 |
/* |
| 2 |
Copyright 2014 Canonical Ltd. |
| 3 |
|
| 4 |
This program is free software: you can redistribute it and/or modify it |
| 5 |
under the terms of the GNU General Public License version 3, as published |
| 6 |
by the Free Software Foundation. |
| 7 |
|
| 8 |
This program is distributed in the hope that it will be useful, but |
| 9 |
WITHOUT ANY WARRANTY; without even the implied warranties of |
| 10 |
MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR |
| 11 |
PURPOSE. See the GNU General Public License for more details. |
| 12 |
|
| 13 |
You should have received a copy of the GNU General Public License along |
| 14 |
with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
*/ |
| 16 |
|
| 17 |
// package cmessaging wraps libmessaging-menu |
| 18 |
package curldispatcher |
| 19 |
|
| 20 |
/* |
| 21 |
#cgo pkg-config: lomiri-url-dispatcher |
| 22 |
|
| 23 |
#include <lomiri-url-dispatcher.h> |
| 24 |
#include <glib.h> |
| 25 |
|
| 26 |
void dispatch_url(const gchar* url, gpointer user_data); |
| 27 |
|
| 28 |
gchar** test_url(const gchar** urls); |
| 29 |
*/ |
| 30 |
import "C" |
| 31 |
import "unsafe" |
| 32 |
import "fmt" |
| 33 |
|
| 34 |
func gchar(s string) *C.gchar { |
| 35 |
return (*C.gchar)(C.CString(s)) |
| 36 |
} |
| 37 |
|
| 38 |
func gfree(s *C.gchar) { |
| 39 |
C.g_free((C.gpointer)(s)) |
| 40 |
} |
| 41 |
|
| 42 |
func getCharPtr(p uintptr) *C.char { |
| 43 |
return *((**C.char)(unsafe.Pointer(p))) |
| 44 |
} |
| 45 |
|
| 46 |
func TestURL(urls []string) []string { |
| 47 |
c_urls := make([]*C.gchar, len(urls)+1) |
| 48 |
for i, url := range urls { |
| 49 |
c_urls[i] = gchar(url) |
| 50 |
defer gfree(c_urls[i]) |
| 51 |
} |
| 52 |
results := C.test_url((**C.gchar)(unsafe.Pointer(&c_urls[0]))) |
| 53 |
// if there result is nil, just return empty []string |
| 54 |
if results == nil { |
| 55 |
return nil |
| 56 |
} |
| 57 |
packages := make([]string, len(urls)) |
| 58 |
ptrSz := unsafe.Sizeof(unsafe.Pointer(nil)) |
| 59 |
i := 0 |
| 60 |
for p := uintptr(unsafe.Pointer(results)); getCharPtr(p) != nil; p += ptrSz { |
| 61 |
pkg := C.GoString(getCharPtr(p)) |
| 62 |
packages[i] = pkg |
| 63 |
i += 1 |
| 64 |
} |
| 65 |
return packages |
| 66 |
} |
| 67 |
|
| 68 |
type DispatchPayload struct { |
| 69 |
doneCh chan bool |
| 70 |
} |
| 71 |
|
| 72 |
func DispatchURL(url string, appPackage string) error { |
| 73 |
c_url := gchar(url) |
| 74 |
defer gfree(c_url) |
| 75 |
c_app_package := gchar(appPackage) |
| 76 |
defer gfree(c_app_package) |
| 77 |
doneCh := make(chan bool) |
| 78 |
payload := DispatchPayload{doneCh: doneCh} |
| 79 |
C.dispatch_url(c_url, (C.gpointer)(&payload)) |
| 80 |
success := <-doneCh |
| 81 |
if !success { |
| 82 |
return fmt.Errorf("failed to DispatchURL: %s for %s", url, appPackage) |
| 83 |
} |
| 84 |
return nil |
| 85 |
} |
| 86 |
|
| 87 |
//export handleDispatchURLResult |
| 88 |
func handleDispatchURLResult(c_action *C.char, c_success C.gboolean, obj unsafe.Pointer) { |
| 89 |
payload := (*DispatchPayload)(obj) |
| 90 |
success := false |
| 91 |
if c_success == C.TRUE { |
| 92 |
success = true |
| 93 |
} |
| 94 |
payload.doneCh <- success |
| 95 |
} |