Utils.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /*
  2. * Copyright 2009-2011, Pier Luigi Fiorini. All rights reserved.
  3. * Copyright 2014, Funky Idea Software
  4. * Copyright 2021, Jaidyn Levesque
  5. * Distributed under the terms of the MIT License.
  6. */
  7. #include <memory.h>
  8. #include <Bitmap.h>
  9. #include <Catalog.h>
  10. #include <InterfaceDefs.h>
  11. #include <Directory.h>
  12. #include <FindDirectory.h>
  13. #include <IconUtils.h>
  14. #include <Menu.h>
  15. #include <MenuItem.h>
  16. #include <Path.h>
  17. #include <StringList.h>
  18. #include <kernel/fs_attr.h>
  19. #include "ChatOMatic.h"
  20. #include "Utils.h"
  21. #undef B_TRANSLATION_CONTEXT
  22. #define B_TRANSLATION_CONTEXT "Utils ― Status names"
  23. const char*
  24. UserStatusToString(UserStatus status)
  25. {
  26. switch (status) {
  27. case STATUS_ONLINE:
  28. return B_TRANSLATE("Available");
  29. case STATUS_AWAY:
  30. return B_TRANSLATE("Away");
  31. case STATUS_DO_NOT_DISTURB:
  32. return B_TRANSLATE("Busy");
  33. case STATUS_CUSTOM_STATUS:
  34. return B_TRANSLATE("Custom Status");
  35. case STATUS_INVISIBLE:
  36. return B_TRANSLATE("Invisible");
  37. case STATUS_OFFLINE:
  38. return B_TRANSLATE("Offline");
  39. default:
  40. return NULL;
  41. }
  42. }
  43. const char*
  44. UserStatusToImageKey(UserStatus status)
  45. {
  46. switch (status) {
  47. case STATUS_ONLINE:
  48. return "kOnlineReplicant";
  49. case STATUS_AWAY:
  50. return "kAwayReplicant";
  51. case STATUS_OFFLINE:
  52. return "kOfflineReplicant";
  53. default:
  54. return "kBusyReplicant";
  55. }
  56. }
  57. bool
  58. IsCommand(BString line)
  59. {
  60. return line.StartsWith("/");
  61. }
  62. BString
  63. CommandName(BString line)
  64. {
  65. BStringList words;
  66. line.Split(" ", true, words);
  67. return words.StringAt(0).RemoveFirst("/");
  68. }
  69. BString
  70. CommandArgs(BString line)
  71. {
  72. BString remove("/");
  73. remove << CommandName(line) << "";
  74. line.RemoveFirst(remove);
  75. return line.Trim();
  76. }
  77. BResources
  78. ChatResources()
  79. {
  80. BResources res;
  81. image_info info;
  82. if (our_image(info) == B_OK) {
  83. BFile file(info.name, B_READ_ONLY);
  84. if (file.InitCheck() == B_OK)
  85. res.SetTo(&file);
  86. }
  87. return res;
  88. }
  89. const char*
  90. SettingsPath()
  91. {
  92. BPath path;
  93. if (find_directory(B_USER_SETTINGS_DIRECTORY, &path) != B_OK)
  94. return NULL;
  95. path.Append(APP_NAME);
  96. if (create_directory(path.Path(), 0755) != B_OK)
  97. return NULL;
  98. return path.Path();
  99. }
  100. const char*
  101. AccountsPath()
  102. {
  103. BPath path(SettingsPath());
  104. if (path.InitCheck() != B_OK)
  105. return NULL;
  106. path.Append("Accounts");
  107. if (create_directory(path.Path(), 0755) != B_OK)
  108. return NULL;
  109. return path.Path();
  110. }
  111. const char*
  112. AccountPath(const char* signature, const char* subsignature)
  113. {
  114. BPath path(AccountsPath());
  115. if (path.InitCheck() != B_OK)
  116. return NULL;
  117. path.Append(signature);
  118. if (BString(signature) != BString(subsignature)
  119. && BString(subsignature).IsEmpty() == false)
  120. path.Append(subsignature);
  121. if (create_directory(path.Path(), 0755) != B_OK)
  122. return NULL;
  123. return path.Path();
  124. }
  125. const char*
  126. CachePath()
  127. {
  128. BPath path(SettingsPath());
  129. if (path.InitCheck() != B_OK)
  130. return NULL;
  131. path.Append("Cache");
  132. if (create_directory(path.Path(), 0755) != B_OK)
  133. return NULL;
  134. return path.Path();
  135. }
  136. const char*
  137. AccountCachePath(const char* accountName)
  138. {
  139. BPath path(CachePath());
  140. path.Append("Accounts");
  141. if (path.InitCheck() != B_OK)
  142. return NULL;
  143. path.Append(accountName);
  144. if (create_directory(path.Path(), 0755) != B_OK)
  145. return NULL;
  146. return path.Path();
  147. }
  148. const char*
  149. RoomsCachePath(const char* accountName)
  150. {
  151. BPath path(AccountCachePath(accountName));
  152. if (path.InitCheck() != B_OK)
  153. return NULL;
  154. path.Append("Rooms");
  155. if (create_directory(path.Path(), 0755) != B_OK)
  156. return NULL;
  157. return path.Path();
  158. }
  159. const char*
  160. RoomCachePath(const char* accountName, const char* roomIdentifier)
  161. {
  162. BPath path(RoomsCachePath(accountName));
  163. if (path.InitCheck() != B_OK)
  164. return NULL;
  165. path.Append(roomIdentifier);
  166. return path.Path();
  167. }
  168. const char*
  169. UserCachePath(const char* accountName, const char* userIdentifier)
  170. {
  171. BPath path(AccountCachePath(accountName));
  172. if (path.InitCheck() != B_OK)
  173. return NULL;
  174. path.Append("Users");
  175. if (create_directory(path.Path(), 0755) != B_OK)
  176. return NULL;
  177. path.Append(userIdentifier);
  178. return path.Path();
  179. }
  180. const char*
  181. ContactCachePath(const char* accountName, const char* userIdentifier)
  182. {
  183. BPath path(AccountCachePath(accountName));
  184. if (path.InitCheck() != B_OK)
  185. return NULL;
  186. path.Append("Contacts");
  187. if (create_directory(path.Path(), 0755) != B_OK)
  188. return NULL;
  189. path.Append(userIdentifier);
  190. return path.Path();
  191. }
  192. rgb_color
  193. TintColor(rgb_color color, int severity)
  194. {
  195. bool dark = false;
  196. if (color.Brightness() < 127)
  197. dark = true;
  198. switch (severity)
  199. {
  200. case 1:
  201. if (dark == true)
  202. return tint_color(color, B_LIGHTEN_1_TINT + 0.2f);
  203. else
  204. return tint_color(color, B_DARKEN_1_TINT);
  205. case 2:
  206. if (dark == true)
  207. return tint_color(color, B_LIGHTEN_1_TINT);
  208. else
  209. return tint_color(color, B_DARKEN_2_TINT);
  210. case 3:
  211. if (dark == true)
  212. return tint_color(color, B_LIGHTEN_2_TINT + 0.1f);
  213. else
  214. return tint_color(color, B_DARKEN_3_TINT);
  215. }
  216. return color;
  217. }
  218. rgb_color
  219. ForegroundColor(rgb_color background)
  220. {
  221. rgb_color foreground;
  222. int32 brighter;
  223. int32 darker;
  224. float ratio;
  225. do {
  226. foreground.set_to(rand() % 255, rand() % 255, rand() %255);
  227. if (foreground.Brightness() > background.Brightness()) {
  228. brighter = foreground.Brightness();
  229. darker = background.Brightness();
  230. }
  231. else {
  232. brighter = background.Brightness();
  233. darker = foreground.Brightness();
  234. }
  235. ratio = (brighter + .05) / (darker + .05);
  236. }
  237. while (ratio > 5 || ratio < 4);
  238. return foreground;
  239. }
  240. status_t
  241. ReadAttributeData(BNode* node, const char* name, char** buffer, int32 *size) {
  242. attr_info info;
  243. status_t ret = node->GetAttrInfo(name, &info);
  244. if (ret == B_OK) {
  245. *buffer = (char *)calloc(info.size, sizeof(char));
  246. ret = node->ReadAttr(name, info.type, 0, *buffer, info.size);
  247. if (ret > B_OK) {
  248. *size = ret;
  249. ret = B_OK;
  250. }
  251. else
  252. free(*buffer);
  253. }
  254. return ret;
  255. }
  256. status_t
  257. WriteAttributeMessage(BNode* node, const char* name, BMessage* data)
  258. {
  259. BMallocIO malloc;
  260. status_t ret=data->Flatten(&malloc);
  261. if( ret == B_OK) {
  262. ret = node->WriteAttr(name,B_ANY_TYPE,0,malloc.Buffer(),malloc.BufferLength());
  263. if(ret > B_OK)
  264. ret=B_OK;
  265. }
  266. return ret;
  267. }
  268. status_t
  269. ReadAttributeMessage(BNode* node, const char* name, BMessage* data)
  270. {
  271. char *buffer = NULL;
  272. int32 size = 0;
  273. status_t ret = ReadAttributeData(node,name,&buffer,&size);
  274. if(size>0 && buffer!=NULL) {
  275. BMemoryIO mem(buffer,size);
  276. ret = data->Unflatten(&mem);
  277. free(buffer);
  278. }
  279. return ret;
  280. }
  281. extern "C" {
  282. status_t
  283. our_image(image_info& image)
  284. {
  285. team_id team = B_CURRENT_TEAM;
  286. int32 cookie = 0;
  287. while (get_next_image_info(team, &cookie, &image) == B_OK) {
  288. if ((char*)our_image >= (char*)image.text
  289. && (char*)our_image <= (char*)image.text + image.text_size)
  290. return B_OK;
  291. }
  292. return B_ERROR;
  293. }
  294. }