Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve native notifications support #4

Merged
merged 7 commits into from
Feb 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions src/Models/Messenger.vala
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright (c) 2020 Manexim (https://github.com/manexim)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
*
* Authored by: Marius Meisenzahl <[email protected]>
*/

public class Models.Messenger : Object {
private string _id;
private string _name;
private string _url;
private uint _unread_notifications = 0;

public string id {
get {
return _id;
}
set {
_id = value;
}
}

public string name {
get {
return _name;
}
set {
_name = value;
}
}

public string url {
get {
return _url;
}
set {
_url = value;
}
}

public uint unread_notifications {
get {
return _unread_notifications;
}
set {
_unread_notifications = value;
}
}
}
62 changes: 62 additions & 0 deletions src/Services/Messengers.vala
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright (c) 2020 Manexim (https://github.com/manexim)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
*
* Authored by: Marius Meisenzahl <[email protected]>
*/

public class Services.Messengers {
private static Messengers instance;
private Models.Messenger[] _data;

public static Messengers get_default () {
if (instance == null) {
instance = new Messengers ();
}

return instance;
}

public Models.Messenger[] data {
get {
return _data;
}
}

private Messengers () {
_data = new Models.Messenger[4];
_data[0] = new Models.Messenger ();
_data[0].id = "com.messenger";
_data[0].name = "Messenger";
_data[0].url = "https://www.messenger.com/";

_data[1] = new Models.Messenger ();
_data[1].id = "com.slack";
_data[1].name = "Slack";
_data[1].url = "https://slack.com/signin/";

_data[2] = new Models.Messenger ();
_data[2].id = "org.telegram.web";
_data[2].name = "Telegram";
_data[2].url = "https://web.telegram.org/";

_data[3] = new Models.Messenger ();
_data[3].id = "com.whatsapp.web";
_data[3].name = "WhatsApp";
_data[3].url = "https://web.whatsapp.com/";
}
}
99 changes: 46 additions & 53 deletions src/Views/MainView.vala
Original file line number Diff line number Diff line change
Expand Up @@ -26,82 +26,75 @@ public class Views.MainView : Gtk.Paned {
var grid = new Gtk.Grid ();
var stack = new Gtk.Stack ();

stack.add_named (new Widgets.WebView ("https://www.messenger.com/", "com.messenger"), "0");
stack.add_named (new Widgets.WebView ("https://slack.com/signin/", "com.slack"), "1");
stack.add_named (new Widgets.WebView ("https://web.telegram.org/", "org.telegram.web"), "2");
stack.add_named (new Widgets.WebView ("https://web.whatsapp.com/", "com.whatsapp.web"), "3");

grid.orientation = Gtk.Orientation.VERTICAL;
var list_box = new Gtk.ListBox ();
list_box.selection_mode = Gtk.SelectionMode.SINGLE;
list_box.activate_on_single_click = true;

{
var menu_item = new Gtk.MenuItem ();

var image = new Gtk.Image.from_gicon (Utilities.load_shared_icon ("com.messenger"), Gtk.IconSize.DND);
var label = new Gtk.Label ("Messenger");

var box = new Gtk.Box (Gtk.Orientation.HORIZONTAL, 0);
box.pack_start (image, false, false, 0);
box.pack_start (label, false, false, 0);

menu_item.add (box);

list_box.insert (menu_item, 0);
}

{
var menu_item = new Gtk.MenuItem ();

var image = new Gtk.Image.from_gicon (Utilities.load_shared_icon ("com.slack"), Gtk.IconSize.DND);
var label = new Gtk.Label ("Slack");

var box = new Gtk.Box (Gtk.Orientation.HORIZONTAL, 0);
box.pack_start (image, false, false, 0);
box.pack_start (label, false, false, 0);

menu_item.add (box);

list_box.insert (menu_item, 1);
}

{
var menu_item = new Gtk.MenuItem ();

var image = new Gtk.Image.from_gicon (Utilities.load_shared_icon ("org.telegram.web"), Gtk.IconSize.DND);
var label = new Gtk.Label ("Telegram");

var box = new Gtk.Box (Gtk.Orientation.HORIZONTAL, 0);
box.pack_start (image, false, false, 0);
box.pack_start (label, false, false, 0);

menu_item.add (box);
var messengers = Services.Messengers.get_default ().data;
for (var i = 0; i < messengers.length; i++) {
var messenger = messengers[i];

list_box.insert (menu_item, 2);
}
var view = new Widgets.MessengerView (messenger);
stack.add_named (view, "%d".printf (i));

{
var menu_item = new Gtk.MenuItem ();

var image = new Gtk.Image.from_gicon (Utilities.load_shared_icon ("com.whatsapp.web"), Gtk.IconSize.DND);
var label = new Gtk.Label ("WhatsApp");
var image = new Gtk.Image.from_gicon (Utilities.load_shared_icon (messenger.id), Gtk.IconSize.DND);
var label = new Gtk.Label (messenger.name);
var unread_notifications = new Gtk.Label (
(messenger.unread_notifications > 0) ? "%u".printf (messenger.unread_notifications) : ""
);

messenger.notify.connect (() => {
debug ("[%s] unread_notifications: %u", messenger.id, messenger.unread_notifications);

if (stack.get_visible_child () != view) {
unread_notifications.label =
(messenger.unread_notifications > 0) ? "%u".printf (messenger.unread_notifications) : "";
} else if (messenger.unread_notifications == 0) {
unread_notifications.label = "";
}

uint unread_notifications_sum = 0;
foreach (var m in messengers) {
unread_notifications_sum += m.unread_notifications;
}

Granite.Services.Application.set_badge.begin (unread_notifications_sum, (obj, res) => {
try {
Granite.Services.Application.set_badge.end (res);
} catch (GLib.Error e) {
critical (e.message);
}
});

Granite.Services.Application.set_badge_visible.begin (unread_notifications_sum != 0U, (obj, res) => {
try {
Granite.Services.Application.set_badge_visible.end (res);
} catch (GLib.Error e) {
critical (e.message);
}
});
});

var box = new Gtk.Box (Gtk.Orientation.HORIZONTAL, 0);
box.pack_start (image, false, false, 0);
box.pack_start (label, false, false, 0);
box.pack_end (unread_notifications, false, false, 0);

menu_item.add (box);

list_box.insert (menu_item, 3);
list_box.insert (menu_item, i);
}

list_box.row_activated.connect ((row) => {
stack.set_visible_child_name ("%d".printf (row.get_index ()));
var view = stack.get_visible_child ();
(view as Widgets.MessengerView).model.unread_notifications = 0;
});

var scroll = new Gtk.ScrolledWindow (null, null);
scroll.set_size_request (150, 150);
scroll.hscrollbar_policy = Gtk.PolicyType.NEVER;
scroll.expand = true;
scroll.add (list_box);
Expand Down
28 changes: 19 additions & 9 deletions src/Widgets/WebView.vala → src/Widgets/MessengerView.vala
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,23 @@
* Authored by: Marius Meisenzahl <[email protected]>
*/

public class Widgets.WebView : WebKit.WebView {
private string id;
public class Widgets.MessengerView : WebKit.WebView {
private Models.Messenger messenger;
private GLib.Icon icon;

public WebView (string url, string id) {
this.id = id;
this.icon = Utilities.load_shared_icon (this.id);
public MessengerView (Models.Messenger messenger) {
this.messenger = messenger;
this.icon = Utilities.load_shared_icon (this.messenger.id);

var settings = this.get_settings();
var settings = this.get_settings ();
settings.enable_plugins = true;
settings.enable_javascript = true;
settings.enable_html5_database = true;
settings.enable_html5_local_storage = true;

web_context.initialize_notification_permissions.connect (() => {
var allowed_origins = new List<WebKit.SecurityOrigin> ();
allowed_origins.append (new WebKit.SecurityOrigin.for_uri (url));
allowed_origins.append (new WebKit.SecurityOrigin.for_uri (this.messenger.url));
var disallowed_origins = new List<WebKit.SecurityOrigin> ();

web_context.init_notification_permissions (allowed_origins, disallowed_origins);
Expand All @@ -45,11 +45,21 @@ public class Widgets.WebView : WebKit.WebView {
var native_notification = new GLib.Notification (notification.title);
native_notification.set_body (notification.body);
native_notification.set_icon (this.icon);
Application.instance.send_notification (this.id, native_notification);
Application.instance.send_notification (this.messenger.id, native_notification);

this.messenger.unread_notifications += 1;

debug ("[%s] got notification".printf (this.messenger.id));

return true;
});

load_uri (url);
load_uri (this.messenger.url);
}

public Models.Messenger model {
get {
return this.messenger;
}
}
}
4 changes: 3 additions & 1 deletion src/meson.build
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
sources = [
'Config/Constants.vala',
'Models/Messenger.vala',
'Services/Messengers.vala',
'Services/Settings.vala',
'Utilities/Icon.vala',
'Views/MainView.vala',
'Widgets/WebView.vala',
'Widgets/MessengerView.vala',
'MainWindow.vala'
]

Expand Down