Skip to content
This repository has been archived by the owner on Feb 20, 2023. It is now read-only.

Commit

Permalink
Merge pull request #4 from freehaha/gyp
Browse files Browse the repository at this point in the history
use node-gyp and libnotify
  • Loading branch information
olalonde committed Feb 15, 2015
2 parents 9757ac2 + 895dfe7 commit 1e2e1ae
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 12 deletions.
18 changes: 13 additions & 5 deletions README.md
Expand Up @@ -5,25 +5,33 @@ This is a follow up to [How to roll out your own Javascript API with V8](http://

## Install ##

Dependencies(Ubuntu 11.04):
Dependencies(Debian, Ubuntu):

sudo apt-get install libnotifymm-dev
sudo apt-get install libnotify-dev

Using npm:

npm install notify

Manually:

node-waf configure && node-waf build
node-gyp configure && node-gyp build

## Usage ##

var notify = require("notify"); // or "../build/default/gtknotify.node" if you build manually
var notify = require("notify"); // or "../build/Release/gtknotify.node" if you build manually
var notification = new notify.notification();
notification.title = "Notification title";
notification.icon = "emblem-default"; // see /usr/share/icons/gnome/16x16
notification.send("Notification message");
notification.send("Notification message"); // using libnotify's default timeout

To send notification with a timeout:

notification.send("Notification message", 3000); // 3 sec timeout
// or
notification.send("Notification message", 0); // lasts forever
notification.close(); // closes it


## References ##

Expand Down
14 changes: 14 additions & 0 deletions binding.gyp
@@ -0,0 +1,14 @@
{
"targets": [
{
"target_name": "gtknotify",
"sources": [ "src/node_gtknotify.cpp" ],
"include_dirs": [
"<!@(pkg-config libnotify --cflags-only-I | sed s/-I//g)"
],
"libraries": [
"<!@(pkg-config libnotify --libs)"
]
}
]
}
4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -13,8 +13,8 @@
, "url" : "https://github.com/olalonde/node-notify.git"
}
, "scripts" : {
"preinstall" : "node-waf configure && node-waf build"
"preinstall" : "node-gyp configure && node-gyp build"
, "preuninstall" : "rm -rf build/*"
}
, "main" : "build/default/gtknotify.node"
, "main" : "build/Release/gtknotify.node"
}
29 changes: 24 additions & 5 deletions src/node_gtknotify.cpp
Expand Up @@ -3,10 +3,10 @@
//
#include <v8.h>
#include <node.h>
#include <node_object_wrap.h>
// We need those two libraries for the GTK+ notification
#include <string>
#include <gtkmm.h>
#include <libnotifymm.h>
#include <libnotify/notify.h>

/*
Expand Down Expand Up @@ -46,6 +46,7 @@ using namespace v8;

class Gtknotify : node::ObjectWrap {
private:
NotifyNotification *n;
public:
Gtknotify() {}
~Gtknotify() {}
Expand Down Expand Up @@ -88,9 +89,11 @@ class Gtknotify : node::ObjectWrap {
// @Node.js macro to help bind C++ methods to Javascript methods (see https://github.com/joyent/node/blob/v0.2.0/src/node.h#L34)
// Arguments: our constructor function, Javascript method name, C++ method
NODE_SET_PROTOTYPE_METHOD(Gtknotify::persistent_function_template, "send", Send);
NODE_SET_PROTOTYPE_METHOD(Gtknotify::persistent_function_template, "close", Close);

// Set the "notification" property to the target and assign it to our constructor function
target->Set(String::NewSymbol("notification"), Gtknotify::persistent_function_template->GetFunction());
notify_init("node-notify");
}

// new Notification();
Expand All @@ -101,6 +104,7 @@ class Gtknotify : node::ObjectWrap {
// Set some default values
gtknotify_instance->title = "Node.js";
gtknotify_instance->icon = "terminal";
gtknotify_instance->n = notify_notification_new(gtknotify_instance->title.c_str(), "", gtknotify_instance->icon.c_str());

// Wrap our C++ object as a Javascript object
gtknotify_instance->Wrap(args.This());
Expand All @@ -115,21 +119,36 @@ class Gtknotify : node::ObjectWrap {
// This is a method part of the constructor function's prototype
static v8::Handle<Value> Send(const Arguments& args) {
v8::HandleScope scope;
gint timeout = NOTIFY_EXPIRES_DEFAULT;
// Extract C++ object reference from "this" aka args.This() argument
Gtknotify* gtknotify_instance = node::ObjectWrap::Unwrap<Gtknotify>(args.This());

// Convert first argument to V8 String
v8::String::Utf8Value v8str(args[0]);
if(args.Length() > 1) {
if(!args[1]->IsNumber()) {
return v8::Undefined();
}
timeout = args[1]->ToUint32()->Value();
}

// see http://library.gnome.org/devel/libnotify/0.7/NotifyNotification.html
Notify::init("Basic");
// Args: title, content, icon
Notify::Notification n(gtknotify_instance->title.c_str(), *v8str, gtknotify_instance->icon.c_str()); // *v8str points to the C string
// Display notification
n.show();
notify_notification_update(gtknotify_instance->n, gtknotify_instance->title.c_str(), *v8str, gtknotify_instance->icon.c_str()); // *v8str points to the C string
notify_notification_set_timeout(gtknotify_instance->n, timeout);
notify_notification_show(gtknotify_instance->n, NULL);
// n.show();
// Return value
return v8::Boolean::New(true);
}

static v8::Handle<Value> Close(const Arguments& args) {
v8::HandleScope scope;
Gtknotify* gtknotify_instance = node::ObjectWrap::Unwrap<Gtknotify>(args.This());
notify_notification_close(gtknotify_instance->n, NULL);
return v8::Undefined();
}

// notification.title
static v8::Handle<Value> GetTitle(v8::Local<v8::String> property, const v8::AccessorInfo& info) {
Expand Down

0 comments on commit 1e2e1ae

Please sign in to comment.