~ubuntu-branches/ubuntu/quantal/kde-runtime/quantal

« back to all changes in this revision

Viewing changes to nepomuk/services/storage/modelcopyjob.cpp

  • Committer: Package Import Robot
  • Author(s): Philip Muškovac
  • Date: 2012-06-03 21:50:00 UTC
  • mto: This revision was merged to the branch mainline in revision 21.
  • Revision ID: package-import@ubuntu.com-20120603215000-vn7oarsq0ynrydj5
Tags: upstream-4.8.80
Import upstream version 4.8.80

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 *
3
 
 * $Id: sourceheader 511311 2006-02-19 14:51:05Z trueg $
4
 
 *
5
 
 * This file is part of the Nepomuk KDE project.
6
 
 * Copyright (C) 2006-2007 Sebastian Trueg <trueg@kde.org>
7
 
 *
8
 
 * This program is free software; you can redistribute it and/or modify
9
 
 * it under the terms of the GNU General Public License as published by
10
 
 * the Free Software Foundation; either version 2 of the License, or
11
 
 * (at your option) any later version.
12
 
 * See the file "COPYING" for the exact licensing terms.
13
 
 */
14
 
 
15
 
#include "modelcopyjob.h"
16
 
 
17
 
#include <Soprano/StorageModel>
18
 
#include <Soprano/Backend>
19
 
#include <Soprano/Error/Error>
20
 
 
21
 
#include <KLocale>
22
 
#include <KDebug>
23
 
#include <kuiserverjobtracker.h>
24
 
 
25
 
#include <QtCore/QThread>
26
 
#include <QtCore/QTimer>
27
 
 
28
 
 
29
 
class Nepomuk::ModelCopyJob::Private : public QThread
30
 
{
31
 
public:
32
 
    void run();
33
 
 
34
 
    Soprano::Model* m_source;
35
 
    Soprano::Model* m_dest;
36
 
 
37
 
    bool m_allCopied;
38
 
    bool m_stopped;
39
 
 
40
 
    KUiServerJobTracker* m_jobTracker;
41
 
 
42
 
    ModelCopyJob* q;
43
 
};
44
 
 
45
 
 
46
 
void Nepomuk::ModelCopyJob::Private::run()
47
 
{
48
 
    m_stopped = false;
49
 
    unsigned long size = m_source->statementCount();
50
 
    unsigned long done = 0;
51
 
    kDebug() << "Need to copy" << size << "statements.";
52
 
 
53
 
    Soprano::StatementIterator it = m_source->listStatements();
54
 
 
55
 
    while ( !m_stopped ) {
56
 
        if ( it.next() ) {
57
 
            ++done;
58
 
 
59
 
            if ( m_dest->addStatement( *it ) != Soprano::Error::ErrorNone ) {
60
 
                kDebug() << m_dest->lastError();
61
 
                q->setErrorText( m_dest->lastError().message() );
62
 
                break;
63
 
            }
64
 
 
65
 
            // progress
66
 
            if ( size > 0 ) {
67
 
                // emitPercent does only emit a signal if the percent value actually changes
68
 
                q->emitPercent( done, size );
69
 
            }
70
 
        }
71
 
        else {
72
 
            if ( it.lastError() ) {
73
 
                q->setErrorText( it.lastError().message() );
74
 
            }
75
 
            break;
76
 
        }
77
 
    }
78
 
}
79
 
 
80
 
 
81
 
Nepomuk::ModelCopyJob::ModelCopyJob( Soprano::Model* source, Soprano::Model* dest, QObject* parent )
82
 
    : KJob( parent ),
83
 
      d( new Private() )
84
 
{
85
 
    kDebug();
86
 
 
87
 
    d->q = this;
88
 
    d->m_source = source;
89
 
    d->m_dest = dest;
90
 
 
91
 
    setCapabilities( KJob::Killable );
92
 
 
93
 
    d->m_jobTracker = new KUiServerJobTracker();
94
 
    d->m_jobTracker->registerJob( this );
95
 
 
96
 
    connect( d, SIGNAL( finished() ),
97
 
             this, SLOT( slotThreadFinished() ) );
98
 
}
99
 
 
100
 
 
101
 
Nepomuk::ModelCopyJob::~ModelCopyJob()
102
 
{
103
 
    if ( d->isRunning() ) {
104
 
        kill();
105
 
    }
106
 
 
107
 
    d->m_jobTracker->deleteLater();
108
 
}
109
 
 
110
 
 
111
 
Soprano::Model* Nepomuk::ModelCopyJob::source() const
112
 
{
113
 
    return d->m_source;
114
 
}
115
 
 
116
 
 
117
 
Soprano::Model* Nepomuk::ModelCopyJob::dest() const
118
 
{
119
 
    return d->m_dest;
120
 
}
121
 
 
122
 
 
123
 
void Nepomuk::ModelCopyJob::start()
124
 
{
125
 
    kDebug();
126
 
    emit description( this,
127
 
                      i18nc( "@title job", "Converting Nepomuk database" ),
128
 
                      qMakePair( i18n( "Old backend" ), qobject_cast<Soprano::StorageModel*>( d->m_source )->backend()->pluginName() ),
129
 
                      qMakePair( i18n( "New backend" ), qobject_cast<Soprano::StorageModel*>( d->m_dest )->backend()->pluginName() ) );
130
 
    d->start();
131
 
}
132
 
 
133
 
 
134
 
void Nepomuk::ModelCopyJob::slotThreadFinished()
135
 
{
136
 
    if ( !d->m_stopped ) {
137
 
        emitResult();
138
 
    }
139
 
}
140
 
 
141
 
 
142
 
bool Nepomuk::ModelCopyJob::doKill()
143
 
{
144
 
    if ( d->isRunning() ) {
145
 
        d->m_stopped = true;
146
 
        d->wait();
147
 
        return true;
148
 
    }
149
 
    else {
150
 
        return false;
151
 
    }
152
 
}
153
 
 
154
 
#include "modelcopyjob.moc"