~ubuntu-branches/ubuntu/precise/linpsk/precise

« back to all changes in this revision

Viewing changes to src/processlogdata.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jerry Stueve
  • Date: 2010-08-21 20:00:00 UTC
  • mfrom: (4.1.3 sid)
  • Revision ID: james.westby@ubuntu.com-20100821200000-q5luegsye0b7tfs1
Tags: 1.1-1
* Move to alsa sound system (Closes: #575243)
* Move to qt4 packaging
* Add menu & desktop entries
* Update standards revision to 3.9.1
* Update to 3.0 (quilt) for patch management.
* Update to debhelper 7
* Correct spelling error in messages.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *   Copyright (C) 2005 by Volker Schroer   *
 
3
 *   dl1ksv@gmx.de   *
 
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 2 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 "processlogdata.h"
 
21
#include <QString>
 
22
#include <QLabel>
 
23
#include <QHostAddress>
 
24
#include <QEventLoop>
 
25
#include <QMessageBox>
 
26
#include <QMetaType>
 
27
 
 
28
ProcessLogData::ProcessLogData ( QObject *parent )
 
29
    : QThread ( parent )
 
30
{
 
31
  tcpSocket = 0;
 
32
  connectionEstablished = false;
 
33
  connectionError = false;
 
34
}
 
35
 
 
36
 
 
37
ProcessLogData::~ProcessLogData()
 
38
{
 
39
}
 
40
void ProcessLogData::saveQsoData ( QString s )
 
41
{
 
42
  qDebug ( "Request save" );
 
43
  requestType = Save;
 
44
  actionString = s;
 
45
  doAction();
 
46
}
 
47
void ProcessLogData::requestCallSign ( QLabel **r, QString s )
 
48
{
 
49
  qDebug ( "Request Callsign" );
 
50
  requestType = Request;
 
51
  actionString.clear();
 
52
  actionString.append ( "@@@@" );
 
53
  actionString.append ( s );
 
54
  actionString.append ( "\r\n" );
 
55
 
 
56
  for ( int i = 0; i < 6;i++ )
 
57
    results[i] = r[i];
 
58
  doAction();
 
59
}
 
60
void ProcessLogData::run()
 
61
{
 
62
  connectionEstablished = false;
 
63
  connectionError = false;
 
64
  qRegisterMetaType<QAbstractSocket::SocketError> ( "QAbstractSocket::SocketError" );
 
65
  tcpSocket = new QTcpSocket();
 
66
  connect ( tcpSocket, SIGNAL ( disconnected() ), this, SLOT ( connectionClosedbyHost() ) );
 
67
  connect ( tcpSocket, SIGNAL ( readyRead() ), this, SLOT ( readAnswer() ) );
 
68
  connect ( tcpSocket, SIGNAL ( connected() ), this, SLOT ( setConnected() ) );
 
69
  connect ( tcpSocket, SIGNAL ( error ( QAbstractSocket::SocketError ) ), this, SLOT ( setError ( QAbstractSocket::SocketError ) ) );
 
70
  tcpSocket->connectToHost ( QHostAddress::LocalHost, 8080, QIODevice::ReadWrite );
 
71
  exec();
 
72
}
 
73
void ProcessLogData::doAction()
 
74
{
 
75
  if ( !connectionEstablished )
 
76
    usleep ( 6000 );
 
77
  if ( !connectionEstablished && tcpSocket->state() != 3)
 
78
  {
 
79
    qDebug ( "Waiting for Socket timed out: %d",tcpSocket->state() );
 
80
    QMessageBox::StandardButton reply;
 
81
    reply = QMessageBox::question ( 0, "LinPSK", tr ( "Cannot connect to LinLogBook\nTry again later ?" ), QMessageBox::Yes | QMessageBox::No );
 
82
    if ( reply == QMessageBox::No )
 
83
      emit unabletoConnect();
 
84
  if ( tcpSocket != 0 )
 
85
    delete tcpSocket;
 
86
  tcpSocket = 0;
 
87
 
 
88
    exit();
 
89
    return;
 
90
  }
 
91
// if ( tcpSocket->state() != QAbstractSocket::ConnectedState )
 
92
//  usleep ( 500 );
 
93
 
 
94
  qDebug ( "SocketError %d", tcpSocket->state() );
 
95
  if ( tcpSocket->state() == QAbstractSocket::UnconnectedState )
 
96
  {
 
97
    QMessageBox::information ( 0, "LinPSK", tr ( "Cannot connect to LinLogBook" ) );
 
98
    return;
 
99
  }
 
100
  int n = tcpSocket->write ( actionString.toLatin1(), actionString.length() );
 
101
  qDebug ( "Written %d, to be written %d", n, actionString.length() );
 
102
  if ( n < 0 ) // Retry
 
103
  {
 
104
    usleep ( 100 );
 
105
    n = tcpSocket->write ( actionString.toLatin1(), actionString.length() );
 
106
    qDebug ( "Written %d, to be written %d", n, actionString.length() );
 
107
  }
 
108
  tcpSocket->flush();
 
109
}
 
110
void ProcessLogData::connectionClosedbyHost()
 
111
{
 
112
  connectionEstablished = false;
 
113
  if ( tcpSocket != 0 )
 
114
    delete tcpSocket;
 
115
  tcpSocket = 0;
 
116
  quit();
 
117
 
 
118
}
 
119
 
 
120
void ProcessLogData::readAnswer()
 
121
{
 
122
  qDebug ( "Read Answer" );
 
123
//Has to be improved, to get safer
 
124
  QString s;
 
125
  int i;
 
126
  for ( i = 0; i < 6; i++ )
 
127
  {
 
128
    while ( ! tcpSocket->canReadLine() )
 
129
      usleep ( 100 );
 
130
    s = QLatin1String ( tcpSocket->readLine() );
 
131
    s.remove ( QLatin1Char ( '\n' ) );
 
132
    results[i]->setText ( s );
 
133
    results[i]->show();
 
134
  }
 
135
  qDebug ( "%d Zeilen gelesen", i );
 
136
emit answerAvailable();
 
137
}
 
138
void ProcessLogData::setConnected()
 
139
{
 
140
  connectionEstablished = true;
 
141
}
 
142
void ProcessLogData::setError ( QAbstractSocket::SocketError  )
 
143
{
 
144
  connectionEstablished = false;
 
145
  connectionError = true;
 
146
  if ( tcpSocket != 0 )
 
147
    qDebug ( "SocketError %d", tcpSocket->state() );
 
148
}
 
149
 
 
150