6 Copyright (c) 1991-2000, Be Incorporated. All rights reserved.
8 Permission is hereby granted, free of charge, to any person obtaining a copy of
9 this software and associated documentation files (the "Software"), to deal in
10 the Software without restriction, including without limitation the rights to
11 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
12 of the Software, and to permit persons to whom the Software is furnished to do
13 so, subject to the following conditions:
15 The above copyright notice and this permission notice applies to all licensees
16 and shall be included in all copies or substantial portions of the Software.
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF TITLE, MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 BE INCORPORATED BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF, OR IN CONNECTION
23 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 Except as contained in this notice, the name of Be Incorporated shall not be
26 used in advertising or otherwise to promote the sale, use or other dealings in
27 this Software without prior written authorization from Be Incorporated.
29 Tracker(TM), Be(R), BeOS(R), and BeIA(TM) are trademarks or registered trademarks
30 of Be Incorporated in the United States and other countries. Other brand product
31 names are registered trademarks or trademarks of their respective holders.
36 #include "FavoritesMenu.h"
38 #include <compat/sys/stat.h>
40 #include <Application.h>
42 #include <FindDirectory.h>
43 #include <FilePanel.h>
53 #include "IconMenuItem.h"
55 #include "QueryPoseView.h"
57 #include "Utilities.h"
58 #include "VirtualDirectoryEntryList.h"
61 #undef B_TRANSLATION_CONTEXT
62 #define B_TRANSLATION_CONTEXT "FavoritesMenu"
65 // #pragma mark - FavoritesMenu
68 FavoritesMenu::FavoritesMenu(const char* title
, BMessage
* openFolderMessage
,
69 BMessage
* openFileMessage
, const BMessenger
&target
,
70 bool isSavePanel
, BRefFilter
* filter
)
73 fOpenFolderMessage(openFolderMessage
),
74 fOpenFileMessage(openFileMessage
),
78 fSectionItemCount(-1),
79 fAddedSeparatorForSection(false),
83 fIsSavePanel(isSavePanel
),
89 FavoritesMenu::~FavoritesMenu()
91 delete fOpenFolderMessage
;
92 delete fOpenFileMessage
;
98 FavoritesMenu::SetRefFilter(BRefFilter
* filter
)
105 FavoritesMenu::StartBuildingItemList()
107 // initialize the menu building state
109 if (fInitialItemCount
== 0)
110 fInitialItemCount
= CountItems();
112 // strip the old items so we can add new fresh ones
113 int32 count
= CountItems() - fInitialItemCount
;
114 // keep the items that were added by the FavoritesMenu creator
116 delete RemoveItem(fInitialItemCount
);
119 fUniqueRefCheck
.clear();
126 FavoritesMenu::AddNextItem()
128 // run the next chunk of code for a given item adding state
130 if (fState
== kStart
) {
131 fState
= kAddingFavorites
;
132 fSectionItemCount
= 0;
133 fAddedSeparatorForSection
= false;
134 // set up adding the GoTo menu items
138 ThrowOnError(find_directory(B_USER_SETTINGS_DIRECTORY
,
140 path
.Append(kGoDirectory
);
141 mkdir(path
.Path(), 0777);
143 BEntry
entry(path
.Path());
144 Model
startModel(&entry
, true);
145 ThrowOnInitCheckError(&startModel
);
147 if (!startModel
.IsContainer())
150 if (startModel
.IsQuery())
151 fContainer
= new QueryEntryListCollection(&startModel
);
152 else if (startModel
.IsVirtualDirectory())
153 fContainer
= new VirtualDirectoryEntryList(&startModel
);
155 BDirectory
* directory
156 = dynamic_cast<BDirectory
*>(startModel
.Node());
157 if (directory
!= NULL
)
158 fContainer
= new DirectoryEntryList(*directory
);
161 ThrowOnInitCheckError(fContainer
);
162 ThrowOnError(fContainer
->Rewind());
169 if (fState
== kAddingFavorites
) {
171 if (fContainer
!= NULL
&& fContainer
->GetNextRef(&ref
) == B_OK
) {
172 Model
model(&ref
, true);
173 if (model
.InitCheck() != B_OK
)
176 if (!ShouldShowModel(&model
))
179 BMenuItem
* item
= BNavMenu::NewModelItem(&model
,
180 model
.IsDirectory() ? fOpenFolderMessage
: fOpenFileMessage
,
186 item
->SetLabel(ref
.name
);
187 // this is the name of the link in the Go dir
189 if (!fAddedSeparatorForSection
) {
190 fAddedSeparatorForSection
= true;
191 AddItem(new TitledSeparatorItem(B_TRANSLATE("Favorites")));
193 fUniqueRefCheck
.push_back(*model
.EntryRef());
200 // done with favorites, set up for adding recent files
201 fState
= kAddingFiles
;
203 fAddedSeparatorForSection
= false;
206 be_app
->GetAppInfo(&info
);
209 int32 apps
, docs
, folders
;
210 TrackerSettings().RecentCounts(&apps
, &docs
, &folders
);
212 BRoster().GetRecentDocuments(&fItems
, docs
, NULL
, info
.signature
);
214 fSectionItemCount
= 0;
217 if (fState
== kAddingFiles
) {
218 // if this is a Save panel, not an Open panel
219 // then don't add the recent documents
223 if (fItems
.FindRef("refs", fIndex
++, &ref
) != B_OK
)
226 Model
model(&ref
, true);
227 if (model
.InitCheck() != B_OK
)
230 if (!ShouldShowModel(&model
))
233 BMenuItem
* item
= BNavMenu::NewModelItem(&model
,
234 fOpenFileMessage
, fTarget
);
236 if (!fAddedSeparatorForSection
) {
237 fAddedSeparatorForSection
= true;
238 AddItem(new TitledSeparatorItem(
239 B_TRANSLATE("Recent documents")));
248 // done with recent files, set up for adding recent folders
249 fState
= kAddingFolders
;
251 fAddedSeparatorForSection
= false;
254 be_app
->GetAppInfo(&info
);
257 int32 apps
, docs
, folders
;
258 TrackerSettings().RecentCounts(&apps
, &docs
, &folders
);
260 BRoster().GetRecentFolders(&fItems
, folders
, info
.signature
);
264 if (fState
== kAddingFolders
) {
267 if (fItems
.FindRef("refs", fIndex
++, &ref
) != B_OK
)
270 // don't add folders that are already in the GoTo section
271 if (find_if(fUniqueRefCheck
.begin(), fUniqueRefCheck
.end(),
272 bind2nd(std::equal_to
<entry_ref
>(), ref
))
273 != fUniqueRefCheck
.end()) {
277 Model
model(&ref
, true);
278 if (model
.InitCheck() != B_OK
)
281 if (!ShouldShowModel(&model
))
284 BMenuItem
* item
= BNavMenu::NewModelItem(&model
,
285 fOpenFolderMessage
, fTarget
, true);
287 if (!fAddedSeparatorForSection
) {
288 fAddedSeparatorForSection
= true;
289 AddItem(new TitledSeparatorItem(
290 B_TRANSLATE("Recent folders")));
293 item
->SetEnabled(true);
294 // BNavMenu::NewModelItem returns a disabled item here -
295 // need to fix this in BNavMenu::NewModelItem
307 FavoritesMenu::DoneBuildingItemList()
309 SetTargetForItems(fTarget
);
314 FavoritesMenu::ClearMenuBuildingState()
320 // force the menu to get rebuilt each time
326 FavoritesMenu::ShouldShowModel(const Model
* model
)
328 if (fIsSavePanel
&& model
->IsFile())
331 if (!fRefFilter
|| model
->Node() == NULL
)
334 struct stat_beos statBeOS
;
335 convert_to_stat_beos(model
->StatBuf(), &statBeOS
);
337 return fRefFilter
->Filter(model
->EntryRef(), model
->Node(), &statBeOS
,
342 // #pragma mark - RecentsMenu
345 RecentsMenu::RecentsMenu(const char* name
, int32 which
, uint32 what
,
348 BNavMenu(name
, what
, target
),
356 TrackerSettings().RecentCounts(&applications
,&documents
,&folders
);
359 fRecentsCount
= documents
;
360 else if (fWhich
== 1)
361 fRecentsCount
= applications
;
362 else if (fWhich
== 2)
363 fRecentsCount
= folders
;
368 RecentsMenu::DetachedFromWindow()
371 // BNavMenu::DetachedFromWindow sets the TypesList to NULL
373 BMenu::DetachedFromWindow();
378 RecentsMenu::StartBuildingItemList()
380 int32 count
= CountItems()-1;
381 for (int32 index
= count
; index
>= 0; index
--) {
382 BMenuItem
* item
= ItemAt(index
);
389 // !! note: don't call inherited from here
390 // the navref is not set for this menu
391 // but it still needs to be a draggable navmenu
392 // simply return true so that AddNextItem is called
394 // return BNavMenu::StartBuildingItemList();
400 RecentsMenu::AddNextItem()
402 if (fRecentsCount
> 0 && AddRecents(fRecentsCount
))
411 RecentsMenu::AddRecents(int32 count
)
413 if (fItemIndex
== 0) {
414 fRecentList
.MakeEmpty();
419 roster
.GetRecentDocuments(&fRecentList
, count
);
422 roster
.GetRecentApps(&fRecentList
, count
);
425 roster
.GetRecentFolders(&fRecentList
, count
);
434 if (fRecentList
.FindRef("refs", fItemIndex
++, &ref
) != B_OK
)
437 if (ref
.name
!= NULL
&& strlen(ref
.name
) > 0) {
438 Model
model(&ref
, true);
439 ModelMenuItem
* item
= BNavMenu::NewModelItem(&model
,
440 new BMessage(fMessage
.what
),
441 Target(), false, NULL
, TypesList());
446 // return true so that we know to reenter this list
455 // return false if we are done with this list
462 RecentsMenu::DoneBuildingItemList()
465 // !! note: don't call inherited here
466 // the object list is not built
467 // and this list does not need to be sorted
468 // BNavMenu::DoneBuildingItemList();
471 if (CountItems() <= 0) {
472 BMenuItem
* item
= new BMenuItem(B_TRANSLATE("<No recent items>"), 0);
473 item
->SetEnabled(false);
476 SetTargetForItems(Target());
481 RecentsMenu::ClearMenuBuildingState()
484 BNavMenu::ClearMenuBuildingState();