func (snoty Notification) Show() {

	var message *C.char = C.CString(snoty.Message)
	var title *C.char = C.CString(snoty.Title)
	var appname *C.char = C.CString(snoty.Appname)
	var n *C.NotifyNotification

	if C.strlen(appname) > 0 {
		if C.notify_is_initted() == C.FALSE {
			C.notify_init(appname)
		}
	} else {
		if C.notify_is_initted() == C.FALSE {
			C.notify_init(C.CString("Simple.Notification"))

		}
	}

	if snoty.Icon_path != "" {
		var tmpPath *C.char = C.CString(snoty.Icon_path)
		var buf *C.GdkPixbuf = C.gdk_pixbuf_new_from_file(tmpPath, nil)
		C.notify_notification_set_image_from_pixbuf(n, buf)
		//C.free(unsafe.Pointer(tmpPath))
	}

	n = C.notify_notification_new(title, message, nil)
	C.notify_notification_show(n, nil)
	C.my_own_unref(n)

	C.notify_uninit()
	C.free(unsafe.Pointer(appname))
	C.free(unsafe.Pointer(message))
	C.free(unsafe.Pointer(title))

}
Ejemplo n.º 2
0
func IsInitted() bool {
	return C.notify_is_initted() != 0
}
Ejemplo n.º 3
0
// processNotification sends the notification to libnotify.
func processNotification(note *Notification, cache *FileCache) {
	if inited := bool(C.notify_is_initted() != 0); !inited {
		// We might be able to initialize libnotify here, if doing so is thread
		// safe and can be called multiple times.
		log.Println("gntp: libnotify is not initted")
		return
	}

	notify_title := C.CString(note.Title)
	defer C.free(unsafe.Pointer(notify_title))

	notify_text := C.CString(note.Text)
	defer C.free(unsafe.Pointer(notify_text))

	notify_icon := C.CString("")
	icon := note.Icon
	var iconFileName string
	if strings.HasPrefix(strings.ToLower(icon), "x-growl-resource://") {
		icon = icon[19:]
		iconFileName = cache.GetFileName(icon)
	} else if icon != "" {
		hash := md5.New()
		io.WriteString(hash, icon)
		sum := fmt.Sprintf("%x", hash.Sum(nil))
		iconFileName = cache.GetFileName(sum)
	}
	if _, err := os.Stat(iconFileName); err == nil {
		notify_icon = C.CString(iconFileName)
	}
	defer C.free(unsafe.Pointer(notify_icon))

	notify_notification := C.notify_notification_new(notify_title, notify_text, notify_icon)
	// TODO: Find the correct way to free notify_notification.
	//defer C.free(unsafe.Pointer(&notify_notification))

	notify_app_name := C.CString(note.App.Name)
	C.notify_notification_set_app_name(notify_notification, notify_app_name)
	defer C.free(unsafe.Pointer(notify_app_name))

	var urgency NotifyUrgency
	switch note.Priority {
	case -2, -1:
		urgency = NOTIFY_URGENCY_LOW
	case 0:
		urgency = NOTIFY_URGENCY_NORMAL
	case 1, 2:
		urgency = NOTIFY_URGENCY_CRITICAL
	default:
		log.Printf("gntp: unknown priority %v for notification %v from app %v\n", note.Priority, note.Name, note.App.Name)
		urgency = NOTIFY_URGENCY_NORMAL
	}
	notify_urgency := C.NotifyUrgency(urgency)
	C.notify_notification_set_urgency(notify_notification, notify_urgency)

	timeout := NOTIFY_EXPIRES_DEFAULT
	if note.Sticky {
		timeout = NOTIFY_EXPIRES_NEVER
	}
	notify_timeout := C.gint(timeout)
	C.notify_notification_set_timeout(notify_notification, notify_timeout)

	// Actually show the notification and report any error.
	var err *C.GError
	if shown := bool(C.notify_notification_show(notify_notification, &err) != 0); shown {
		log.Printf("Notification %s shown\n", note.Id)
	} else {
		log.Printf("Notification %s not shown\n", note.Id)
		if err != nil {
			message := C.GoString((*C.char)(err.message))
			log.Printf("  %s\n", message)
		}
	}
}