summaryrefslogtreecommitdiffstats
path: root/kbugbuster/backend/bugsystem.cpp
blob: 9d9624357194a6075701258aa098e7d8db8b6ccd (plain)
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436

#include "bugsystem.h"
#include "packagelistjob.h"
#include "buglistjob.h"
#include "bugmybugsjob.h"
#include "bugdetailsjob.h"
#include "bugcommand.h"

#include <kstaticdeleter.h>
#include <kdebug.h>
#include <tdelocale.h>
#include <tdeemailsettings.h>
#include <kstandarddirs.h>
#include <ksimpleconfig.h>
#include <tdeconfig.h>

#include "packageimpl.h"
#include "bugimpl.h"
#include "bugdetailsimpl.h"
#include "mailsender.h"
#include "kbbprefs.h"
#include "bugserver.h"
#include "bugserverconfig.h"
#include "bugcache.h"

KStaticDeleter<BugSystem> bssd;

BugSystem *BugSystem::s_self = 0;

TQString BugSystem::mLastResponse;

BugSystem *BugSystem::self()
{
    if ( !s_self )
        s_self = bssd.setObject( s_self, new BugSystem );

    return s_self;
}

BugSystem::BugSystem()
    : m_disconnected( false )
{
    mServer = 0;
}

BugSystem::~BugSystem()
{
    TQValueList<BugServer *>::ConstIterator it;
    for( it = mServerList.begin(); it != mServerList.end(); ++it ) {
        delete *it;
    }
}

BugCache *BugSystem::cache()const
{
  return mServer->cache();
}

void BugSystem::setDisconnected( bool disconnected )
{
    m_disconnected = disconnected;
}

bool BugSystem::disconnected() const
{
    return m_disconnected;
}

void BugSystem::retrievePackageList()
{
    mServer->setPackages( mServer->cache()->loadPackageList() );

    if( !mServer->packages().isEmpty() ) {
        emit packageListAvailable( mServer->packages() );
    } else {
        emit packageListCacheMiss();

        if ( !m_disconnected )
        {
            emit packageListLoading();

            PackageListJob *job = new PackageListJob( mServer );
            connect( job, TQT_SIGNAL( packageListAvailable( const Package::List & ) ),
                     this, TQT_SIGNAL( packageListAvailable( const Package::List & ) ) );
            connect( job, TQT_SIGNAL( packageListAvailable( const Package::List & ) ),
                     this, TQT_SLOT( setPackageList( const Package::List & ) ) );
            connect( job, TQT_SIGNAL( error( const TQString & ) ),
                     this, TQT_SIGNAL( loadingError( const TQString & ) ) );
            connectJob( job );

            registerJob( job );

            job->start();
        }
    }
}

void BugSystem::retrieveBugList( const Package &pkg, const TQString &component )
{
    kdDebug() << "BugSystem::retrieveBugList(): " << pkg.name() << endl;

    if ( pkg.isNull() )
        return;

    mServer->setBugs( pkg, component,
                      mServer->cache()->loadBugList( pkg, component,
                                                     m_disconnected ) );

    // Since the GUI stops showing the splash widget after this signal,
    // we should not emit anything on a cache miss...
    if( !mServer->bugs( pkg, component ).isEmpty() )
        emit bugListAvailable( pkg, component, mServer->bugs( pkg, component ) );
    else
    {
        emit bugListCacheMiss( pkg );

        if ( !m_disconnected )
        {
            kdDebug() << "BugSystem::retrieveBugList() starting job" << endl;
            emit bugListLoading( pkg, component );

            BugListJob *job = new BugListJob( mServer );
            connect( job, TQT_SIGNAL( bugListAvailable( const Package &, const TQString &, const Bug::List & ) ),
                     this, TQT_SIGNAL( bugListAvailable( const Package &, const TQString &, const Bug::List & ) ) );
            connect( job, TQT_SIGNAL( bugListAvailable( const Package &, const TQString &, const Bug::List & ) ),
                     this, TQT_SLOT( setBugList( const Package &, const TQString &, const Bug::List & ) ) );
            connect( job, TQT_SIGNAL( error( const TQString & ) ),
                     this, TQT_SIGNAL( loadingError( const TQString & ) ) );
            connectJob( job );

            registerJob( job );

            job->start( pkg, component );
        }
    }
}

