• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.10.5 API Reference
  • KDE Home
  • Contact Us
 

akonadi

  • akonadi
  • conflicthandling
conflictresolvedialog.cpp
1 /*
2  Copyright (c) 2010 KDAB
3  Author: Tobias Koenig <tokoe@kde.org>
4 
5  This library is free software; you can redistribute it and/or modify it
6  under the terms of the GNU Library General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or (at your
8  option) any later version.
9 
10  This library is distributed in the hope that it will be useful, but WITHOUT
11  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13  License for more details.
14 
15  You should have received a copy of the GNU Library General Public License
16  along with this library; see the file COPYING.LIB. If not, write to the
17  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18  02110-1301, USA.
19 */
20 
21 #include "conflictresolvedialog_p.h"
22 
23 #include "abstractdifferencesreporter.h"
24 #include "differencesalgorithminterface.h"
25 #include "typepluginloader_p.h"
26 
27 #include <QVBoxLayout>
28 #include <QLabel>
29 
30 #include <kcolorscheme.h>
31 #include <klocale.h>
32 #include <kpushbutton.h>
33 #include <ktextbrowser.h>
34 
35 using namespace Akonadi;
36 
37 static inline QString textToHTML( const QString &text )
38 {
39  return Qt::convertFromPlainText( text );
40 }
41 
42 class HtmlDifferencesReporter : public AbstractDifferencesReporter
43 {
44  public:
45  HtmlDifferencesReporter()
46  {
47  }
48 
49  QString toHtml() const
50  {
51  return header() + mContent + footer();
52  }
53 
54  void setPropertyNameTitle( const QString &title )
55  {
56  mNameTitle = title;
57  }
58 
59  void setLeftPropertyValueTitle( const QString &title )
60  {
61  mLeftTitle = title;
62  }
63 
64  void setRightPropertyValueTitle( const QString &title )
65  {
66  mRightTitle = title;
67  }
68 
69  void addProperty( Mode mode, const QString &name, const QString &leftValue, const QString &rightValue )
70  {
71  switch ( mode ) {
72  case NormalMode:
73  mContent.append( QString::fromLatin1( "<tr><td align=\"right\"><b>%1:</b></td><td>%2</td><td></td><td>%3</td></tr>" )
74  .arg( name,
75  textToHTML( leftValue ),
76  textToHTML( rightValue ) ) );
77  break;
78  case ConflictMode:
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>" )
80  .arg( name,
81  textToHTML( leftValue ),
82  textToHTML( rightValue ) ) );
83  break;
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>" )
86  .arg( name,
87  textToHTML( leftValue ) ) );
88  break;
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>" )
91  .arg( name,
92  textToHTML( rightValue ) ) );
93  break;
94  }
95  }
96 
97  private:
98  QString header() const
99  {
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>&nbsp;</td><th align=\"center\">%3</th></tr>" )
106  .arg( mNameTitle )
107  .arg( mLeftTitle )
108  .arg( mRightTitle );
109 
110  return header;
111  }
112 
113  QString footer() const
114  {
115  return QLatin1String( "</table></center>"
116  "</body>"
117  "</html>" );
118  }
119 
120  QString mContent;
121  QString mNameTitle;
122  QString mLeftTitle;
123  QString mRightTitle;
124 };
125 
126 static void compareItems( AbstractDifferencesReporter *reporter, const Akonadi::Item &localItem, const Akonadi::Item &otherItem )
127 {
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 ) );
132  }
133 
134  if ( localItem.flags() != otherItem.flags() ) {
135  QStringList localFlags;
136  foreach ( const QByteArray &localFlag, localItem.flags() ) {
137  localFlags.append( QString::fromUtf8( localFlag ) );
138  }
139 
140  QStringList otherFlags;
141  foreach ( const QByteArray &otherFlag, otherItem.flags() ) {
142  otherFlags.append( QString::fromUtf8( otherFlag ) );
143  }
144 
145  reporter->addProperty( AbstractDifferencesReporter::ConflictMode, i18n( "Flags" ),
146  localFlags.join( QLatin1String( ", " ) ),
147  otherFlags.join( QLatin1String( ", " ) ) );
148  }
149 
150  QHash<QByteArray, QByteArray> localAttributes;
151  foreach ( Akonadi::Attribute *attribute, localItem.attributes() ) {
152  localAttributes.insert( attribute->type(), attribute->serialized() );
153  }
154 
155  QHash<QByteArray, QByteArray> otherAttributes;
156  foreach ( Akonadi::Attribute *attribute, otherItem.attributes() ) {
157  otherAttributes.insert( attribute->type(), attribute->serialized() );
158  }
159 
160  if ( localAttributes != otherAttributes ) {
161  foreach ( const QByteArray &localKey, localAttributes ) {
162  if ( !otherAttributes.contains( localKey ) ) {
163  reporter->addProperty( AbstractDifferencesReporter::AdditionalLeftMode, i18n( "Attribute: %1", QString::fromUtf8( localKey ) ),
164  QString::fromUtf8( localAttributes.value( localKey ) ),
165  QString() );
166  } else {
167  const QByteArray localValue = localAttributes.value( localKey );
168  const QByteArray otherValue = otherAttributes.value( localKey );
169  if ( localValue != otherValue ) {
170  reporter->addProperty( AbstractDifferencesReporter::ConflictMode, i18n( "Attribute: %1", QString::fromUtf8( localKey ) ),
171  QString::fromUtf8( localValue ),
172  QString::fromUtf8( otherValue ) );
173  }
174  }
175  }
176 
177  foreach ( const QByteArray &otherKey, otherAttributes ) {
178  if ( !localAttributes.contains( otherKey ) ) {
179  reporter->addProperty( AbstractDifferencesReporter::AdditionalRightMode, i18n( "Attribute: %1", QString::fromUtf8( otherKey ) ),
180  QString(),
181  QString::fromUtf8( otherAttributes.value( otherKey ) ) );
182  }
183  }
184  }
185 }
186 
187 ConflictResolveDialog::ConflictResolveDialog( QWidget *parent )
188  : KDialog( parent ), mResolveStrategy( ConflictHandler::UseBothItems )
189 {
190  setCaption( i18nc( "@title:window", "Conflict Resolution" ) );
191  setButtons( User1 | User2 | User3 );
192  setDefaultButton( User3 );
193 
194  button( User3 )->setText( i18n( "Take left one" ) );
195  button( User2 )->setText( i18n( "Take right one" ) );
196  button( User1 )->setText( i18n( "Keep both" ) );
197 
198  connect( this, SIGNAL(user1Clicked()), SLOT(slotUseBothItemsChoosen()) );
199  connect( this, SIGNAL(user2Clicked()), SLOT(slotUseOtherItemChoosen()) );
200  connect( this, SIGNAL(user3Clicked()), SLOT(slotUseLocalItemChoosen()) );
201 
202  QWidget *mainWidget = new QWidget;
203  QVBoxLayout *layout = new QVBoxLayout( mainWidget );
204 
205  QLabel* label = new QLabel( i18nc( "@label", "Two updates conflict with each other.<nl/>Please choose which update(s) to apply." ), mainWidget );
206  layout->addWidget( label );
207 
208  mView = new KTextBrowser;
209 
210  layout->addWidget( mView );
211 
212  setMainWidget( mainWidget );
213 }
214 
215 void ConflictResolveDialog::setConflictingItems( const Akonadi::Item &localItem, const Akonadi::Item &otherItem )
216 {
217  mLocalItem = localItem;
218  mOtherItem = otherItem;
219 
220  HtmlDifferencesReporter reporter;
221  compareItems( &reporter, localItem, otherItem );
222 
223  if ( mLocalItem.hasPayload() && mOtherItem.hasPayload() ) {
224 
225  QObject *object = TypePluginLoader::objectForMimeTypeAndClass( localItem.mimeType(), localItem.availablePayloadMetaTypeIds() );
226  if ( object ) {
227  DifferencesAlgorithmInterface *algorithm = qobject_cast<DifferencesAlgorithmInterface*>( object );
228  if ( algorithm ) {
229  algorithm->compare( &reporter, localItem, otherItem );
230  mView->setHtml( reporter.toHtml() );
231  return;
232  }
233  }
234 
235  reporter.addProperty( HtmlDifferencesReporter::NormalMode, i18n( "Data" ),
236  QString::fromUtf8( mLocalItem.payloadData() ),
237  QString::fromUtf8( mOtherItem.payloadData() ) );
238  }
239 
240  mView->setHtml( reporter.toHtml() );
241 }
242 
243 ConflictHandler::ResolveStrategy ConflictResolveDialog::resolveStrategy() const
244 {
245  return mResolveStrategy;
246 }
247 
248 void ConflictResolveDialog::slotUseLocalItemChoosen()
249 {
250  mResolveStrategy = ConflictHandler::UseLocalItem;
251  accept();
252 }
253 
254 void ConflictResolveDialog::slotUseOtherItemChoosen()
255 {
256  mResolveStrategy = ConflictHandler::UseOtherItem;
257  accept();
258 }
259 
260 void ConflictResolveDialog::slotUseBothItemsChoosen()
261 {
262  mResolveStrategy = ConflictHandler::UseBothItems;
263  accept();
264 }
265 
266 #include "moc_conflictresolvedialog_p.cpp"
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Sat Jul 13 2013 01:27:34 by doxygen 1.8.3.1 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

akonadi

Skip menu "akonadi"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • Modules
  • Related Pages

kdepimlibs-4.10.5 API Reference

Skip menu "kdepimlibs-4.10.5 API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal