PageRenderTime 27ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/q4wine-0.121/src/q4wine-gui/widgets/appdb/appdbwidget.cpp

#
C++ | 418 lines | 345 code | 47 blank | 26 comment | 17 complexity | 6dd77df63c42aa2bfee65c651c4ed926 MD5 | raw file
Possible License(s): GPL-3.0
  1. /***************************************************************************
  2. * Copyright (C) 2008, 2009, 2010, 2011 by Malakhov Alexey *
  3. * brezerk@gmail.com *
  4. * *
  5. * This program is free software: you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License as published by *
  7. * the Free Software Foundation, either version 3 of the License, or *
  8. * (at your option) any later version. *
  9. * *
  10. * This program is distributed in the hope that it will be useful, *
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  13. * GNU General Public License for more details. *
  14. * *
  15. * You should have received a copy of the GNU General Public License *
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>. *
  17. * *
  18. ***************************************************************************/
  19. #include "appdbwidget.h"
  20. AppDBWidget::AppDBWidget(QWidget *parent) : QWidget(parent)
  21. {
  22. // Loading libq4wine-core.so
  23. #ifdef RELEASE
  24. libq4wine.setFileName("libq4wine-core");
  25. #else
  26. libq4wine.setFileName("../q4wine-lib/libq4wine-core");
  27. #endif
  28. if (!libq4wine.load()){
  29. libq4wine.load();
  30. }
  31. // Getting corelib calss pointer
  32. CoreLibClassPointer = (CoreLibPrototype *) libq4wine.resolve("createCoreLib");
  33. CoreLib.reset((corelib *)CoreLibClassPointer(true));
  34. //Init AppDB Core Classes
  35. xmlparser.reset(new XmlParser());
  36. httpcore.reset(new HttpCore());
  37. //Init delay timer
  38. timer.reset(new QTimer(this));
  39. this->createActions();
  40. std::auto_ptr<QToolBar> toolbar (new QToolBar());
  41. toolbar->addAction(appdbOpen.get());
  42. toolbar->addAction(appdbAppPage.get());
  43. toolbar->addSeparator();
  44. toolbar->addAction(appdbCat.get());
  45. toolbar->addSeparator();
  46. toolbar->addAction(appdbClear.get());
  47. toolbar->addAction(appdbClearSearch.get());
  48. searchField.reset (new QLineEdit(this));
  49. connect(searchField.get(), SIGNAL(returnPressed()), this, SLOT(appdbSearch_Click()));
  50. toolbar->addWidget(searchField.get());
  51. toolbar->addAction(appdbSearch.get());
  52. //Init custom widgets
  53. appdbHeader.reset(new AppDBHeaderWidget());
  54. connect(appdbHeader.get(), SIGNAL(itemTrigged(short int, QString, int, int, int)), this, SLOT(itemTrigged(short int, QString, int, int, int)));
  55. appdbScrollArea.reset(new AppDBScrollWidget());
  56. connect(appdbScrollArea.get(), SIGNAL(itemTrigged(short int, QString, int, int, int)), this, SLOT(itemTrigged(short int, QString, int, int, int)));
  57. //Add custom widgets to mail layout
  58. std::auto_ptr<QVBoxLayout> contentLayout(new QVBoxLayout(this));
  59. contentLayout->setMargin(0);
  60. contentLayout->setSpacing(0);
  61. contentLayout->addWidget(toolbar.release());
  62. contentLayout->addWidget(appdbHeader.get());
  63. contentLayout->addWidget(appdbScrollArea.get());
  64. this->setLayout(contentLayout.release());
  65. //Connect slots and signals
  66. connect(timer.get(), SIGNAL(timeout()), this, SLOT(timer_timeout()));
  67. connect(httpcore.get(), SIGNAL(pageReaded()), this, SLOT(httpcore_pageDownloaded()));
  68. connect(httpcore.get(), SIGNAL(requestError(QString)), this, SLOT(requestError(QString)));
  69. connect(httpcore.get(), SIGNAL(updateDataReadProgress(int, int)), this, SLOT(updateDataReadProgress(int, int)));
  70. connect(httpcore.get(), SIGNAL(stateChanged(int)), this, SLOT(stateChanged(int)));
  71. this->appdbHeader->addLabel(tr("Status: Ready"));
  72. timer->stop();
  73. return;
  74. }
  75. void AppDBWidget::itemTrigged(short int action, QString search, int val1, int val2, int val3){
  76. #ifdef DEBUG
  77. qDebug()<<"[ii] itemTrigged"<<action<<search<<val1<<val2<<val3;
  78. #endif
  79. //Set state variables
  80. this->action=action;
  81. switch (action){
  82. case 1:
  83. this->setCursor(QCursor(Qt::WaitCursor));
  84. appdbClear->setEnabled(false);
  85. appdbAppPage->setEnabled(false);
  86. this->appdbHeader->clear();
  87. this->appdbHeader->addLabel(tr("Status: Connecting to %1").arg(APPDB_HOSTNAME));
  88. this->search=search;
  89. this->page=0;
  90. this->appid=0;
  91. this->verid=0;
  92. this->testid=0;
  93. this->catid=0;
  94. this->appdbScrollArea->clear();
  95. if (searchField->text()!=search)
  96. searchField->setText(search);
  97. timer->start(1000);
  98. break;
  99. case 2:
  100. this->setCursor(QCursor(Qt::WaitCursor));
  101. appdbClear->setEnabled(false);
  102. appdbAppPage->setEnabled(false);
  103. this->appdbHeader->clear();
  104. this->appdbHeader->addLabel(tr("Status: Connecting to %1").arg(APPDB_HOSTNAME));
  105. this->search=search;
  106. this->page=val1;
  107. this->appid=0;
  108. this->verid=0;
  109. this->testid=0;
  110. this->catid=0;
  111. this->appdbScrollArea->clear();
  112. timer->start(1000);
  113. break;
  114. case 3:
  115. this->setCursor(QCursor(Qt::WaitCursor));
  116. appdbClear->setEnabled(false);
  117. appdbAppPage->setEnabled(false);
  118. this->appdbHeader->clear();
  119. this->appdbHeader->addLabel(tr("Status: Connecting to %1").arg(APPDB_HOSTNAME));
  120. this->search="";
  121. this->page=0;
  122. this->appid=val1;
  123. this->verid=0;
  124. this->testid=0;
  125. this->catid=0;
  126. this->appdbScrollArea->clear();
  127. timer->start(1000);
  128. break;
  129. case 4:
  130. if (CoreLib->getSetting("AppDB", "useSystemBrowser", false, 1)==1){
  131. QDesktopServices::openUrl(QUrl(QString("http://%1/objectManager.php?sClass=version&iId=%2").arg("appdb.winehq.org").arg(val2), QUrl::TolerantMode));
  132. } else {
  133. this->setCursor(QCursor(Qt::WaitCursor));
  134. appdbClear->setEnabled(false);
  135. appdbAppPage->setEnabled(false);
  136. this->appdbHeader->clear();
  137. this->appdbHeader->addLabel(tr("Status: Connecting to %1").arg(APPDB_HOSTNAME));
  138. this->search="";
  139. this->page=0;
  140. this->appid=val1;
  141. this->verid=val2;
  142. this->testid=val3;
  143. this->catid=0;
  144. this->appdbScrollArea->clear();
  145. timer->start(1000);
  146. }
  147. break;
  148. case 5:
  149. this->setCursor(QCursor(Qt::WaitCursor));
  150. appdbClear->setEnabled(false);
  151. appdbAppPage->setEnabled(false);
  152. this->appdbHeader->clear();
  153. this->appdbHeader->addLabel(tr("Status: Connecting to %1").arg(APPDB_HOSTNAME));
  154. this->search="";
  155. this->page=0;
  156. this->appid=0;
  157. this->verid=0;
  158. this->testid=0;
  159. this->catid=val1;
  160. this->appdbScrollArea->clear();
  161. timer->start(1000);
  162. break;
  163. case 6:
  164. if (search.contains(QRegExp("^http"))){
  165. QDesktopServices::openUrl(QUrl(search, QUrl::TolerantMode));
  166. } else {
  167. QDesktopServices::openUrl(QUrl(QString("http://%1").arg(search), QUrl::TolerantMode));
  168. }
  169. break;
  170. case 7:
  171. QDesktopServices::openUrl(QUrl(QString("http://%1/show_bug.cgi?id=%2").arg(BUGS_HOSTNAME).arg(val1), QUrl::TolerantMode));
  172. break;
  173. }
  174. return;
  175. }
  176. void AppDBWidget::httpcore_pageDownloaded(void){
  177. int ret=0;
  178. QList<WineAppDBInfo> applist;
  179. WineAppDBInfo appinfo;
  180. this->appdbHeader->clear();
  181. ret = xmlparser->parseIOSream(httpcore->getXMLReply());
  182. if (ret>0){
  183. this->showXmlError(ret);
  184. return;
  185. }
  186. switch (this->action){
  187. case 1:
  188. appdbHeader->createPagesList(xmlparser->getPageCount(), xmlparser->getPageCurrent(), this->search);
  189. applist = xmlparser->getAppSearchInfoList();
  190. for (int i=0; i<applist.count(); i++){
  191. this->appdbScrollArea->addSearchWidget(applist.at(i));
  192. }
  193. this->appdbScrollArea->insertStretch();
  194. appdbClear->setEnabled(true);
  195. break;
  196. case 2:
  197. appdbHeader->createPagesList(xmlparser->getPageCount(), xmlparser->getPageCurrent(), this->search);
  198. applist = xmlparser->getAppSearchInfoList();
  199. for (int i=0; i<applist.count(); i++){
  200. this->appdbScrollArea->addSearchWidget(applist.at(i));
  201. }
  202. this->appdbScrollArea->insertStretch();
  203. appdbClear->setEnabled(true);
  204. break;
  205. case 3:
  206. appinfo = xmlparser->getAppSearchInfo();
  207. appdbHeader->createCategoryList(appinfo.categorys);
  208. appdbHeader->insertStretch();
  209. this->appdbScrollArea->addSearchWidget(appinfo);
  210. this->appdbScrollArea->insertStretch();
  211. appdbClear->setEnabled(true);
  212. appdbAppPage->setEnabled(true);
  213. break;
  214. case 4:
  215. this->appdbHeader->createCategoryList(xmlparser->getAppSearchInfo().categorys);
  216. this->appdbHeader->addLabel(">");
  217. this->appdbHeader->addLink(xmlparser->getAppSearchInfo().name, true, 3, "", xmlparser->getAppSearchInfo().id);
  218. this->appdbHeader->addLabel(QString("- %1").arg(xmlparser->getAppSearchInfo().appver));
  219. this->appdbHeader->insertStretch();
  220. this->appdbScrollArea->addTestWidget(xmlparser->getAppSearchInfo());
  221. appdbClear->setEnabled(true);
  222. appdbAppPage->setEnabled(true);
  223. break;
  224. case 5:
  225. this->appdbHeader->createCategoryList(xmlparser->getCategorysList());
  226. this->appdbHeader->insertStretch();
  227. this->appdbScrollArea->addVersionFrame(xmlparser->getSubCategorysList(), tr("Subcategories"), 5);
  228. this->appdbScrollArea->addVersionFrame(xmlparser->getAppsList(), tr("Applications"), 3);
  229. this->appdbScrollArea->insertStretch();
  230. appdbClear->setEnabled(true);
  231. break;
  232. }
  233. this->setCursor(QCursor(Qt::ArrowCursor));
  234. return;
  235. }
  236. void AppDBWidget::showXmlError(int id){
  237. switch (id){
  238. case 1:
  239. this->appdbHeader->addLabel(tr("Error: can't read data from %1.").arg(APP_WEBSITTE));
  240. break;
  241. case 2:
  242. this->appdbHeader->addLabel(tr("Error: wrong or broken xml data. Try again later."));
  243. break;
  244. case 3:
  245. this->appdbHeader->addLabel(tr("Error: wrong or broken appdb xml version. Application needs to be updated?"));
  246. break;
  247. case 4:
  248. this->appdbHeader->addLabel(tr("Error: xml parse error."));
  249. break;
  250. case 6:
  251. this->appdbHeader->addLabel(tr("Error: unexpected error."));
  252. break;
  253. case 404:
  254. this->appdbHeader->addLabel(tr("Search: No matches found"));
  255. break;
  256. }
  257. this->setCursor(QCursor(Qt::ArrowCursor));
  258. return;
  259. }
  260. void AppDBWidget::createActions(void){
  261. appdbOpen.reset(new QAction(CoreLib->loadIcon("data/wineappdb.png"), tr("Open AppDB"), this));
  262. appdbOpen->setStatusTip(tr("Open Wine AppDB web site"));
  263. connect(appdbOpen.get(), SIGNAL(triggered()), this, SLOT(appdbOpen_Click()));
  264. appdbAppPage.reset(new QAction(CoreLib->loadIcon("data/wineappdb-app.png"), tr("Open App page"), this));
  265. appdbAppPage->setStatusTip(tr("Open current application page at Wine AppDB web site"));
  266. appdbAppPage->setEnabled(false);
  267. connect(appdbAppPage.get(), SIGNAL(triggered()), this, SLOT(appdbAppPage_Click()));
  268. appdbClear.reset(new QAction(CoreLib->loadIcon("data/clear-list.png"), tr("Clear results"),this));
  269. appdbClear->setStatusTip(tr("Clear results"));
  270. appdbClear->setEnabled(false);
  271. connect(appdbClear.get(), SIGNAL(triggered()), this, SLOT(appdbClear_Click()));
  272. appdbClearSearch.reset(new QAction(CoreLib->loadIcon("data/clear-ltr.png"), tr("Clear search field"),this));
  273. appdbClearSearch->setStatusTip(tr("Clear search field"));
  274. connect(appdbClearSearch.get(), SIGNAL(triggered()), this, SLOT(appdbClearSearch_Click()));
  275. appdbSearch.reset(new QAction(CoreLib->loadIcon("data/find.png"), tr("Search in appdb"),this));
  276. appdbSearch->setStatusTip(tr("Search in wine appdb"));
  277. connect(appdbSearch.get(), SIGNAL(triggered()), this, SLOT(appdbSearch_Click()));
  278. appdbCat.reset(new QAction(CoreLib->loadIcon("data/list.png"), tr("Appdb categoryes list"),this));
  279. appdbCat->setStatusTip(tr("View wine appdb categoryes list"));
  280. connect(appdbCat.get(), SIGNAL(triggered()), this, SLOT(appdbCat_Click()));
  281. return;
  282. }
  283. void AppDBWidget::requestError(QString error){
  284. this->appdbHeader->clear();
  285. this->appdbHeader->addLabel(tr("Error: %1").arg(error));
  286. return;
  287. }
  288. void AppDBWidget::updateDataReadProgress(int bytesRead, int totalBytes){
  289. this->appdbHeader->updateFirstLabelText(QString("Downloaded: %1 of %2 bytes").arg(bytesRead).arg(totalBytes));
  290. return;
  291. }
  292. void AppDBWidget::timer_timeout(void){
  293. switch (this->action){
  294. case 1:
  295. httpcore->getAppDBXMLPage(APPDB_HOSTNAME, APPDB_PORT, QString("/xmlexport/searchApp/%1/").arg(this->search));
  296. break;
  297. case 2:
  298. httpcore->getAppDBXMLPage(APPDB_HOSTNAME, APPDB_PORT, QString("/xmlexport/searchApp/%1/%2/").arg(this->search).arg(page));
  299. break;
  300. case 3:
  301. httpcore->getAppDBXMLPage(APPDB_HOSTNAME, APPDB_PORT, QString("/xmlexport/viewApp/%1/").arg(this->appid));
  302. break;
  303. case 4:
  304. httpcore->getAppDBXMLPage(APPDB_HOSTNAME, APPDB_PORT, QString("/xmlexport/viewTest/%1/%2/%3/").arg(this->appid).arg(this->verid).arg(this->testid));
  305. break;
  306. case 5:
  307. httpcore->getAppDBXMLPage(APPDB_HOSTNAME, APPDB_PORT, QString("/xmlexport/viewCategory/%1/").arg(this->catid));
  308. break;
  309. }
  310. timer->stop();
  311. }
  312. void AppDBWidget::stateChanged (int state){
  313. switch (state){
  314. case 2:
  315. this->appdbHeader->updateFirstLabelText(tr("Status: Connecting to %1").arg(APPDB_HOSTNAME));
  316. break;
  317. case 3:
  318. this->appdbHeader->updateFirstLabelText(tr("Status: Sending request to the server."));
  319. break;
  320. case 4:
  321. this->appdbHeader->updateFirstLabelText(tr("Status: Reading server's response."));
  322. break;
  323. case 6:
  324. this->appdbHeader->updateFirstLabelText(tr("Status: Closing down connection."));
  325. this->setCursor(QCursor(Qt::ArrowCursor));
  326. break;
  327. case 0:
  328. this->appdbHeader->updateFirstLabelText(tr("Status: Not connected."));
  329. this->setCursor(QCursor(Qt::ArrowCursor));
  330. break;
  331. }
  332. return;
  333. }
  334. void AppDBWidget::setFocus(void){
  335. this->searchField->setFocus(Qt::OtherFocusReason);
  336. return;
  337. }
  338. void AppDBWidget::appdbOpen_Click(void){
  339. QDesktopServices::openUrl(QUrl(QString("http://%1/").arg("appdb.winehq.org"), QUrl::TolerantMode));
  340. return;
  341. }
  342. void AppDBWidget::appdbAppPage_Click(void){
  343. if (this->verid==0){
  344. QDesktopServices::openUrl(QUrl(QString("http://%1/objectManager.php?sClass=application&iId=%2").arg("appdb.winehq.org").arg(this->appid), QUrl::TolerantMode));
  345. } else {
  346. QDesktopServices::openUrl(QUrl(QString("http://%1/objectManager.php?sClass=version&iId=%2").arg("appdb.winehq.org").arg(this->verid), QUrl::TolerantMode));
  347. }
  348. return;
  349. }
  350. void AppDBWidget::appdbClear_Click(void){
  351. appdbClear->setEnabled(false);
  352. appdbAppPage->setEnabled(false);
  353. searchField->clear();
  354. this->appdbHeader->clear();
  355. this->appdbHeader->addLabel(tr("Status: Ready"));
  356. this->appdbScrollArea->clear();
  357. return;
  358. }
  359. void AppDBWidget::appdbClearSearch_Click(void){
  360. searchField->clear();
  361. return;
  362. }
  363. void AppDBWidget::appdbSearch_Click(void){
  364. if (!searchField->text().isEmpty())
  365. this->itemTrigged(1, searchField->text());
  366. return;
  367. }
  368. void AppDBWidget::appdbCat_Click(void){
  369. this->itemTrigged(5, "", 0);
  370. return;
  371. }