1 /* 2 * Copyright 2018-2021, 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 { 28 BMessage message(MSG_ALERT_SIMPLE_ERROR); 29 30 if (title != NULL && strlen(title) != 0) 31 message.AddString(KEY_ALERT_TITLE, title); 32 33 if (text != NULL && strlen(text) != 0) 34 message.AddString(KEY_ALERT_TEXT, text); 35 36 be_app->PostMessage(&message); 37 } 38 39 40 /*static*/ status_t 41 AppUtils::MarkItemWithCodeInMenuOrFirst(const BString& code, BMenu* menu) 42 { 43 status_t result = AppUtils::MarkItemWithCodeInMenu(code, menu); 44 if (result != B_OK) 45 menu->ItemAt(0)->SetMarked(true); 46 return result; 47 } 48 49 50 /*static*/ status_t 51 AppUtils::MarkItemWithCodeInMenu(const BString& code, BMenu* menu) 52 { 53 if (menu->CountItems() == 0) 54 HDFATAL("menu contains no items; not able to mark the item"); 55 56 int32 index = AppUtils::IndexOfCodeInMenu(code, menu); 57 58 if (index == -1) { 59 HDINFO("unable to find the menu item [%s]", code.String()); 60 return B_ERROR; 61 } 62 63 menu->ItemAt(index)->SetMarked(true); 64 return B_OK; 65 } 66 67 68 /*static*/ int32 69 AppUtils::IndexOfCodeInMenu(const BString& code, BMenu* menu) 70 { 71 BString itemCode; 72 for (int32 i = 0; i < menu->CountItems(); i++) { 73 if (AppUtils::GetCodeAtIndexInMenu(menu, i, &itemCode) == B_OK 74 && itemCode == code) { 75 return i; 76 } 77 } 78 79 return -1; 80 } 81 82 83 /*static*/ status_t 84 AppUtils::GetCodeAtIndexInMenu(BMenu* menu, int32 index, BString* result) 85 { 86 BMessage *itemMessage = menu->ItemAt(index)->Message(); 87 if (itemMessage == NULL) 88 return B_ERROR; 89 return itemMessage->FindString("code", result); 90 } 91 92 93 /*static*/ status_t 94 AppUtils::GetAppVersionString(BString& result) 95 { 96 app_info info; 97 98 if (be_app->GetAppInfo(&info) != B_OK) { 99 HDERROR("Unable to get the application info"); 100 return B_ERROR; 101 } 102 103 BFile file(&info.ref, B_READ_ONLY); 104 105 if (file.InitCheck() != B_OK) { 106 HDERROR("Unable to access the application info file"); 107 return B_ERROR; 108 } 109 110 BAppFileInfo appFileInfo(&file); 111 version_info versionInfo; 112 113 if (appFileInfo.GetVersionInfo( 114 &versionInfo, B_APP_VERSION_KIND) != B_OK) { 115 HDERROR("Unable to establish the application version"); 116 return B_ERROR; 117 } 118 119 result.SetToFormat("%" B_PRId32 ".%" B_PRId32 ".%" B_PRId32, 120 versionInfo.major, versionInfo.middle, versionInfo.minor); 121 122 return B_OK; 123 } 124