| 1 |
/* |
| 2 |
Copyright 2013-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 cclick has the internal cgo wrapping libclick for package click. |
| 18 |
package cclick |
| 19 |
|
| 20 |
/* |
| 21 |
#cgo pkg-config: click-0.4 |
| 22 |
#cgo pkg-config: glib-2.0 gobject-2.0 |
| 23 |
|
| 24 |
#include <click-0.4/click.h> |
| 25 |
*/ |
| 26 |
import "C" |
| 27 |
|
| 28 |
import ( |
| 29 |
"fmt" |
| 30 |
"runtime" |
| 31 |
) |
| 32 |
|
| 33 |
type CClickUser struct { |
| 34 |
cref *C.ClickUser |
| 35 |
} |
| 36 |
|
| 37 |
func gchar(s string) *C.gchar { |
| 38 |
return (*C.gchar)(C.CString(s)) |
| 39 |
} |
| 40 |
|
| 41 |
func gfree(s *C.gchar) { |
| 42 |
C.g_free((C.gpointer)(s)) |
| 43 |
} |
| 44 |
|
| 45 |
func (ccu *CClickUser) CInit(holder interface{}) error { |
| 46 |
var gerr *C.GError |
| 47 |
cref := C.click_user_new_for_user(nil, nil, &gerr) |
| 48 |
defer C.g_clear_error(&gerr) |
| 49 |
if gerr != nil { |
| 50 |
return fmt.Errorf("faild to make ClickUser: %s", C.GoString((*C.char)(gerr.message))) |
| 51 |
} |
| 52 |
ccu.cref = cref |
| 53 |
runtime.SetFinalizer(holder, func(interface{}) { |
| 54 |
ccu.cref = nil // blocks gc really for now, 1.3 otherwise panics |
| 55 |
C.g_object_unref((C.gpointer)(cref)) |
| 56 |
}) |
| 57 |
return nil |
| 58 |
} |
| 59 |
|
| 60 |
func (ccu *CClickUser) CGetVersion(pkgName string) string { |
| 61 |
pkgname := gchar(pkgName) |
| 62 |
defer gfree(pkgname) |
| 63 |
var gerr *C.GError |
| 64 |
defer C.g_clear_error(&gerr) |
| 65 |
ver := C.click_user_get_version(ccu.cref, pkgname, &gerr) |
| 66 |
if gerr != nil { |
| 67 |
return "" |
| 68 |
} |
| 69 |
defer gfree(ver) |
| 70 |
return C.GoString((*C.char)(ver)) |
| 71 |
} |