Minor change again -- still learning git :)
[qcheapbastard.git] / qcheapbastard.cpp
blob82b920d9ba069763d098cb8c424933f4d5576732
1 #include "qcheapbastard.h"
3 #include <QtNetwork>
4 #include <qfile.h>
6 QCheapBastard::QCheapBastard( QWidget *p )
7 : QWidget (p) {
9 resize(400,600);
10 setWindowTitle( "QCheapBastard" );
11 queue = new QQueue<QStandardItem*>();
12 model = new QStandardItemModel();
14 view = new QListView( this );
15 view->setModel( model );
16 connect( view,SIGNAL( doubleClicked( const QModelIndex & ) ),
17 this, SLOT( viewDoubleClicked( QModelIndex ) ) );
19 QPushButton *pb = new QPushButton( "Update!" );
20 connect( pb, SIGNAL( clicked() ),this, SLOT( findDeals() ) );
22 QGridLayout *layout = new QGridLayout;
23 layout->addWidget( pb, 0, 0 );
24 layout->addWidget( view, 1, 0 );
25 setLayout(layout);
27 menu = new QMenu( this );
28 menu->addAction("&Quit", qApp, SLOT(quit()));
30 trayIcon = new QSystemTrayIcon( QIcon( "icon.png" ), this );
31 connect( trayIcon, SIGNAL( messageClicked() ),
32 this, SLOT( balloonClicked() ) );
33 connect( trayIcon, SIGNAL( activated( QSystemTrayIcon::ActivationReason ) ),
34 this, SLOT( activated( QSystemTrayIcon::ActivationReason ) ) );
35 trayIcon->setContextMenu( menu );
36 trayIcon->show();
38 timer = new QTimer(this);
39 connect( timer, SIGNAL( timeout() ), this, SLOT( findDeals() ) );
40 timer->start( 60000 );
43 void QCheapBastard::balloonClicked(){
44 qDebug() << "Trying to open browser to visit" << currentOffer->toolTip();
45 QDesktopServices::openUrl( QUrl( currentOffer->toolTip() ) );
47 popNextOffer();
50 void QCheapBastard::activated( QSystemTrayIcon::ActivationReason reason ) {
52 if ( reason == QSystemTrayIcon::DoubleClick )
53 this->setVisible( !this->isVisible() );
54 if ( reason == QSystemTrayIcon::Trigger )
55 popNextOffer();
58 void QCheapBastard::popNextOffer(){
60 if ( queue->isEmpty() ) {
61 trayIcon->showMessage( "", "", QSystemTrayIcon::Warning, 1 );
62 return;
65 currentOffer = queue->dequeue();
66 trayIcon->showMessage( "More free crap!", currentOffer->text(), QSystemTrayIcon::Information, 100000000 );
70 void QCheapBastard::viewDoubleClicked( QModelIndex idx ){
71 QDesktopServices::openUrl( QUrl( idx.data(Qt::ToolTipRole).toString() ) );
74 void QCheapBastard::findDeals(){
75 http = new QHttp( this );
76 file = new QFile( "get.html" );
78 connect( http, SIGNAL( requestFinished( int, bool ) )
79 ,this, SLOT( httpRequestFinished( int, bool ) ) );
81 if ( !file->open( QIODevice::WriteOnly ) ){
82 qDebug() << "file open broke!";
83 return;
85 http->setHost( "www.finn.no" );
86 getId = http->get( "/finn/bap/all/result?channelId=1&SEGMENT=1%3B2&userid=0&TYPE=1&areaId=20061&searchKey=SEARCH_ID_BAP_ALL&siteId=1&CATEGORY/MAINCATEGORY=93", file );
87 //getId = http->get( "/finn/bap/free/result?TYPE=2&SEGMENT=1&rows=10", file );
90 void QCheapBastard::httpRequestFinished( int id, bool error ){
92 if ( error )
93 qDebug() << "httpRequestFinished reports that an error occured";
95 if ( id == getId ) {
96 file->close();
97 parseResults();
101 void QCheapBastard::parseResults(){
103 if ( !file->open( QIODevice::ReadOnly ) ){
104 qDebug() << "file open broke in parser!";
105 return;
108 QByteArray htmlData = file->readAll();
110 delete file;
111 // delete http;
113 QRegExp rx( "<a href=\"(/finn/bap/object\\\?finnkode=.{0,45}tot=[\\d]{0,6})\">([^<].*)</a>" );
114 rx.setMinimal( TRUE );
115 rx.setPatternSyntax( QRegExp::RegExp2 );
117 QStringList newResults;
118 QList<QStandardItem*> newList;
119 int pos = 0;
120 while ( ( pos = rx.indexIn( htmlData, pos ) ) != -1 ) {
121 QStandardItem *item = new QStandardItem( rx.cap( 2 ) );
122 item->setToolTip( "http://www.finn.no" + rx.cap( 1 ) );
123 item->setEditable( FALSE );
124 newList << item;
125 pos += rx.matchedLength();
128 addResults( newList );
130 view->repaint();
133 void QCheapBastard::addResults(QList<QStandardItem *> newResults){
134 for( int i=0; i < newResults.size(); ++i ){
136 if ( model->findItems( newResults[i]->text(), Qt::MatchExactly ).isEmpty() ){
137 model->appendRow( newResults[i] );
138 queue->enqueue( newResults[i] );
141 popNextOffer();