xref: /haiku/src/apps/haikudepot/util/LanguageMenuUtils.cpp (revision 5889cb5e7e8e7bfea6072ddfe881f55d364a0cf0)
1 /*
2  * Copyright 2019-2020, Andrew Lindesay <apl@lindesay.co.nz>.
3  * All rights reserved. Distributed under the terms of the MIT License.
4  */
5 #include "LanguageMenuUtils.h"
6 
7 #include <string.h>
8 
9 #include <Application.h>
10 #include <MenuItem.h>
11 #include <Messenger.h>
12 
13 #include "HaikuDepotConstants.h"
14 #include "Logger.h"
15 
16 
17 /*! This method will add the supplied languages to the menu.  It will
18     add first the popular languages, followed by a separator and then
19     other less popular languages.
20 */
21 
22 /* static */ void
23 LanguageMenuUtils::AddLanguagesToMenu(
24 	const LanguageModel* languagesModel, BMenu* menu)
25 {
26 	if (languagesModel->CountSupportedLanguages() == 0)
27 		HDINFO("there are no languages defined");
28 
29 	int32 addedPopular = LanguageMenuUtils::_AddLanguagesToMenu(
30 		languagesModel, menu, true);
31 
32 	if (addedPopular > 0)
33 		menu->AddSeparatorItem();
34 
35 	int32 addedNonPopular = LanguageMenuUtils::_AddLanguagesToMenu(
36 		languagesModel, menu, false);
37 
38 	HDDEBUG("did add %" B_PRId32 " popular languages and %" B_PRId32
39 		" non-popular languages to a menu", addedPopular,
40 		addedNonPopular);
41 }
42 
43 
44 /* static */ void
45 LanguageMenuUtils::MarkLanguageInMenu(
46 	const BString& languageCode, BMenu* menu) {
47 	if (menu->CountItems() == 0) {
48 		debugger("menu contains no items; not able to set the "
49 			"language");
50 		return;
51 	}
52 
53 	int32 index = LanguageMenuUtils::_IndexOfLanguageInMenu(
54 		languageCode, menu);
55 
56 	if (index == -1) {
57 		HDINFO("unable to find the language [%s] in the menu",
58 			languageCode.String());
59 		menu->ItemAt(0)->SetMarked(true);
60 	}
61 	else
62 		menu->ItemAt(index)->SetMarked(true);
63 }
64 
65 
66 /* static */ void
67 LanguageMenuUtils::_AddLanguageToMenu(
68 	const BString& code, const BString& name, BMenu* menu)
69 {
70 	BMessage* message = new BMessage(MSG_LANGUAGE_SELECTED);
71 	message->AddString("code", code);
72 	BMenuItem* item = new BMenuItem(name, message);
73 	menu->AddItem(item);
74 }
75 
76 
77 /* static */ void
78 LanguageMenuUtils::_AddLanguageToMenu(
79 	const LanguageRef& language, BMenu* menu)
80 {
81 	BString name;
82 	if (language->GetName(name) != B_OK || name.IsEmpty())
83 		name.SetTo("???");
84 	LanguageMenuUtils::_AddLanguageToMenu(language->Code(), name, menu);
85 }
86 
87 
88 /* static */ int32
89 LanguageMenuUtils::_AddLanguagesToMenu(const LanguageModel* languageModel,
90 	BMenu* menu, bool isPopular)
91 {
92 	int32 count = 0;
93 
94 	for (int32 i = 0; i < languageModel->CountSupportedLanguages(); i++) {
95 		const LanguageRef language = languageModel->SupportedLanguageAt(i);
96 
97 		if (language->IsPopular() == isPopular) {
98 			LanguageMenuUtils::_AddLanguageToMenu(language, menu);
99 			count++;
100 		}
101 	}
102 
103 	return count;
104 }
105 
106 
107 /* static */ status_t
108 LanguageMenuUtils::_GetLanguageAtIndexInMenu(BMenu* menu, int32 index,
109 	BString* result)
110 {
111 	BMessage *itemMessage = menu->ItemAt(index)->Message();
112 
113 	if (itemMessage == NULL)
114 		return B_ERROR;
115 
116 	return itemMessage->FindString("code", result);
117 }
118 
119 
120 /* static */ int32
121 LanguageMenuUtils::_IndexOfLanguageInMenu(
122 	const BString& languageCode, BMenu* menu)
123 {
124 	BString itemLanguageCode;
125 	for (int32 i = 0; i < menu->CountItems(); i++) {
126 		if (_GetLanguageAtIndexInMenu(
127 			menu, i, &itemLanguageCode) == B_OK) {
128 			if (itemLanguageCode == languageCode) {
129 				return i;
130 			}
131 		}
132 	}
133 
134 	return -1;
135 }