summaryrefslogtreecommitdiffabout
path: root/pwmanager/pwmanager/kcmconfigs/pwmconfigwidget.cpp
blob: a8696ead2dbd457ddd4f6d6c219e00b53f2a7173 (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
/*
    This file is part of KAddressBook.
    Copyright (c) 2003 Tobias Koenig <tokoe@kde.org>

    This program 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.

    This program 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

    As a special exception, permission is given to link this program
    with any edition of Qt, and distribute the resulting executable,
    without including the source code for Qt in the source distribution.
*/

#include <qcheckbox.h>
#include <qframe.h>
#include <qgroupbox.h>
#include <qlayout.h>
#include <qpushbutton.h>
#include <qtabwidget.h>
#include <qcombobox.h>
#include <qlineedit.h>
#include <qspinbox.h>
#include <qlabel.h>
#include <qfile.h>
#include <qvbox.h>

#include <kconfig.h>
#include <kdebug.h>
#include <kdialog.h>
#include <klistview.h>
#include <klocale.h>
#include <kglobal.h>
#include <kmessagebox.h>
#include <kstandarddirs.h>
#include <kio/kfile/kurlrequester.h>

#include "pwmprefs.h"

#include "pwmconfigwidget.h"
#include "pwmexception.h"

PWMConfigWidget::PWMConfigWidget(PWMPrefs *prefs, QWidget *parent, const char *name )
  : KPrefsWidget(prefs,  parent, name )
{
  QVBoxLayout *topLayout = new QVBoxLayout( this, 0,
                                            KDialog::spacingHint() );

  QTabWidget *tabWidget = new QTabWidget( this );
  topLayout->addWidget( tabWidget );

  // windowsStyle page
  //////////////////////////////////////////////////////
  QWidget *windowStylePage = new QWidget( this );
  QGridLayout *windowStyleLayout = new QGridLayout( windowStylePage, 3, 3);
  
  int i = 0;
  KPrefsWidRadios * windowStyle = addWidRadios(i18n("Window-style:") ,&(prefs->mMainViewStyle), windowStylePage);
  windowStyle->addRadio(i18n("Category on top"));
  windowStyle->addRadio(i18n("Category-list left/top")); 
  windowStyleLayout->addMultiCellWidget( (QWidget*)windowStyle->groupBox(),i,i,0,2);
   ++i;   
   
   QLabel* lab = new QLabel(i18n("<b>Font for Password entries:</b>"), windowStylePage);
   windowStyleLayout->addMultiCellWidget( lab,i,i,0,2);
   

   ++i;
  
  KPrefsWidFont *selEntrFont =
      addWidFont(i18n("Password"),i18n("Font:"),
                 &(prefs->mEntryFont),windowStylePage);
  windowStyleLayout->addWidget(selEntrFont->label(),i,0);
  windowStyleLayout->addWidget(selEntrFont->preview(),i,1);
  windowStyleLayout->addWidget(selEntrFont->button(),i,2);
  ++i;
  lab = new QLabel(i18n(""), windowStylePage);
   windowStyleLayout->addMultiCellWidget( lab,i,i,0,2);

  // File page
  //////////////////////////////////////////////////////
  QWidget *filePage = new QWidget( this );
  QGridLayout *fileLayout = new QGridLayout( filePage, 3, 2);
  
  i = 0;
  QLabel* kcfg_compression_label = new QLabel(i18n("Compression:"), filePage);
  fileLayout->addWidget(kcfg_compression_label,i,0);
  kcfg_compression = new QComboBox(filePage, "kcfg_compression");
  kcfg_compression->insertItem(i18n("None"));
  kcfg_compression->insertItem(i18n("gzip"));
  //US not yet supported:  kcfg_compression->insertItem(i18n("bzip2"));
  fileLayout->addWidget( kcfg_compression,i,1);
  ++i;   

  QLabel* kcfg_crypt_label = new QLabel(i18n("Encryption:"), filePage);
  fileLayout->addWidget(kcfg_crypt_label,i,0);
  kcfg_cryptAlgo = new QComboBox(filePage, "kcfg_cryptAlgo");
  kcfg_cryptAlgo->insertItem(i18n("Blowfish (128 bit)"));
#ifdef CONFIG_PWMANAGER_GCRY
  kcfg_cryptAlgo->insertItem(i18n("AES-128, Rijndael (128 bit)"));
  kcfg_cryptAlgo->insertItem(i18n("AES-192, Rijndael (192 bit)"));
  kcfg_cryptAlgo->insertItem(i18n("AES-256, Rijndael (256 bit)"));
  kcfg_cryptAlgo->insertItem(i18n("Triple-DES (168 bit)"));
  kcfg_cryptAlgo->insertItem(i18n("Twofish (256 bit)"));
  kcfg_cryptAlgo->insertItem(i18n("Twofish-128 (128 bit)"));
#endif // CONFIG_PWMANAGER_GCRY
  fileLayout->addWidget( kcfg_cryptAlgo,i,1);
  ++i;   

  QLabel* kcfg_hash_label = new QLabel(i18n("Hashing:"), filePage);
  fileLayout->addWidget(kcfg_hash_label,i,0);
  kcfg_hashAlgo = new QComboBox(filePage, "kcfg_hashAlgo");
  kcfg_hashAlgo->insertItem(i18n("SHA-160, SHA1 (160 bit)"));
#ifdef CONFIG_PWMANAGER_GCRY
  kcfg_hashAlgo->insertItem(i18n("SHA-256 (256 bit)"));
  kcfg_hashAlgo->insertItem(i18n("SHA-384 (384 bit)"));
  kcfg_hashAlgo->insertItem(i18n("SHA-512 (512 bit)"));
  kcfg_hashAlgo->insertItem(i18n("MD5 (128 bit)"));
  kcfg_hashAlgo->insertItem(i18n("RIPE-MD-160 (160 bit)"));
  kcfg_hashAlgo->insertItem(i18n("Tiger (192 bit)"));
#endif // CONFIG_PWMANAGER_GCRY
  fileLayout->addWidget( kcfg_hashAlgo,i,1);
  ++i;   

  permissionLineEdit = new QLineEdit(filePage);
  QLabel* permissionLineLabel = new QLabel(permissionLineEdit, i18n("Permissions:"), filePage);
  fileLayout->addWidget(permissionLineLabel,i,0);
  fileLayout->addWidget(permissionLineEdit,i,1);
  ++i;

  KPrefsWidBool *sb = addWidBool(i18n("Make backup before saving"),
                      &(prefs->mMakeFileBackup),filePage);
  fileLayout->addMultiCellWidget(sb->checkBox(), i,i,0,1);
  ++i;   

  // Timeout page
  //////////////////////////////////////////////////////
  QWidget *timeoutPage = new QWidget( this );
  QGridLayout *timeoutLayout = new QGridLayout( timeoutPage, 3, 2);

  i = 0;
  pwTimeoutSpinBox = new QSpinBox( 0,600,10,timeoutPage, "pwTimeoutSpinBox" );
  QLabel* timeoutLabel = new QLabel(pwTimeoutSpinBox, i18n("Password timeout\n(timeout to hold password in\nmemory,so you don't have to\nre-enter it,if you\nalready have entered it)\n[set to 0 to disable]:"), timeoutPage);
  timeoutLayout->addMultiCellWidget(timeoutLabel,i, i, 0 ,0);
  timeoutLayout->addWidget(pwTimeoutSpinBox,i,1);
  ++i;

  lockTimeoutSpinBox = new QSpinBox( 0,600,10,timeoutPage, "lockTimeoutSpinBox" );
  QLabel* lockTimeoutLabel = new QLabel(lockTimeoutSpinBox, i18n("Auto-lock timeout\n(auto lock document after this\namount of seconds)\n[set to 0 to disable]:"), timeoutPage);
  timeoutLayout->addMultiCellWidget(lockTimeoutLabel,i, i, 0 ,0);
  timeoutLayout->addWidget(lockTimeoutSpinBox,i,1);
  ++i;

  sb = addWidBool(i18n("deep-lock on autolock"),
                      &(prefs->mAutoDeeplock),timeoutPage);
  timeoutLayout->addMultiCellWidget(sb->checkBox(), i,i,0,1);
  ++i;   
 

  // Autostart page
  //////////////////////////////////////////////////////
  QWidget *autostartPage = new QWidget( this );
  QGridLayout *autostartLayout = new QGridLayout( autostartPage, 3, 2);

  i = 0;

  autostartLineEdit = new KURLRequester(autostartPage, "autoStartLineEdit");
  QLabel* autostartLineLabel = new QLabel(autostartLineEdit, "Open this file automatically on startup:",autostartPage);
  autostartLayout->addMultiCellWidget(autostartLineLabel,i,i,0,1);
  ++i;
  autostartLayout->addMultiCellWidget(autostartLineEdit,i,i,0,1);
  ++i;

  sb = addWidBool(i18n("open deeplocked"),
                      &(prefs->mAutostartDeeplocked),autostartPage);
  autostartLayout->addMultiCellWidget(sb->checkBox(), i,i,0,1);
  ++i;   


  // external app page
  //////////////////////////////////////////////////////
  QWidget *externalappPage = new QWidget( this );
  QGridLayout *externalappLayout = new QGridLayout( externalappPage, 3, 2);

  i = 0;

  browserLineEdit = new QLineEdit(externalappPage);
  QLabel* browserLineLabel = new QLabel(browserLineEdit, i18n("Favourite browser:"), externalappPage);
  externalappLayout->addWidget(browserLineLabel,i,0);
  externalappLayout->addWidget(browserLineEdit,i,1);
  ++i;

  xtermLineEdit = new QLineEdit(externalappPage);
  QLabel* xtermLineLabel = new QLabel(xtermLineEdit, i18n("Favourite x-terminal:"), externalappPage);
  externalappLayout->addWidget(xtermLineLabel,i,0);
  externalappLayout->addWidget(xtermLineEdit,i,1);
  ++i;


  // miscelaneous page
  //////////////////////////////////////////////////////
  QWidget *miscPage = new QWidget( this );
  QGridLayout *miscLayout = new QGridLayout( miscPage, 3, 2);

  i = 0;

  /*US ENH: PWM/Pi has no tray and con be minimized
  sb = addWidBool(i18n("Show icon in system-tray"),&(prefs->mTray),miscPage);
  miscLayout->addMultiCellWidget(sb->checkBox(), i,i,0,1);
  ++i;   
  */

  sb = addWidBool(i18n("Open document with passwords unlocked"),&(prefs->mUnlockOnOpen),miscPage);
  miscLayout->addMultiCellWidget(sb->checkBox(), i,i,0,1);
  ++i;   

  /*US ENH: PWM/Pi has no tray and con be minimized
  sb = addWidBool(i18n("auto-minimize to tray on startup"),&(prefs->mAutoMinimizeOnStart),miscPage);
  miscLayout->addMultiCellWidget(sb->checkBox(), i,i,0,1);
  ++i;   

  KPrefsWidRadios * minimizeRadio = addWidRadios(i18n("auto-lock on minimize:") ,&(prefs->mMinimizeLock), miscPage);
  minimizeRadio->addRadio(i18n("don't lock"));
  minimizeRadio->addRadio(i18n("normal lock")); 
  minimizeRadio->addRadio(i18n("deep-lock")); 
  miscLayout->addMultiCellWidget( (QWidget*)minimizeRadio->groupBox(),i,i,0,2);
   ++i;   

  sb = addWidBool(i18n("KWallet emulation"),&(prefs->mKWalletEmu),miscPage);
  miscLayout->addMultiCellWidget(sb->checkBox(), i,i,0,1);
  ++i;   

  sb = addWidBool(i18n("Close instead Minimize into tray"),&(prefs->mClose),miscPage);
  miscLayout->addMultiCellWidget(sb->checkBox(), i,i,0,1);
  ++i;   
  */


  tabWidget->addTab( windowStylePage, i18n( "Look && feel" ) );
  tabWidget->addTab( filePage, i18n( "File" ) );
  tabWidget->addTab( timeoutPage, i18n( "Timeout" ) );
  tabWidget->addTab( autostartPage, i18n( "Autostart" ) );
  tabWidget->addTab( externalappPage, i18n( "External apps" ) );
  tabWidget->addTab( miscPage, i18n( "Miscellaneous" ) );


  connect( permissionLineEdit, SIGNAL( textChanged(const QString&) ), this, SLOT( modified() ) );
  connect( pwTimeoutSpinBox, SIGNAL( valueChanged(int) ), this, SLOT( modified() ) );
  connect( lockTimeoutSpinBox, SIGNAL( valueChanged(int) ), this, SLOT( modified() ) );
  connect( autostartLineEdit, SIGNAL( textChanged(const QString&) ), this, SLOT( modified() ) );
  connect( browserLineEdit, SIGNAL( textChanged(const QString&) ), this, SLOT( modified() ) );
  connect( xtermLineEdit, SIGNAL( textChanged(const QString&) ), this, SLOT( modified() ) );

}


void PWMConfigWidget::usrReadConfig()
{
  PWMPrefs* prefs = PWMPrefs::instance();


  setFilePermissions(prefs->mFilePermissions);

  pwTimeoutSpinBox->setValue(prefs->mPwTimeout);
  lockTimeoutSpinBox->setValue(prefs->mLockTimeout);
  autostartLineEdit->setURL(prefs->mAutoStart);
  browserLineEdit->setText(prefs->mBrowserCommand);
  xtermLineEdit->setText(prefs->mXTermCommand);

  kcfg_compression->setCurrentItem(prefs->mCompression);
  kcfg_cryptAlgo->setCurrentItem(prefs->mCryptAlgo);
  kcfg_hashAlgo->setCurrentItem(prefs->mHashAlgo);
}

void PWMConfigWidget::usrWriteConfig()
{
  PWMPrefs* prefs = PWMPrefs::instance();

  prefs->mFilePermissions = getFilePermissions();

  prefs->mPwTimeout = pwTimeoutSpinBox->value();
  prefs->mLockTimeout = lockTimeoutSpinBox->value();
  prefs->mAutoStart  = autostartLineEdit->url();

  prefs->mBrowserCommand = browserLineEdit->text();
  prefs->mXTermCommand = xtermLineEdit->text();

  prefs->mCompression = kcfg_compression->currentItem();
  prefs->mCryptAlgo = kcfg_cryptAlgo->currentItem();
  prefs->mHashAlgo = kcfg_hashAlgo->currentItem();

}

int PWMConfigWidget::getFilePermissions()
{
	char octalDigits[] = "01234567";
	bool isOctal;
	QString permString(permissionLineEdit->text());
	int i, j, length = permString.length();
	if (length != 3) {
		printWarn("Wrong permission string length! Please enter "
			  "the string like the following example: 600");
		return CONF_DEFAULT_FILEPERMISSIONS;
	}
	for (i = 0; i < length; ++i) {
		isOctal = false;
		for (j = 0; j < 8; ++j) {
			if (permString.at(i) == octalDigits[j]) {
				isOctal = true;
				break;
			}
		}
		if (!isOctal) {
			printWarn("CONFIG: File-permissions: This is "
				  "not an octal number ");
			return CONF_DEFAULT_FILEPERMISSIONS;
		}
	}

	int ret = strtol(permString.latin1(), 0, 8);
	if (ret == 0) {
		/* either an error occured, or the user did really type 000 */
		printWarn("CONFIG: File-permissions: Hm, either conversion error, "
			  "or you really typed 000. 8-)");
		return CONF_DEFAULT_FILEPERMISSIONS;
	}
	return ret;
}

void PWMConfigWidget::setFilePermissions(int perm)
{
	char tmpBuf[30];
	sprintf(tmpBuf, "%o", perm);
	permissionLineEdit->setText(tmpBuf);
}



#ifndef PWM_EMBEDDED
#include "pwmconfigwidget.moc"
#endif //PWM_EMBEDDED