34 #include <config-kleopatra.h>
37 #include "ui_appearanceconfigwidget.h"
42 #include "libkleo/kleo/cryptobackendfactory.h"
43 #include "libkleo/kleo/keyfiltermanager.h"
44 #include "libkleo/kleo/dn.h"
45 #include "libkleo/ui/dnattributeorderconfigwidget.h"
47 #ifdef KDEPIM_ONLY_KLEO
50 # include <kicondialog.h>
55 #include <kconfiggroup.h>
62 #include <QApplication>
63 #include <QColorDialog>
64 #include <QFontDialog>
66 #include <boost/range.hpp>
67 #include <boost/bind.hpp>
73 using namespace Kleo::Config;
74 using namespace boost;
75 #ifdef KDEPIM_ONLY_KLEO
76 using namespace Kleo::KioAvoidance;
97 if (
const QListWidget *
const lw = item->listWidget() )
99 return QApplication::font(
"QListWidget" );
102 static bool is(
const QListWidgetItem * item,
bool (QFont::*func)()
const ) {
105 const QVariant v = item->data( Qt::FontRole );
106 if ( !v.isValid() || v.type() != QVariant::Font )
108 return (v.value<QFont>().*func)();
112 return is( item, &QFont::italic );
114 static bool is_bold(
const QListWidgetItem * item ) {
115 return is( item, &QFont::bold );
118 return is( item, &QFont::strikeOut );
122 static void set( QListWidgetItem * item,
bool on,
void (QFont::*func)(
bool) ) {
125 const QVariant v = item->data( Qt::FontRole );
126 QFont font = v.isValid() && v.type() == QVariant::Font ? v.value<QFont>() :
tryToFindFontFor( item ) ;
128 item->setData( Qt::FontRole, font );
132 set( item, on, &QFont::setItalic );
134 static void set_bold( QListWidgetItem * item,
bool on ) {
135 set( item, on, &QFont::setBold );
138 set( item, on, &QFont::setStrikeOut );
142 static void apply_config(
const KConfigGroup & group, QListWidgetItem * item ) {
146 const QString
name = group.readEntry(
"Name" );
147 item->setText( name.isEmpty() ? i18nc(
"Key filter without user-assigned name",
"<unnamed>") :
name );
151 const QColor fg = group.readEntry(
"foreground-color", QColor() );
152 item->setData( Qt::ForegroundRole, fg.isValid() ? QBrush( fg ) : QVariant() );
155 const QColor bg = group.readEntry(
"background-color", QColor() );
156 item->setData( Qt::BackgroundRole, bg.isValid() ? QBrush( bg ) : QVariant() );
160 if ( group.hasKey(
"font" ) ) {
161 const QFont font = group.readEntry(
"font", defaultFont );
162 item->setData( Qt::FontRole, font != defaultFont ? font : QVariant() );
165 QFont font = defaultFont;
166 font.setStrikeOut( group.readEntry(
"font-strikeout",
false ) );
167 font.setItalic( group.readEntry(
"font-italic",
false ) );
168 font.setBold( group.readEntry(
"font-bold",
false ) );
169 item->setData( Qt::FontRole, font );
177 const QString iconName = group.readEntry(
"icon" );
178 item->setData( Qt::DecorationRole, iconName.isEmpty() ? QVariant() : KIcon( iconName ) );
179 item->setData(
IconNameRole, iconName.isEmpty() ? QVariant() : iconName );
184 if ( item && item->data( allowRole ).toBool() )
185 item->setData( role, QVariant() );
189 static void erase_if_allowed( QListWidgetItem * item,
const int role[],
size_t numRoles,
int allowRole ) {
190 if ( item && item->data( allowRole ).toBool() )
191 for (
unsigned int i = 0 ; i < numRoles ; ++i )
192 item->setData( role[i], QVariant() );
195 static void erase_if_allowed( QListWidgetItem * item,
int role,
const int allowRole[],
size_t numAllowRoles ) {
198 for (
unsigned int i = 0 ; i < numAllowRoles ; ++i )
199 if ( !item->data( allowRole[i] ).toBool() )
201 item->setData( role, QVariant() );
205 static void erase_if_allowed( QListWidgetItem * item,
const int role[],
size_t numRoles,
const int allowRole[],
size_t numAllowRoles ) {
208 for (
unsigned int i = 0 ; i < numAllowRoles ; ++i )
209 if ( !item->data( allowRole[i] ).toBool() )
211 for (
unsigned int i = 0 ; i < numRoles ; ++i )
212 item->setData( role[i], QVariant() );
221 static const int fontRoles[] = { Qt::FontRole,
HasFontRole };
222 static const int fontAllowRoles[] = {
228 erase_if_allowed( item, fontRoles, size( fontRoles ), fontAllowRoles, size( fontAllowRoles ) );
231 static void writeOrDelete( KConfigGroup & group,
const char * key,
const QVariant & value ) {
232 if ( value.isValid() )
233 group.writeEntry( key, value );
235 group.deleteEntry( key );
240 if ( v.type() == QVariant::Color )
242 else if ( v.type() == QVariant::Brush )
243 return v.value<QBrush>().color();
248 static void save_to_config(
const QListWidgetItem * item, KConfigGroup & group ) {
256 group.deleteEntry(
"font" );
257 group.deleteEntry(
"font-strikeout" );
258 group.deleteEntry(
"font-italic" );
259 group.deleteEntry(
"font-bold" );
267 group.writeEntry(
"font-strikeout",
true );
269 group.writeEntry(
"font-italic",
true );
271 group.writeEntry(
"font-bold",
true );
277 if ( item && !item->data( allowRole ).toBool() ) {
278 w->setEnabled(
false );
279 w->setToolTip( i18n(
"This parameter has been locked down by the system administrator." ) );
281 w->setEnabled( item );
282 w->setToolTip( QString() );
292 class AppearanceConfigWidget::Private :
public Ui_AppearanceConfigWidget {
293 friend class ::Kleo::Config::AppearanceConfigWidget;
297 : Ui_AppearanceConfigWidget(),
303 if ( QLayout *
const l =
q->layout() )
307 dnOrderWidget = Kleo::DNAttributeMapper::instance()->configWidget( w );
308 dnOrderWidget->setObjectName( QLatin1String(
"dnOrderWidget" ) );
309 (
new QVBoxLayout( w ) )->addWidget( dnOrderWidget );
311 tabWidget->addTab( w, i18n(
"DN-Attribute Order") );
313 connect( dnOrderWidget, SIGNAL(changed()),
q, SIGNAL(changed()) );
315 connect( iconButton, SIGNAL(clicked()),
q, SLOT(slotIconClicked()) );
316 #ifndef QT_NO_COLORDIALOG
317 connect( foregroundButton, SIGNAL(clicked()),
q, SLOT(slotForegroundClicked()) );
318 connect( backgroundButton, SIGNAL(clicked()),
q, SLOT(slotBackgroundClicked()) );
320 foregroundButton->hide();
321 backgroundButton->hide();
323 #ifndef QT_NO_FONTDIALOG
324 connect( fontButton, SIGNAL(clicked()),
q, SLOT(slotFontClicked()) );
328 connect( categoriesLV, SIGNAL(itemSelectionChanged()),
q, SLOT(slotSelectionChanged()) );
329 connect( defaultLookPB, SIGNAL(clicked()),
q, SLOT(slotDefaultClicked()) );
330 connect( italicCB, SIGNAL(toggled(
bool)),
q, SLOT(slotItalicToggled(
bool)) );
331 connect( boldCB, SIGNAL(toggled(
bool)),
q, SLOT(slotBoldToggled(
bool)) );
332 connect( strikeoutCB, SIGNAL(toggled(
bool)),
q, SLOT(slotStrikeOutToggled(
bool)) );
333 connect( tooltipValidityCheckBox, SIGNAL(toggled(
bool)),
q, SLOT(slotTooltipValidityChanged(
bool)) );
334 connect( tooltipOwnerCheckBox, SIGNAL(toggled(
bool)),
q, SLOT(slotTooltipOwnerChanged(
bool)) );
335 connect( tooltipDetailsCheckBox, SIGNAL(toggled(
bool)),
q, SLOT(slotTooltipDetailsChanged(
bool)) );
339 void enableDisableActions( QListWidgetItem * item );
340 QListWidgetItem * selectedItem()
const;
343 void slotIconClicked();
344 #ifndef QT_NO_COLORDIALOG
345 void slotForegroundClicked();
346 void slotBackgroundClicked();
348 #ifndef QT_NO_FONTDIALOG
349 void slotFontClicked();
351 void slotSelectionChanged();
352 void slotDefaultClicked();
353 void slotItalicToggled(
bool);
354 void slotBoldToggled(
bool);
355 void slotStrikeOutToggled(
bool);
356 void slotTooltipValidityChanged(
bool);
357 void slotTooltipOwnerChanged(
bool);
358 void slotTooltipDetailsChanged(
bool);
361 Kleo::DNAttributeOrderConfigWidget * dnOrderWidget;
365 :
QWidget( p, f ),
d( new Private( this ) )
373 void AppearanceConfigWidget::Private::slotSelectionChanged() {
374 enableDisableActions( selectedItem() );
377 QListWidgetItem * AppearanceConfigWidget::Private::selectedItem()
const {
379 return items.empty() ? 0 : items.front() ;
382 void AppearanceConfigWidget::Private::enableDisableActions( QListWidgetItem * item ) {
384 #ifndef QT_NO_COLORDIALOG
388 #ifndef QT_NO_FONTDIALOG
395 defaultLookPB->setEnabled( item );
397 italicCB->setChecked(
is_italic( item ) );
398 boldCB->setChecked(
is_bold( item ) );
402 void AppearanceConfigWidget::Private::slotDefaultClicked() {
404 QListWidgetItem *
const item = selectedItem();
409 enableDisableActions( item );
417 for (
int i = 0, end = d->categoriesLV->count() ; i != end ; ++i )
419 d->tooltipValidityCheckBox->setChecked(
true );
420 d->tooltipOwnerCheckBox->setChecked(
false );
421 d->tooltipDetailsCheckBox->setChecked(
false );
423 d->dnOrderWidget->defaults();
430 d->dnOrderWidget->load();
432 d->categoriesLV->clear();
433 KConfig *
const config = CryptoBackendFactory::instance()->configObject();
436 const QStringList groups = config->groupList().filter( QRegExp( QLatin1String(
"^Key Filter #\\d+$") ) );
437 Q_FOREACH(
const QString & group, groups ) {
439 apply_config( KConfigGroup( config, group ),
new QListWidgetItem( d->categoriesLV ) );
443 d->tooltipValidityCheckBox->setChecked( prefs.
showValidity() );
450 d->dnOrderWidget->save();
458 KConfig *
const config = CryptoBackendFactory::instance()->configObject();
463 const QStringList groups = config->groupList().filter( QRegExp( QLatin1String(
"^Key Filter #\\d+$") ) );
465 if ( groups.isEmpty() ) {
467 Q3ListViewItemIterator lvit( categoriesLV );
468 for ( ; lvit.current() ; ++lvit )
469 groups << lvit.current()->text( 0 );
472 for (
int i = 0, end = std::min( groups.size(), d->categoriesLV->count() ) ; i != end ; ++i ) {
473 const QListWidgetItem *
const item = d->categoriesLV->item( i );
475 KConfigGroup group( config, groups[i] );
480 KeyFilterManager::instance()->reload();
483 void AppearanceConfigWidget::Private::slotIconClicked() {
484 QListWidgetItem *
const item = selectedItem();
489 const QString iconName = KIconDialog::getIcon(
490 KIconLoader::Desktop, KIconLoader::Application,
false, 0,
false,
493 if ( iconName.isEmpty() )
498 item->setIcon( KIcon( iconName ) );
504 #ifndef QT_NO_COLORDIALOG
505 void AppearanceConfigWidget::Private::slotForegroundClicked() {
506 QListWidgetItem *
const item = selectedItem();
510 const QVariant v =
brush2color( item->data( Qt::ForegroundRole ) );
512 const QColor initial = v.isValid() ? v.value<QColor>() : categoriesLV->palette().color( QPalette::Normal, QPalette::Text );
513 const QColor c = QColorDialog::getColor( initial,
q );
516 item->setData( Qt::ForegroundRole, QBrush( c ) );
521 void AppearanceConfigWidget::Private::slotBackgroundClicked() {
522 QListWidgetItem *
const item = selectedItem();
526 const QVariant v =
brush2color( item->data( Qt::BackgroundRole ) );
528 const QColor initial = v.isValid() ? v.value<QColor>() : categoriesLV->palette().color( QPalette::Normal, QPalette::Base );
529 const QColor c = QColorDialog::getColor( initial,
q );
532 item->setData( Qt::BackgroundRole, QBrush( c ) );
536 #endif // QT_NO_COLORDIALOG
538 #ifndef QT_NO_FONTDIALOG
539 void AppearanceConfigWidget::Private::slotFontClicked() {
540 QListWidgetItem *
const item = selectedItem();
544 const QVariant v = item->data( Qt::FontRole );
548 const QFont initial = v.isValid() && v.type() == QVariant::Font ? v.value<QFont>() : defaultFont ;
549 QFont f = QFontDialog::getFont( &ok, initial,
q );
555 f.setItalic( initial.italic() );
557 f.setBold( initial.bold() );
559 f.setStrikeOut( initial.strikeOut() );
561 item->setData( Qt::FontRole, f != defaultFont ? f : QVariant() );
565 #endif // QT_NO_FONTDIALOG
567 void AppearanceConfigWidget::Private::slotItalicToggled(
bool on ) {
572 void AppearanceConfigWidget::Private::slotBoldToggled(
bool on ) {
577 void AppearanceConfigWidget::Private::slotStrikeOutToggled(
bool on ) {
582 void AppearanceConfigWidget::Private::slotTooltipValidityChanged(
bool )
587 void AppearanceConfigWidget::Private::slotTooltipOwnerChanged(
bool )
592 void AppearanceConfigWidget::Private::slotTooltipDetailsChanged(
bool )
598 #include "appearanceconfigwidget.moc"