~ubuntu-branches/ubuntu/breezy/kdemultimedia/breezy

« back to all changes in this revision

Viewing changes to libkcddb/client.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2005-03-24 04:48:58 UTC
  • mfrom: (1.2.1 upstream) (2.1.1 sarge)
  • Revision ID: james.westby@ubuntu.com-20050324044858-8ff88o9jxej6ii3d
Tags: 4:3.4.0-0ubuntu3
Add kubuntu_02_hide_arts_menu_entries.diff to hide artsbuilder and artscontrol k-menu entries

Show diffs side-by-side

added added

removed removed

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