void BugSystem::retrieveMyBugsList()
{
    kdDebug() << k_funcinfo << endl;

    if ( m_disconnected )
    {
        // This function is not cached for now
        emit bugListCacheMiss( i18n( "My Bugs" ) );
    }
    else
    {
        kdDebug() << k_funcinfo << "Starting job" << endl;

        emit bugListLoading( i18n( "Retrieving My Bugs list..." ) );

        BugMyBugsJob *job = new BugMyBugsJob( mServer );

        connect( job, TQT_SIGNAL( bugListAvailable( const TQString &, const Bug::List & ) ),
                 this, TQT_SIGNAL( bugListAvailable( const TQString &, const Bug::List & ) ) );
        connect( job, TQT_SIGNAL( error( const TQString & ) ),
                 this, TQT_SIGNAL( loadingError( const TQString & ) ) );
        connectJob( job );

        registerJob( job );

        job->start();
    }
}

void BugSystem::retrieveBugDetails( const Bug &bug )
{
    if ( bug.isNull() )
        return;

    kdDebug() << "BugSystem::retrieveBugDetails(): " << bug.number() << endl;

    mServer->setBugDetails( bug, mServer->cache()->loadBugDetails( bug ) );

    if ( !mServer->bugDetails( bug ).isNull() ) {
//        kdDebug() << "Found in cache." << endl;
        emit bugDetailsAvailable( bug, mServer->bugDetails( bug ) );
    } else {
//        kdDebug() << "Not found in cache." << endl;
        emit bugDetailsCacheMiss( bug );

        if ( !m_disconnected ) {
            emit bugDetailsLoading( bug );

            BugDetailsJob *job = new BugDetailsJob( mServer );
            connect( job, TQT_SIGNAL( bugDetailsAvailable( const Bug &, const BugDetails & ) ),
                     this, TQT_SIGNAL( bugDetailsAvailable( const Bug &, const BugDetails & ) ) );
            connect( job, TQT_SIGNAL( bugDetailsAvailable( const Bug &, const BugDetails & ) ),
                     this, TQT_SLOT( setBugDetails( const Bug &, const BugDetails & ) ) );
            connect( job, TQT_SIGNAL( error( const TQString & ) ),
                     this, TQT_SIGNAL( bugDetailsLoadingError() ) );
            connectJob( job );

            registerJob( job );

            job->start( bug );
        }
    }
}

void BugSystem::connectJob( BugJob *job )
{
    connect( job, TQT_SIGNAL( infoMessage( const TQString & ) ),
             this, TQT_SIGNAL( infoMessage( const TQString & ) ) );
    connect( job, TQT_SIGNAL( infoPercent( unsigned long ) ),
             this, TQT_SIGNAL( infoPercent( unsigned long ) ) );
    connect( job, TQT_SIGNAL( jobEnded( BugJob * ) ),
             TQT_SLOT( unregisterJob( BugJob * ) ) );
}

void BugSystem::setPackageList( const Package::List &pkgs )
{
    mServer->setPackages( pkgs );

    mServer->cache()->savePackageList( pkgs );
}

void BugSystem::setBugList( const Package &pkg, const TQString &component, const Bug::List &bugs )
{
    mServer->setBugs( pkg, component, bugs );
    mServer->cache()->saveBugList( pkg, component, bugs );
}

void BugSystem::setBugDetails( const Bug &bug, const BugDetails &details )
{
    mServer->setBugDetails( bug , details );

    mServer->cache()->saveBugDetails( bug, details );
}

Package::List BugSystem::packageList() const
{
    return mServer->packages();
}

Package BugSystem::package( const TQString &pkgname ) const
{
    Package::List::ConstIterator it;
    for( it = mServer->packages().begin(); it != mServer->packages().end(); ++it ) {
        if( pkgname == (*it).name() ) return (*it);
    }
    return Package();
}

Bug BugSystem::bug( const Package &pkg, const TQString &component, const TQString &number ) const
{
    Bug::List bugs = mServer->bugs( pkg, component );

    Bug::List::ConstIterator it;
    for( it = bugs.begin(); it != bugs.end(); ++it ) {
        if( number == (*it).number() ) return (*it);
    }
    return Bug();
}

void BugSystem::queueCommand( BugCommand *cmd )
{
    if ( mServer->queueCommand( cmd ) ) emit commandQueued( cmd );
}

void BugSystem::clearCommands( const TQString &bug )
{
    mServer->clearCommands( bug );

    emit commandCanceled( bug );
}

void BugSystem::clearCommands()
{
    TQStringList bugs = mServer->bugsWithCommands();

    TQStringList::ConstIterator it;
    for( it = bugs.begin(); it != bugs.end(); ++it ) {
        clearCommands( *it );
    }
}

