diff --git a/Makefile b/Makefile index 1d6fab2..d6be5dd 100644 --- a/Makefile +++ b/Makefile @@ -40,6 +40,7 @@ SRCS = \ src/LocalSource.cpp \ src/MainWindow.cpp \ src/Mimetypes.cpp \ + src/OpenWithMenu.cpp \ src/Preferences.cpp \ src/UpdatesView.cpp \ src/Util.cpp diff --git a/src/EntriesView.cpp b/src/EntriesView.cpp index 22f9261..8c6838c 100644 --- a/src/EntriesView.cpp +++ b/src/EntriesView.cpp @@ -18,6 +18,7 @@ #include #include "App.h" +#include "OpenWithMenu.h" #include "Util.h" @@ -95,8 +96,9 @@ EntriesView::MessageReceived(BMessage* msg) } case kOpenWithSelect: { - ((App*)be_app)->fPreferences->SetEntryOpenWith( - fOpenWithMenuField->MenuItem()->Label()); + BString signature; + if (msg->FindString("signature", &signature) == B_OK) + ((App*)be_app)->fPreferences->SetEntryOpenWith(signature.String()); break; } case kOpenWithBrowse: @@ -162,7 +164,8 @@ EntriesView::_InitInterface() fOpenWithLabel = new BStringView("openWithLabel", B_TRANSLATE("Open with:")); - fOpenWithMenu = new BPopUpMenu("openWith"); + fOpenWithMenu = new OpenWithMenu("openWith", "text/html", + ((App*)be_app)->fPreferences->EntryOpenWith()); fOpenWithMenuField = new BMenuField("openWithMenu", NULL, fOpenWithMenu); fOpenWithSelectButton = new BButton("openWithSelect", B_TRANSLATE("Select…"), new BMessage(kOpenWithBrowse)); @@ -177,8 +180,6 @@ EntriesView::_InitInterface() fEntryFolderText->SetText(prefs->EntryDir()); - _PopulateOpenWithMenu(); - BLayoutBuilder::Group<>(fSavingBox, B_HORIZONTAL) .SetInsets(B_USE_ITEM_INSETS) .AddGroup(B_VERTICAL, B_USE_DEFAULT_SPACING) @@ -224,31 +225,6 @@ EntriesView::_InitInterface() } -void -EntriesView::_PopulateOpenWithMenu() -{ - BString preferred = ((App*)be_app)->fPreferences->EntryOpenWith(); - BMimeType html("text/html"); - BStringList signatures; - BMessage types; - - BMenuItem* prefItem = new BMenuItem(preferred, new BMessage(kOpenWithSelect)); - prefItem->SetMarked(true); - fOpenWithMenu->AddItem(prefItem); - - html.GetSupportingApps(&types); - if (types.FindStrings("applications", &signatures) != B_OK) - return; - - for (int i = 0; i < signatures.CountStrings(); i++) { - BString string = signatures.StringAt(i); - BMenuItem* item = new BMenuItem(string, new BMessage(kOpenWithSelect)); - if (string != preferred) - fOpenWithMenu->AddItem(item); - } -} - - void EntriesView::_FileError(status_t result) { diff --git a/src/EntriesView.h b/src/EntriesView.h index 8f78065..3156f26 100644 --- a/src/EntriesView.h +++ b/src/EntriesView.h @@ -5,7 +5,6 @@ #ifndef ENTRIESVIEW_H #define ENTRIESVIEW_H -#include #include class BBox; @@ -13,10 +12,10 @@ class BButton; class BFilePanel; class BMenuField; class BMessage; -class BPopUpMenu; class BRadioButton; class BStringView; class BTextControl; +class OpenWithMenu; enum @@ -41,11 +40,9 @@ public: private: void _InitInterface(); - void _PopulateOpenWithMenu(); void _FileError(status_t result); - BBox* fSavingBox; BStringView* fEntryFolderLabel; BTextControl* fEntryFolderText; @@ -57,7 +54,7 @@ private: BRadioButton* fOpenAsHtmlRadio; BRadioButton* fOpenAsUrlRadio; BStringView* fOpenWithLabel; - BPopUpMenu* fOpenWithMenu; + OpenWithMenu* fOpenWithMenu; BMenuField* fOpenWithMenuField; BButton* fOpenWithSelectButton; BFilePanel* fOpenWithPanel; diff --git a/src/Mimetypes.h b/src/Mimetypes.h index c621a74..db9c7ae 100644 --- a/src/Mimetypes.h +++ b/src/Mimetypes.h @@ -2,6 +2,7 @@ * Copyright 2020, Jaidyn Levesque * All rights reserved. Distributed under the terms of the MIT license. */ + #ifndef MIME_H #define MIME_H diff --git a/src/OpenWithMenu.cpp b/src/OpenWithMenu.cpp new file mode 100644 index 0000000..a566073 --- /dev/null +++ b/src/OpenWithMenu.cpp @@ -0,0 +1,80 @@ +/* + * Copyright 2021, Jaidyn Levesque + * All rights reserved. Distributed under the terms of the MIT license. + */ + +#include "OpenWithMenu.h" + +#include + +#include "EntriesView.h" + + +OpenWithMenu::OpenWithMenu(const char* name, const char* type, BString preferred) + : BPopUpMenu(name), + fType(type), + fPreferredApp(preferred) +{ + _PopulateMenu(); +} + + +void +OpenWithMenu::_PopulateMenu() +{ + BStringList signatures; + BMessage types; + + fType.GetSupportingApps(&types); + if (types.FindStrings("applications", &signatures) != B_OK) + return; + + int32 subTypeCount = types.GetInt32("be:sub", signatures.CountStrings()); + + bool preferredAdded = false; + for (int i = 0; i < signatures.CountStrings(); i++) { + BString signature = signatures.StringAt(i); + + BMessage* msg = new BMessage(kOpenWithSelect); + msg->AddString("signature", signature); + + BMenuItem* item = new BMenuItem(signature, msg); + AddItem(item); + + BMimeType mime(signature.String()); + char appName[B_MIME_TYPE_LENGTH] = {'\0'}; + mime.GetShortDescription(appName); + item->SetLabel(appName); + + if (signature == fPreferredApp) { + item->SetMarked(true); + preferredAdded = true; + } + + if (i + 1 == subTypeCount) + AddSeparatorItem(); + } + + // If the current preference isn't associated with text/html, or is a path, + // add it to the menu. + if (preferredAdded == false) { + AddSeparatorItem(); + + BMessage* msg = new BMessage(kOpenWithSelect); + msg->AddString("signature", fPreferredApp); + + BMenuItem* prefItem = new BMenuItem(fPreferredApp.String(), msg); + prefItem->SetLabel(fPreferredApp.String()); + prefItem->SetMarked(true); + AddItem(prefItem); + + BMimeType prefMime(fPreferredApp.String()); + if (prefMime.IsValid() == true) { + char appName[B_MIME_TYPE_LENGTH] = {'\0'}; + prefMime.GetShortDescription(appName); + prefItem->SetLabel(appName); + } + } +} + + diff --git a/src/OpenWithMenu.h b/src/OpenWithMenu.h new file mode 100644 index 0000000..3ed46b1 --- /dev/null +++ b/src/OpenWithMenu.h @@ -0,0 +1,27 @@ +/* + * Copyright 2021, Jaidyn Levesque + * All rights reserved. Distributed under the terms of the MIT license. + */ +#ifndef OPENWITHMENU_H +#define OPENWITHMENU_H + +#include +#include +#include +#include + + +class OpenWithMenu : public BPopUpMenu { +public: + OpenWithMenu(const char* name, const char* type, BString preferred); + +private: + void _PopulateMenu(); + + BMimeType fType; + BString fPreferredApp; +}; + + +#endif // OPENWITHMENU_H +