1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/notifmsg.cpp
3 // Purpose: wxNotificationMessage for wxGTK using libnotify.
4 // Author: Vadim Zeitlin
7 // Copyright: (c) 2012 Vadim Zeitlin <vadim@wxwidgets.org>
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // for compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
26 #if wxUSE_NOTIFICATION_MESSAGE && wxUSE_LIBNOTIFY
28 #include "wx/notifmsg.h"
34 #include <libnotify/notify.h>
36 #include "wx/module.h"
38 // General note about error handling: as notifications are meant to be
39 // non-intrusive, we use wxLogDebug() and not wxLogError() if anything goes
40 // wrong here to avoid spamming the user with message boxes. As all methods
41 // return boolean indicating success or failure, the caller could show the
42 // notification in some other way or notify about the error itself if needed.
43 #include "wx/gtk/private/error.h"
45 // ----------------------------------------------------------------------------
46 // A module for cleaning up libnotify on exit.
47 // ----------------------------------------------------------------------------
49 class wxLibnotifyModule
: public wxModule
54 // We're initialized on demand.
60 if ( notify_is_initted() )
64 // Do initialize the library.
65 static bool Initialize()
67 if ( !notify_is_initted() )
69 if ( !notify_init(wxTheApp
->GetAppName().utf8_str()) )
77 wxDECLARE_DYNAMIC_CLASS(wxLibnotifyModule
);
80 wxIMPLEMENT_DYNAMIC_CLASS(wxLibnotifyModule
, wxModule
);
82 // ============================================================================
83 // wxNotificationMessage implementation
84 // ============================================================================
86 bool wxNotificationMessage::GTKSetIconName(const wxString
& name
)
93 bool wxNotificationMessage::Show(int timeout
)
95 if ( !wxLibnotifyModule::Initialize() )
98 // Determine the GTK+ icon to use from flags and also set the urgency
101 NotifyUrgency urgency
;
102 switch ( GetFlags() )
104 case wxICON_INFORMATION
:
105 icon
= "dialog-information";
106 urgency
= NOTIFY_URGENCY_LOW
;
110 icon
= "dialog-warning";
111 urgency
= NOTIFY_URGENCY_NORMAL
;
115 icon
= "dialog-error";
116 urgency
= NOTIFY_URGENCY_CRITICAL
;
120 wxFAIL_MSG( "Unknown notification message flags." );
124 // Explicitly specified icon name overrides the implicit one determined by
126 wxScopedCharBuffer buf
;
127 if ( !m_iconName
.empty() )
129 buf
= m_iconName
.utf8_str();
133 // Create the notification or update an existing one if we had already been
135 if ( !m_notification
)
137 m_notification
= notify_notification_new
139 GetTitle().utf8_str(),
140 GetMessage().utf8_str(),
142 #if !wxUSE_LIBNOTIFY_0_7
143 // There used to be an "associated window"
144 // parameter in this function but it has
145 // disappeared by 0.7, so use it for previous
148 #endif // libnotify < 0.7
150 if ( !m_notification
)
152 wxLogDebug("Failed to creation notification.");
159 if ( !notify_notification_update
162 GetTitle().utf8_str(),
163 GetMessage().utf8_str(),
167 wxLogDebug(wxS("notify_notification_update() unexpectedly failed."));
172 // Set the notification parameters not specified during creation.
173 notify_notification_set_timeout
176 timeout
== Timeout_Auto
? NOTIFY_EXPIRES_DEFAULT
177 : timeout
== Timeout_Never
? NOTIFY_EXPIRES_NEVER
181 notify_notification_set_urgency(m_notification
, urgency
);
184 // Finally do show the notification.
186 if ( !notify_notification_show(m_notification
, error
.Out()) )
188 wxLogDebug("Failed to shown notification: %s", error
.GetMessage());
196 bool wxNotificationMessage::Close()
198 wxCHECK_MSG( m_notification
, false,
199 wxS("Can't close not shown notification.") );
202 if ( !notify_notification_close(m_notification
, error
.Out()) )
204 wxLogDebug("Failed to hide notification: %s", error
.GetMessage());
212 wxNotificationMessage::~wxNotificationMessage()
214 if ( m_notification
)
215 g_object_unref(m_notification
);
218 #endif // wxUSE_NOTIFICATION_MESSAGE && wxUSE_LIBNOTIFY