PageRenderTime 50ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/FHM/screens/ShopCategoriesScreen.cpp

https://github.com/mytcg/GameCards
C++ | 466 lines | 425 code | 38 blank | 3 comment | 96 complexity | cdaf7a8dd4153871e0e9fe41be67ef5d MD5 | raw file
  1. #include <conprint.h>
  2. #include "ShopCategoriesScreen.h"
  3. #include "ShopProductsScreen.h"
  4. #include "AuctionListScreen.h"
  5. #include "AlbumLoadScreen.h"
  6. #include "../utils/Util.h"
  7. #include "DetailScreen.h"
  8. #include "../UI/Button.h"
  9. void ShopCategoriesScreen::refresh() {
  10. show();
  11. categories.clear();
  12. category.clear();
  13. if(mHttp.isOpen()){
  14. mHttp.close();
  15. }
  16. mHttp = HttpConnection(this);
  17. int res = -1;
  18. int urlLength = 100 + URLSIZE;
  19. char *url = new char[urlLength+1];
  20. memset(url,'\0',urlLength+1);
  21. switch(screenType) {
  22. case ST_SHOP:
  23. notice->setCaption("Checking for shop categories...");
  24. sprintf(url, "%s?productcategories=2", URL);
  25. lprintfln("%s", url);
  26. res = mHttp.create(url, HTTP_GET);
  27. break;
  28. case ST_AUCTIONS:
  29. notice->setCaption("Checking for auction categories...");
  30. sprintf(url, "%s?auctioncategories=1", URL);
  31. lprintfln("%s", url);
  32. res = mHttp.create(url, HTTP_GET);
  33. break;
  34. }
  35. delete [] url;
  36. url = NULL;
  37. if(res < 0) {
  38. } else {
  39. mHttp.setRequestHeader("AUTH_USER", feed->getUsername().c_str());
  40. mHttp.setRequestHeader("AUTH_PW", feed->getEncrypt().c_str());
  41. feed->addHttp();
  42. mHttp.finish();
  43. }
  44. }
  45. ShopCategoriesScreen::ShopCategoriesScreen(MainScreen *previous, Feed *feed, int screenType) : mHttp(this), screenType(screenType) {
  46. lprintfln("ShopCategoriesScreen::Memory Heap %d, Free Heap %d", heapTotalMemory(), heapFreeMemory());
  47. this->previous = previous;
  48. this->feed = feed;
  49. next = NULL;
  50. label = NULL;
  51. currentSelectedKey = NULL;
  52. currentKeyPosition = -1;
  53. mainLayout = Util::createMainLayout("", "Back", true);
  54. kinListBox = (KineticListBox*) mainLayout->getChildren()[0]->getChildren()[2];
  55. notice = (Label*) mainLayout->getChildren()[0]->getChildren()[1];
  56. notice->setDrawBackground(false);
  57. kinListBox->setHeight(kinListBox->getHeight() - 20);
  58. int res = -1;
  59. int urlLength = 100 + URLSIZE;
  60. char *url = new char[urlLength+1];
  61. memset(url,'\0',urlLength+1);
  62. switch(screenType) {
  63. case ST_SHOP:
  64. notice->setCaption("Checking for shop categories...");
  65. sprintf(url, "%s?productcategories=2", URL);
  66. lprintfln("%s", url);
  67. res = mHttp.create(url, HTTP_GET);
  68. break;
  69. case ST_AUCTIONS:
  70. notice->setCaption("Checking for auction categories...");
  71. sprintf(url, "%s?auctioncategories=1", URL);
  72. lprintfln("%s", url);
  73. res = mHttp.create(url, HTTP_GET);
  74. break;
  75. case ST_RANKING:
  76. case ST_FRIEND:
  77. notice->setCaption("Checking the latest rankings...");
  78. sprintf(url, "%s?leaders=1", URL);
  79. lprintfln("%s", url);
  80. res = mHttp.create(url, HTTP_GET);
  81. break;
  82. }
  83. delete [] url;
  84. url = NULL;
  85. if(res < 0) {
  86. drawList();
  87. notice->setCaption("Unable to connect, try again later...");
  88. } else {
  89. mHttp.setRequestHeader("AUTH_USER", feed->getUsername().c_str());
  90. mHttp.setRequestHeader("AUTH_PW", feed->getEncrypt().c_str());
  91. feed->addHttp();
  92. mHttp.finish();
  93. }
  94. this->setMain(mainLayout);
  95. moved = 0;
  96. }
  97. ShopCategoriesScreen::~ShopCategoriesScreen() {
  98. lprintfln("~ShopCategoriesScreen::Memory Heap %d, Free Heap %d", heapTotalMemory(), heapFreeMemory());
  99. clearListBox();
  100. kinListBox->clear();
  101. delete mainLayout;
  102. mainLayout = NULL;
  103. if (next != NULL) {
  104. delete next;
  105. feed->remHttp();
  106. next = NULL;
  107. }
  108. parentTag="";
  109. temp="";
  110. temp1="";
  111. error_msg="";
  112. }
  113. void ShopCategoriesScreen::clearListBox() {
  114. Vector<Widget*> tempWidgets;
  115. for (int i = 0; i < kinListBox->getChildren().size(); i++) {
  116. tempWidgets.add(kinListBox->getChildren()[i]);
  117. }
  118. kinListBox->clear();
  119. kinListBox->getChildren().clear();
  120. for (int j = 0; j < tempWidgets.size(); j++) {
  121. delete tempWidgets[j];
  122. tempWidgets[j] = NULL;
  123. }
  124. tempWidgets.clear();
  125. }
  126. void ShopCategoriesScreen::pointerPressEvent(MAPoint2d point)
  127. {
  128. locateItem(point);
  129. }
  130. void ShopCategoriesScreen::pointerMoveEvent(MAPoint2d point)
  131. {
  132. moved++;
  133. locateItem(point);
  134. }
  135. void ShopCategoriesScreen::pointerReleaseEvent(MAPoint2d point)
  136. {
  137. if (moved <= 8) {
  138. if (right) {
  139. keyPressEvent(MAK_SOFTRIGHT);
  140. } else if (left) {
  141. keyPressEvent(MAK_SOFTLEFT);
  142. } else if (list) {
  143. keyPressEvent(MAK_FIRE);
  144. }
  145. }
  146. moved=0;
  147. }
  148. void ShopCategoriesScreen::locateItem(MAPoint2d point)
  149. {
  150. list = false;
  151. left = false;
  152. right = false;
  153. Point p;
  154. p.set(point.x, point.y);
  155. for(int i = 0; i < (this->getMain()->getChildren()[0]->getChildren()[2]->getChildren()).size(); i++)
  156. {
  157. if(this->getMain()->getChildren()[0]->getChildren()[2]->getChildren()[i]->contains(p))
  158. {
  159. list = true;
  160. }
  161. }
  162. for(int i = 0; i < (this->getMain()->getChildren()[1]->getChildren()).size(); i++)
  163. {
  164. if(this->getMain()->getChildren()[1]->getChildren()[i]->contains(p))
  165. {
  166. if (i == 0) {
  167. moved=0;
  168. left = true;
  169. } else if (i == 2) {
  170. moved=0;
  171. right = true;
  172. }
  173. return;
  174. }
  175. }
  176. }
  177. void ShopCategoriesScreen::drawList() {
  178. empt = false;
  179. kinListBox->getChildren().clear();
  180. if (screenType == ST_AUCTIONS)
  181. {
  182. label = Util::createSubLabel("Create New Auction");
  183. label->addWidgetListener(this);
  184. kinListBox->add(label);
  185. }
  186. for(Vector<String>::iterator itr = category.begin(); itr != category.end(); itr++) {
  187. label = Util::createSubLabel(itr->c_str());
  188. label->addWidgetListener(this);
  189. kinListBox->add(label);
  190. }
  191. if (categories.size() > 1) {
  192. kinListBox->setSelectedIndex(0);
  193. } else if (categories.size() == 1) {
  194. kinListBox->setSelectedIndex(0);
  195. if ((screenType != ST_AUCTIONS)&&(screenType != ST_RANKING)&&(screenType != ST_FRIEND)) {
  196. keyPressEvent(MAK_FIRE);
  197. }
  198. } else {
  199. label = Util::createSubLabel("Empty");
  200. //label->addWidgetListener(this);
  201. empt = true;
  202. kinListBox->add(label);
  203. //kinListBox->setSelectedIndex(0);
  204. }
  205. }
  206. void ShopCategoriesScreen::selectionChanged(Widget *widget, bool selected) {
  207. if(selected) {
  208. ((Label *)widget)->setFont(Util::getDefaultSelected());
  209. } else {
  210. ((Label *)widget)->setFont(Util::getDefaultFont());
  211. }
  212. }
  213. void ShopCategoriesScreen::keyPressEvent(int keyCode) {
  214. int ind = kinListBox->getSelectedIndex();
  215. int max = kinListBox->getChildren().size();
  216. Widget *currentSoftKeys = mainLayout->getChildren()[mainLayout->getChildren().size() - 1];
  217. switch(keyCode) {
  218. case MAK_UP:
  219. if(currentSelectedKey!=NULL){
  220. currentSelectedKey->setSelected(false);
  221. currentSelectedKey = NULL;
  222. currentKeyPosition = -1;
  223. kinListBox->getChildren()[kinListBox->getChildren().size()-1]->setSelected(true);
  224. } else if (ind > 0) {
  225. kinListBox->setSelectedIndex(ind-1);
  226. }
  227. break;
  228. case MAK_DOWN:
  229. if (ind+1 < kinListBox->getChildren().size()) {
  230. kinListBox->setSelectedIndex(ind+1);
  231. } else {
  232. kinListBox->getChildren()[ind]->setSelected(false);
  233. for(int i = 0; i < currentSoftKeys->getChildren().size();i++){
  234. if(((Button *)currentSoftKeys->getChildren()[i])->isSelectable()){
  235. currentKeyPosition=i;
  236. currentSelectedKey= currentSoftKeys->getChildren()[i];
  237. currentSelectedKey->setSelected(true);
  238. break;
  239. }
  240. }
  241. }
  242. break;
  243. case MAK_BACK:
  244. case MAK_SOFTRIGHT:
  245. previous->show();
  246. break;
  247. case MAK_FIRE:
  248. if(currentSoftKeys->getChildren()[0]->isSelected()){
  249. keyPressEvent(MAK_SOFTLEFT);
  250. break;
  251. }else if(currentSoftKeys->getChildren()[2]->isSelected()){
  252. keyPressEvent(MAK_SOFTRIGHT);
  253. break;
  254. }
  255. case MAK_SOFTLEFT:
  256. orig = this;
  257. switch (screenType) {
  258. case ST_RANKING:
  259. if (!empt) {
  260. orig = this;
  261. String selectedCaption = ((Label*)kinListBox->getChildren()[kinListBox->getSelectedIndex()])->getCaption();
  262. String category = categories.find(selectedCaption)->second.c_str();
  263. if (next != NULL) {
  264. delete next;
  265. feed->remHttp();
  266. next = NULL;
  267. }
  268. next = new DetailScreen(this, feed, DetailScreen::RANKING, NULL, category, selectedCaption);
  269. next->show();
  270. }
  271. break;
  272. case ST_FRIEND:
  273. if (!empt) {
  274. orig = this;
  275. String selectedCaption = ((Label*)kinListBox->getChildren()[kinListBox->getSelectedIndex()])->getCaption();
  276. String category = categories.find(selectedCaption)->second.c_str();
  277. if (next != NULL) {
  278. delete next;
  279. feed->remHttp();
  280. next = NULL;
  281. }
  282. next = new DetailScreen(this, feed, DetailScreen::FRIEND, NULL, category, selectedCaption);
  283. next->show();
  284. }
  285. break;
  286. case ST_SHOP:
  287. if (!empt) {
  288. orig = this;
  289. String selectedCaption = ((Label*)kinListBox->getChildren()[kinListBox->getSelectedIndex()])->getCaption();
  290. String category = categories.find(selectedCaption)->second.c_str();
  291. if (next != NULL) {
  292. delete next;
  293. feed->remHttp();
  294. next = NULL;
  295. }
  296. next = new ShopProductsScreen(this, feed, category, false);
  297. next->show();
  298. }
  299. break;
  300. case ST_AUCTIONS:
  301. int i = kinListBox->getSelectedIndex();
  302. String selectedCaption = ((Label*)kinListBox->getChildren()[kinListBox->getSelectedIndex()])->getCaption();
  303. orig = this;
  304. if (!strcmp(selectedCaption.c_str(), "My Auctions")) {
  305. if (next != NULL) {
  306. delete next;
  307. feed->remHttp();
  308. next = NULL;
  309. }
  310. next = new AuctionListScreen(this, feed, AuctionListScreen::ST_USER);
  311. next->show();
  312. } else if (!strcmp(selectedCaption.c_str(), "Create New Auction")) {
  313. if (next != NULL) {
  314. delete next;
  315. feed->remHttp();
  316. next = NULL;
  317. }
  318. next = new AlbumLoadScreen(this, feed, AlbumLoadScreen::ST_AUCTION, NULL, true);
  319. next->show();
  320. } else if (!empt) {
  321. if (next != NULL) {
  322. delete next;
  323. feed->remHttp();
  324. next = NULL;
  325. }
  326. orig = this;
  327. String selectedCaption = ((Label*)kinListBox->getChildren()[i])->getCaption();
  328. String category = categories.find(selectedCaption)->second.c_str();
  329. next = new AuctionListScreen(this, feed, AuctionListScreen::ST_CATEGORY, category);
  330. next->show();
  331. }
  332. break;
  333. }
  334. break;
  335. case MAK_LEFT:
  336. if(currentSelectedKey!=NULL){
  337. if(currentKeyPosition > 0){
  338. currentKeyPosition = currentKeyPosition - 1;
  339. for(int i = currentKeyPosition; i >= 0;i--){
  340. if(((Button *)currentSoftKeys->getChildren()[i])->isSelectable()){
  341. currentSelectedKey->setSelected(false);
  342. currentKeyPosition=i;
  343. currentSelectedKey= currentSoftKeys->getChildren()[i];
  344. currentSelectedKey->setSelected(true);
  345. break;
  346. }
  347. }
  348. }
  349. }
  350. break;
  351. case MAK_RIGHT:
  352. if(currentSelectedKey!=NULL){
  353. if(currentKeyPosition+1 < currentSelectedKey->getParent()->getChildren().size()){
  354. currentKeyPosition = currentKeyPosition + 1;
  355. for(int i = currentKeyPosition; i < currentSoftKeys->getChildren().size();i++){
  356. if(((Button *)currentSoftKeys->getChildren()[i])->isSelectable()){
  357. currentSelectedKey->setSelected(false);
  358. currentKeyPosition=i;
  359. currentSelectedKey= currentSoftKeys->getChildren()[i];
  360. currentSelectedKey->setSelected(true);
  361. break;
  362. }
  363. }
  364. }
  365. }
  366. break;
  367. }
  368. }
  369. void ShopCategoriesScreen::httpFinished(MAUtil::HttpConnection* http, int result) {
  370. if (result == 200) {
  371. xmlConn = XmlConnection::XmlConnection();
  372. xmlConn.parse(http, this, this);
  373. } else {
  374. mHttp.close();
  375. feed->remHttp();
  376. drawList();
  377. notice->setCaption("Unable to connect, try again later...");
  378. }
  379. }
  380. void ShopCategoriesScreen::connReadFinished(Connection* conn, int result) {}
  381. void ShopCategoriesScreen::xcConnError(int code) {
  382. feed->remHttp();
  383. if (code == -6) {
  384. return;
  385. } else {
  386. //TODO handle error
  387. }
  388. }
  389. void ShopCategoriesScreen::mtxEncoding(const char* ) {
  390. }
  391. void ShopCategoriesScreen::mtxTagStart(const char* name, int len) {
  392. parentTag = name;
  393. }
  394. void ShopCategoriesScreen::mtxTagAttr(const char* attrName, const char* attrValue) {
  395. }
  396. void ShopCategoriesScreen::mtxTagData(const char* data, int len) {
  397. if (!strcmp(parentTag.c_str(), "cardcategories")) {
  398. categories.clear();
  399. category.clear();
  400. } else if(!strcmp(parentTag.c_str(), "albumname")) {
  401. temp1 = data;
  402. } else if(!strcmp(parentTag.c_str(), "albumid")) {
  403. temp = data;
  404. } else if(!strcmp(parentTag.c_str(), "error")) {
  405. error_msg = data;
  406. }
  407. }
  408. void ShopCategoriesScreen::mtxTagEnd(const char* name, int len) {
  409. if(!strcmp(name, "albumname")) {
  410. categories.insert(temp1, temp);
  411. category.add(temp1);
  412. temp1 = "";
  413. temp = "";
  414. } else if (!strcmp(name, "cardcategories")) {
  415. notice->setCaption("Choose a category.");
  416. drawList();
  417. } else if(!strcmp(name, "error")) {
  418. notice->setCaption(error_msg.c_str());
  419. } else {
  420. notice->setCaption("");
  421. }
  422. }
  423. void ShopCategoriesScreen::mtxParseError(int) {
  424. }
  425. void ShopCategoriesScreen::mtxEmptyTagEnd() {
  426. }
  427. void ShopCategoriesScreen::mtxTagStartEnd() {
  428. }