void BugSystem::sendCommands()
{
    TQString recipient = KBBPrefs::instance()->mOverrideRecipient;
    bool sendBCC = KBBPrefs::instance()->mSendBCC;

    KEMailSettings emailSettings;
    TQString senderName = emailSettings.getSetting( KEMailSettings::RealName );
    TQString senderEmail = emailSettings.getSetting( KEMailSettings::EmailAddress );
    TQString smtpServer = emailSettings.getSetting( KEMailSettings::OutServer );

    MailSender::MailClient client = (MailSender::MailClient)KBBPrefs::instance()->mMailClient;

    // ### connect to signals
    MailSender *mailer = new MailSender( client, smtpServer );
    connect( mailer, TQT_SIGNAL( status( const TQString & ) ),
             TQT_SIGNAL( infoMessage( const TQString & ) ) );

    mServer->sendCommands( mailer, senderName, senderEmail, sendBCC, recipient );
}

void BugSystem::setServerList( const TQValueList<BugServerConfig> &servers )
{
    if ( servers.isEmpty() ) return;

    TQString currentServer;
    if ( mServer ) currentServer = mServer->serverConfig().name();
    else currentServer = KBBPrefs::instance()->mCurrentServer;

    killAllJobs();

    TQValueList<BugServer *>::ConstIterator serverIt;
    for( serverIt = mServerList.begin(); serverIt != mServerList.end();
         ++serverIt ) {
        delete *serverIt;
    }
    mServerList.clear();

    TQValueList<BugServerConfig>::ConstIterator cfgIt;
    for( cfgIt = servers.begin(); cfgIt != servers.end(); ++cfgIt ) {
        mServerList.append( new BugServer( *cfgIt ) );
    }

    setCurrentServer( currentServer );
}

TQValueList<BugServer *> BugSystem::serverList()
{
    return mServerList;
}

void BugSystem::setCurrentServer( const TQString &name )
{
    killAllJobs();

    BugServer *server = findServer( name );
    if ( server ) {
        mServer = server;
    } else {
        kdError() << "Server '" << name << "' not known." << endl;
        if ( mServerList.isEmpty() ) {
            kdError() << "Fatal error: server list empty." << endl;
        } else {
            mServer = mServerList.first();
        }
    }

    if ( mServer ) {
      KBBPrefs::instance()->mCurrentServer = mServer->serverConfig().name();
    }
}

BugServer *BugSystem::findServer( const TQString &name )
{
    TQValueList<BugServer *>::ConstIterator serverIt;
    for( serverIt = mServerList.begin(); serverIt != mServerList.end();
         ++serverIt ) {
        if ( (*serverIt)->serverConfig().name() == name ) return *serverIt;
    }
    return 0;
}

void BugSystem::saveQuery( const KURL &url )
{
  mLastResponse = "Query: " + url.url();
  mLastResponse += "\n\n";
}

void BugSystem::saveResponse( const TQByteArray &response )
{
  mLastResponse += response;
}

TQString BugSystem::lastResponse()
{
  return mLastResponse;
}

void BugSystem::readConfig( TDEConfig *config )
{
  config->setGroup("Servers");
  TQStringList servers = config->readListEntry( "Servers" );

  TQValueList<BugServerConfig> serverList;

  if ( servers.isEmpty() ) {
    serverList.append( BugServerConfig() );
  } else {
    TQStringList::ConstIterator it;
    for( it = servers.begin(); it != servers.end(); ++it ) {
      BugServerConfig cfg;
      cfg.readConfig( config, *it );
      serverList.append( cfg );
    }
  }

  setServerList( serverList );
}

void BugSystem::writeConfig( TDEConfig *config )
{
  TQValueList<BugServer *>::ConstIterator itServer;
  TQStringList servers;
  TQValueList<BugServer *> serverList = BugSystem::self()->serverList();
  for( itServer = serverList.begin(); itServer != serverList.end();
       ++itServer ) {
    BugServerConfig serverConfig = (*itServer)->serverConfig();
    servers.append( serverConfig.name() );
    serverConfig.writeConfig( config );
  }

  config->setGroup("Servers");
  config->writeEntry( "Servers", servers );
}

void BugSystem::registerJob( BugJob *job )
{
  mJobs.append( job );
}

void BugSystem::unregisterJob( BugJob *job )
{
  mJobs.removeRef( job );
}

void BugSystem::killAllJobs()
{
  BugJob *job;
  for( job = mJobs.first(); job; job = mJobs.next() ) {
    job->kill();
    unregisterJob( job );
  }
}

#include "bugsystem.moc"

/*
 * vim:sw=4:ts=4:et
 */