summaryrefslogtreecommitdiffstats
path: root/certmanager/conf/appearanceconfigwidget.cpp
blob: 19224cf9fe2887304055b418609d10a0de388ae3 (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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/*
    appearanceconfigwidget.cpp

    This file is part of kleopatra, the KDE key manager
    Copyright (c) 2002,2004 Klarälvdalens Datakonsult AB
    Copyright (c) 2002,2003 Marc Mutz <mutz@kde.org>

    Libkleopatra 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 of the
    License, or (at your option) any later version.

    Libkleopatra 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.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

    In addition, as a special exception, the copyright holders give
    permission to link the code of this program with any edition of
    the TQt library by Trolltech AS, Norway (or with modified versions
    of TQt that use the same license as TQt), and distribute linked
    combinations including the two.  You must obey the GNU General
    Public License in all respects for all of the code used other than
    TQt.  If you modify this file, you may extend this exception to
    your version of the file, but you are not obligated to do so.  If
    you do not wish to do so, delete this exception statement from
    your version.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "appearanceconfigwidget.h"

#include <kleo/cryptobackendfactory.h>
#include <kleo/keyfiltermanager.h>

#include <tdelistview.h>
#include <tdeconfig.h>
#include <kdialog.h>
#include <klocale.h>
#include <kdebug.h>
#include <kmessagebox.h>
#include <tdefontdialog.h>
#include <kcolordialog.h>

#include <tqpushbutton.h>
#include <tqlayout.h>
#include <tqheader.h>
#include <tqcolor.h>
#include <tqfont.h>
#include <tqstring.h>
#include <tqpainter.h>
#include <tqregexp.h>
#include <tqcheckbox.h>

#include <assert.h>

using namespace Kleo;

class CategoryListViewItem : public TQListViewItem
{
public:
  CategoryListViewItem( TQListView* lv, TQListViewItem* prev, const TDEConfigBase& config )
    : TQListViewItem( lv, prev ) {

    setName( config.readEntry( "Name", i18n("<unnamed>") ) );
    mForegroundColor = config.readColorEntry( "foreground-color" );
    mBackgroundColor = config.readColorEntry( "background-color" );
    mHasFont = config.hasKey( "font" );
    if ( mHasFont ) {
      setFont( config.readFontEntry( "font" ) ); // sets mItalic and mBold
    }
    else {
      mItalic = config.readBoolEntry( "font-italic", false );
      mBold = config.readBoolEntry( "font-bold", false );
    }
    mStrikeOut = config.readBoolEntry( "font-strikeout", false );
    mIsExpired = config.readBoolEntry( "is-expired", false );
    mDirty = false;
  }

  void save( TDEConfigBase& config ) {
    config.writeEntry( "Name", text( 0 ) );
    config.writeEntry( "foreground-color", mForegroundColor );
    config.writeEntry( "background-color", mBackgroundColor );
    if ( mHasFont )
      config.writeEntry( "font", mFont );
    else {
      config.deleteEntry( "font" );
      config.writeEntry( "font-italic", mItalic );
      config.writeEntry( "font-bold", mBold );
    }
    config.writeEntry( "font-strikeout", mStrikeOut );
  }

  void setForegroundColor( const TQColor& foreground ) { mForegroundColor = foreground; mDirty = true; }
  void setBackgroundColor( const TQColor& background ) { mBackgroundColor = background; mDirty = true; }
  void setFont( const TQFont& font ) {
    mFont = font;
    mHasFont = true;
    mItalic = font.italic();
    mBold = font.bold();
    mDirty = true;
  }

  TQColor foregroundColor() const { return mForegroundColor; }
  TQColor backgroundColor() const { return mBackgroundColor; }
  TQFont font() const { return mFont; }

  void setDefaultAppearance() {
    mForegroundColor = mIsExpired ? TQt::red : TQColor();
    mBackgroundColor = TQColor();
    mHasFont = false;
    mFont = TQFont();
    mBold = false;
    mItalic = false;
    mStrikeOut = false;
    mDirty = true;
  }

  bool isDirty() const { return mDirty; }
  bool isItalic() const { return mItalic; }
  bool isBold() const { return mBold; }
  bool isStrikeout() const { return mStrikeOut; }
  bool hasFont() const { return mHasFont; }

  void toggleItalic() { mItalic = !mItalic; if ( mHasFont ) mFont.setItalic( mItalic ); mDirty = true; }
  void toggleBold() { mBold = !mBold; if ( mHasFont ) mFont.setBold( mBold ); mDirty = true; }
  void toggleStrikeout() { mStrikeOut = !mStrikeOut; mDirty = true; }

private:
  void setName( const TQString& name ) {
    setText( 0, name );
  }

  void paintCell( TQPainter * p, const TQColorGroup & cg, int column, int width, int alignment );

private:
  TQColor mForegroundColor, mBackgroundColor;
  TQFont mFont;
  bool mHasFont;
  bool mIsExpired; // used for default settings
  bool mItalic;
  bool mBold;
  bool mStrikeOut;
  bool mDirty;
};

void CategoryListViewItem::paintCell( TQPainter * p, const TQColorGroup & cg, int column, int width, int alignment ) {
  TQColorGroup _cg = cg;
  TQFont font = p->font();
  if ( mHasFont )
    font = mFont;
  else {
    if ( mItalic )
      font.setItalic( true );
    if ( mBold )
      font.setBold( true );
  }
  if ( mStrikeOut )
    font.setStrikeOut( true );
  p->setFont( font );

  if ( mForegroundColor.isValid() )
    _cg.setColor( TQColorGroup::Text, mForegroundColor );
  if ( mBackgroundColor.isValid() )
    _cg.setColor( TQColorGroup::Base, mBackgroundColor );

  TQListViewItem::paintCell( p, _cg, column, width, alignment );
}

////

Kleo::AppearanceConfigWidget::AppearanceConfigWidget (
  TQWidget* parent,  const char* name, WFlags fl )
  : AppearanceConfigWidgetBase( parent, name, fl )
{
    categoriesLV->setSorting( -1 );
    load();
}


/*
 *  Destroys the object and frees any allocated resources
 */

AppearanceConfigWidget::~AppearanceConfigWidget()
{
  // no need to delete child widgets, TQt does it all for us
}


void AppearanceConfigWidget::slotSelectionChanged( TQListViewItem* item )
{
  bool sel = item != 0;
  foregroundButton->setEnabled( sel );
  backgroundButton->setEnabled( sel );
  fontButton->setEnabled( sel );
  italicCB->setEnabled( item );
  boldCB->setEnabled( item );
  strikeoutCB->setEnabled( item );
  defaultLookPB->setEnabled( sel );
  if ( item ) {
    CategoryListViewItem* clvi = static_cast<CategoryListViewItem *>( item );
    italicCB->setChecked( clvi->isItalic() );
    boldCB->setChecked( clvi->isBold() );
    strikeoutCB->setChecked( clvi->isStrikeout() );
  } else {
    italicCB->setChecked( false );
    boldCB->setChecked( false );
    strikeoutCB->setChecked( false );
  }
}

/*
 * set default appearance for selected category
 */
void AppearanceConfigWidget::slotDefaultClicked()
{
  CategoryListViewItem* item = static_cast<CategoryListViewItem*>(categoriesLV->selectedItem() );
  if ( !item )
    return;
  item->setDefaultAppearance();
  item->repaint();
  slotSelectionChanged( item );
  emit changed();
}

void AppearanceConfigWidget::load()
{
  categoriesLV->clear();
  TDEConfig * config = Kleo::CryptoBackendFactory::instance()->configObject();
  if ( !config )
    return;
  TQStringList groups = config->groupList().grep( TQRegExp( "^Key Filter #\\d+$" ) );
  for ( TQStringList::const_iterator it = groups.begin() ; it != groups.end() ; ++it ) {
    TDEConfigGroup cfg( config, *it );
    (void) new CategoryListViewItem( categoriesLV, categoriesLV->lastItem(), cfg );
  }
}

void AppearanceConfigWidget::save()
{
  TDEConfig * config = Kleo::CryptoBackendFactory::instance()->configObject();
  if ( !config )
    return;
  // We know (assume) that the groups in the config object haven't changed,
  // so we just iterate over them and over the listviewitems, and map one-to-one.
  TQStringList groups = config->groupList().grep( TQRegExp( "^Key Filter #\\d+$" ) );
  if ( groups.isEmpty() ) {
    // If we created the default categories ourselves just now, then we need to make up their list
    TQListViewItemIterator lvit( categoriesLV );
    for ( ; lvit.current() ; ++lvit )
      groups << lvit.current()->text( 0 );
  }

  TQListViewItemIterator lvit( categoriesLV );
  for ( TQStringList::const_iterator it = groups.begin() ; it != groups.end() && lvit.current(); ++it, ++lvit ) {
    CategoryListViewItem* item = static_cast<CategoryListViewItem*>(lvit.current() );
    TDEConfigGroup cfg( config, *it );
    item->save( cfg );
  }
  config->sync();
  Kleo::KeyFilterManager::instance()->reload();
}


void AppearanceConfigWidget::slotForegroundClicked() {
  CategoryListViewItem* item = static_cast<CategoryListViewItem*>(categoriesLV->selectedItem() );
  Q_ASSERT( item );
  if( !item )
    return;
  TQColor fg = item->foregroundColor();
  int result = KColorDialog::getColor( fg );
  if ( result == KColorDialog::Accepted ) {
    item->setForegroundColor( fg );
    item->repaint();
    emit changed();
  }
}

void AppearanceConfigWidget::slotBackgroundClicked() {
  CategoryListViewItem* item = static_cast<CategoryListViewItem*>(categoriesLV->selectedItem() );
  Q_ASSERT( item );
  if( !item )
    return;
  TQColor bg = item->backgroundColor();
  int result = KColorDialog::getColor( bg );
  if ( result == KColorDialog::Accepted ) {
    item->setBackgroundColor( bg );
    item->repaint();
    emit changed();
  }
}

void AppearanceConfigWidget::slotFontClicked() {
  CategoryListViewItem* item = static_cast<CategoryListViewItem*>(categoriesLV->selectedItem() );
  Q_ASSERT( item );
  if( !item )
    return;
  TQFont font = item->font();
  int result = TDEFontDialog::getFont( font );
  if ( result == TDEFontDialog::Accepted ) {
    item->setFont( font );
    item->repaint();
    emit changed();
  }
}

void AppearanceConfigWidget::defaults()
{
  // This simply means "default look for every category"
  TQListViewItemIterator lvit( categoriesLV );
  for ( ; lvit.current() ; ++lvit ) {
    CategoryListViewItem* item = static_cast<CategoryListViewItem *>( lvit.current() );
    item->setDefaultAppearance();
    item->repaint();
  }
  emit changed();
}

void AppearanceConfigWidget::slotItalicClicked()
{
  CategoryListViewItem* item = static_cast<CategoryListViewItem*>(categoriesLV->selectedItem() );
  if ( item ) {
    item->toggleItalic();
    item->repaint();
    emit changed();
  }
}

void AppearanceConfigWidget::slotBoldClicked()
{
  CategoryListViewItem* item = static_cast<CategoryListViewItem*>(categoriesLV->selectedItem() );
  if ( item ) {
    item->toggleBold();
    item->repaint();
    emit changed();
  }
}

void AppearanceConfigWidget::slotStrikeoutClicked()
{
  CategoryListViewItem* item = static_cast<CategoryListViewItem*>(categoriesLV->selectedItem() );
  if ( item ) {
    item->toggleStrikeout();
    item->repaint();
    emit changed();
  }
}

#include "appearanceconfigwidget.moc"