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
31 #include "wx/notifmsg.h"
33 #include <libnotify/notify.h>
35 #include "wx/module.h"
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"
44 // ----------------------------------------------------------------------------
45 // A module for cleaning up libnotify on exit.
46 // ----------------------------------------------------------------------------
48 class wxLibnotifyModule
: public wxModule
53 // We're initialized on demand.
59 if ( notify_is_initted() )
63 // Do initialize the library.
64 static bool Initialize()
66 if ( !notify_is_initted() )
68 if ( !notify_init(wxTheApp
->GetAppName().utf8_str()) )
76 wxDECLARE_DYNAMIC_CLASS(wxLibnotifyModule
);
79 wxIMPLEMENT_DYNAMIC_CLASS(wxLibnotifyModule
, wxModule
);
81 // ============================================================================
82 // wxNotificationMessage implementation
83 // ============================================================================
85 bool wxNotificationMessage::GTKSetIconName(const wxString
& name
)
92 bool wxNotificationMessage::Show(int timeout
)
94 if ( !wxLibnotifyModule::Initialize() )
97 // Determine the GTK+ icon to use from flags and also set the urgency
100 NotifyUrgency urgency
;
101 switch ( GetFlags() )
103 case wxICON_INFORMATION
:
104 icon
= "dialog-information";
105 urgency
= NOTIFY_URGENCY_LOW
;
109 icon
= "dialog-warning";
110 urgency
= NOTIFY_URGENCY_NORMAL
;
114 icon
= "dialog-error";
115 urgency
= NOTIFY_URGENCY_CRITICAL
;
119 wxFAIL_MSG( "Unknown notification message flags." );
123 // Explicitly specified icon name overrides the implicit one determined by
125 wxScopedCharBuffer buf
;
126 if ( !m_iconName
.empty() )
128 buf
= m_iconName
.utf8_str();
132 // Create the notification or update an existing one if we had already been
134 if ( !m_notification
)
136 m_notification
= notify_notification_new
138 GetTitle().utf8_str(),
139 GetMessage().utf8_str(),
141 #if !wxUSE_LIBNOTIFY_0_7
142 // There used to be an "associated window"
143 // parameter in this function but it has
144 // disappeared by 0.7, so use it for previous
147 #endif // libnotify < 0.7
149 if ( !m_notification
)
151 wxLogDebug("Failed to creation notification.");
158 if ( !notify_notification_update
161 GetTitle().utf8_str(),
162 GetMessage().utf8_str(),
166 wxLogDebug(wxS("notify_notification_update() unexpectedly failed."));
171 // Set the notification parameters not specified during creation.
172 notify_notification_set_timeout
175 timeout
== Timeout_Auto
? NOTIFY_EXPIRES_DEFAULT
176 : timeout
== Timeout_Never
? NOTIFY_EXPIRES_NEVER
180 notify_notification_set_urgency(m_notification
, urgency
);
183 // Finally do show the notification.
185 if ( !notify_notification_show(m_notification
, error
.Out()) )
187 wxLogDebug("Failed to shown notification: %s", error
.GetMessage());
195 bool wxNotificationMessage::Close()
197 wxCHECK_MSG( m_notification
, false,
198 wxS("Can't close not shown notification.") );
201 if ( !notify_notification_close(m_notification
, error
.Out()) )
203 wxLogDebug("Failed to hide notification: %s", error
.GetMessage());
211 wxNotificationMessage::~wxNotificationMessage()
213 if ( m_notification
)
214 g_object_unref(m_notification
);
217 #endif // wxUSE_NOTIFICATION_MESSAGE && wxUSE_LIBNOTIFY