~ubuntu-branches/ubuntu/vivid/kdesdk/vivid

« back to all changes in this revision

Viewing changes to okteta/kasten/gui/system/bytearrayviewprofilemanager.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2012-06-06 11:49:54 UTC
  • mfrom: (0.4.21)
  • Revision ID: package-import@ubuntu.com-20120606114954-rdls73fzlpzxglbx
Tags: 4:4.8.80-0ubuntu1
* New uptream beta release
* Update dont_export_private_classes.diff

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
2
    This file is part of the Okteta Kasten module, made within the KDE community.
3
3
 
4
 
    Copyright 2010 Friedrich W. H. Kossebau <kossebau@kde.org>
 
4
    Copyright 2010,2012 Friedrich W. H. Kossebau <kossebau@kde.org>
5
5
 
6
6
    This library is free software; you can redistribute it and/or
7
7
    modify it under the terms of the GNU Lesser General Public
22
22
 
23
23
#include "bytearrayviewprofilemanager.h"
24
24
 
25
 
namespace Kasten1
26
 
{
27
 
 
 
25
// library
 
26
#include "bytearrayviewprofilelock.h"
 
27
// KDE
 
28
#include <KGlobal>
 
29
#include <KStandardDirs>
 
30
#include <KConfigGroup>
 
31
#include <KConfig>
 
32
#include <KDirWatch>
 
33
#include <KLockFile>
 
34
// Qt
 
35
#include <QtCore/QFileInfo>
 
36
#include <QtCore/QDir>
 
37
#include <QtCore/QUuid>
 
38
 
 
39
#include <KDebug>
 
40
 
 
41
namespace Kasten2
 
42
{
 
43
static const QStringList viewProfileFileNameFilter =
 
44
    QStringList() << QLatin1String( "*.obavp" ) << QLatin1String( "*.olock" );
 
45
static const QString viewProfileFileSuffix = QLatin1String( ".obavp" );
 
46
static const QString viewProfileSubDir = QLatin1String( "okteta/viewprofiles" );
 
47
static const int DefaultNoOfBytesPerLine = 16;
 
48
static const int DefaultNoOfBytesPerGroup = 4;
 
49
static const int DefaultLayoutStyle = 0;
 
50
static const int DefaultViewModus = 0;
 
51
static const int DefaultVisibleByteArrayCodings = 3;
 
52
static const int DefaultValueCoding = 0;
 
53
static const QString DefaultCharCoding;
 
54
 
 
55
static QList<ByteArrayViewProfile::Id>
 
56
viewProfileIds( const QList<ByteArrayViewProfile>& viewProfiles )
 
57
{
 
58
    QList<ByteArrayViewProfile::Id> result;
 
59
    result.reserve(viewProfiles.count());
 
60
 
 
61
    foreach( const ByteArrayViewProfile& viewProfile, viewProfiles )
 
62
        result.append( viewProfile.id() );
 
63
 
 
64
    return result;
 
65
}
 
66
 
 
67
static QList<ByteArrayViewProfile::Id>
 
68
lockedViewProfileIds( const ByteArrayViewProfileFileInfoLookup& viewProfileFileInfoLookup )
 
69
{
 
70
    QList<ByteArrayViewProfile::Id> result;
 
71
 
 
72
    ByteArrayViewProfileFileInfoLookup::ConstIterator end =
 
73
        viewProfileFileInfoLookup.constEnd();
 
74
    for( ByteArrayViewProfileFileInfoLookup::ConstIterator it =
 
75
                viewProfileFileInfoLookup.constBegin();
 
76
         it != end;
 
77
         ++it )
 
78
    {
 
79
        if( it.value().isLocked() )
 
80
            result.append( it.key() );
 
81
    }
 
82
 
 
83
    return result;
 
84
}
 
85
 
 
86
static void
 
87
updateLockStatus( ByteArrayViewProfileFileInfoLookup& viewProfileFileInfoLookup,
 
88
                  const QList<ByteArrayViewProfile::Id>& lockedViewProfileIds,
 
89
                  const QList<ByteArrayViewProfile::Id>& unlockedViewProfileIds )
 
90
{
 
91
    if( lockedViewProfileIds.isEmpty() && unlockedViewProfileIds.isEmpty() )
 
92
        return;
 
93
 
 
94
    ByteArrayViewProfileFileInfoLookup::Iterator end =
 
95
        viewProfileFileInfoLookup.end();
 
96
    for( ByteArrayViewProfileFileInfoLookup::Iterator it = viewProfileFileInfoLookup.begin();
 
97
         it != end;
 
98
         ++it )
 
99
    {
 
100
        bool isLocked;
 
101
 
 
102
        if( lockedViewProfileIds.contains(it.key()) )
 
103
            isLocked = true;
 
104
        else if( unlockedViewProfileIds.contains(it.key()) )
 
105
            isLocked = false;
 
106
        else
 
107
            continue;
 
108
 
 
109
        it.value().setLocked( isLocked );
 
110
    }
 
111
}
 
112
 
 
113
static QString
 
114
viewProfileFilePath( const ByteArrayViewProfile::Id& viewProfileId )
 
115
{
 
116
    return KGlobal::dirs()->saveLocation( "data", viewProfileSubDir ) + viewProfileId + viewProfileFileSuffix;
 
117
}
 
118
 
 
119
static QString
 
120
viewProfileFileName( const ByteArrayViewProfile::Id& viewProfileId )
 
121
{
 
122
    return viewProfileId + viewProfileFileSuffix;
 
123
}
 
124
 
 
125
// TODO: add global lock
 
126
// TODO: make calls async
 
127
// TODO: only load view profiles on demand
28
128
ByteArrayViewProfileManager::ByteArrayViewProfileManager()
29
129
{
30
 
}
31
 
 
32
 
void ByteArrayViewProfileManager::addViewProfiles( const QList<ByteArrayViewProfile>& viewProfiles )
33
 
{
34
 
    mViewProfiles << viewProfiles;
35
 
 
36
 
    emit viewProfilesAdded( viewProfiles );
37
 
}
 
130
    mViewProfileFileWatcher = new KDirWatch( this );
 
131
    connect( mViewProfileFileWatcher, SIGNAL(dirty(QString)),
 
132
             SLOT(onViewProfilesFolderChanged(QString)) );
 
133
 
 
134
    // get all folder where viewProfiles could be stored
 
135
    const KStandardDirs* const standardDirs = KGlobal::dirs();
 
136
    const QStringList dataFolderPaths = standardDirs->resourceDirs( "data" );
 
137
 
 
138
    foreach( const QString& dataFolderPath, dataFolderPaths )
 
139
    {
 
140
        const QString viewProfileFolderPath = dataFolderPath + viewProfileSubDir;
 
141
        // watch folder for changes
 
142
        mViewProfileFileWatcher->addDir( viewProfileFolderPath, KDirWatch::WatchDirOnly );
 
143
kDebug() << "adding Dir"<<viewProfileFolderPath;
 
144
 
 
145
        // read current files
 
146
        onViewProfilesFolderChanged( viewProfileFolderPath );
 
147
    }
 
148
 
 
149
    // tmp
 
150
    if( ! mViewProfiles.isEmpty() )
 
151
        mDefaultViewProfileId = mViewProfiles.at(0).id();
 
152
 
 
153
    // report any problems with existing view profiles?
 
154
}
 
155
 
 
156
int
 
157
ByteArrayViewProfileManager::viewProfilesCount() const
 
158
{
 
159
    return mViewProfiles.count();
 
160
}
 
161
 
 
162
 
 
163
QList<ByteArrayViewProfile>
 
164
ByteArrayViewProfileManager::viewProfiles() const
 
165
{
 
166
    return mViewProfiles;
 
167
}
 
168
 
 
169
ByteArrayViewProfile
 
170
ByteArrayViewProfileManager::viewProfile( const ByteArrayViewProfile::Id& viewProfileId ) const
 
171
{
 
172
    ByteArrayViewProfile result;
 
173
 
 
174
    foreach( const ByteArrayViewProfile& viewProfile, mViewProfiles )
 
175
    {
 
176
        if( viewProfile.id() == viewProfileId )
 
177
        {
 
178
            result = viewProfile;
 
179
            break;
 
180
        }
 
181
    }
 
182
 
 
183
    return result;
 
184
}
 
185
 
 
186
ByteArrayViewProfile::Id
 
187
ByteArrayViewProfileManager::defaultViewProfileId() const
 
188
{
 
189
    return mDefaultViewProfileId;
 
190
}
 
191
 
 
192
ByteArrayViewProfile
 
193
ByteArrayViewProfileManager::defaultViewProfile() const
 
194
{
 
195
    return viewProfile( mDefaultViewProfileId );
 
196
}
 
197
 
 
198
bool
 
199
ByteArrayViewProfileManager::isViewProfileLocked( const ByteArrayViewProfile::Id& viewProfileId ) const
 
200
{
 
201
    bool result = false;
 
202
 
 
203
    // search in all folders for the info
 
204
    foreach( const ByteArrayViewProfileFileInfoLookup& viewProfileFileInfoLookup, mViewProfileFileInfoLookupPerFolder )
 
205
    {
 
206
        ByteArrayViewProfileFileInfoLookup::ConstIterator it =
 
207
            viewProfileFileInfoLookup.find( viewProfileId );
 
208
        if( it != viewProfileFileInfoLookup.constEnd() )
 
209
        {
 
210
            result = it->isLocked();
 
211
            break;
 
212
        }
 
213
    }
 
214
 
 
215
    return result;
 
216
}
 
217
 
 
218
void ByteArrayViewProfileManager::saveViewProfiles( QList<ByteArrayViewProfile>& viewProfiles )
 
219
{
 
220
    //TODO: do not save if locked by someone else -> needs passing of our lock? or just registering our own and check?
 
221
    // create and set unique id
 
222
    for( QList<ByteArrayViewProfile>::Iterator it = viewProfiles.begin(); it != viewProfiles.end(); ++it )
 
223
    {
 
224
        ByteArrayViewProfile& viewProfile = *it;
 
225
        const ByteArrayViewProfile::Id viewProfileId = viewProfile.id();
 
226
        const ByteArrayViewProfile::Id oldViewProfileId = viewProfile.id();
 
227
 
 
228
        bool needsId = true;
 
229
        if( ! viewProfileId.isEmpty() )
 
230
        {
 
231
            // already existing?
 
232
            QList<ByteArrayViewProfile>::ConstIterator existingIt = mViewProfiles.constBegin();
 
233
            QList<ByteArrayViewProfile>::ConstIterator existingEnd = mViewProfiles.constEnd();
 
234
            for( ; existingIt != existingEnd; ++existingIt )
 
235
            {
 
236
                if( viewProfileId == existingIt->id() )
 
237
                {
 
238
                    needsId = false;
 
239
                    break;
 
240
                }
 
241
            }
 
242
        }
 
243
 
 
244
        // set new uuid for non-existing
 
245
        if( needsId )
 
246
            viewProfile.setId( QUuid::createUuid().toString() );
 
247
 
 
248
kDebug() << "going to save"<<viewProfile.viewProfileTitle()<<viewProfile.id()<<oldViewProfileId;
 
249
        saveViewProfile( viewProfile );
 
250
    }
 
251
}
 
252
 
 
253
 
 
254
void
 
255
ByteArrayViewProfileManager::removeViewProfiles( const QList<ByteArrayViewProfile::Id>& viewProfileIds )
 
256
{
 
257
    foreach( const ByteArrayViewProfile::Id& viewProfileId, viewProfileIds )
 
258
    {
 
259
        removeViewProfile( viewProfileId );
 
260
    }
 
261
}
 
262
 
 
263
 
 
264
void
 
265
ByteArrayViewProfileManager::setDefaultViewProfile( const ByteArrayViewProfile::Id& viewProfileId )
 
266
{
 
267
    // no change?
 
268
    if( mDefaultViewProfileId == viewProfileId )
 
269
        return;
 
270
 
 
271
    bool isExisting = false;
 
272
    foreach( const ByteArrayViewProfile& viewProfile, mViewProfiles )
 
273
    {
 
274
        if( viewProfile.id() == viewProfileId )
 
275
        {
 
276
            isExisting = true;
 
277
            break;
 
278
        }
 
279
    }
 
280
 
 
281
    if( isExisting )
 
282
    {
 
283
        mDefaultViewProfileId = viewProfileId;
 
284
        emit defaultViewProfileChanged( mDefaultViewProfileId );
 
285
    }
 
286
}
 
287
 
 
288
ByteArrayViewProfileLock
 
289
ByteArrayViewProfileManager::createLock( const ByteArrayViewProfile::Id& viewProfileId )
 
290
{
 
291
    const QString viewProfileFilePath = filePathOfViewProfile( viewProfileId );
 
292
 
 
293
    return ByteArrayViewProfileLock( viewProfileFilePath, viewProfileId );
 
294
}
 
295
 
 
296
/*
 
297
bool
 
298
ByteArrayViewProfileManager::lockViewProfile( const Kasten::ByteArrayViewProfile::Id& viewProfileId )
 
299
{
 
300
    bool isSuccessfull;
 
301
 
 
302
    const QString viewProfileFilePath = filePathOfViewProfile( viewProfileId );
 
303
 
 
304
    // viewProfile known
 
305
    if( not viewProfileFilePath.isEmpty() )
 
306
    {
 
307
        const QString lockFilePath = viewProfileFileLockPath( viewProfileFilePath );
 
308
        KLockFile::Ptr lock = new KLockFile( lockFilePath );
 
309
        const KLockFile::LockResult lockResult =
 
310
            lock->lock(KLockFile::NoBlockFlag | KLockFile::ForceFlag);
 
311
        isSuccessfull = (lockResult == KLockFile::LockOK );
 
312
    }
 
313
    // if found
 
314
    // try to create lock file
 
315
    return isSuccessfull;
 
316
}
 
317
*/
 
318
// bool
 
319
// ByteArrayViewProfileManager::unlockViewProfile( const Kasten::ByteArrayViewProfile::Id& viewProfileId )
 
320
// {
 
321
//     const QString filePath = filePathOfViewProfile( viewProfileId );
 
322
// 
 
323
//     if( filePath.isEmpty() )
 
324
//     return false;
 
325
// }
 
326
 
 
327
ByteArrayViewProfile
 
328
ByteArrayViewProfileManager::loadViewProfile( const QString& absoluteFilePath ) const
 
329
{
 
330
kDebug() << "Loading" << QFileInfo(absoluteFilePath).baseName() << absoluteFilePath;
 
331
    ByteArrayViewProfile result;
 
332
 
 
333
    KConfig configFile( absoluteFilePath, KConfig::SimpleConfig );
 
334
    // TODO: version check
 
335
 
 
336
    result.setId( QFileInfo(absoluteFilePath).baseName() );
 
337
 
 
338
    KConfigGroup generalConfigGroup = configFile.group( "General" );
 
339
    result.setViewProfileTitle( generalConfigGroup.readEntry("Title") );
 
340
 
 
341
    KConfigGroup layoutConfigGroup = configFile.group( "Layout" );
 
342
    result.setNoOfBytesPerLine( layoutConfigGroup.readEntry("NoOfBytesPerLine", DefaultNoOfBytesPerLine) );
 
343
    result.setNoOfGroupedBytes( layoutConfigGroup.readEntry("NoOfBytesPerGroup", DefaultNoOfBytesPerGroup) );
 
344
    result.setLayoutStyle( layoutConfigGroup.readEntry("LayoutStyle", DefaultLayoutStyle) );
 
345
 
 
346
    KConfigGroup displayConfigGroup = configFile.group( "Display" );
 
347
    result.setOffsetColumnVisible( displayConfigGroup.readEntry("OffsetColumnVisible", true) );
 
348
    result.setViewModus( displayConfigGroup.readEntry("ViewModus", DefaultViewModus) );
 
349
    result.setVisibleByteArrayCodings( displayConfigGroup.readEntry("VisibleByteArrayCodings", DefaultVisibleByteArrayCodings) );
 
350
 
 
351
    KConfigGroup interpretationConfigGroup = configFile.group( "Interpretation" );
 
352
 
 
353
    KConfigGroup valuesConfigGroup = interpretationConfigGroup.group( "Values" );
 
354
    result.setValueCoding( valuesConfigGroup.readEntry("Coding", DefaultValueCoding) );
 
355
 
 
356
    KConfigGroup charsConfigGroup = interpretationConfigGroup.group( "Chars" );
 
357
    result.setCharCoding( charsConfigGroup.readEntry("Coding", DefaultCharCoding) );
 
358
    result.setShowsNonprinting( charsConfigGroup.readEntry("NonprintingShown", false) );
 
359
    result.setSubstituteChar( charsConfigGroup.readEntry("SubstituteChar", ".").at(0) );
 
360
    result.setUndefinedChar( charsConfigGroup.readEntry("UndefinedChar", "?").at(0) );
 
361
 
 
362
    return result;
 
363
}
 
364
 
 
365
void
 
366
ByteArrayViewProfileManager::saveViewProfile( const ByteArrayViewProfile& viewProfile ) const
 
367
{
 
368
kDebug() << "------------------ Saving"<<viewProfile.id();
 
369
{
 
370
    const QString fileName = viewProfileFilePath( viewProfile.id() );
 
371
    KConfig configFile( fileName, KConfig::SimpleConfig );
 
372
 
 
373
    KConfigGroup generalConfigGroup = configFile.group( "General" );
 
374
    generalConfigGroup.writeEntry( "Title", viewProfile.viewProfileTitle() );
 
375
 
 
376
    KConfigGroup layoutConfigGroup = configFile.group( "Layout" );
 
377
    layoutConfigGroup.writeEntry( "NoOfBytesPerLine", viewProfile.noOfBytesPerLine() );
 
378
    layoutConfigGroup.writeEntry( "NoOfBytesPerGroup", viewProfile.noOfGroupedBytes() );
 
379
    layoutConfigGroup.writeEntry( "LayoutStyle", viewProfile.layoutStyle() );
 
380
 
 
381
    KConfigGroup displayConfigGroup = configFile.group( "Display" );
 
382
    displayConfigGroup.writeEntry( "OffsetColumnVisible", viewProfile.offsetColumnVisible() );
 
383
    displayConfigGroup.writeEntry( "ViewModus", viewProfile.viewModus() );
 
384
    displayConfigGroup.writeEntry( "VisibleByteArrayCodings", viewProfile.visibleByteArrayCodings() );
 
385
 
 
386
    KConfigGroup interpretationConfigGroup = configFile.group( "Interpretation" );
 
387
 
 
388
    KConfigGroup valuesConfigGroup = interpretationConfigGroup.group( "Values" );
 
389
    valuesConfigGroup.writeEntry( "Coding", viewProfile.valueCoding() );
 
390
 
 
391
    KConfigGroup charsConfigGroup = interpretationConfigGroup.group( "Chars" );
 
392
    charsConfigGroup.writeEntry( "Coding", viewProfile.charCodingName() );
 
393
    charsConfigGroup.writeEntry( "NonprintingShown", viewProfile.showsNonprinting() );
 
394
    charsConfigGroup.writeEntry( "SubstituteChar", QString(viewProfile.substituteChar()) );
 
395
    charsConfigGroup.writeEntry( "UndefinedChar", QString(viewProfile.undefinedChar()) );
 
396
}
 
397
kDebug() << "------------------ Saved"<<viewProfile.id();
 
398
}
 
399
 
 
400
void
 
401
ByteArrayViewProfileManager::removeViewProfile( const ByteArrayViewProfile::Id& viewProfileId )
 
402
{
 
403
    const QString filePath = filePathOfViewProfile( viewProfileId );
 
404
kDebug() << "------------------ Removing"<<viewProfileId<<filePath;
 
405
    if( ! filePath.isEmpty() )
 
406
        QFile::remove( filePath );
 
407
}
 
408
 
 
409
QString
 
410
ByteArrayViewProfileManager::filePathOfViewProfile( const ByteArrayViewProfile::Id& viewProfileId ) const
 
411
{
 
412
    QString result;
 
413
 
 
414
    for( QHash<QString, ByteArrayViewProfileFileInfoLookup>::ConstIterator foldersIt =
 
415
             mViewProfileFileInfoLookupPerFolder.constBegin();
 
416
         foldersIt != mViewProfileFileInfoLookupPerFolder.constEnd() && result.isEmpty();
 
417
         ++foldersIt )
 
418
    {
 
419
        const ByteArrayViewProfileFileInfoLookup& fileInfoList = foldersIt.value();
 
420
        for( ByteArrayViewProfileFileInfoLookup::ConstIterator folderIt = fileInfoList.constBegin();
 
421
             folderIt != fileInfoList.constEnd();
 
422
             ++folderIt )
 
423
        {
 
424
            if( folderIt.key() == viewProfileId )
 
425
            {
 
426
                result = foldersIt.key() + QLatin1Char('/') + viewProfileFileName( viewProfileId );
 
427
                break;
 
428
            }
 
429
        }
 
430
    }
 
431
 
 
432
    return result;
 
433
}
 
434
 
 
435
void
 
436
ByteArrayViewProfileManager::onViewProfilesFolderChanged( const QString& viewProfileFolderPath )
 
437
{
 
438
kDebug() << "looking into folder"<< viewProfileFolderPath;
 
439
    ByteArrayViewProfileFileInfoLookup& viewProfileFileInfoLookup =
 
440
        mViewProfileFileInfoLookupPerFolder[viewProfileFolderPath];
 
441
 
 
442
    // TODO: reparse for new, removed and changed files
 
443
    // assume all are removed and unlocked in the beginning
 
444
    QList<ByteArrayViewProfile::Id> removedViewProfileIds = viewProfileFileInfoLookup.keys();
 
445
    QList<ByteArrayViewProfile> newViewProfiles;
 
446
    QList<ByteArrayViewProfile> changedViewProfiles;
 
447
 
 
448
    QList<ByteArrayViewProfile::Id> newUnlockedViewProfileIds = lockedViewProfileIds(viewProfileFileInfoLookup);
 
449
    QList<ByteArrayViewProfile::Id> newLockedViewProfileIds;
 
450
kDebug() << "old profiles:" << removedViewProfileIds;
 
451
kDebug() << "locked profiles:" << newUnlockedViewProfileIds;
 
452
    // iterate all files in folder
 
453
    const QFileInfoList viewProfileFileInfoList =
 
454
        QDir( viewProfileFolderPath ).entryInfoList( viewProfileFileNameFilter, QDir::Files );
 
455
 
 
456
    foreach( const QFileInfo& viewProfileFileInfo, viewProfileFileInfoList )
 
457
    {
 
458
       // a lock file ?
 
459
       if( viewProfileFileInfo.suffix() == QLatin1String("olock") )
 
460
       {
 
461
           const ByteArrayViewProfile::Id lockedViewProfileId = viewProfileFileInfo.baseName();
 
462
           // if not in old locks, is a new lock
 
463
           if( ! newUnlockedViewProfileIds.removeOne(lockedViewProfileId) )
 
464
                newLockedViewProfileIds.append( lockedViewProfileId );
 
465
           continue;
 
466
       }
 
467
 
 
468
       // not a viewprofile file ?
 
469
       if( viewProfileFileInfo.suffix() != QLatin1String("obavp") )
 
470
            continue;
 
471
 
 
472
       // all other files assumed to be viewProfile files
 
473
       // TODO: check validness
 
474
        const QDateTime fileInfoLastModified = viewProfileFileInfo.lastModified();
 
475
        const ByteArrayViewProfile::Id viewProfileId = viewProfileFileInfo.baseName();
 
476
        ByteArrayViewProfileFileInfoLookup::Iterator infoIt =
 
477
            viewProfileFileInfoLookup.find( viewProfileId );
 
478
        const bool isKnown = ( infoIt != viewProfileFileInfoLookup.end() );
 
479
        // is known?
 
480
        if( isKnown )
 
481
        {
 
482
            removedViewProfileIds.removeOne( viewProfileId );
 
483
 
 
484
            // check timestamp
 
485
            if( fileInfoLastModified == infoIt->lastModified() )
 
486
                continue;
 
487
 
 
488
            // update timestamp
 
489
            infoIt->setLastModified( fileInfoLastModified );
 
490
        }
 
491
        else
 
492
        {
 
493
            ByteArrayViewProfileFileInfo info( fileInfoLastModified, false );
 
494
            viewProfileFileInfoLookup.insert( viewProfileId, info );
 
495
        }
 
496
 
 
497
kDebug() <<"going to load"<<viewProfileId;
 
498
        // load file
 
499
        // TODO: check validness
 
500
        const ByteArrayViewProfile viewProfile = loadViewProfile( viewProfileFileInfo.absoluteFilePath() );
 
501
        if( isKnown )
 
502
        {
 
503
            QList<ByteArrayViewProfile>::Iterator it = mViewProfiles.begin();
 
504
            QList<ByteArrayViewProfile>::Iterator end = mViewProfiles.end();
 
505
            for( ; it != end; ++it )
 
506
            {
 
507
                if( it->id() == viewProfileId )
 
508
                {
 
509
                    *it = viewProfile;
 
510
                    break;
 
511
                }
 
512
            }
 
513
        }
 
514
        else
 
515
            newViewProfiles.append( viewProfile );
 
516
        changedViewProfiles.append( viewProfile );
 
517
    }
 
518
 
 
519
    // remove all removed viewprofiles
 
520
    {
 
521
        QList<ByteArrayViewProfile>::Iterator it = mViewProfiles.begin();
 
522
        while( it != mViewProfiles.end() )
 
523
        {
 
524
            // contained in removed?
 
525
            if( removedViewProfileIds.contains(it->id()) )
 
526
                it = mViewProfiles.erase(it);
 
527
            else
 
528
                ++it;
 
529
        }
 
530
    }
 
531
    foreach( const ByteArrayViewProfile::Id& viewProfileId, removedViewProfileIds )
 
532
    {
 
533
        viewProfileFileInfoLookup.remove( viewProfileId );
 
534
        if( viewProfileId == mDefaultViewProfileId )
 
535
            mDefaultViewProfileId.clear();
 
536
        // TODO: how to select new one?
 
537
    }
 
538
 
 
539
    // add new viewprofiles
 
540
    mViewProfiles.append( newViewProfiles );
 
541
    // if there was no default viewprofile before, set default to first
 
542
    const bool isDefaultViewProfileChanged = ( mDefaultViewProfileId.isEmpty() && ! mViewProfiles.isEmpty() );
 
543
    if( isDefaultViewProfileChanged )
 
544
        mDefaultViewProfileId = mViewProfiles.at(0).id();
 
545
 
 
546
    // update lock info
 
547
    updateLockStatus( viewProfileFileInfoLookup, newLockedViewProfileIds, newUnlockedViewProfileIds );
 
548
 
 
549
kDebug() << "new profiles:" << viewProfileFileInfoLookup.keys();
 
550
 
 
551
    // signal changes
 
552
    if( ! changedViewProfiles.isEmpty() )
 
553
        emit viewProfilesChanged( changedViewProfiles );
 
554
kDebug() << "changed profiles" << viewProfileIds(changedViewProfiles);
 
555
    if( ! removedViewProfileIds.isEmpty() )
 
556
        emit viewProfilesRemoved( removedViewProfileIds );
 
557
kDebug() << "removed profiles" << removedViewProfileIds;
 
558
 
 
559
    if( ! newUnlockedViewProfileIds.isEmpty() )
 
560
        emit viewProfilesUnlocked( newUnlockedViewProfileIds );
 
561
kDebug() << "unlocked profiles" << newUnlockedViewProfileIds;
 
562
    if( ! newLockedViewProfileIds.isEmpty() )
 
563
        emit viewProfilesLocked( newLockedViewProfileIds );
 
564
kDebug() << "locked profiles" << newLockedViewProfileIds;
 
565
    if( isDefaultViewProfileChanged )
 
566
        emit defaultViewProfileChanged( mDefaultViewProfileId );
 
567
}
 
568
 
38
569
 
39
570
ByteArrayViewProfileManager::~ByteArrayViewProfileManager()
40
571
{