1 /* 2 * Copyright 2018-2024, Andrew Lindesay <apl@lindesay.co.nz>. 3 * All rights reserved. Distributed under the terms of the MIT License. 4 */ 5 6 7 #include "AppUtils.h" 8 9 #include <string.h> 10 11 #include <AppFileInfo.h> 12 #include <Application.h> 13 #include <MenuItem.h> 14 #include <Roster.h> 15 #include <String.h> 16 17 #include "HaikuDepotConstants.h" 18 #include "Logger.h" 19 20 21 /*! This method can be called to pop up an error in the user interface; 22 typically in a background thread. 23 */ 24 25 /*static*/ void 26 AppUtils::NotifySimpleError(const char* title, const char* text, 27 alert_type type) 28 { 29 BMessage message(MSG_ALERT_SIMPLE_ERROR); 30 31 if (title != NULL && strlen(title) != 0) 32 message.AddString(KEY_ALERT_TITLE, title); 33 34 if (text != NULL && strlen(text) != 0) 35 message.AddString(KEY_ALERT_TEXT, text); 36 37 message.AddInt32(KEY_ALERT_TYPE, static_cast<int>(type)); 38 39 be_app->PostMessage(&message); 40 } 41 42 43 /*static*/ status_t 44 AppUtils::MarkItemWithKeyValueInMenuOrFirst(BMenu* menu, const BString& key, const BString& value) 45 { 46 status_t result = AppUtils::MarkItemWithKeyValueInMenu(menu, key, value); 47 if (result != B_OK && menu->CountItems() > 0) 48 menu->ItemAt(0)->SetMarked(true); 49 return result; 50 } 51 52 53 /*static*/ status_t 54 AppUtils::MarkItemWithKeyValueInMenu(BMenu* menu, const BString& key, const BString& value) 55 { 56 if (menu->CountItems() == 0) 57 HDFATAL("menu contains no items; not able to mark the item"); 58 59 int32 index = AppUtils::IndexOfKeyValueInMenu(menu, key, value); 60 61 if (index == -1) { 62 HDINFO("unable to find the menu item with [%s] = [%s]", key.String(), value.String()); 63 return B_ERROR; 64 } 65 66 menu->ItemAt(index)->SetMarked(true); 67 return B_OK; 68 } 69 70 71 /*static*/ int32 72 AppUtils::IndexOfKeyValueInMenu(BMenu* menu, const BString& key, const BString& value) 73 { 74 BString itemCode; 75 for (int32 i = 0; i < menu->CountItems(); i++) { 76 if (AppUtils::GetValueForKeyAtIndexInMenu(menu, i, key, &itemCode) == B_OK) { 77 if (itemCode == value) 78 return i; 79 } 80 } 81 82 return -1; 83 } 84 85 86 /*static*/ status_t 87 AppUtils::GetValueForKeyAtIndexInMenu(BMenu* menu, int32 index, const BString& key, BString* result) 88 { 89 BMessage *itemMessage = menu->ItemAt(index)->Message(); 90 if (itemMessage == NULL) 91 return B_ERROR; 92 return itemMessage->FindString(key, result); 93 } 94 95 96 /*static*/ status_t 97 AppUtils::GetAppVersionString(BString& result) 98 { 99 app_info info; 100 101 if (be_app->GetAppInfo(&info) != B_OK) { 102 HDERROR("Unable to get the application info"); 103 return B_ERROR; 104 } 105 106 BFile file(&info.ref, B_READ_ONLY); 107 108 if (file.InitCheck() != B_OK) { 109 HDERROR("Unable to access the application info file"); 110 return B_ERROR; 111 } 112 113 BAppFileInfo appFileInfo(&file); 114 version_info versionInfo; 115 116 if (appFileInfo.GetVersionInfo( 117 &versionInfo, B_APP_VERSION_KIND) != B_OK) { 118 HDERROR("Unable to establish the application version"); 119 return B_ERROR; 120 } 121 122 result.SetToFormat("%" B_PRId32 ".%" B_PRId32 ".%" B_PRId32, 123 versionInfo.major, versionInfo.middle, versionInfo.minor); 124 125 return B_OK; 126 } 127