~ubuntu-branches/ubuntu/oneiric/koffice/oneiric-updates

« back to all changes in this revision

Viewing changes to kplato/libs/kernel/kptschedulerplugin.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Alessandro Ghersi
  • Date: 2010-10-27 17:52:57 UTC
  • mfrom: (0.12.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20101027175257-s04zqqk5bs8ckm9o
Tags: 1:2.2.83-0ubuntu1
* Merge with Debian git remaining changes:
 - Add build-deps on librcps-dev, opengtl-dev, libqtgtl-dev, freetds-dev,
   create-resources, libspnav-dev
 - Remove needless build-dep on libwv2-dev
 - koffice-libs recommends create-resources
 - krita recommends pstoedit
 - Keep our patches
* New upstream release 2.3 beta 3
  - Remove debian/patches fixed by upstream
  - Update install files

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* This file is part of the KDE project
2
 
  Copyright (C) 2009 Dag Andersen <danders@get2net.dk>
 
2
  Copyright (C) 2009, 2010 Dag Andersen <danders@get2net.dk>
3
3
 
4
4
  This library is free software; you can redistribute it and/or
5
5
  modify it under the terms of the GNU Library General Public
21
21
 
22
22
#include "kptproject.h"
23
23
#include "kptschedule.h"
24
 
 
 
24
#include "kptxmlloaderobject.h"
 
25
 
 
26
#include "KoXmlReader.h"
 
27
 
 
28
#include <QDomDocument>
 
29
 
25
30
namespace KPlato
26
31
{
27
32
 
37
42
SchedulerPlugin::SchedulerPlugin(QObject *parent)
38
43
    : QObject(parent),
39
44
    d( new SchedulerPlugin::Private() )
40
 
{}
 
45
{
 
46
    m_synctimer.setInterval( 500 );
 
47
    connect(&m_synctimer, SIGNAL(timeout()), SLOT(slotSyncData()));
 
48
}
41
49
 
42
50
SchedulerPlugin::~SchedulerPlugin()
43
51
{
 
52
    foreach ( SchedulerThread *s, m_jobs ) {
 
53
        s->haltScheduling();
 
54
    }
44
55
    delete d;
45
56
}
46
57
 
64
75
    return d->comment;
65
76
}
66
77
 
 
78
void SchedulerPlugin::stopCalculation( ScheduleManager *sm )
 
79
{
 
80
    foreach ( SchedulerThread *j, m_jobs ) {
 
81
        if ( sm == j->mainManager() ) {
 
82
            j->stopScheduling();
 
83
        }
 
84
    }
 
85
}
 
86
 
 
87
void SchedulerPlugin::haltCalculation( ScheduleManager *sm )
 
88
{
 
89
    qDebug()<<"SchedulerPlugin::haltCalculation:"<<sm;
 
90
    foreach ( SchedulerThread *j, m_jobs ) {
 
91
        if ( sm == j->mainManager() ) {
 
92
            haltCalculation( j );
 
93
            break;
 
94
        }
 
95
    }
 
96
    sm->setCalculationResult( ScheduleManager::CalculationCanceled );
 
97
    sm->setScheduling( false );
 
98
}
 
99
 
 
100
void SchedulerPlugin::stopCalculation( SchedulerThread *job )
 
101
{
 
102
    job->stopScheduling();
 
103
}
 
104
 
 
105
void SchedulerPlugin::haltCalculation( SchedulerThread *job )
 
106
{
 
107
    qDebug()<<"SchedulerPlugin::haltCalculation:"<<job<<m_jobs.contains( job );
 
108
    disconnect(this, 0, job, 0 );
 
109
    job->haltScheduling();
 
110
    if ( m_jobs.contains( job ) ) {
 
111
        qDebug()<<"SchedulerPlugin::haltCalculation: remove"<<job;
 
112
        m_jobs.removeAt( m_jobs.indexOf( job ) );
 
113
    }
 
114
}
 
115
 
 
116
void SchedulerPlugin::slotSyncData()
 
117
{
 
118
    updateProgress();
 
119
    updateLog();
 
120
}
 
121
 
 
122
void SchedulerPlugin::updateProgress()
 
123
{
 
124
    foreach ( SchedulerThread *j, m_jobs ) {
 
125
        ScheduleManager *sm = j->mainManager();
 
126
        if ( sm->maxProgress() != j->maxProgress() ) {
 
127
            sm->setMaxProgress( j->maxProgress() );
 
128
        }
 
129
        sm->setProgress( j->progress() );
 
130
    }
 
131
}
 
132
 
 
133
void SchedulerPlugin::updateLog()
 
134
{
 
135
    foreach ( SchedulerThread *j, m_jobs ) {
 
136
        updateLog( j );
 
137
    }
 
138
}
 
139
 
 
140
void SchedulerPlugin::updateLog( SchedulerThread *j )
 
141
{
 
142
    ScheduleManager *sm = j->mainManager();
 
143
    Project *p = j->mainProject();
 
144
    Q_ASSERT( p == &(sm->project()) );
 
145
    QList<Schedule::Log> logs;
 
146
    //qDebug()<<"SchedulerPlugin::updateLog:"<<j<<logs.count();
 
147
    foreach ( const Schedule::Log log, j->log() ) {
 
148
        // map log from temporary project to real project
 
149
        Schedule::Log l = log;
 
150
        if ( l.resource ) {
 
151
            const Resource *r = l.resource;
 
152
            l.resource = sm->project().findResource( l.resource->id() );
 
153
//            qDebug()<<"SchedulerPlugin::updateLog: mapped"<<r<<l.resource;
 
154
            Q_ASSERT( r != l.resource );
 
155
            Q_ASSERT( l.resource->project() == p );
 
156
        }
 
157
        if ( l.node ) {
 
158
            const Node *n = l.node;
 
159
            if ( l.node->type() == Node::Type_Project ) {
 
160
                l.node = &( sm->project() );
 
161
            } else {
 
162
                l.node = sm->project().findNode( l.node->id() );
 
163
            }
 
164
//            qDebug()<<"SchedulerPlugin::updateLog: mapped"<<n<<l.node;
 
165
            Q_ASSERT( n != l.node );
 
166
            Q_ASSERT( l.node->projectNode() == p );
 
167
        }
 
168
        logs << l;
 
169
    }
 
170
    sm->slotAddLog( logs );
 
171
}
 
172
 
 
173
void SchedulerPlugin::updateProject( const Project *tp, const ScheduleManager *tm, Project *mp, ScheduleManager *sm ) const
 
174
{
 
175
    Q_CHECK_PTR( tp );
 
176
    Q_CHECK_PTR( tm );
 
177
    Q_CHECK_PTR( mp );
 
178
    Q_CHECK_PTR( sm );
 
179
    //qDebug()<<"SchedulerPlugin::updateProject:"<<tp<<tp->name()<<"->"<<mp<<mp->name()<<sm;
 
180
    Q_ASSERT( tp != mp && tm != sm );
 
181
    long sid = tm->scheduleId();
 
182
    Q_ASSERT( sid == sm->scheduleId() );
 
183
 
 
184
    XMLLoaderObject status;
 
185
    status.setVersion( KPLATO_FILE_SYNTAX_VERSION );
 
186
    status.setProject( mp );
 
187
    status.setProjectSpec( mp->timeSpec() );
 
188
 
 
189
    foreach ( const Node *tn, tp->allNodes() ) {
 
190
        Node *mn = mp->findNode( tn->id() );
 
191
        Q_ASSERT( mn );
 
192
        if ( mn ) {
 
193
            updateNode( tn, mn, sid, status );
 
194
        }
 
195
    }
 
196
    // update main schedule and appointments
 
197
    updateAppointments( tp, tm, mp, sm, status );
 
198
}
 
199
 
 
200
void SchedulerPlugin::updateNode( const Node *tn, Node *mn, long sid, XMLLoaderObject &status ) const
 
201
{
 
202
    //qDebug()<<"SchedulerPlugin::updateNode:"<<tn<<tn->name()<<"->"<<mn<<mn->name();
 
203
    NodeSchedule *s = static_cast<NodeSchedule*>( tn->schedule( sid ) );
 
204
    if ( s == 0 ) {
 
205
        kWarning()<<"SchedulerPlugin::updateNode:"<<"Task:"<<tn->name()<<"could not find schedule with id:"<<sid;
 
206
        return;
 
207
    }
 
208
    QDomDocument doc( "tmp" );
 
209
    QDomElement e = doc.createElement( "schedules" );
 
210
    doc.appendChild( e );
 
211
    s->saveXML( e );
 
212
 
 
213
    Q_ASSERT( ! mn->findSchedule( sid ) );
 
214
    s = static_cast<NodeSchedule*>( mn->schedule( sid ) );
 
215
    Q_ASSERT( s == 0 );
 
216
    s = new NodeSchedule();
 
217
 
 
218
    KoXmlDocument xd;
 
219
    xd.setContent( doc.toString() );
 
220
    KoXmlElement se = xd.documentElement().namedItem( "schedule" ).toElement();
 
221
    Q_ASSERT( ! se.isNull() );
 
222
 
 
223
    s->loadXML( se, status );
 
224
    s->setDeleted( false );
 
225
    s->setNode( mn );
 
226
    mn->addSchedule( s );
 
227
}
 
228
 
 
229
void SchedulerPlugin::updateAppointments( const Project *tp, const ScheduleManager *tm, Project *mp, ScheduleManager *sm, XMLLoaderObject &status ) const
 
230
{
 
231
    MainSchedule *sch = tm->expected();
 
232
    Q_ASSERT( sch );
 
233
    Q_ASSERT( sch != sm->expected() );
 
234
    Q_ASSERT( sch->id() == sm->expected()->id() );
 
235
 
 
236
    QDomDocument doc( "tmp" );
 
237
    QDomElement e = doc.createElement( "schedule" );
 
238
    doc.appendChild( e );
 
239
    sch->saveXML( e );
 
240
    tp->saveAppointments( e, sch->id() );
 
241
 
 
242
    KoXmlDocument xd;
 
243
    xd.setContent( doc.toString() );
 
244
    KoXmlElement se = xd.namedItem( "schedule" ).toElement();
 
245
    Q_ASSERT( ! se.isNull() );
 
246
 
 
247
    bool ret = sm->loadMainSchedule( sm->expected(), se, status ); // also loads appointments
 
248
    Q_ASSERT( ret );
 
249
    mp->setCurrentSchedule( sch->id() );
 
250
    sm->expected()->setPhaseNames( sch->phaseNames() );
 
251
    mp->changed( sm );
 
252
}
 
253
 
 
254
//----------------------
 
255
SchedulerThread::SchedulerThread( Project *project, ScheduleManager *manager, QObject *parent )
 
256
    : QThread( parent ),
 
257
    m_mainproject( project ),
 
258
    m_mainmanager( manager ),
 
259
    m_mainmanagerId( manager->managerId() ),
 
260
    m_project( 0 ),
 
261
    m_manager( 0 ),
 
262
    m_stopScheduling(false ),
 
263
    m_haltScheduling( false ),
 
264
    m_progress( 0 )
 
265
{
 
266
    KGlobal::ref(); // keep locale around
 
267
 
 
268
    manager->createSchedules(); // creates expected() to get log messages during calculation
 
269
 
 
270
    QDomDocument document( "kplato" );
 
271
    saveProject( project, document );
 
272
 
 
273
    m_pdoc.setContent( document.toString() );
 
274
 
 
275
    
 
276
    connect( this, SIGNAL(started()), this, SLOT(slotStarted()));
 
277
    connect( this, SIGNAL(finished()), this, SLOT(slotFinished()));
 
278
}
 
279
 
 
280
SchedulerThread::~SchedulerThread()
 
281
{
 
282
    qDebug()<<"SchedulerThread::~SchedulerThread:"<<QThread::currentThreadId();
 
283
    delete m_project;
 
284
    m_project = 0;
 
285
    KGlobal::deref();
 
286
}
 
287
 
 
288
void SchedulerThread::setMaxProgress( int value )
 
289
{
 
290
    m_maxprogressMutex.lock();
 
291
    m_maxprogress = value;
 
292
    m_maxprogressMutex.unlock();
 
293
    emit maxProgressChanged( value, m_mainmanager );
 
294
}
 
295
 
 
296
int SchedulerThread::maxProgress() const
 
297
{
 
298
    QMutexLocker m( &m_maxprogressMutex );
 
299
    return m_maxprogress;
 
300
}
 
301
 
 
302
void SchedulerThread::setProgress( int value )
 
303
{
 
304
    m_progressMutex.lock();
 
305
    m_progress = value;
 
306
    m_progressMutex.unlock();
 
307
    emit progressChanged( value, m_mainmanager );
 
308
}
 
309
 
 
310
int SchedulerThread::progress() const
 
311
{
 
312
    QMutexLocker m( &m_progressMutex );
 
313
    return m_progress;
 
314
}
 
315
 
 
316
void SchedulerThread::slotAddLog( KPlato::Schedule::Log log )
 
317
{
 
318
    //qDebug()<<"SchedulerThread::slotAddLog:"<<log;
 
319
    QMutexLocker m( &m_logMutex );
 
320
    m_logs << log;
 
321
}
 
322
 
 
323
QList<Schedule::Log> SchedulerThread::log()
 
324
{
 
325
    QMutexLocker m( &m_logMutex );
 
326
    QList<KPlato::Schedule::Log> l = m_logs;
 
327
    m_logs.clear();
 
328
    return l;
 
329
}
 
330
 
 
331
 
 
332
void SchedulerThread::slotStarted()
 
333
{
 
334
    emit jobStarted( this );
 
335
}
 
336
 
 
337
void SchedulerThread::slotFinished()
 
338
{
 
339
    if ( m_haltScheduling ) {
 
340
        deleteLater();
 
341
    } else {
 
342
        emit jobFinished( this );
 
343
    }
 
344
}
 
345
 
 
346
void SchedulerThread::doRun()
 
347
{
 
348
    slotStarted();
 
349
    run();
 
350
    slotFinished();
 
351
}
 
352
 
 
353
ScheduleManager *SchedulerThread::manager() const
 
354
{
 
355
    QMutexLocker m( &m_managerMutex );
 
356
    return m_manager;
 
357
}
 
358
 
 
359
Project *SchedulerThread::project() const
 
360
{
 
361
    QMutexLocker m( &m_projectMutex );
 
362
    return m_project;
 
363
}
 
364
 
 
365
void SchedulerThread::stopScheduling()
 
366
{
 
367
    qDebug()<<"SchedulerThread::stopScheduling:";
 
368
    m_stopScheduling = true;
 
369
}
 
370
 
 
371
void SchedulerThread::haltScheduling()
 
372
{
 
373
    qDebug()<<"SchedulerThread::haltScheduling:";
 
374
    m_haltScheduling = true;
 
375
}
 
376
 
 
377
//static
 
378
void SchedulerThread::saveProject( Project *project, QDomDocument &document )
 
379
{
 
380
    document.appendChild( document.createProcessingInstruction( "xml", "version=\"1.0\" encoding=\"UTF-8\"" ) );
 
381
 
 
382
    QDomElement doc = document.createElement( "kplato" );
 
383
    doc.setAttribute( "editor", "KPlato" );
 
384
    doc.setAttribute( "mime", "application/x-vnd.kde.kplato" );
 
385
    doc.setAttribute( "version", KPLATO_FILE_SYNTAX_VERSION );
 
386
    document.appendChild( doc );
 
387
    project->save( doc );
 
388
}
 
389
 
 
390
//static
 
391
bool SchedulerThread::loadProject( Project *project, const KoXmlDocument &doc )
 
392
{
 
393
    KoXmlElement pel = doc.documentElement().namedItem( "project" ).toElement();
 
394
    if ( pel.isNull() ) {
 
395
        return false;
 
396
    }
 
397
    XMLLoaderObject status;
 
398
    status.setVersion( KPLATO_FILE_SYNTAX_VERSION );
 
399
    status.setProject( project );
 
400
    return project->load( pel, status );
 
401
}
 
402
 
 
403
 
67
404
} //namespace KPlato
68
405