2 * Copyright 2010, Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
6 * Pier Luigi Fiorini, pierluigi.fiorini@gmail.com
7 * Stephan Aßmus, superstippi@gmx.de
11 #include <Notification.h>
18 #include <notification/Notifications.h>
24 BNotification::BNotification(notification_type type
)
36 BNotification::BNotification(BMessage
* archive
)
45 if (archive
->FindInt32("_type", &type
) == B_OK
)
46 fType
= (notification_type
)type
;
48 fInitStatus
= B_ERROR
;
51 if (archive
->FindString("_group", &group
) == B_OK
)
55 if (archive
->FindString("_title", &title
) == B_OK
)
59 if (archive
->FindString("_content", &content
) == B_OK
)
63 if (archive
->FindString("_messageID", &messageID
) == B_OK
)
64 SetMessageID(messageID
);
67 if (type
== B_PROGRESS_NOTIFICATION
68 && archive
->FindFloat("_progress", &progress
) == B_OK
)
69 SetProgress(progress
);
72 if (archive
->FindString("_onClickApp", &onClickApp
) == B_OK
)
73 SetOnClickApp(onClickApp
);
75 entry_ref onClickFile
;
76 if (archive
->FindRef("_onClickFile", &onClickFile
) == B_OK
)
77 SetOnClickFile(&onClickFile
);
81 while (archive
->FindRef("_onClickRef", index
++, &onClickRef
) == B_OK
)
82 AddOnClickRef(&onClickRef
);
86 while (archive
->FindString("_onClickArgv", index
++, &onClickArgv
) == B_OK
)
87 AddOnClickArg(onClickArgv
);
91 if ((ret
= archive
->FindMessage("_icon", &icon
)) == B_OK
) {
92 BBitmap
bitmap(&icon
);
93 ret
= bitmap
.InitCheck();
95 ret
= SetIcon(&bitmap
);
100 BNotification::~BNotification()
105 for (int32 i
= fRefs
.CountItems() - 1; i
>= 0; i
--)
106 delete (entry_ref
*)fRefs
.ItemAtFast(i
);
108 for (int32 i
= fArgv
.CountItems() - 1; i
>= 0; i
--)
109 free(fArgv
.ItemAtFast(i
));
113 /*! \brief Returns initialization status.
116 BNotification::InitCheck() const
122 /*! \brief Returns a new BNotification object from @archive.
124 Returns a new BNotification object, allocated by new and created
125 with the version of the constructor that takes BMessage archive.
126 However, if the message doesn't contain an archived data for a
127 BNotification object, this method returns NULL.
129 \return BNotification object from @archive or NULL if it doesn't
130 contain a valid BNotification object.
133 BNotification::Instantiate(BMessage
* archive
)
135 if (validate_instantiation(archive
, "BNotification"))
136 return new(std::nothrow
) BNotification(archive
);
142 /*! \brief Archives the BNotification in the BMessages @archive.
144 \sa BArchivable::Archive(), Instantiate() static function.
146 - \c B_OK: Everything went fine.
147 - \c Other errors: Archiving has failed.
150 BNotification::Archive(BMessage
* archive
, bool deep
) const
152 status_t status
= BArchivable::Archive(archive
, deep
);
155 status
= archive
->AddInt32("_type", (int32
)fType
);
157 if (status
== B_OK
&& Group() != NULL
)
158 status
= archive
->AddString("_group", Group());
160 if (status
== B_OK
&& Title() != NULL
)
161 status
= archive
->AddString("_title", Title());
163 if (status
== B_OK
&& Content() != NULL
)
164 status
= archive
->AddString("_content", Content());
166 if (status
== B_OK
&& MessageID() != NULL
)
167 status
= archive
->AddString("_messageID", MessageID());
169 if (status
== B_OK
&& Type() == B_PROGRESS_NOTIFICATION
)
170 status
= archive
->AddFloat("_progress", Progress());
172 if (status
== B_OK
&& OnClickApp() != NULL
)
173 status
= archive
->AddString("_onClickApp", OnClickApp());
175 if (status
== B_OK
&& OnClickFile() != NULL
)
176 status
= archive
->AddRef("_onClickFile", OnClickFile());
178 if (status
== B_OK
) {
179 for (int32 i
= 0; i
< CountOnClickRefs(); i
++) {
180 status
= archive
->AddRef("_onClickRef", OnClickRefAt(i
));
186 if (status
== B_OK
) {
187 for (int32 i
= 0; i
< CountOnClickArgs(); i
++) {
188 status
= archive
->AddString("_onClickArgv", OnClickArgAt(i
));
194 if (status
== B_OK
) {
195 const BBitmap
* icon
= Icon();
197 BMessage iconArchive
;
198 status
= icon
->Archive(&iconArchive
);
200 archive
->AddMessage("_icon", &iconArchive
);
208 /*! \brief Notification's type.
210 \return A value of the notification_type enum that represents
214 BNotification::Type() const
220 /*! \brief Returns notification's group.
222 \return Notification's group.
225 BNotification::Group() const
233 /*! \brief Sets notification's group.
235 Notifications can be grouped together setting the same group.
238 BNotification::SetGroup(const BString
& group
)
244 /*! \brief Returns notification's title.
246 \return Notification's title.
249 BNotification::Title() const
257 /*! \brief Set notification's title.
260 BNotification::SetTitle(const BString
& title
)
266 /*! \brief Returns notification's message.
268 \return Notification's message.
271 BNotification::Content() const
279 /*! \brief Sets notification's message.
282 BNotification::SetContent(const BString
& content
)
288 /*! \brief Returns notification's message identifier.
290 \return Notification's message identifier.
293 BNotification::MessageID() const
301 /*! \brief Sets notification's message identifier.
304 BNotification::SetMessageID(const BString
& id
)
310 /*! \brief Returns progress information.
312 If notification's type is \c B_PROGRESS_NOTIFICATION, returns a value
313 between 0.0 and 1.0 that represent progress percentage.
315 If notification's type is not \c B_PROGRESS_NOTIFICATION, returns -1.
317 \return Percentage if notification's type is B_PROGRESS_NOTIFICATION
321 BNotification::Progress() const
323 if (fType
!= B_PROGRESS_NOTIFICATION
)
329 /*! \brief Sets progress information.
331 Sets progress percentage, this information will be used only
332 if notification's type is \c B_PROGRESS_NOTIFICATION.
334 The value of @progress must be between 0.0 and 1.0.
337 BNotification::SetProgress(float progress
)
341 else if (progress
> 1)
344 fProgress
= progress
;
349 BNotification::OnClickApp() const
358 BNotification::SetOnClickApp(const BString
& app
)
365 BNotification::OnClickFile() const
372 BNotification::SetOnClickFile(const entry_ref
* file
)
377 fFile
= new(std::nothrow
) entry_ref(*file
);
388 BNotification::AddOnClickRef(const entry_ref
* ref
)
393 entry_ref
* clonedRef
= new(std::nothrow
) entry_ref(*ref
);
394 if (clonedRef
== NULL
|| !fRefs
.AddItem(clonedRef
))
402 BNotification::CountOnClickRefs() const
404 return fRefs
.CountItems();
409 BNotification::OnClickRefAt(int32 index
) const
411 return (entry_ref
*)fArgv
.ItemAt(index
);
416 BNotification::AddOnClickArg(const BString
& arg
)
418 char* clonedArg
= strdup(arg
.String());
419 if (clonedArg
== NULL
|| !fArgv
.AddItem(clonedArg
))
427 BNotification::CountOnClickArgs() const
429 return fArgv
.CountItems();
434 BNotification::OnClickArgAt(int32 index
) const
436 return (char*)fArgv
.ItemAt(index
);
440 /*! \brief Notification's icon.
442 \return Notification's icon.
445 BNotification::Icon() const
451 /*! \brief Sets notification's icon.
453 Sets notification's icon.
454 This method does not assume ownership of @icon.
458 - \c B_OK: Everything went fine.
459 - \c B_NO_MEMORY: Allocation of @icon copy has failed.
460 - \c Other errors: Creation of @icon copy failed for some reason.
463 BNotification::SetIcon(const BBitmap
* icon
)
468 fBitmap
= new(std::nothrow
) BBitmap(icon
);
471 return fBitmap
->InitCheck();
479 /*! \brief Sends a notification to the notification_server.
481 The notification is delivered asynchronously to the notification_server,
482 which will display it according to its settings and filters.
484 \param timeout Microseconds after the message fades out.
486 - \c B_OK: Everything went fine.
487 - \c B_BAD_PORT_ID: A connection to notification_server could not be
488 established or the server is not up and running anymore.
489 - \c Other errors: Building the message from the notification failed.
492 BNotification::Send(bigtime_t timeout
)
494 BMessage
msg(kNotificationMessage
);
496 // Archive notification
497 status_t ret
= Archive(&msg
);
500 if (ret
== B_OK
&& timeout
> 0)
501 ret
= msg
.AddInt64("timeout", timeout
);
505 BMessenger
server(kNotificationServerSignature
);
506 ret
= server
.SendMessage(&msg
);