summaryrefslogtreecommitdiffstats
path: root/kbugbuster/backend/bugcache.cpp
blob: a6ef25df2b7ea4b17feea73f2f1e584df8fda74c (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
// (C) 2001, Cornelius Schumacher

#include <tqstringlist.h>
#include <tqfile.h>

#include <ksimpleconfig.h>
#include <kstandarddirs.h>
#include <kurl.h>
#include <kdebug.h>

#include "packageimpl.h"
#include "bugimpl.h"
#include "bugdetailsimpl.h"

#include "bugcache.h"

BugCache::BugCache( const TQString &id )
{
    mId = id;

    init();
}

BugCache::~BugCache()
{
    m_cachePackages->sync();
    m_cacheBugs->sync();

    delete m_cachePackages;
    delete m_cacheBugs;
}

void BugCache::init()
{
    mCachePackagesFileName = locateLocal( "appdata", mId + "-packages.cache" );
    mCacheBugsFileName = locateLocal( "appdata", mId + "-bugs.cache" );

    m_cachePackages = new KSimpleConfig( mCachePackagesFileName );
    m_cacheBugs = new KSimpleConfig( mCacheBugsFileName );
}

void BugCache::savePackageList( const Package::List &pkgs )
{
    Package::List::ConstIterator it;
    for (it = pkgs.begin(); it != pkgs.end(); ++it) {
        m_cachePackages->setGroup((*it).name());
        m_cachePackages->writeEntry("description",(*it).description());
        m_cachePackages->writeEntry("numberOfBugs",(*it).numberOfBugs());
        m_cachePackages->writeEntry("components",(*it).components());
        writePerson(m_cachePackages,"Maintainer",(*it).maintainer());
    }
}

Package::List BugCache::loadPackageList()
{
    Package::List pkgs;

    TQStringList packages = m_cachePackages->groupList();
    TQStringList::ConstIterator it;
    for( it = packages.begin(); it != packages.end(); ++it ) {
        if ((*it) == "<default>") continue;
        if ((*it).contains("/")) continue;

        m_cachePackages->setGroup(*it);

        TQString description = m_cachePackages->readEntry("description");
        int numberOfBugs = m_cachePackages->readNumEntry("numberOfBugs");
        Person maintainer = readPerson( m_cachePackages, "Maintainer");
        TQStringList components = m_cachePackages->readListEntry("components");
 
        pkgs.append( Package( new PackageImpl( (*it), description, numberOfBugs,
                                                maintainer, components ) ) );
    }

    return pkgs;
}

void BugCache::invalidatePackageList()
{
    // Completely wipe out packages.cache
    TQStringList packages = m_cachePackages->groupList();
    TQStringList::ConstIterator it;
    for( it = packages.begin(); it != packages.end(); ++it ) {
        if ((*it) == "<default>") continue;
        m_cachePackages->deleteGroup(*it, true);
    }
}

void BugCache::saveBugList( const Package &pkg, const TQString &component, const Bug::List &bugs )
{
    TQStringList bugList;

    Bug::List::ConstIterator it;
    for( it = bugs.begin(); it != bugs.end(); ++it ) {
        TQString number = (*it).number();
        bugList.append( number );
        m_cacheBugs->setGroup( number );
        m_cacheBugs->writeEntry( "Title", (*it).title() );
        m_cacheBugs->writeEntry( "Severity", Bug::severityToString((*it).severity()) );
        m_cacheBugs->writeEntry( "Status", Bug::statusToString((*it).status()) );
        m_cacheBugs->writeEntry( "MergedWith" , (*it).mergedWith() );
        m_cacheBugs->writeEntry( "Age", ( *it ).age() );
        writePerson( m_cacheBugs, "Submitter", (*it).submitter() );
        writePerson( m_cacheBugs, "TODO", (*it).developerTODO() );
    }

    if ( component.isEmpty() )
        m_cachePackages->setGroup( pkg.name() );
    else {
        m_cachePackages->setGroup( pkg.name() + "/" + component );
    }

    m_cachePackages->writeEntry( "bugList", bugList );
}

Bug::List BugCache::loadBugList( const Package &pkg, const TQString &component, bool disconnected )
{
//    kdDebug() << "Loading bug list for " << pkg.name() << endl;

    Bug::List bugList;

    if ( component.isEmpty() )
        m_cachePackages->setGroup( pkg.name() );
    else
        m_cachePackages->setGroup( pkg.name() + "/" + component );

    TQStringList bugs = m_cachePackages->readListEntry( "bugList" );

//    kdDebug() << "  Bugs: " << (bugs.join(",")) << endl;

    TQStringList::ConstIterator it;
    for( it = bugs.begin(); it != bugs.end(); ++it ) {
        if ( m_cacheBugs->hasGroup(*it) )
        {
            m_cacheBugs->setGroup(*it);
            TQString title = m_cacheBugs->readEntry("Title");
            if ( !title.isEmpty() ) // dunno how I ended up with an all empty bug in the cache
            {
                Person submitter = readPerson( m_cacheBugs, "Submitter" );
                Bug::Status status = Bug::stringToStatus( m_cacheBugs->readEntry("Status") );
                Bug::Severity severity = Bug::stringToSeverity( m_cacheBugs->readEntry("Severity") );
                Person developerTODO = readPerson( m_cacheBugs, "TODO" );
                Bug::BugMergeList mergedWith = m_cacheBugs->readIntListEntry( "MergedWith" );
                uint age = m_cacheBugs->readUnsignedNumEntry( "Age", 0xFFFFFFFF );
                bugList.append( Bug( new BugImpl( title, submitter, ( *it ), age,
                                                  severity, developerTODO,
                                                  status, mergedWith ) ) );
            }
        } else {
            // This bug is in the package cache's buglist but not in the bugs cache
            // Probably a new bug, we need to fetch it - if we're not in disconnected mode
            kdWarning() << "Bug " << *it << " not in bug cache" << endl;
            if ( !disconnected )
                return Bug::List(); // returning an empty list will trigger a reload of the buglist
        }
    }

    return bugList;
}

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

    // Erase bug list for this package
    if ( component.isEmpty() ) {
        m_cachePackages->setGroup( pkg.name() );
    } else {
        TQString key = pkg.name() + "/" + component;
        m_cachePackages->setGroup( key );
        m_cachePackages->setGroup( pkg.name() + "/" + component );
    }

    m_cachePackages->writeEntry("bugList",TQString::null);
}

