Show app names in Open With menu

Fixes #8
This commit is contained in:
Jaidyn Ann 2021-05-02 20:21:05 -05:00
parent be8bfdc9fc
commit 10d03a4803
6 changed files with 117 additions and 35 deletions

View File

@ -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

View File

@ -18,6 +18,7 @@
#include <TextControl.h>
#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)
{

View File

@ -5,7 +5,6 @@
#ifndef ENTRIESVIEW_H
#define ENTRIESVIEW_H
#include <SupportDefs.h>
#include <GroupView.h>
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;

View File

@ -2,6 +2,7 @@
* Copyright 2020, Jaidyn Levesque <jadedctrl@teknik.io>
* All rights reserved. Distributed under the terms of the MIT license.
*/
#ifndef MIME_H
#define MIME_H

80
src/OpenWithMenu.cpp Normal file
View File

@ -0,0 +1,80 @@
/*
* Copyright 2021, Jaidyn Levesque <jadedctrl@teknik.io>
* All rights reserved. Distributed under the terms of the MIT license.
*/
#include "OpenWithMenu.h"
#include <StringList.h>
#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);
}
}
}

27
src/OpenWithMenu.h Normal file
View File

@ -0,0 +1,27 @@
/*
* Copyright 2021, Jaidyn Levesque <jadedctrl@teknik.io>
* All rights reserved. Distributed under the terms of the MIT license.
*/
#ifndef OPENWITHMENU_H
#define OPENWITHMENU_H
#include <Menu.h>
#include <MenuItem.h>
#include <MimeType.h>
#include <PopUpMenu.h>
class OpenWithMenu : public BPopUpMenu {
public:
OpenWithMenu(const char* name, const char* type, BString preferred);
private:
void _PopulateMenu();
BMimeType fType;
BString fPreferredApp;
};
#endif // OPENWITHMENU_H