xref: /haiku/src/kits/locale/InitLocaleKit.cpp (revision 7a74a5df454197933bc6e80a542102362ee98703)
1 /*
2  * This file contains library initialization code.
3  * The required mimetypes and attribute-indices are created here.
4  */
5 
6 #include <syslog.h>
7 
8 #include <fs_attr.h>
9 #include <fs_index.h>
10 #include <Volume.h>
11 #include <VolumeRoster.h>
12 
13 #include <DefaultCatalog.h>
14 #include <MutableLocaleRoster.h>
15 #include <SystemCatalog.h>
16 
17 
18 namespace BPrivate {
19 
20 BCatalog gSystemCatalog;
21 
22 }
23 
24 
25 using BPrivate::DefaultCatalog;
26 using BPrivate::MutableLocaleRoster;
27 using BPrivate::gSystemCatalog;
28 
29 
30 // helper function that makes sure an attribute-index exists:
31 static void EnsureIndexExists(const char *attrName)
32 {
33 	BVolume bootVol;
34 	BVolumeRoster volRoster;
35 	if (volRoster.GetBootVolume(&bootVol) != B_OK)
36 		return;
37 	struct index_info idxInfo;
38 	if (fs_stat_index(bootVol.Device(), attrName, &idxInfo) != 0) {
39 		status_t res = fs_create_index(bootVol.Device(), attrName,
40 			B_STRING_TYPE, 0);
41 		if (res == 0) {
42 			log_team(LOG_INFO,
43 				"successfully created the required index for attribute %s",
44 				attrName);
45 		} else {
46 			log_team(LOG_ERR,
47 				"failed to create the required index for attribute %s (%s)",
48 				attrName, strerror(res));
49 		}
50 	}
51 }
52 
53 
54 /*
55  * prepares the system for use by the Locale Kit catalogs,
56  * it makes sure that the required indices and mimetype exist:
57  */
58 static void
59 SetupCatalogBasics()
60 {
61 	// make sure the indices required for catalog-traversal are there:
62 	EnsureIndexExists(BLocaleRoster::kCatLangAttr);
63 	EnsureIndexExists(BLocaleRoster::kCatSigAttr);
64 
65 	// install mimetype for default-catalog:
66 	BMimeType mt;
67 	status_t res = mt.SetTo(DefaultCatalog::kCatMimeType);
68 	if (res == B_OK && !mt.IsInstalled()) {
69 		// install supertype, if it isn't available
70 		BMimeType supertype;
71 		res = mt.GetSupertype(&supertype);
72 		if (res == B_OK && !supertype.IsInstalled()) {
73 			res = supertype.Install();
74 		}
75 
76 		if (res == B_OK) {
77 			// info about the attributes of a catalog...
78 			BMessage attrMsg;
79 			// ...the catalog signature...
80 			attrMsg.AddString("attr:public_name", "Signature");
81 			attrMsg.AddString("attr:name", BLocaleRoster::kCatSigAttr);
82 			attrMsg.AddInt32("attr:type", B_STRING_TYPE);
83 			attrMsg.AddBool("attr:editable", false);
84 			attrMsg.AddBool("attr:viewable", true);
85 			attrMsg.AddBool("attr:extra", false);
86 			attrMsg.AddInt32("attr:alignment", 0);
87 			attrMsg.AddInt32("attr:width", 140);
88 			// ...the catalog language...
89 			attrMsg.AddString("attr:public_name", "Language");
90 			attrMsg.AddString("attr:name", BLocaleRoster::kCatLangAttr);
91 			attrMsg.AddInt32("attr:type", B_STRING_TYPE);
92 			attrMsg.AddBool("attr:editable", false);
93 			attrMsg.AddBool("attr:viewable", true);
94 			attrMsg.AddBool("attr:extra", false);
95 			attrMsg.AddInt32("attr:alignment", 0);
96 			attrMsg.AddInt32("attr:width", 60);
97 			// ...and the catalog fingerprint...
98 			attrMsg.AddString("attr:public_name", "Fingerprint");
99 			attrMsg.AddString("attr:name", BLocaleRoster::kCatFingerprintAttr);
100 			attrMsg.AddInt32("attr:type", B_INT32_TYPE);
101 			attrMsg.AddBool("attr:editable", false);
102 			attrMsg.AddBool("attr:viewable", true);
103 			attrMsg.AddBool("attr:extra", false);
104 			attrMsg.AddInt32("attr:alignment", 0);
105 			attrMsg.AddInt32("attr:width", 70);
106 			res = mt.SetAttrInfo(&attrMsg);
107 		}
108 
109 		if (res == B_OK) {
110 			// file extensions (.catalog):
111 			BMessage extMsg;
112 			extMsg.AddString("extensions", "catalog");
113 			res = mt.SetFileExtensions(&extMsg);
114 		}
115 
116 		if (res == B_OK) {
117 			// short and long descriptions:
118 			mt.SetShortDescription("Translation Catalog");
119 			res = mt.SetLongDescription("Catalog with translated application resources");
120 		}
121 
122 		if (res == B_OK)
123 			res = mt.Install();
124 	}
125 	if (res != B_OK) {
126 		log_team(LOG_ERR, "Could not install mimetype %s (%s)",
127 			DefaultCatalog::kCatMimeType, strerror(res));
128 	}
129 }
130 
131 
132 void
133 __initialize_locale_kit()
134 {
135 	SetupCatalogBasics();
136 
137 	MutableLocaleRoster::Default()->LoadSystemCatalog(&gSystemCatalog);
138 }
139