void BugCache::saveBugDetails( const Bug &bug, const BugDetails &details )
{
    m_cacheBugs->setGroup( bug.number() );

    m_cacheBugs->writeEntry( "Version", details.version() );
    m_cacheBugs->writeEntry( "Source", details.source() );
    m_cacheBugs->writeEntry( "Compiler", details.compiler() );
    m_cacheBugs->writeEntry( "OS", details.os() );

    TQStringList senders;
    TQStringList texts;
    TQStringList dates;

    BugDetailsPart::List parts = details.parts();
    BugDetailsPart::List::ConstIterator it;
    for ( it = parts.begin(); it != parts.end(); ++it ) {
        senders.append( (*it).sender.fullName() );
        texts.append( (*it).text );
        dates.append( (*it).date.toString( Qt::ISODate ) );
    }

    m_cacheBugs->writeEntry( "Details", texts );
    m_cacheBugs->writeEntry( "Senders", senders );
    m_cacheBugs->writeEntry( "Dates", dates );
}

bool BugCache::hasBugDetails( const Bug& bug ) const
{
    if ( !m_cacheBugs->hasGroup( bug.number() ) )
        return false;

    m_cacheBugs->setGroup( bug.number() );
    return m_cacheBugs->hasKey( "Details" );
}

BugDetails BugCache::loadBugDetails( const Bug &bug )
{
    if ( !m_cacheBugs->hasGroup( bug.number() ) ) {
        return BugDetails();
    }

    m_cacheBugs->setGroup( bug.number() );

    BugDetailsPart::List parts;

    TQStringList texts = m_cacheBugs->readListEntry( "Details" );
    TQStringList senders = m_cacheBugs->readListEntry( "Senders" );
    TQStringList dates = m_cacheBugs->readListEntry( "Dates" );

    TQStringList::ConstIterator itTexts = texts.begin();
    TQStringList::ConstIterator itSenders = senders.begin();
    TQStringList::ConstIterator itDates = dates.begin();
    while( itTexts != texts.end() ) {
        TQDateTime date = TQDateTime::fromString( *itDates, Qt::ISODate );
        parts.append( BugDetailsPart( Person(*itSenders), date, *itTexts ) );

        ++itTexts;
        ++itSenders;
        ++itDates;
    }

    if ( parts.count() == 0 ) {
        return BugDetails();
    }

    TQString version = m_cacheBugs->readEntry( "Version" );
    TQString source = m_cacheBugs->readEntry( "Source" );
    TQString compiler = m_cacheBugs->readEntry( "Compiler" );
    TQString os = m_cacheBugs->readEntry( "OS" );

    return BugDetails( new BugDetailsImpl( version, source, compiler, os,
                                           parts ) );
}

void BugCache::invalidateBugDetails( const Bug& bug )
{
    m_cacheBugs->deleteGroup( bug.number(), true );
}

void BugCache::clear()
{
    delete m_cachePackages;
    delete m_cacheBugs;

    TQFile f1( mCachePackagesFileName );
    f1.remove();

    TQFile f2( mCacheBugsFileName );
    f2.remove();

    init();    
}

void BugCache::writePerson( KSimpleConfig *file, const TQString &key,
                            const Person &p )
{
    TQStringList values;
    values.append(p.name);
    values.append(p.email);
    file->writeEntry( key, values );
}

struct Person BugCache::readPerson( KSimpleConfig *file, const TQString &key )
{
    struct Person p;
    TQStringList values = file->readListEntry(key);
    if ( values.count() > 0 )
        p.name = values[0];
    if ( values.count() > 1 )
        p.email = values[1];
    return p;
}