/patchexchange.cpp

http://ewitool.googlecode.com/ · C++ · 266 lines · 187 code · 46 blank · 33 comment · 27 complexity · c7aaa83c08fa04f8ed90c47a99aa71bb MD5 · raw file

  1. /***************************************************************************
  2. * Copyright (C) 2008 by Steve Merrony *
  3. * steve@brahma *
  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, write to the *
  17. * Free Software Foundation, Inc., *
  18. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  19. ***************************************************************************/
  20. #include <iostream>
  21. using namespace std;
  22. #include <QMessageBox>
  23. #include <QSettings>
  24. #include <QString>
  25. #include <QUrl>
  26. #include "patchexchange.h"
  27. patchExchange::patchExchange( QWidget * parent) :QObject( parent )
  28. {
  29. owner = parent;
  30. http = new QHttp( this );
  31. html_arr = new QByteArray();
  32. html_buff = new QBuffer( html_arr );
  33. host_id = 0; ct_id = 0; vu_id = 0; dd_id = 0; ins_id = 0;
  34. qry_id = 0, details_id = 0; delete_id = 0; stats_id = 0;
  35. //connect( http, SIGNAL( done(bool) ), this, SLOT( httpDone( bool ) ) );
  36. connect( http, SIGNAL( requestFinished( int, bool )), this, SLOT( finished( int, bool ) ) );
  37. }
  38. patchExchange::~patchExchange()
  39. {
  40. }
  41. void patchExchange::testConnection( QString url ) {
  42. host_id = http->setHost( url );
  43. ct_id = http->get( "/EPX/epx.php?action=connectionTest", html_buff );
  44. }
  45. void patchExchange::testUser( QString url, QString userid, QString passwd ) {
  46. host_id = http->setHost( url );
  47. vu_id = http->get( "/EPX/epx.php?action=validUser&userid=" + userid + "&passwd=" + passwd, html_buff );
  48. }
  49. void patchExchange::getDropdowns( QString url ) {
  50. // Added the OS string in 0.7 to get some idea of what platform most users are on
  51. #ifdef Q_WS_X11
  52. const QString OS = "Linux";
  53. #endif
  54. #ifdef Q_WS_WIN
  55. const QString OS = "Windows";
  56. #endif
  57. #ifdef Q_WS_MAC
  58. const QString OS = "Mac";
  59. #endif
  60. host_id = http->setHost( url );
  61. dd_id = http->get( "/EPX/epx.php?action=dropdownData&OS=" + OS, html_buff );
  62. }
  63. void patchExchange::insertPatch(
  64. QString url,
  65. QString userid,
  66. QString passwd,
  67. QString patch_name,
  68. QString origin,
  69. QString patch_type,
  70. QString description,
  71. QString isprivate,
  72. QString tags,
  73. QString hex_patch ) {
  74. QString encoded;
  75. QList<QPair<QString, QString> > ql;
  76. QUrl tmp_url;
  77. ql.append( QPair<QString, QString>("action", "insertPatch"));
  78. ql.append( QPair<QString, QString>( "userid",userid ));
  79. ql.append( QPair<QString, QString>( "passwd",passwd ));
  80. ql.append( QPair<QString, QString>( "name",patch_name ));
  81. ql.append( QPair<QString, QString>( "origin",origin ));
  82. ql.append( QPair<QString, QString>( "type",patch_type ));
  83. ql.append( QPair<QString, QString>( "desc",description ));
  84. ql.append( QPair<QString, QString>( "private",isprivate ));
  85. ql.append( QPair<QString, QString>( "tags", tags));
  86. ql.append( QPair<QString, QString>( "hexpatch",hex_patch ));
  87. tmp_url.setQueryItems( ql );
  88. encoded = tmp_url.encodedQuery();
  89. host_id = http->setHost( url );
  90. ins_id = http->get( "/EPX/epx.php?" + encoded, html_buff );
  91. }
  92. void patchExchange::query( QString url,
  93. QString userid,
  94. QString passwd,
  95. QString ptype,
  96. QString since,
  97. QString contrib,
  98. QString origin,
  99. QString tags ) {
  100. QString encoded;
  101. QList<QPair<QString, QString> > ql;
  102. QUrl tmp_url;
  103. ql.append( QPair<QString, QString>("action", "query"));
  104. ql.append( QPair<QString, QString>( "userid",userid ));
  105. ql.append( QPair<QString, QString>( "passwd",passwd ));
  106. ql.append( QPair<QString, QString>( "type",ptype ));
  107. ql.append( QPair<QString, QString>( "since",since ));
  108. ql.append( QPair<QString, QString>( "contrib",contrib ));
  109. ql.append( QPair<QString, QString>( "origin",origin ));
  110. ql.append( QPair<QString, QString>( "tags",tags ));
  111. tmp_url.setQueryItems( ql );
  112. encoded = tmp_url.encodedQuery();
  113. host_id = http->setHost( url );
  114. qry_id = http->get( "/EPX/epx.php?" + encoded, html_buff );
  115. }
  116. void patchExchange::getDetails( QString url,
  117. QString userid,
  118. QString passwd,
  119. int patch_id ) {
  120. host_id = http->setHost( url );
  121. QString id_str;
  122. id_str.setNum( patch_id );
  123. details_id = http->get( "/EPX/epx.php?action=fetchPatch&userid=" + userid + "&passwd=" + passwd +
  124. "&id=" + id_str,
  125. html_buff );
  126. }
  127. void patchExchange::getStats( QString url ) {
  128. host_id = http->setHost( url );
  129. stats_id = http->get( "/EPX/epx.php?action=stats",
  130. html_buff );
  131. }
  132. void patchExchange::deletePatch( QString url,
  133. QString userid,
  134. QString passwd,
  135. int patch_id ) {
  136. host_id = http->setHost( url );
  137. QString id_str;
  138. id_str.setNum( patch_id );
  139. delete_id = http->get( "/EPX/epx.php?action=deletePatch&userid=" + userid + "&passwd=" + passwd +
  140. "&id=" + id_str,
  141. html_buff );
  142. }
  143. void patchExchange::finished( int id, bool error ) {
  144. //cout << "Request " << id << " finished" << endl;
  145. if ( id == host_id ) {}
  146. if ( id == ct_id ) {
  147. if ( !error )
  148. emit connectionState( requestStatus() );
  149. else
  150. emit connectionState( qPrintable( http->errorString() ) );
  151. }
  152. if ( id == vu_id ) {
  153. if (!error)
  154. emit loginState( requestStatus() );
  155. else
  156. emit loginState( qPrintable( http->errorString() ) );
  157. }
  158. if ( id == dd_id ) {
  159. if (!error)
  160. emit dropdownData( requestStatus().split( "\n" ) );
  161. //else
  162. // emit dropdownData( qPrintable( http->errorString() ) );
  163. }
  164. if (id == ins_id ) {
  165. emit insertResponse( requestStatus() );
  166. }
  167. if (id == delete_id ) {
  168. emit deleteResponse( requestStatus() );
  169. }
  170. if ( id == qry_id ) {
  171. if (!error)
  172. //cout << "Query response: " << qPrintable( requestStatus() ) << endl;
  173. emit queryResponse( requestStatus() );
  174. //else
  175. // emit dropdownData( qPrintable( http->errorString() ) );
  176. }
  177. if ( id == details_id ) {
  178. if (!error)
  179. //cout << "Query response: " << qPrintable( requestStatus() ) << endl;
  180. emit detailsResponse( requestStatus() );
  181. //else
  182. // emit dropdownData( qPrintable( http->errorString() ) );
  183. }
  184. if ( id == stats_id ) {
  185. emit statsResponse( requestStatus() );
  186. }
  187. }
  188. QString patchExchange::requestStatus() {
  189. QByteArray tmp;
  190. QString status;
  191. int sstart = html_arr->lastIndexOf("<body>") + 7;
  192. int sfinish = html_arr->lastIndexOf("</body>") - 2;
  193. tmp = html_arr->mid( sstart, sfinish - sstart );
  194. status = QString::fromUtf8( tmp.constData(), tmp.length()); // required so that UTF8 chars do not get mangled
  195. //cout << qPrintable( status );
  196. return status;
  197. }
  198. void patchExchange::exchangeClipboardResponse( QString response ) {
  199. // cout << "EPX Export response: " << qPrintable( response ) << endl;
  200. if (response.startsWith( "Resource id #" )) { // success
  201. QMessageBox::information( owner, "EWItool - Patch Exchanged",
  202. tr("Patch Succesfully sent to EWI Patch Exchange - Thank You") );
  203. QSettings settings( "EWItool", "EWItool" );
  204. QString url = settings.value( "PatchExchange/Server" ).toString();
  205. getStats( url );
  206. }
  207. else {
  208. // duplicate?
  209. if (response.contains( "duplicate key" )) {
  210. QMessageBox::warning( owner, "EWItool - Exchange Error",
  211. tr( "EPX Export Error\n\nThat patch is already in the Exchange" ) );
  212. }
  213. else { // some other error
  214. QMessageBox::warning( owner, "EWItool - Exchange Error",
  215. tr( "EPX Export Error\n\n" ) +
  216. response.mid( response.indexOf( "ERROR:" ) + 7,
  217. response.indexOf( " in <b>" ) - response.indexOf( "ERROR:" ) + 7) );
  218. }
  219. }
  220. }