21 #include "conflictresolvedialog_p.h"
23 #include "abstractdifferencesreporter.h"
24 #include "differencesalgorithminterface.h"
25 #include "typepluginloader_p.h"
27 #include <QtGui/QVBoxLayout>
28 #include <QtGui/QLabel>
30 #include <kcolorscheme.h>
32 #include <kpushbutton.h>
33 #include <ktextbrowser.h>
35 using namespace Akonadi;
37 static inline QString textToHTML(
const QString &text )
39 return Qt::convertFromPlainText( text );
42 class HtmlDifferencesReporter :
public AbstractDifferencesReporter
45 HtmlDifferencesReporter()
49 QString toHtml()
const
51 return header() + mContent + footer();
54 void setPropertyNameTitle(
const QString &title )
59 void setLeftPropertyValueTitle(
const QString &title )
64 void setRightPropertyValueTitle(
const QString &title )
69 void addProperty( Mode mode,
const QString &name,
const QString &leftValue,
const QString &rightValue )
73 mContent.append( QString::fromLatin1(
"<tr><td align=\"right\"><b>%1:</b></td><td>%2</td><td></td><td>%3</td></tr>" )
75 textToHTML( leftValue ),
76 textToHTML( rightValue ) ) );
79 mContent.append( QString::fromLatin1(
"<tr><td align=\"right\"><b>%1:</b></td><td bgcolor=\"#ff8686\">%2</td><td></td><td bgcolor=\"#ff8686\">%3</td></tr>" )
81 textToHTML( leftValue ),
82 textToHTML( rightValue ) ) );
84 case AdditionalLeftMode:
85 mContent.append( QString::fromLatin1(
"<tr><td align=\"right\"><b>%1:</b></td><td bgcolor=\"#9cff83\">%2</td><td></td><td></td></tr>" )
87 textToHTML( leftValue ) ) );
89 case AdditionalRightMode:
90 mContent.append( QString::fromLatin1(
"<tr><td align=\"right\"><b>%1:</b></td><td></td><td></td><td bgcolor=\"#9cff83\">%2</td></tr>" )
92 textToHTML( rightValue ) ) );
98 QString header()
const
100 QString header = QLatin1String(
"<html>" );
101 header += QString::fromLatin1(
"<body text=\"%1\" bgcolor=\"%2\">" )
102 .arg( KColorScheme( QPalette::Active, KColorScheme::View ).foreground().color().name() )
103 .arg( KColorScheme( QPalette::Active, KColorScheme::View ).background().color().name() );
104 header += QLatin1String(
"<center><table>" );
105 header += QString::fromLatin1(
"<tr><th align=\"center\">%1</th><th align=\"center\">%2</th><td> </td><th align=\"center\">%3</th></tr>" )
113 QString footer()
const
115 return QLatin1String(
"</table></center>"
126 static void compareItems( AbstractDifferencesReporter *reporter,
const Akonadi::Item &localItem,
const Akonadi::Item &otherItem )
128 if ( localItem.modificationTime() != otherItem.modificationTime() ) {
129 reporter->addProperty( AbstractDifferencesReporter::ConflictMode, i18n(
"Modification Time" ),
130 KGlobal::locale()->formatDateTime( localItem.modificationTime(), KLocale::ShortDate, true ),
131 KGlobal::locale()->formatDateTime( otherItem.modificationTime(), KLocale::ShortDate, true ) );
134 if ( localItem.flags() != otherItem.flags() ) {
135 QStringList localFlags;
136 foreach (
const QByteArray &localFlag, localItem.flags() )
137 localFlags.append( QString::fromUtf8( localFlag ) );
139 QStringList otherFlags;
140 foreach (
const QByteArray &otherFlag, otherItem.flags() )
141 otherFlags.append( QString::fromUtf8( otherFlag ) );
143 reporter->addProperty( AbstractDifferencesReporter::ConflictMode, i18n(
"Flags" ),
144 localFlags.join( QLatin1String(
", " ) ),
145 otherFlags.join( QLatin1String(
", " ) ) );
148 QHash<QByteArray, QByteArray> localAttributes;
150 localAttributes.insert( attribute->
type(), attribute->
serialized() );
153 QHash<QByteArray, QByteArray> otherAttributes;
155 otherAttributes.insert( attribute->
type(), attribute->
serialized() );
158 if ( localAttributes != otherAttributes ) {
159 foreach (
const QByteArray &localKey, localAttributes ) {
160 if ( !otherAttributes.contains( localKey ) ) {
161 reporter->addProperty( AbstractDifferencesReporter::AdditionalLeftMode, i18n(
"Attribute: %1", QString::fromUtf8( localKey ) ),
162 QString::fromUtf8( localAttributes.value( localKey ) ),
165 const QByteArray localValue = localAttributes.value( localKey );
166 const QByteArray otherValue = otherAttributes.value( localKey );
167 if ( localValue != otherValue ) {
168 reporter->addProperty( AbstractDifferencesReporter::ConflictMode, i18n(
"Attribute: %1", QString::fromUtf8( localKey ) ),
169 QString::fromUtf8( localValue ),
170 QString::fromUtf8( otherValue ) );
175 foreach (
const QByteArray &otherKey, otherAttributes ) {
176 if ( !localAttributes.contains( otherKey ) ) {
177 reporter->addProperty( AbstractDifferencesReporter::AdditionalRightMode, i18n(
"Attribute: %1", QString::fromUtf8( otherKey ) ),
179 QString::fromUtf8( otherAttributes.value( otherKey ) ) );
185 ConflictResolveDialog::ConflictResolveDialog( QWidget *parent )
188 setCaption( i18nc(
"@title:window",
"Conflict Resolution" ) );
189 setButtons( User1 | User2 | User3 );
190 setDefaultButton( User3 );
192 button( User3 )->setText( i18n(
"Take left one" ) );
193 button( User2 )->setText( i18n(
"Take right one" ) );
194 button( User1 )->setText( i18n(
"Keep both" ) );
196 connect(
this, SIGNAL(user1Clicked()), SLOT(slotUseBothItemsChoosen()) );
197 connect(
this, SIGNAL(user2Clicked()), SLOT(slotUseOtherItemChoosen()) );
198 connect(
this, SIGNAL(user3Clicked()), SLOT(slotUseLocalItemChoosen()) );
200 QWidget *mainWidget =
new QWidget;
201 QVBoxLayout *layout =
new QVBoxLayout( mainWidget );
203 QLabel* label =
new QLabel( i18nc(
"@label",
"Two updates conflict with each other.<nl/>Please choose which update(s) to apply." ), mainWidget );
204 layout->addWidget( label );
206 mView =
new KTextBrowser;
208 layout->addWidget( mView );
210 setMainWidget( mainWidget );
213 void ConflictResolveDialog::setConflictingItems(
const Akonadi::Item &localItem,
const Akonadi::Item &otherItem )
215 mLocalItem = localItem;
216 mOtherItem = otherItem;
218 HtmlDifferencesReporter reporter;
219 compareItems( &reporter, localItem, otherItem );
221 if ( mLocalItem.hasPayload() && mOtherItem.hasPayload() ) {
227 algorithm->
compare( &reporter, localItem, otherItem );
228 mView->setHtml( reporter.toHtml() );
233 reporter.addProperty( HtmlDifferencesReporter::NormalMode, i18n(
"Data" ),
234 QString::fromUtf8( mLocalItem.payloadData() ),
235 QString::fromUtf8( mOtherItem.payloadData() ) );
238 mView->setHtml( reporter.toHtml() );
243 return mResolveStrategy;
246 void ConflictResolveDialog::slotUseLocalItemChoosen()
248 mResolveStrategy = ConflictHandler::UseLocalItem;
252 void ConflictResolveDialog::slotUseOtherItemChoosen()
254 mResolveStrategy = ConflictHandler::UseOtherItem;
258 void ConflictResolveDialog::slotUseBothItemsChoosen()
260 mResolveStrategy = ConflictHandler::UseBothItems;
264 #include "conflictresolvedialog_p.moc"