Nodejs extension to implement message pop-up windows

Source: Internet
Author: User

The implementation of reference https://github.com/olalonde/node-notify

C + + code node_gtknotify.cc for modules

#include <v8.h>

#include <node.h>

#include <string>

#include <gtkmm-3.0/gtkmm.h>

#include <libnotifymm.h>

using namespace V8;

Class gtknotify:node::objectwrap{

Public

Gtknotify () {}

~gtknotify () {}

std::string title;

std::string icon;

Static persistent<functiontemplate> persistent_function_template;

static void Init (handle<object> target) {

Handlescope scope;

local<functiontemplate> local_function_template = functiontemplate::new (New);

Gtknotify::p ersistent_function_template = persistent<functiontemplate>::new (local_function_template);

Gtknotify::p ersistent_function_template->instancetemplate ()->setinternalfieldcount (1);

Gtknotify::p ersistent_function_template->setclassname (String::newsymbol ("Notification"));

Gtknotify::p ersistent_function_template->instancetemplate ()->setaccessor (String::new ("title"), GetTitle, Settitle);

Gtknotify::p ersistent_function_template->instancetemplate ()->setaccessor (string::new ("icon"), GetIcon, SetIcon);

Node_set_prototype_method (gtknotify::p ersistent_function_template, "send", send);

Target->set (String::newsymbol ("Notification"), Gtknotify::p ersistent_function_template->getfunction ());

}

Static handle<value> New (const arguments& args) {

Handlescope scope;

gtknotify* instance = new Gtknotify ();

Instance->title = "node. js";

Instance->icon = "Terminal";

Instance->wrap (args. This ());

return args. This ();

}

Static handle<value> Send (const arguments& args) {

Handlescope scope;

gtknotify* instance = node::objectwrap::unwrap<gtknotify> (args. This ());

String::utf8value v8str (args[0]);

Pop-up message box

Notify::init ("Basic");

Notify::notification N (instance->title.c_str (), *v8str, Instance->icon.c_str ());

N.show ();

Return Boolean::new (TRUE);

}

Static handle<value> GetTitle (local<string> property, const accessorinfo& info) {

gtknotify* Instance = node::objectwrap::unwrap<gtknotify> (info. Holder ());

Return String::new (Instance->title.c_str ());

}

Static handle<value> GetIcon (local<string> property, const accessorinfo& info) {

gtknotify* Instance = node::objectwrap::unwrap<gtknotify> (info. Holder ());

Return String::new (Instance->icon.c_str ());

}

static void Settitle (Local<string> property, local<value> Value, const accessorinfo& info) {

gtknotify* Instance = node::objectwrap::unwrap<gtknotify> (info. Holder ());

String::utf8value v8str (value);

Instance->title = *v8str;

}

static void SetIcon (Local<string> property, local<value> Value, const accessorinfo& info) {

gtknotify* Instance = node::objectwrap::unwrap<gtknotify> (info. Holder ());

String::utf8value v8str (value);

Instance->icon = *v8str;

}

};

Persistent<functiontemplate> gtknotify::p ersistent_function_template;

extern "C" {

static void Init (handle<object> target) {

Gtknotify::init (target);

}

Node_module (node_gtknotify, init);

}

Node-gyp configuration file Binding.gyp

{

"Targets": [

{

"Target_name": "node_gtknotify",

"Sources": ["src/node_gtknotify.cc"]

}

]

}

Directory structure

Run command

Node-gyp Configure

NODE-GYP Build

If not installed the corresponding library/path cannot be found, in the middle of the header file can not find errors;

Clumsy workaround, add in the makefile below the build

Cxxflags + = -i/usr/include/glibmm-2.4-i/usr/lib/x86_64-linux-gnu/glibmm-2.4/include-i/usr/include/ glib-2.0-i/usr/lib/x86_64-linux-gnu/glib-2.0/include-i/usr/include/sigc++-2.0-i/usr/lib/x86_64-linux-gnu/sigc+ +-2.0/include-i/usr/include/giomm-2.4-i/usr/include/gdkmm-3.0-i/usr/lib/x86_64-linux-gnu/gdkmm-3.0/include-i/ usr/include/pangomm-1.4-i/usr/lib/x86_64-linux-gnu/pangomm-1.4/include-i/usr/include/gtk-3.0-i/usr/include/ Pango-1.0-i/usr/include/cairo-i/usr/include/gdk-pixbuf-2.0-i/usr/include/cairomm-1.0-i/usr/include/freetype2-i /usr/include/gtkmm-3.0-i/usr/lib/x86_64-linux-gnu/gtkmm-3.0/include-i/usr/include/atkmm-1.6-i/usr/include/ atk-1.0-i/usr/include/libnotifymm-1.0

The above libraries are installed according to the error prompts

JavaScript test Code

var notify = require ("./build/release/node_gtknotify");

var notification = new Notify.notification ();

Notification.title = "notification title";

Notification.icon = "Emblem-default"; see/usr/share/icons/gnome/16x16

Notification.send ("Hello,world");

Note that the runtime may have an error message: * * Symbol not found because the corresponding shared link library was not added

Workaround: Add in Build/node_gtknotify.target.mk

LIBS: =-lglibmm-2.4-lnotify-lnotifymm-1.0

Run effect

Nodejs extension to implement message pop-up windows

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.