PageRenderTime 56ms CodeModel.GetById 23ms app.highlight 29ms RepoModel.GetById 1ms app.codeStats 0ms

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