2 * Copyright 2001-2009, Haiku Inc.
3 * Distributed under the terms of the MIT License.
7 * Ingo Weinhold, bonefish@users.sf.net
8 * Axel Dörfler, axeld@pinc-software.de
12 //! Recently launched apps list
15 #include "RecentApps.h"
17 #include <tracker_private.h>
19 #include <AppFileInfo.h>
25 #include <storage_support.h>
33 \brief Manages the roster's list of recently launched applications
38 /*! \var std::list<std::string> RecentApps::fAppList
39 \brief The list of app sigs, most recent first
41 The signatures are expected to be stored all lowercase, as MIME
42 signatures are case-independent.
46 /*! \brief Creates a new list.
48 The list is initially empty.
50 RecentApps::RecentApps()
55 /*! \brief Frees all resources associated with the object.
57 Currently does nothing.
59 RecentApps::~RecentApps()
64 /*! \brief Places the app with the given signature at the front of
67 If the app already exists elsewhere in the list, that item is
68 removed so only one instance exists in the list at any time.
70 \param appSig The application's signature
71 \param appFlags The application's flags. If \a appFlags contains
72 either \c B_ARGV_ONLY or \c B_BACKGROUND_APP, the
73 application is \b not added to the list (but \c B_OK
76 - \c B_OK: success (even if the app was not added due to appFlags)
80 RecentApps::Add(const char *appSig
, int32 appFlags
)
85 // don't add background apps, as well as Tracker and Deskbar to the list
87 if (!strcasecmp(appSig
, kTrackerSignature
)
88 || !strcasecmp(appSig
, kDeskbarSignature
)
89 || (appFlags
& (B_ARGV_ONLY
| B_BACKGROUND_APP
)) != 0)
92 // Remove any previous instance
93 std::list
<std::string
>::iterator i
;
94 for (i
= fAppList
.begin(); i
!= fAppList
.end(); i
++) {
95 if (!strcasecmp((*i
).c_str(), appSig
)) {
103 fAppList
.push_front(appSig
);
112 /*! \brief Adds the signature of the application referred to by \a ref at
113 the front of the recent apps list.
115 The entry is checked for a BEOS:APP_SIG attribute. If that fails, the
116 app's resources are checked. If no signature can be found, the call
120 RecentApps::Add(const entry_ref
*ref
, int32 appFlags
)
127 char signature
[B_MIME_TYPE_LENGTH
];
129 status_t err
= file
.SetTo(ref
, B_READ_ONLY
);
131 err
= info
.SetTo(&file
);
133 err
= info
.GetSignature(signature
);
135 err
= Add(signature
, appFlags
);
140 /*! \brief Returns the first \a maxCount recent apps in the \c BMessage
141 pointed to by \a list.
143 The message is cleared first, and \c entry_refs for the the apps are
144 stored in the \c "refs" field of the message (\c B_REF_TYPE).
146 If there are fewer than \a maxCount items in the list, the entire
149 Since BRoster::GetRecentApps() returns \c void, the message pointed
150 to by \a list is simply cleared if maxCount is invalid (i.e. <= 0).
153 RecentApps::Get(int32 maxCount
, BMessage
*list
)
162 std::list
<std::string
>::iterator item
;
163 status_t status
= B_OK
;
165 for (item
= fAppList
.begin();
166 status
== B_OK
&& counter
< maxCount
&& item
!= fAppList
.end();
169 if (GetRefForApp(item
->c_str(), &ref
) == B_OK
)
170 status
= list
->AddRef("refs", &ref
);
172 D(PRINT("WARNING: RecentApps::Get(): No ref found for app '%s'\n",
181 /*! \brief Clears the list of recently launched apps
191 /*! \brief Dumps the the current list of apps to stdout.
196 std::list
<std::string
>::iterator item
;
198 for (item
= fAppList
.begin(); item
!= fAppList
.end(); item
++) {
199 printf("%d: '%s'\n", counter
++, item
->c_str());
205 /*! \brief Outputs a textual representation of the current recent
206 apps list to the given file stream.
210 RecentApps::Save(FILE* file
)
212 status_t error
= file
? B_OK
: B_BAD_VALUE
;
214 fprintf(file
, "# Recent applications\n");
215 std::list
<std::string
>::iterator item
;
216 for (item
= fAppList
.begin(); item
!= fAppList
.end(); item
++) {
217 fprintf(file
, "RecentApp %s\n", item
->c_str());
225 /*! \brief Fetches an \c entry_ref for the application with the
228 First the MIME database is checked for a matching application type
229 with a valid app hint attribute. If that fails, a query is established
230 to track down such an application, if there is one available.
233 RecentApps::GetRefForApp(const char *appSig
, entry_ref
*result
)
235 if (appSig
== NULL
|| result
== NULL
)
238 // We'll use BMimeType to check for the app hint, since I'm lazy
239 // and Ingo's on vacation :-P :-)
240 BMimeType
mime(appSig
);
241 status_t err
= mime
.InitCheck();
243 err
= mime
.GetAppHint(result
);