1# Notifier.tcl --
2#
3#       Notifies the user of certain events when application is in the
4#       background.
5#       This is just a first sketch.
6#
7#  Copyright (c) 2007 Mats Bengtsson
8#
9#   This program is free software: you can redistribute it and/or modify
10#   it under the terms of the GNU General Public License as published by
11#   the Free Software Foundation, either version 3 of the License, or
12#   (at your option) any later version.
13#
14#   This program is distributed in the hope that it will be useful,
15#   but WITHOUT ANY WARRANTY; without even the implied warranty of
16#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17#   GNU General Public License for more details.
18#
19#   You should have received a copy of the GNU General Public License
20#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
21#
22# $Id: Notifier.tcl,v 1.12 2008-06-09 09:50:59 matben Exp $
23
24namespace eval ::Notifier {
25
26    # Use this on windows only.
27    if {![string equal $::this(platform) "windows"]} {
28	return
29    }
30    if {[catch {package require notebox}]} {
31	return
32    }
33    component::define Notifier  \
34      "Provides a small event notifier window."
35
36  }
37
38proc ::Notifier::Init {} {
39    global  this
40
41    component::register Notifier
42
43    # Add event hooks.
44    ::hooks::register prefsInitHook         [namespace current]::InitPrefsHook
45    ::hooks::register newMessageHook        [namespace current]::MessageHook
46    ::hooks::register newChatThreadHook     [namespace current]::ThreadHook
47    ::hooks::register presenceNewHook       [namespace current]::PresenceHook
48    ::hooks::register fileTransferReceiveHook [namespace current]::FileTransferRecvHook
49
50    set im [::Theme::FindIcon elements/close]
51
52    option add *Notebox.closeButtonImage  $im     widgetDefault
53    option add *Notebox.millisecs         10000   widgetDefault
54}
55
56proc ::Notifier::InitPrefsHook {} {
57    global jprefs
58
59    set jprefs(notifier,state) 0
60
61    ::PrefUtils::Add [list  \
62      [list jprefs(notifier,state)  jprefs_notifier_state  $jprefs(notifier,state)]]
63}
64
65proc ::Notifier::MessageHook {xmldata uuid} {
66
67    set body [wrapper::getcdata [wrapper::getfirstchildwithtag $xmldata body]]
68    if {![string length $body]} {
69	return
70    }
71    set from [wrapper::getattribute $xmldata from]
72    set djid [::Roster::GetDisplayName $from]
73    set str "You just received a new message from $djid"
74    after 200 [list ::Notifier::DisplayMsg $str]
75}
76
77proc ::Notifier::ThreadHook {xmldata} {
78
79    if {![::UI::IsAppInFront]} {
80	set body [wrapper::getcdata [wrapper::getfirstchildwithtag $xmldata body]]
81	if {![string length $body]} {
82	    return
83	}
84	set from [wrapper::getattribute $xmldata from]
85	set djid [::Roster::GetDisplayName $from]
86	set str "The user $djid just started a new chat thread"
87	after 200 [list ::Notifier::DisplayMsg $str]
88    }
89}
90
91proc ::Notifier::PresenceHook {jid type args} {
92
93    if {![::UI::IsAppInFront]} {
94
95	set delay [::Jabber::Jlib roster getx $jid "jabber:x:delay"]
96	if {$delay ne ""} {
97	    return
98	}
99	if {[::Jabber::Jlib service isroom $jid]} {
100	    return
101	}
102	set wasavail [::Jabber::Jlib roster wasavailable $jid]
103	set isavail [expr {$type eq "available"}]
104	if {(!$wasavail && $isavail) || ($wasavail && !$isavail)} {
105
106	    array set argsA $args
107	    set show $type
108	    if {[info exists argsA(-show)]} {
109		set show $argsA(-show)
110	    }
111	    set status ""
112	    if {[info exists argsA(-status)]} {
113		set status $argsA(-status)
114	    }
115
116	    # This just translates the show code into a readable text.
117	    set showMsg [::Roster::MapShowToText $show]
118	    set djid [::Roster::GetDisplayName $jid]
119
120	    set msg "$djid $showMsg"
121	    if {$status ne ""} {
122		append msg "\n$status"
123	    }
124	    after 200 [list ::Notifier::DisplayMsg $msg]
125	}
126    }
127}
128
129proc ::Notifier::FileTransferRecvHook {jid name size} {
130
131    if {![::UI::IsAppInFront]} {
132	set str "\n"
133	append str [mc "File"]
134	append str ": $name\n"
135	append str [mc "Size"]
136	append str ": [::Utils::FormatBytes $size]\n"
137	set djid [::Roster::GetDisplayName $jid]
138	set msg [mc "%s wants to send you this file: %s Do you want to receive this file?" $djid $str]
139	after 200 [list ::Notifier::DisplayMsg $str]
140    }
141}
142
143proc ::Notifier::DisplayMsg {str} {
144    global jprefs
145
146    # @@@ ::UI::IsAppInFront is not reliable...
147    if {$jprefs(notifier,state) && ![::UI::IsAppInFront]} {
148	::notebox::addmsg $str
149    }
150}
151
152#-------------------------------------------------------------------------------
153