PackageView: Refactored a lot of repetitive code...

... to create menu items.
This commit is contained in:
Stephan Aßmus
2014-02-16 12:12:24 +01:00
parent 236dd276ac
commit 193a4de178
2 changed files with 175 additions and 171 deletions
+109 -115
View File
@@ -125,19 +125,20 @@ PackageView::AttachedToWindow()
fOpenPanel->SetTarget(BMessenger(this)); fOpenPanel->SetTarget(BMessenger(this));
fInstallTypes->SetTargetForItems(this); fInstallTypes->SetTargetForItems(this);
if (fInfo.InitCheck() == B_OK) { if (ret != B_OK)
return;
// If the package is valid, we can set up the default group and all // If the package is valid, we can set up the default group and all
// other things. If not, then the application will close just after // other things. If not, then the application will close just after
// attaching the view to the window // attaching the view to the window
_GroupChanged(0); _GroupChanged(0);
fStatusWindow = new PackageStatus fStatusWindow = new PackageStatus(B_TRANSLATE("Installation progress"),
(B_TRANSLATE("Installation progress"),
NULL, NULL, this); NULL, NULL, this);
// Show the splash screen, if present // Show the splash screen, if present
BMallocIO* image = fInfo.GetSplashScreen(); BMallocIO* image = fInfo.GetSplashScreen();
if (image) { if (image != NULL) {
PackageImageViewer* imageViewer = new PackageImageViewer(image); PackageImageViewer* imageViewer = new PackageImageViewer(image);
imageViewer->Go(); imageViewer->Go();
} }
@@ -150,20 +151,16 @@ PackageView::AttachedToWindow()
int32 selection = text->Go(); int32 selection = text->Go();
// The user didn't accept our disclaimer, this means we cannot // The user didn't accept our disclaimer, this means we cannot
// continue. // continue.
if (selection == 0) { if (selection == 0)
BWindow *parent = Window();
if (parent && parent->Lock())
parent->Quit(); parent->Quit();
} }
} }
}
}
void void
PackageView::MessageReceived(BMessage *msg) PackageView::MessageReceived(BMessage* message)
{ {
switch (msg->what) { switch (message->what) {
case P_MSG_INSTALL: case P_MSG_INSTALL:
{ {
fInstall->SetEnabled(false); fInstall->SetEnabled(false);
@@ -177,7 +174,7 @@ PackageView::MessageReceived(BMessage *msg)
case P_MSG_PATH_CHANGED: case P_MSG_PATH_CHANGED:
{ {
BString path; BString path;
if (msg->FindString("path", &path) == B_OK) { if (message->FindString("path", &path) == B_OK) {
fCurrentPath.SetTo(path.String()); fCurrentPath.SetTo(path.String());
} }
break; break;
@@ -188,7 +185,7 @@ PackageView::MessageReceived(BMessage *msg)
case P_MSG_GROUP_CHANGED: case P_MSG_GROUP_CHANGED:
{ {
int32 index; int32 index;
if (msg->FindInt32("index", &index) == B_OK) { if (message->FindInt32("index", &index) == B_OK) {
_GroupChanged(index); _GroupChanged(index);
} }
break; break;
@@ -265,7 +262,7 @@ PackageView::MessageReceived(BMessage *msg)
case B_REFS_RECEIVED: case B_REFS_RECEIVED:
{ {
entry_ref ref; entry_ref ref;
if (msg->FindRef("refs", &ref) == B_OK) { if (message->FindRef("refs", &ref) == B_OK) {
BPath path(&ref); BPath path(&ref);
BMenuItem* item = fDestField->MenuItem(); BMenuItem* item = fDestField->MenuItem();
@@ -288,20 +285,20 @@ PackageView::MessageReceived(BMessage *msg)
break; break;
} }
case B_SIMPLE_DATA: case B_SIMPLE_DATA:
if (msg->WasDropped()) { if (message->WasDropped()) {
uint32 type; uint32 type;
int32 count; int32 count;
status_t ret = msg->GetInfo("refs", &type, &count); status_t ret = message->GetInfo("refs", &type, &count);
// Check whether the message means someone dropped a file // Check whether the message means someone dropped a file
// to our view // to our view
if (ret == B_OK && type == B_REF_TYPE) { if (ret == B_OK && type == B_REF_TYPE) {
// If it is, send it along with the refs to the application // If it is, send it along with the refs to the application
msg->what = B_REFS_RECEIVED; message->what = B_REFS_RECEIVED;
be_app->PostMessage(msg); be_app->PostMessage(message);
} }
} }
default: default:
BView::MessageReceived(msg); BView::MessageReceived(message);
break; break;
} }
} }
@@ -382,10 +379,12 @@ PackageView::ItemExists(PackageItem &item, BPath &path, int32 &policy)
BString actionString; BString actionString;
if (choice == P_EXISTS_OVERWRITE) { if (choice == P_EXISTS_OVERWRITE) {
alertString << B_TRANSLATE("All existing files will be replaced?"); alertString << B_TRANSLATE(
"All existing files will be replaced?");
actionString = B_TRANSLATE("Replace all"); actionString = B_TRANSLATE("Replace all");
} else { } else {
alertString << B_TRANSLATE("All existing files will be skipped?"); alertString << B_TRANSLATE(
"All existing files will be skipped?");
actionString = B_TRANSLATE("Skip all"); actionString = B_TRANSLATE("Skip all");
} }
alert = new BAlert("policy_decision", alertString.String(), alert = new BAlert("policy_decision", alertString.String(),
@@ -553,45 +552,23 @@ PackageView::_InitView()
void void
PackageView::_InitProfiles() PackageView::_InitProfiles()
{ {
// Set all profiles int count = fInfo.GetProfileCount();
int i = 0, num = fInfo.GetProfileCount();
pkg_profile *prof;
BMenuItem *item = 0;
char sizeString[48];
BString name = "";
BMessage *message;
if (num > 0) { // Add the default item if (count > 0) {
prof = fInfo.GetProfile(0); // Add the default item
convert_size(prof->space_needed, sizeString, 48); pkg_profile* profile = fInfo.GetProfile(0);
char buffer[512]; BMenuItem* item = _AddInstallTypeMenuItem(profile->name,
snprintf(buffer, sizeof(buffer), "(%s)", sizeString); profile->space_needed, 0);
name << prof->name << buffer;
message = new BMessage(P_MSG_GROUP_CHANGED);
message->AddInt32("index", 0);
item = new BMenuItem(name.String(), message);
fInstallTypes->AddItem(item);
item->SetMarked(true); item->SetMarked(true);
fCurrentType = 0; fCurrentType = 0;
} }
for (i = 1; i < num; i++) { for (int i = 1; i < count; i++) {
prof = fInfo.GetProfile(i); pkg_profile* profile = fInfo.GetProfile(i);
if (prof) { if (profile != NULL) {
convert_size(prof->space_needed, sizeString, 48); _AddInstallTypeMenuItem(profile->name, profile->space_needed, i);
name = prof->name; } else
char buffer[512];
snprintf(buffer, sizeof(buffer), "(%s)", sizeString);
name << buffer;
message = new BMessage(P_MSG_GROUP_CHANGED);
message->AddInt32("index", i);
item = new BMenuItem(name.String(), message);
fInstallTypes->AddItem(item);
}
else
fInstallTypes->AddSeparatorItem(); fInstallTypes->AddSeparatorItem();
} }
} }
@@ -603,62 +580,37 @@ PackageView::_GroupChanged(int32 index)
if (index < 0) if (index < 0)
return B_ERROR; return B_ERROR;
BMenuItem *iter;
int32 i, num = fDestination->CountItems();
// Clear the choice list // Clear the choice list
for (i = 0;i < num;i++) { for (int32 i = fDestination->CountItems() - 1; i >= 0; i--) {
iter = fDestination->RemoveItem((int32)0); BMenuItem* item = fDestination->RemoveItem(i);
delete iter; delete item;
} }
fCurrentType = index; fCurrentType = index;
pkg_profile *prof = fInfo.GetProfile(index); pkg_profile* profile = fInfo.GetProfile(index);
BString test;
if (profile == NULL)
return B_ERROR;
fInstallDesc->SetText(profile->description.String());
if (prof) {
fInstallDesc->SetText(prof->description.String());
BMenuItem *item = 0;
BPath path; BPath path;
BMessage *temp;
BVolume volume; BVolume volume;
char buffer[512];
const char *tmp = B_TRANSLATE("(%s free)");
if (prof->path_type == P_INSTALL_PATH) {
dev_t device;
BString name;
char sizeString[48];
if (profile->path_type == P_INSTALL_PATH) {
BMenuItem* item = NULL;
if (find_directory(B_BEOS_APPS_DIRECTORY, &path) == B_OK) { if (find_directory(B_BEOS_APPS_DIRECTORY, &path) == B_OK) {
device = dev_for_path(path.Path()); dev_t device = dev_for_path(path.Path());
if (volume.SetTo(device) == B_OK && !volume.IsReadOnly()) { if (volume.SetTo(device) == B_OK && !volume.IsReadOnly()) {
temp = new BMessage(P_MSG_PATH_CHANGED); item = _AddDestinationMenuItem(path.Path(), volume.FreeBytes(),
temp->AddString("path", BString(path.Path())); path.Path());
convert_size(volume.FreeBytes(), sizeString, 48);
name = path.Path();
snprintf(buffer, sizeof(buffer), tmp, sizeString);
name << buffer;
item = new BMenuItem(name.String(), temp);
item->SetTarget(this);
fDestination->AddItem(item);
} }
} }
if (find_directory(B_APPS_DIRECTORY, &path) == B_OK) { if (find_directory(B_APPS_DIRECTORY, &path) == B_OK) {
device = dev_for_path(path.Path()); dev_t device = dev_for_path(path.Path());
if (volume.SetTo(device) == B_OK && !volume.IsReadOnly()) { if (volume.SetTo(device) == B_OK && !volume.IsReadOnly()) {
temp = new BMessage(P_MSG_PATH_CHANGED); item = _AddDestinationMenuItem(path.Path(),
temp->AddString("path", BString(path.Path())); volume.FreeBytes(), path.Path());
convert_size(volume.FreeBytes(), sizeString, 48);
char buffer[512];
snprintf(buffer, sizeof(buffer), tmp, sizeString);
name = path.Path();
name << buffer;
item = new BMenuItem(name.String(), temp);
item->SetTarget(this);
fDestination->AddItem(item);
} }
} }
@@ -668,20 +620,16 @@ PackageView::_GroupChanged(int32 index)
} }
fDestination->AddSeparatorItem(); fDestination->AddSeparatorItem();
item = new BMenuItem(B_TRANSLATE("Other" B_UTF8_ELLIPSIS), _AddMenuItem(B_TRANSLATE("Other" B_UTF8_ELLIPSIS),
new BMessage(P_MSG_OPEN_PANEL)); new BMessage(P_MSG_OPEN_PANEL), fDestination);
item->SetTarget(this);
fDestination->AddItem(item);
fDestField->SetEnabled(true); fDestField->SetEnabled(true);
} else if (prof->path_type == P_USER_PATH) { } else if (profile->path_type == P_USER_PATH) {
BString name;
bool defaultPathSet = false; bool defaultPathSet = false;
char sizeString[48], volumeName[B_FILE_NAME_LENGTH];
BVolumeRoster roster; BVolumeRoster roster;
BDirectory mountPoint;
while (roster.GetNextVolume(&volume) != B_BAD_VALUE) { while (roster.GetNextVolume(&volume) != B_BAD_VALUE) {
BDirectory mountPoint;
if (volume.IsReadOnly() || !volume.IsPersistent() if (volume.IsReadOnly() || !volume.IsPersistent()
|| volume.GetRootDirectory(&mountPoint) != B_OK) { || volume.GetRootDirectory(&mountPoint) != B_OK) {
continue; continue;
@@ -690,17 +638,11 @@ PackageView::_GroupChanged(int32 index)
if (path.SetTo(&mountPoint, NULL) != B_OK) if (path.SetTo(&mountPoint, NULL) != B_OK)
continue; continue;
temp = new BMessage(P_MSG_PATH_CHANGED); char volumeName[B_FILE_NAME_LENGTH];
temp->AddString("path", BString(path.Path()));
convert_size(volume.FreeBytes(), sizeString, 48);
volume.GetName(volumeName); volume.GetName(volumeName);
name = volumeName;
snprintf(buffer, sizeof(buffer), tmp, sizeString); BMenuItem* item = _AddDestinationMenuItem(volumeName,
name << buffer; volume.FreeBytes(), path.Path());
item = new BMenuItem(name.String(), temp);
item->SetTarget(this);
fDestination->AddItem(item);
// The first volume becomes the default element // The first volume becomes the default element
if (!defaultPathSet) { if (!defaultPathSet) {
@@ -713,8 +655,60 @@ PackageView::_GroupChanged(int32 index)
fDestField->SetEnabled(true); fDestField->SetEnabled(true);
} else } else
fDestField->SetEnabled(false); fDestField->SetEnabled(false);
}
return B_OK; return B_OK;
} }
BString
PackageView::_NamePlusSizeString(BString baseName, size_t size,
const char* format) const
{
char sizeString[48];
convert_size(size, sizeString, sizeof(sizeString));
BString name(format);
name.ReplaceAll("%name%", baseName);
name.ReplaceAll("%size%", sizeString);
return name;
}
BMenuItem*
PackageView::_AddInstallTypeMenuItem(BString baseName, size_t size,
int32 index) const
{
BString name = _NamePlusSizeString(baseName, size,
B_TRANSLATE("%name% (%size%)"));
BMessage* message = new BMessage(P_MSG_GROUP_CHANGED);
message->AddInt32("index", index);
return _AddMenuItem(name, message, fInstallTypes);
}
BMenuItem*
PackageView::_AddDestinationMenuItem(BString baseName, size_t size,
const char* path) const
{
BString name = _NamePlusSizeString(baseName, size,
B_TRANSLATE("%name% (%size% free)"));
BMessage* message = new BMessage(P_MSG_PATH_CHANGED);
message->AddString("path", path);
return _AddMenuItem(name, message, fDestination);
}
BMenuItem*
PackageView::_AddMenuItem(const char* name, BMessage* message,
BMenu* menu) const
{
BMenuItem* item = new BMenuItem(name, message);
item->SetTarget(this);
menu->AddItem(item);
return item;
}
+10
View File
@@ -18,6 +18,7 @@
class BBox; class BBox;
class BButton; class BButton;
class BFilePanel; class BFilePanel;
class BMenu;
class BMenuField; class BMenuField;
class BPopUpMenu; class BPopUpMenu;
class BTextView; class BTextView;
@@ -56,6 +57,15 @@ private:
status_t _GroupChanged(int32 index); status_t _GroupChanged(int32 index);
BString _NamePlusSizeString(BString name,
size_t size, const char* format) const;
BMenuItem* _AddInstallTypeMenuItem(BString baseName,
size_t size, int32 index) const;
BMenuItem* _AddDestinationMenuItem(BString baseName,
size_t size, const char* path) const;
BMenuItem* _AddMenuItem(const char* name,
BMessage* message, BMenu* menu) const;
private: private:
BPopUpMenu* fInstallTypes; BPopUpMenu* fInstallTypes;
BTextView* fInstallDesc; BTextView* fInstallDesc;