Fix regressions
[kphotoalbum.git] / ImportExport / MD5CheckPage.cpp
blob7c3259330bf1e9aa3e204e7f4402f853487e0a41
1 #include "MD5CheckPage.h"
2 #include <QFrame>
3 #include <QVBoxLayout>
4 #include <QButtonGroup>
5 #include <QRadioButton>
6 #include <klocale.h>
7 #include <QLabel>
8 #include "DB/ImageDB.h"
9 #include "DB/MD5Map.h"
11 ImportExport::ClashInfo::ClashInfo( const QStringList& categories )
12 : label(false), description(false), orientation(false), date(false)
14 Q_FOREACH( const QString& category, categories )
15 this->categories[category] = false;
19 bool ImportExport::MD5CheckPage::pageNeeded( const ImportSettings& settings)
21 if ( countOfMD5Matches(settings) != 0 && clashes(settings).anyClashes() )
22 return true;
24 return false;
27 ImportExport::MD5CheckPage::MD5CheckPage(const ImportSettings& settings)
29 QVBoxLayout* vlay = new QVBoxLayout( this );
31 const QString txt =
32 i18np("One image from the import file, has the same MD5 sum as an image in the Database, how should that be resolved?",
33 "%1 images from the import file, have the same MD5 sum as images in the Database, how should that be resolved?",
34 countOfMD5Matches(settings) );
35 QLabel* label = new QLabel( txt );
36 label->setWordWrap(true);
38 vlay->addWidget( label );
40 QGridLayout* grid = new QGridLayout;
41 grid->setHorizontalSpacing(0);
42 vlay->addLayout( grid );
44 int row = -1;
46 // Titles
47 label = new QLabel(i18n("Use data from\nImport File") );
48 grid->addWidget( label, ++row, 1 );
50 label = new QLabel( i18n("Use data from\nDatabase") );
51 grid->addWidget( label, row, 2 );
53 label = new QLabel( i18n("Merge data") );
54 grid->addWidget( label, row, 3 );
56 ClashInfo clashes = this->clashes( settings );
57 createRow( grid, row, QString::fromLatin1("*Label*"), i18n("Label"), clashes.label,false );
58 createRow( grid, row, QString::fromLatin1("*Description*"), i18n("Description"), clashes.description, true);
59 createRow( grid, row, QString::fromLatin1( "*Orientation*" ), i18n("Orientation"), clashes.orientation, false );
60 createRow( grid, row, QString::fromLatin1( "*Date*" ), i18n("Date and Time"), clashes.date, false );
61 for ( QMap<QString,bool>::const_iterator it = clashes.categories.constBegin(); it != clashes.categories.constEnd(); ++it ) {
62 createRow( grid, row, it.key(), it.key(), *it, true );
65 vlay->addStretch(1);
68 /**
69 * Return the number of images in the import set which has the same MD5 sum as those from the DB.
71 int ImportExport::MD5CheckPage::countOfMD5Matches( const ImportSettings& settings )
73 int count = 0;
74 DB::ImageInfoList list = settings.selectedImages();
75 Q_FOREACH( DB::ImageInfoPtr info, list ) {
76 if ( DB::ImageDB::instance()->md5Map()->contains(info->MD5Sum()) )
77 ++count;
79 return count;
82 ImportExport::ClashInfo ImportExport::MD5CheckPage::clashes(const ImportSettings& settings)
84 QStringList myCategories;
85 Q_FOREACH( const CategoryMatchSetting& matcher, settings.categoryMatchSetting() ) {
86 myCategories.append( matcher.DBCategoryName() );
89 ClashInfo res( myCategories );
90 DB::ImageInfoList list = settings.selectedImages();
91 Q_FOREACH( DB::ImageInfoPtr info, list ) {
92 if ( !DB::ImageDB::instance()->md5Map()->contains(info->MD5Sum()) )
93 continue;
95 const QString& name = DB::ImageDB::instance()->md5Map()->lookup(info->MD5Sum());
96 DB::ImageInfoPtr other = DB::ImageDB::instance()->info( name, DB::RelativeToImageRoot );
97 if ( info->label() != other->label() )
98 res.label = true;
99 if ( info->description() != other->description() )
100 res.description = true;
102 if ( info->angle() != other->angle() )
103 res.orientation = true;
105 if (info->date() != other->date() )
106 res.date = true;
108 Q_FOREACH( const CategoryMatchSetting& matcher, settings.categoryMatchSetting() ) {
109 const QString XMLFileCategory = matcher.XMLCategoryName();
110 const QString DBCategory = matcher.DBCategoryName();
111 if ( mapCategoriesToDB( matcher, info->itemsOfCategory( XMLFileCategory ) ) != other->itemsOfCategory( DBCategory ) )
112 res.categories[DBCategory] = true;
115 return res;
118 bool ImportExport::ClashInfo::anyClashes()
120 if ( label || description || orientation || date)
121 return true;
123 for( QMap<QString,bool>::ConstIterator categoryIt = categories.constBegin(); categoryIt != categories.constEnd(); ++categoryIt ) {
124 if (categoryIt.value() )
125 return true;
128 return false;
132 void ImportExport::MD5CheckPage::createRow( QGridLayout* layout, int& row, const QString& name, const QString& title, bool anyClashes, bool allowMerge )
134 if ( row % 3 == 0 ) {
135 QFrame* line = new QFrame;
136 line->setFrameShape( QFrame::HLine );
137 layout->addWidget( line, ++row, 0, 1, 4 );
140 QLabel* label = new QLabel( title );
141 label->setEnabled( anyClashes );
142 layout->addWidget( label, ++row, 0 );
144 QButtonGroup* group = new QButtonGroup(this);
145 m_groups[name]=group;
147 for ( int i = 1; i<4;++i ) {
148 if ( i == 3 && !allowMerge )
149 continue;
151 QRadioButton* rb = new QRadioButton;
152 layout->addWidget( rb, row, i );
153 group->addButton( rb, i );
154 rb->setEnabled( anyClashes );
155 if (i == 1 )
156 rb->setChecked(true);
160 Utilities::StringSet ImportExport::MD5CheckPage::mapCategoriesToDB( const CategoryMatchSetting& matcher, const Utilities::StringSet& items )
162 Utilities::StringSet res;
164 Q_FOREACH( const QString& item, items ) {
165 if ( matcher.XMLtoDB().contains( item ) )
166 res.insert(matcher.XMLtoDB()[item]);
168 return res;
171 QMap<QString, ImportExport::ImportSettings::ImportAction> ImportExport::MD5CheckPage::settings()
173 QMap<QString, ImportSettings::ImportAction> res;
174 for( QMap<QString,QButtonGroup*>::Iterator it = m_groups.begin(); it != m_groups.end(); ++it ) {
175 res.insert( it.key(), static_cast<ImportSettings::ImportAction>(it.value()->checkedId()) );
177 return res;