summaryrefslogtreecommitdiffstats
path: root/digikam/utilities/batch/batchalbumssyncmetadata.cpp
blob: a148b4310a51f97845149a2174620bb9962f069c (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
/* ============================================================
 *
 * This file is a part of digiKam project
 * http://www.digikam.org
 *
 * Date        : 2007-22-01
 * Description : batch sync pictures metadata from all Albums 
 *               with digiKam database
 *
 * Copyright (C) 2007 by Gilles Caulier <caulier dot gilles at gmail dot com>
 *
 * This program is free software; you can redistribute it
 * and/or modify it under the terms of the GNU General
 * Public License as published by the Free Software Foundation;
 * either version 2, or (at your option)
 * any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * ============================================================ */

// TQt includes.

#include <tqstring.h>
#include <tqtimer.h>
#include <tqdatetime.h>

// KDE includes.

#include <klocale.h>
#include <tdeapplication.h>
#include <kiconloader.h>

// Local includes.

#include "ddebug.h"
#include "album.h"
#include "albummanager.h"
#include "imageinfojob.h"
#include "metadatahub.h"
#include "batchalbumssyncmetadata.h"
#include "batchalbumssyncmetadata.moc"

namespace Digikam
{

class BatchAlbumsSyncMetadataPriv
{
public:

    BatchAlbumsSyncMetadataPriv()
    {
        cancel       = false;
        imageInfoJob = 0;
        palbumList   = AlbumManager::instance()->allPAlbums();
        duration.start();
    }

    bool                 cancel;

    TQTime                duration;
    
    ImageInfoJob        *imageInfoJob;

    AlbumList            palbumList;
    AlbumList::Iterator  albumsIt;
};

BatchAlbumsSyncMetadata::BatchAlbumsSyncMetadata(TQWidget* parent)
                       : DProgressDlg(parent)
{
    d = new BatchAlbumsSyncMetadataPriv;
    d->imageInfoJob = new ImageInfoJob();
    setValue(0);
    setCaption(i18n("Sync All Images' Metadata"));
    setLabel(i18n("<b>Syncing the metadata of all images with the digiKam database. Please wait...</b>"));
    setButtonText(i18n("&Abort"));
    resize(600, 300);
    TQTimer::singleShot(500, this, TQT_SLOT(slotStart()));
}

BatchAlbumsSyncMetadata::~BatchAlbumsSyncMetadata()
{
    delete d;
}

void BatchAlbumsSyncMetadata::slotStart()
{
    setTitle(i18n("Parsing all albums"));
    setTotalSteps(d->palbumList.count());

    connect(d->imageInfoJob, TQT_SIGNAL(signalItemsInfo(const ImageInfoList&)),
            this, TQT_SLOT(slotAlbumParsed(const ImageInfoList&)));

    connect(d->imageInfoJob, TQT_SIGNAL(signalCompleted()),
            this, TQT_SLOT(slotComplete()));
    
    d->albumsIt = d->palbumList.begin();
    parseAlbum();
}

void BatchAlbumsSyncMetadata::parseAlbum()
{
    if (d->albumsIt == d->palbumList.end())     // All is done.
    {
        TQTime t;
        t = t.addMSecs(d->duration.elapsed());
        setLabel(i18n("<b>The metadata of all images has been synchronized with the digiKam database.</b>"));
        setTitle(i18n("Duration: %1").arg(t.toString()));
        setButtonText(i18n("&Close"));
        advance(1);
        abort();
    }
    else if (!(*d->albumsIt)->isRoot())
    {
        d->imageInfoJob->allItemsFromAlbum(*d->albumsIt);
        DDebug() << "Sync Items from Album :" << (*d->albumsIt)->kurl().directory() << endl;
    }
    else
    {
        d->albumsIt++;
        parseAlbum();
    }
}

void BatchAlbumsSyncMetadata::slotAlbumParsed(const ImageInfoList& list)
{
    TQPixmap pix = TDEApplication::kApplication()->iconLoader()->loadIcon(
                  "folder_image", TDEIcon::NoGroup, 32);

    ImageInfoList imageInfoList = list;

    if (!imageInfoList.isEmpty())
    {
        addedAction(pix, imageInfoList.first()->kurl().directory());
    
        for (ImageInfo *info = imageInfoList.first(); info; info = imageInfoList.next())
        {
            MetadataHub fileHub;
            // read in from database
            fileHub.load(info);
            // write out to file DMetadata
            fileHub.write(info->filePath());
        }
    }

    advance(1);
    d->albumsIt++;
    parseAlbum();
}

void BatchAlbumsSyncMetadata::slotComplete()
{
    advance(1);
    d->albumsIt++;
    parseAlbum();
}

void BatchAlbumsSyncMetadata::slotCancel()
{
    abort();
    done(Cancel);
}

void BatchAlbumsSyncMetadata::closeEvent(TQCloseEvent *e)
{
    abort();
    e->accept();
}

void BatchAlbumsSyncMetadata::abort()
{
    d->cancel = true;
    d->imageInfoJob->stop();
    emit signalComplete();
}

}  // namespace Digikam