2 * Copyright 2010-2017, 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
8 * Brian Hill, supernova@tycho.email
12 #include <Notification.h>
19 #include <notification/Notifications.h>
28 BNotification::BNotification(notification_type type
)
38 get_team_info(B_CURRENT_TEAM
, &teamInfo
);
40 be_roster
->GetRunningAppInfo(teamInfo
.team
, &appInfo
);
42 int32 iconSize
= B_LARGE_ICON
;
43 fBitmap
= new BBitmap(BRect(0, 0, iconSize
- 1, iconSize
- 1), 0, B_RGBA32
);
45 if (BNodeInfo::GetTrackerIcon(&appInfo
.ref
, fBitmap
,
46 icon_size(iconSize
)) != B_OK
) {
51 fSourceSignature
= appInfo
.signature
;
52 BPath
path(&appInfo
.ref
);
53 if (path
.InitCheck() == B_OK
)
54 fSourceName
= path
.Leaf();
58 BNotification::BNotification(BMessage
* archive
)
67 if (archive
->FindString("_appname", &appName
) == B_OK
)
68 fSourceName
= appName
;
71 if (archive
->FindString("_signature", &signature
) == B_OK
)
72 fSourceSignature
= signature
;
75 if (archive
->FindInt32("_type", &type
) == B_OK
)
76 fType
= (notification_type
)type
;
78 fInitStatus
= B_ERROR
;
81 if (archive
->FindString("_group", &group
) == B_OK
)
85 if (archive
->FindString("_title", &title
) == B_OK
)
89 if (archive
->FindString("_content", &content
) == B_OK
)
93 if (archive
->FindString("_messageID", &messageID
) == B_OK
)
94 SetMessageID(messageID
);
97 if (type
== B_PROGRESS_NOTIFICATION
98 && archive
->FindFloat("_progress", &progress
) == B_OK
)
99 SetProgress(progress
);
102 if (archive
->FindString("_onClickApp", &onClickApp
) == B_OK
)
103 SetOnClickApp(onClickApp
);
105 entry_ref onClickFile
;
106 if (archive
->FindRef("_onClickFile", &onClickFile
) == B_OK
)
107 SetOnClickFile(&onClickFile
);
109 entry_ref onClickRef
;
111 while (archive
->FindRef("_onClickRef", index
++, &onClickRef
) == B_OK
)
112 AddOnClickRef(&onClickRef
);
116 while (archive
->FindString("_onClickArgv", index
++, &onClickArgv
) == B_OK
)
117 AddOnClickArg(onClickArgv
);
121 if ((ret
= archive
->FindMessage("_icon", &icon
)) == B_OK
) {
122 BBitmap
bitmap(&icon
);
123 ret
= bitmap
.InitCheck();
125 ret
= SetIcon(&bitmap
);
130 BNotification::~BNotification()
135 for (int32 i
= fRefs
.CountItems() - 1; i
>= 0; i
--)
136 delete (entry_ref
*)fRefs
.ItemAtFast(i
);
138 for (int32 i
= fArgv
.CountItems() - 1; i
>= 0; i
--)
139 free(fArgv
.ItemAtFast(i
));
143 /*! \brief Returns initialization status.
146 BNotification::InitCheck() const
152 /*! \brief Returns a new BNotification object from @archive.
154 Returns a new BNotification object, allocated by new and created
155 with the version of the constructor that takes BMessage archive.
156 However, if the message doesn't contain an archived data for a
157 BNotification object, this method returns NULL.
159 \return BNotification object from @archive or NULL if it doesn't
160 contain a valid BNotification object.
163 BNotification::Instantiate(BMessage
* archive
)
165 if (validate_instantiation(archive
, "BNotification"))
166 return new(std::nothrow
) BNotification(archive
);
172 /*! \brief Archives the BNotification in the BMessages @archive.
174 \sa BArchivable::Archive(), Instantiate() static function.
176 - \c B_OK: Everything went fine.
177 - \c Other errors: Archiving has failed.
180 BNotification::Archive(BMessage
* archive
, bool deep
) const
182 status_t status
= BArchivable::Archive(archive
, deep
);
185 status
= archive
->AddString("_appname", fSourceName
);
188 status
= archive
->AddString("_signature", fSourceSignature
);
191 status
= archive
->AddInt32("_type", (int32
)fType
);
193 if (status
== B_OK
&& Group() != NULL
)
194 status
= archive
->AddString("_group", Group());
196 if (status
== B_OK
&& Title() != NULL
)
197 status
= archive
->AddString("_title", Title());
199 if (status
== B_OK
&& Content() != NULL
)
200 status
= archive
->AddString("_content", Content());
202 if (status
== B_OK
&& MessageID() != NULL
)
203 status
= archive
->AddString("_messageID", MessageID());
205 if (status
== B_OK
&& Type() == B_PROGRESS_NOTIFICATION
)
206 status
= archive
->AddFloat("_progress", Progress());
208 if (status
== B_OK
&& OnClickApp() != NULL
)
209 status
= archive
->AddString("_onClickApp", OnClickApp());
211 if (status
== B_OK
&& OnClickFile() != NULL
)
212 status
= archive
->AddRef("_onClickFile", OnClickFile());
214 if (status
== B_OK
) {
215 for (int32 i
= 0; i
< CountOnClickRefs(); i
++) {
216 status
= archive
->AddRef("_onClickRef", OnClickRefAt(i
));
222 if (status
== B_OK
) {
223 for (int32 i
= 0; i
< CountOnClickArgs(); i
++) {
224 status
= archive
->AddString("_onClickArgv", OnClickArgAt(i
));
230 if (status
== B_OK
) {
231 const BBitmap
* icon
= Icon();
233 BMessage iconArchive
;
234 status
= icon
->Archive(&iconArchive
);
236 archive
->AddMessage("_icon", &iconArchive
);
244 /*! \brief Returns source application signature.
246 \return Source application signature.
249 BNotification::SourceSignature() const
251 return fSourceSignature
;
255 /*! \brief Returns source application name.
257 \return Source application name.
260 BNotification::SourceName() const
266 /*! \brief Notification's type.
268 \return A value of the notification_type enum that represents
272 BNotification::Type() const
278 /*! \brief Returns notification's group.
280 \return Notification's group.
283 BNotification::Group() const
291 /*! \brief Sets notification's group.
293 Notifications can be grouped together setting the same group.
296 BNotification::SetGroup(const BString
& group
)
302 /*! \brief Returns notification's title.
304 \return Notification's title.
307 BNotification::Title() const
315 /*! \brief Set notification's title.
318 BNotification::SetTitle(const BString
& title
)
324 /*! \brief Returns notification's message.
326 \return Notification's message.
329 BNotification::Content() const
337 /*! \brief Sets notification's message.
340 BNotification::SetContent(const BString
& content
)
346 /*! \brief Returns notification's message identifier.
348 \return Notification's message identifier.
351 BNotification::MessageID() const
359 /*! \brief Sets notification's message identifier.
362 BNotification::SetMessageID(const BString
& id
)
368 /*! \brief Returns progress information.
370 If notification's type is \c B_PROGRESS_NOTIFICATION, returns a value
371 between 0.0 and 1.0 that represent progress percentage.
373 If notification's type is not \c B_PROGRESS_NOTIFICATION, returns -1.
375 \return Percentage if notification's type is B_PROGRESS_NOTIFICATION
379 BNotification::Progress() const
381 if (fType
!= B_PROGRESS_NOTIFICATION
)
387 /*! \brief Sets progress information.
389 Sets progress percentage, this information will be used only
390 if notification's type is \c B_PROGRESS_NOTIFICATION.
392 The value of @progress must be between 0.0 and 1.0.
395 BNotification::SetProgress(float progress
)
399 else if (progress
> 1)
402 fProgress
= progress
;
407 BNotification::OnClickApp() const
416 BNotification::SetOnClickApp(const BString
& app
)
423 BNotification::OnClickFile() const
430 BNotification::SetOnClickFile(const entry_ref
* file
)
435 fFile
= new(std::nothrow
) entry_ref(*file
);
446 BNotification::AddOnClickRef(const entry_ref
* ref
)
451 entry_ref
* clonedRef
= new(std::nothrow
) entry_ref(*ref
);
452 if (clonedRef
== NULL
|| !fRefs
.AddItem(clonedRef
))
460 BNotification::CountOnClickRefs() const
462 return fRefs
.CountItems();
467 BNotification::OnClickRefAt(int32 index
) const
469 return (entry_ref
*)fArgv
.ItemAt(index
);
474 BNotification::AddOnClickArg(const BString
& arg
)
476 char* clonedArg
= strdup(arg
.String());
477 if (clonedArg
== NULL
|| !fArgv
.AddItem(clonedArg
))
485 BNotification::CountOnClickArgs() const
487 return fArgv
.CountItems();
492 BNotification::OnClickArgAt(int32 index
) const
494 return (char*)fArgv
.ItemAt(index
);
498 /*! \brief Notification's icon.
500 \return Notification's icon.
503 BNotification::Icon() const
509 /*! \brief Sets notification's icon.
511 Sets notification's icon.
512 This method does not assume ownership of @icon.
516 - \c B_OK: Everything went fine.
517 - \c B_NO_MEMORY: Allocation of @icon copy has failed.
518 - \c Other errors: Creation of @icon copy failed for some reason.
521 BNotification::SetIcon(const BBitmap
* icon
)
526 fBitmap
= new(std::nothrow
) BBitmap(icon
);
529 return fBitmap
->InitCheck();
537 /*! \brief Sends a notification to the notification_server.
539 The notification is delivered asynchronously to the notification_server,
540 which will display it according to its settings and filters.
542 \param timeout Microseconds after the message fades out.
544 - \c B_OK: Everything went fine.
545 - \c B_BAD_PORT_ID: A connection to notification_server could not be
546 established or the server is not up and running anymore.
547 - \c Other errors: Building the message from the notification failed.
550 BNotification::Send(bigtime_t timeout
)
552 BMessage
msg(kNotificationMessage
);
554 // Archive notification
555 status_t ret
= Archive(&msg
);
558 if (ret
== B_OK
&& timeout
> 0)
559 ret
= msg
.AddInt64("timeout", timeout
);
563 BMessenger
server(kNotificationServerSignature
);
564 ret
= server
.SendMessage(&msg
);
571 void BNotification::_ReservedNotification1() {}
572 void BNotification::_ReservedNotification2() {}
573 void BNotification::_ReservedNotification3() {}
574 void BNotification::_ReservedNotification4() {}
575 void BNotification::_ReservedNotification5() {}
576 void BNotification::_ReservedNotification6() {}
577 void BNotification::_ReservedNotification7() {}
578 void BNotification::_ReservedNotification8() {}