]>
Commit | Line | Data |
---|---|---|
afbf46a3 VZ |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: src/gtk/notifmsg.cpp | |
3 | // Purpose: wxNotificationMessage for wxGTK using libnotify. | |
4 | // Author: Vadim Zeitlin | |
5 | // Created: 2012-07-25 | |
6 | // RCS-ID: $Id$ | |
7 | // Copyright: (c) 2012 Vadim Zeitlin <vadim@wxwidgets.org> | |
8 | // Licence: wxWindows licence | |
9 | /////////////////////////////////////////////////////////////////////////////// | |
10 | ||
11 | // ============================================================================ | |
12 | // declarations | |
13 | // ============================================================================ | |
14 | ||
15 | // ---------------------------------------------------------------------------- | |
16 | // headers | |
17 | // ---------------------------------------------------------------------------- | |
18 | ||
19 | // for compilers that support precompilation, includes "wx.h". | |
20 | #include "wx/wxprec.h" | |
21 | ||
22 | #ifdef __BORLANDC__ | |
23 | #pragma hdrstop | |
24 | #endif | |
25 | ||
c61797b6 | 26 | #if wxUSE_NOTIFICATION_MESSAGE && wxUSE_LIBNOTIFY |
afbf46a3 VZ |
27 | |
28 | #ifndef WX_PRECOMP | |
29 | #endif // WX_PRECOMP | |
30 | ||
31 | #include "wx/notifmsg.h" | |
32 | ||
33 | #include <libnotify/notify.h> | |
34 | ||
35 | #include "wx/module.h" | |
36 | ||
37 | // General note about error handling: as notifications are meant to be | |
38 | // non-intrusive, we use wxLogDebug() and not wxLogError() if anything goes | |
39 | // wrong here to avoid spamming the user with message boxes. As all methods | |
40 | // return boolean indicating success or failure, the caller could show the | |
41 | // notification in some other way or notify about the error itself if needed. | |
42 | #include "wx/gtk/private/error.h" | |
43 | ||
44 | // ---------------------------------------------------------------------------- | |
45 | // A module for cleaning up libnotify on exit. | |
46 | // ---------------------------------------------------------------------------- | |
47 | ||
48 | class wxLibnotifyModule : public wxModule | |
49 | { | |
50 | public: | |
51 | virtual bool OnInit() | |
52 | { | |
53 | // We're initialized on demand. | |
54 | return true; | |
55 | } | |
56 | ||
57 | virtual void OnExit() | |
58 | { | |
59 | if ( notify_is_initted() ) | |
60 | notify_uninit(); | |
61 | } | |
62 | ||
63 | // Do initialize the library. | |
64 | static bool Initialize() | |
65 | { | |
66 | if ( !notify_is_initted() ) | |
67 | { | |
68 | if ( !notify_init(wxTheApp->GetAppName().utf8_str()) ) | |
69 | return false; | |
70 | } | |
71 | ||
72 | return true; | |
73 | } | |
74 | ||
75 | private: | |
76 | wxDECLARE_DYNAMIC_CLASS(wxLibnotifyModule); | |
77 | }; | |
78 | ||
79 | wxIMPLEMENT_DYNAMIC_CLASS(wxLibnotifyModule, wxModule); | |
80 | ||
81 | // ============================================================================ | |
82 | // wxNotificationMessage implementation | |
83 | // ============================================================================ | |
84 | ||
85 | bool wxNotificationMessage::Show(int timeout) | |
86 | { | |
87 | if ( !wxLibnotifyModule::Initialize() ) | |
88 | return false; | |
89 | ||
90 | // Determine the GTK+ icon to use from flags and also set the urgency | |
91 | // appropriately. | |
92 | const char* icon; | |
93 | NotifyUrgency urgency; | |
94 | switch ( GetFlags() ) | |
95 | { | |
96 | case wxICON_INFORMATION: | |
97 | icon = "dialog-information"; | |
98 | urgency = NOTIFY_URGENCY_LOW; | |
99 | break; | |
100 | ||
101 | case wxICON_WARNING: | |
102 | icon = "dialog-warning"; | |
103 | urgency = NOTIFY_URGENCY_NORMAL; | |
104 | break; | |
105 | ||
106 | case wxICON_ERROR: | |
107 | icon = "dialog-error"; | |
108 | urgency = NOTIFY_URGENCY_CRITICAL; | |
109 | break; | |
110 | ||
111 | default: | |
112 | wxFAIL_MSG( "Unknown notification message flags." ); | |
113 | return false; | |
114 | } | |
115 | ||
116 | // Create the notification or update an existing one if we had already been | |
117 | // shown before. | |
118 | if ( !m_notification ) | |
119 | { | |
120 | m_notification = notify_notification_new | |
121 | ( | |
122 | GetTitle().utf8_str(), | |
123 | GetMessage().utf8_str(), | |
124 | icon | |
125 | ); | |
126 | if ( !m_notification ) | |
127 | { | |
128 | wxLogDebug("Failed to creation notification."); | |
129 | ||
130 | return false; | |
131 | } | |
132 | } | |
133 | else | |
134 | { | |
135 | if ( !notify_notification_update | |
136 | ( | |
137 | m_notification, | |
138 | GetTitle().utf8_str(), | |
139 | GetMessage().utf8_str(), | |
140 | icon | |
141 | ) ) | |
142 | { | |
143 | wxLogDebug(wxS("notify_notification_update() unexpectedly failed.")); | |
144 | } | |
145 | } | |
146 | ||
147 | ||
148 | // Set the notification parameters not specified during creation. | |
149 | notify_notification_set_timeout | |
150 | ( | |
151 | m_notification, | |
152 | timeout == Timeout_Auto ? NOTIFY_EXPIRES_DEFAULT | |
153 | : timeout == Timeout_Never ? NOTIFY_EXPIRES_NEVER | |
154 | : 1000*timeout | |
155 | ); | |
156 | ||
157 | notify_notification_set_urgency(m_notification, urgency); | |
158 | ||
159 | ||
160 | // Finally do show the notification. | |
161 | wxGtkError error; | |
162 | if ( !notify_notification_show(m_notification, error.Out()) ) | |
163 | { | |
164 | wxLogDebug("Failed to shown notification: %s", error.GetMessage()); | |
165 | ||
166 | return false; | |
167 | } | |
168 | ||
169 | return true; | |
170 | } | |
171 | ||
172 | bool wxNotificationMessage::Close() | |
173 | { | |
174 | wxCHECK_MSG( m_notification, false, | |
175 | wxS("Can't close not shown notification.") ); | |
176 | ||
177 | wxGtkError error; | |
178 | if ( !notify_notification_close(m_notification, error.Out()) ) | |
179 | { | |
180 | wxLogDebug("Failed to hide notification: %s", error.GetMessage()); | |
181 | ||
182 | return false; | |
183 | } | |
184 | ||
185 | return true; | |
186 | } | |
187 | ||
188 | wxNotificationMessage::~wxNotificationMessage() | |
189 | { | |
190 | if ( m_notification ) | |
191 | g_object_unref(m_notification); | |
192 | } | |
193 | ||
c61797b6 | 194 | #endif // wxUSE_NOTIFICATION_MESSAGE && wxUSE_LIBNOTIFY |