1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
|
/*
Copyright (C) 2002 Rik Hemsley (rikkus) <rik@kde.org>
Copyright (C) 2002 Benjamin Meyer <ben-devel@meyerhome.net>
Copyright (C) 2003 Richard Lärkäng <nouseforaname@home.se>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
*/
#include "client.h"
#include "synccddbplookup.h"
#include "asynccddbplookup.h"
#include "synchttplookup.h"
#include "asynchttplookup.h"
#include "syncsmtpsubmit.h"
#include "asyncsmtpsubmit.h"
#include "synchttpsubmit.h"
#include "asynchttpsubmit.h"
#include "cache.h"
#include "lookup.h"
#include <kdebug.h>
namespace KCDDB
{
class Client::Private
{
public:
Private()
: block( true )
{}
Config config;
CDInfoList cdInfoList;
bool block;
};
Client::Client()
: QObject(),
cdInfoLookup(0),
cdInfoSubmit(0)
{
d = new Private;
d->config.readConfig();
}
Client::~Client()
{
delete d;
delete cdInfoLookup;
delete cdInfoSubmit;
}
Config &
Client::config() const
{
return d->config;
}
void
Client::setBlockingMode( bool enable )
{
d->block = enable;
}
bool
Client::blockingMode() const
{
return d->block;
}
CDInfoList
Client::lookupResponse() const
{
return d->cdInfoList;
}
CDInfo
Client::bestLookupResponse() const
{
CDInfo info;
uint maxrev = 0;
for ( CDInfoList::Iterator it = d->cdInfoList.begin();
it != d->cdInfoList.end(); ++it )
{
if ( ( *it ).revision >= maxrev )
{
info = *it;
maxrev = info.revision;
}
}
return info;
}
CDDB::Result
Client::lookup(const TrackOffsetList & trackOffsetList)
{
d->cdInfoList.clear();
QString cddbId = Lookup::trackOffsetListToId( trackOffsetList );
if ( cddbId.isNull() )
{
kdDebug(60010) << "Can't create cddbid from offset list" << endl;
return Lookup::NoRecordFound;
}
if ( Cache::Ignore != d->config.cachePolicy() )
{
d->cdInfoList = Cache::lookup( cddbId );
kdDebug(60010) << "Found " << d->cdInfoList.count() << " hit(s)" << endl;
if ( !d->cdInfoList.isEmpty() )
{
if ( !blockingMode() )
emit finished( Lookup::Success );
return CDDB::Success;
}
}
if ( Cache::Only == d->config.cachePolicy() )
{
kdDebug(60010) << "Only trying cache. Give up now." << endl;
if ( !blockingMode() )
emit finished( Lookup::NoRecordFound );
return CDDB::NoRecordFound;
}
CDDB::Result r;
Lookup::Transport t = ( Lookup::Transport )d->config.lookupTransport();
// just in case we have an info lookup hanging around, prevent mem leakage
delete cdInfoLookup;
if ( blockingMode() )
{
if( Lookup::CDDBP == t )
cdInfoLookup = new SyncCDDBPLookup();
else
cdInfoLookup = new SyncHTTPLookup();
r = cdInfoLookup->lookup( d->config.hostname(),
d->config.port(), trackOffsetList );
if ( CDDB::Success == r )
{
d->cdInfoList = cdInfoLookup->lookupResponse();
Cache::store( d->cdInfoList );
}
delete cdInfoLookup;
cdInfoLookup = 0L;
}
else
{
if( Lookup::CDDBP == t )
{
cdInfoLookup = new AsyncCDDBPLookup();
connect( static_cast<AsyncCDDBPLookup *>( cdInfoLookup ),
SIGNAL( finished( CDDB::Result ) ),
SLOT( slotFinished( CDDB::Result ) ) );
}
else
{
cdInfoLookup = new AsyncHTTPLookup();
connect( static_cast<AsyncHTTPLookup *>( cdInfoLookup ),
SIGNAL( finished( CDDB::Result ) ),
SLOT( slotFinished( CDDB::Result ) ) );
}
r = cdInfoLookup->lookup( d->config.hostname(),
d->config.port(), trackOffsetList );
if ( Lookup::Success != r )
{
delete cdInfoLookup;
cdInfoLookup = 0L;
}
}
return r;
}
void
Client::slotFinished( CDDB::Result r )
{
if ( cdInfoLookup && CDDB::Success == r )
{
d->cdInfoList = cdInfoLookup->lookupResponse();
Cache::store( d->cdInfoList );
}
else
d->cdInfoList.clear();
emit finished( r );
if ( cdInfoLookup ) // in case someone called lookup() while finished() was being processed, and deleted cdInfoLookup.
{
cdInfoLookup->deleteLater();
cdInfoLookup = 0L;
}
}
void
Client::slotSubmitFinished( CDDB::Result r )
{
emit finished( r );
cdInfoSubmit->deleteLater();
cdInfoSubmit=0L;
}
CDDB::Result
Client::submit(const CDInfo &cdInfo, const TrackOffsetList& offsetList)
{
// Check if it's valid
if (!cdInfo.isValid())
return CDDB::CannotSave;
uint last=0;
for (uint i=0; i < (offsetList.count()-2); i++)
{
if(last >= offsetList[i])
return CDDB::CannotSave;
last = offsetList[i];
}
//TODO Check that it is edited
// just in case we have a cdInfoSubmit, prevent memory leakage
delete cdInfoSubmit;
QString from = d->config.emailAddress();
switch (d->config.submitTransport())
{
case Submit::HTTP:
{
QString hostname = d->config.httpSubmitServer();
uint port = d->config.httpSubmitPort();
if ( blockingMode() )
cdInfoSubmit = new SyncHTTPSubmit(from, hostname, port);
else
{
cdInfoSubmit = new AsyncHTTPSubmit(from, hostname, port);
connect( static_cast<AsyncHTTPSubmit *>( cdInfoSubmit ),
SIGNAL(finished( CDDB::Result ) ),
SLOT( slotSubmitFinished( CDDB::Result ) ) );
}
break;
}
case Submit::SMTP:
{
QString hostname = d->config.smtpHostname();
uint port = d->config.smtpPort();
QString username = d->config.smtpUsername();
if ( blockingMode() )
cdInfoSubmit = new SyncSMTPSubmit( hostname, port, username, from, d->config.submitAddress() );
else
{
cdInfoSubmit = new AsyncSMTPSubmit( hostname, port, username, from, d->config.submitAddress() );
connect( static_cast<AsyncSMTPSubmit *>( cdInfoSubmit ),
SIGNAL( finished( CDDB::Result ) ),
SLOT( slotSubmitFinished( CDDB::Result ) ) );
}
break;
}
default:
kdDebug(60010) << k_funcinfo << "Unsupported transport: " << endl;
// << CDDB::transportToString(d->config.submitTransport()) << endl;
return CDDB::UnknownError;
break;
}
CDDB::Result r = cdInfoSubmit->submit( cdInfo, offsetList );
if ( blockingMode() )
{
delete cdInfoSubmit;
cdInfoSubmit = 0L;
}
return r;
}
}
#include "client.moc"
// vim:tabstop=2:shiftwidth=2:expandtab:cinoptions=(s,U1,m1
|