Skip to content

Commit

Permalink
moved notification creation to Message class, minimize to tray on clo…
Browse files Browse the repository at this point in the history
…se if enabled
  • Loading branch information
naxuroqa committed Jul 7, 2014
1 parent ce78d4f commit 741e4ba
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 45 deletions.
47 changes: 44 additions & 3 deletions src/core/Message.vala
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,29 @@ namespace Venom {
public abstract MessageDirection message_direction {get; protected set;}
public abstract bool important {get; set;}
public abstract bool is_action {get; set;}

/*
* Get plain sender string
*/
public abstract string get_sender_plain();

/*
* Get plain message string
*/
public abstract string get_message_plain();

/*
* Get plain time string
*/
public virtual string get_time_plain() {
return timestamp.format(_("%R:%S"));
}

/*
* Create a notification from message content
*/
public abstract Notify.Notification create_notification();

/*
* Compare this senders of two messages
*/
Expand Down Expand Up @@ -85,6 +90,15 @@ namespace Venom {
public virtual string get_message_plain() {
return message;
}
public virtual Notify.Notification create_notification() {
Notify.Notification notification = new Notify.Notification(
get_sender_plain() + _(" says:"),
get_message_plain(),
null
);
notification.set_image_from_pixbuf(from.image ?? ResourceFactory.instance.default_contact);
return notification;
}
public bool compare_sender(IMessage to) {
if(to is Message) {
return (from == (to as Message).from);
Expand Down Expand Up @@ -115,6 +129,15 @@ namespace Venom {
public override string get_message_plain() {
return "%s %s".printf(message_direction == MessageDirection.INCOMING ? from.name : User.instance.name, message);
}
public override Notify.Notification create_notification() {
Notify.Notification notification = new Notify.Notification(
from.get_name_string() + ":",
get_message_plain(),
null
);
notification.set_image_from_pixbuf(from.image ?? ResourceFactory.instance.default_contact);
return notification;
}
}
public class GroupMessage : IMessage, GLib.Object {
public unowned GroupChat from {get; protected set;}
Expand Down Expand Up @@ -155,6 +178,15 @@ namespace Venom {
public virtual string get_message_plain() {
return message;
}
public virtual Notify.Notification create_notification() {
Notify.Notification notification = new Notify.Notification(
from_contact.get_name_string() + _(" in ") + from.get_name_string() + _(" says:"),
get_message_plain(),
null
);
notification.set_image_from_pixbuf(from.image ?? ResourceFactory.instance.default_groupchat);
return notification;
}
public bool compare_sender(IMessage to) {
if(to is GroupMessage) {
GroupMessage gm = to as GroupMessage;
Expand Down Expand Up @@ -194,5 +226,14 @@ namespace Venom {
}
return "%s %s".printf(message_direction == MessageDirection.INCOMING ? from_contact.name : User.instance.name, message);
}
public override Notify.Notification create_notification() {
Notify.Notification notification = new Notify.Notification(
from_contact.get_name_string() + _(" in ") + from.get_name_string() + ":",
get_message_plain(),
null
);
notification.set_image_from_pixbuf(from.image ?? ResourceFactory.instance.default_groupchat);
return notification;
}
}
}
2 changes: 1 addition & 1 deletion src/core/Settings.vala
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ namespace Venom {
public int window_width { get; set; default = 600; }
public int window_height { get; set; default = 600; }
public string default_host { get; set; default = "toxme.se";}
public bool enable_tray { get; set; default = false;}
public bool enable_tray { get; set; default = false;}

private static Settings? _instance;
public static Settings instance {
Expand Down
61 changes: 20 additions & 41 deletions src/ui/Client.vala
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,24 @@ namespace Venom {

set_app_menu(menu);

contact_list_window.delete_event.connect((e) => {
if(Settings.instance.enable_tray) {
contact_list_window.hide();
return true;
}
return false;
});

//tray_icon = new Gtk.StatusIcon.from_pixbuf(get_contact_list_window().get_icon());
tray_icon = new Gtk.StatusIcon.from_icon_name("venom");
tray_icon.set_tooltip_text ("Venom");
Settings.instance.bind_property(Settings.TRAY_KEY, tray_icon, "visible", BindingFlags.SYNC_CREATE);
tray_icon.activate.connect(()=>{
if(contact_list_window == null){
this.activate();
var w = get_contact_list_window();
if(w.visible) {
w.hide();
} else {
get_contact_list_window().show();
w.show();
}
});
}
Expand All @@ -94,61 +103,31 @@ namespace Venom {
base.startup();
}

public void show_notification(string summary, string? body, Gdk.Pixbuf image) {
private void show_notification_for_message(IMessage m) {
if(get_contact_list_window().is_active) {
return;
}
try {
Notify.Notification notification = new Notify.Notification(summary, body, null);
notification.set_image_from_pixbuf(image);
Notify.Notification notification = m.create_notification();
notification.show();
} catch (Error e) {
Logger.log(LogLevel.ERROR, _("Error showing notification: ") + e.message);
}
}

protected override void activate() {
hold();
hold();

Notify.init ("Venom");
var window = get_contact_list_window();
window.incoming_message.connect((m)=>{
show_notification(
m.get_sender_plain() + _(" says:"),
m.get_message_plain(),
m.from.image ?? ResourceFactory.instance.default_contact
);
});

window.incoming_group_message.connect((m)=>{
show_notification(
m.from_contact.get_name_string() + _(" in ") + m.from.get_name_string() + _(" says:"),
m.get_message_plain(),
m.from.image ?? ResourceFactory.instance.default_groupchat
);
});

window.incoming_action.connect((m)=>{
show_notification(
m.from.get_name_string() + _(":"),
m.get_message_plain(),
m.from.image ?? ResourceFactory.instance.default_contact
);
});

window.incoming_group_action.connect((m)=>{
show_notification(
m.from_contact.get_name_string() + _(" in ") + m.from.get_name_string() + _(":"),
m.get_message_plain(),
m.from.image ?? ResourceFactory.instance.default_groupchat
);
});
window.incoming_message.connect(show_notification_for_message);
window.incoming_group_message.connect(show_notification_for_message);
window.incoming_action.connect(show_notification_for_message);
window.incoming_group_action.connect(show_notification_for_message);

window.present();
release();
}




protected override void open(GLib.File[] files, string hint) {
hold();
Expand Down

0 comments on commit 741e4ba

Please sign in to comment.