xref: /haiku/src/apps/haikudepot/packagemodel/Language.cpp (revision eea5774f46bba925156498abf9cb1a1165647bf7)
1 /*
2  * Copyright 2013-2014, Stephan Aßmus <superstippi@gmx.de>.
3  * Copyright 2013, Rene Gollent <rene@gollent.com>.
4  * Copyright 2016-2024, Andrew Lindesay <apl@lindesay.co.nz>.
5  * All rights reserved. Distributed under the terms of the MIT License.
6  */
7 
8 
9 #include "Language.h"
10 
11 
12 #include "Logger.h"
13 #include "StringUtils.h"
14 
15 
16 /*! serverName is the name of the language on the server.
17  */
18 
19 Language::Language(const char* language, const BString& serverName,
20 	bool isPopular)
21 	:
22 	BLanguage(language),
23 	fServerName(serverName),
24 	fIsPopular(isPopular)
25 {
26 }
27 
28 
29 Language::Language(const Language& other)
30 	:
31 	BLanguage(other.ID()),
32 	fServerName(other.fServerName),
33 	fIsPopular(other.fIsPopular)
34 {
35 }
36 
37 
38 status_t
39 Language::GetName(BString& name, const BLanguage* displayLanguage) const
40 {
41 	status_t result = BLanguage::GetName(name, displayLanguage);
42 
43 	if (result == B_OK && (name.IsEmpty() || name == Code()))
44 		name.SetTo(fServerName);
45 
46 	return result;
47 }
48 
49 
50 int
51 Language::Compare(const Language& other) const
52 {
53 	int result = StringUtils::NullSafeCompare(Code(), other.Code());
54 	if (0 == result)
55 		result = StringUtils::NullSafeCompare(CountryCode(), other.CountryCode());
56 	if (0 == result)
57 		result = StringUtils::NullSafeCompare(ScriptCode(), other.ScriptCode());
58 	return result;
59 }
60 
61 
62 bool
63 IsLanguageBefore(const LanguageRef& l1, const LanguageRef& l2)
64 {
65 	if (!l1.IsSet() || !l2.IsSet())
66 		HDFATAL("unexpected NULL reference in a referencable");
67 	return l1.Get()->Compare(*(l2.Get())) < 0;
68 }
69