summaryrefslogtreecommitdiffstats
path: root/kresources/scalix/scalixadmin/passwordpage.cpp
blob: facc291701a142dbab9fd49354e31f5139804f22 (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
/*
 *   This file is part of ScalixAdmin.
 *
 *   Copyright (C) 2007 Trolltech ASA. All rights reserved.
 *
 *   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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include <tqapplication.h>
#include <tqlabel.h>
#include <tqlayout.h>
#include <tqlineedit.h>
#include <tqpushbutton.h>

#include <kconfig.h>
#include <klocale.h>
#include <kmessagebox.h>
#include <kstringhandler.h>
#include <kwallet.h>

#include "jobs.h"
#include "settings.h"

#include "passwordpage.h"

PasswordPage::PasswordPage( TQWidget *parent )
  : TQWidget( parent ), mJob( 0 )
{
  TQGridLayout *layout = new TQGridLayout( this, 2, 3, 11, 6 );

  TQLabel *label = new TQLabel( i18n( "New password:" ), this );
  layout->addWidget( label, 0, 0 );

  mPassword = new TQLineEdit( this );
  mPassword->setEchoMode( TQLineEdit::Password );
  label->setBuddy( mPassword );
  layout->addWidget( mPassword, 0, 1 );

  label = new TQLabel( i18n( "Retype new password:" ), this );
  layout->addWidget( label, 1, 0 );

  mPasswordRetype = new TQLineEdit( this );
  mPasswordRetype->setEchoMode( TQLineEdit::Password );
  label->setBuddy( mPasswordRetype );
  layout->addWidget( mPasswordRetype, 1, 1 );

  mButton = new TQPushButton( i18n( "Change" ), this );
  mButton->setEnabled( false );
  layout->addWidget( mButton, 2, 1 );

  layout->setRowSpacing( 3, 1 );

  connect( mPassword, TQT_SIGNAL( textChanged( const TQString& ) ), this, TQT_SLOT( textChanged() ) );
  connect( mPasswordRetype, TQT_SIGNAL( textChanged( const TQString& ) ), this, TQT_SLOT( textChanged() ) );
  connect( mButton, TQT_SIGNAL( clicked() ), this, TQT_SLOT( buttonClicked() ) );
}

void PasswordPage::buttonClicked()
{
  if ( !mJob ) {
    if ( mPassword->text() != mPasswordRetype->text() ) {
      KMessageBox::error( this, i18n( "The two passwords differ!" ) );
      return;
    }

    mJob = Scalix::setPassword( Settings::self()->globalSlave(), Settings::self()->accountUrl(),
                                Settings::self()->accountPassword(), mPassword->text() );
    connect( mJob, TQT_SIGNAL( result( KIO::Job* ) ), this, TQT_SLOT( finished( KIO::Job* ) ) );

    updateState( true );
  } else {
    mJob->kill();
    mJob = 0;

    updateState( false );
  }
}

void PasswordPage::updateState( bool isWorking )
{
  if ( isWorking ) {
    mPassword->setEnabled( false );
    mPasswordRetype->setEnabled( false );
    mButton->setText( i18n( "Stop" ) );
  } else {
    mPassword->setEnabled( true );
    mPasswordRetype->setEnabled( true );
    mButton->setText( i18n( "Change" ) );
  }
}

void PasswordPage::textChanged()
{
  mButton->setEnabled( !mPassword->text().isEmpty() &&
                       !mPasswordRetype->text().isEmpty() );
}

void PasswordPage::finished( KIO::Job* job )
{
  mJob = 0;

  updateState( false );

  if ( job->error() ) {
    KMessageBox::error( this, i18n( "Unable to change the password" ) + "\n" + job->errorString() );
    return;
  }

  // Update configuration files to the new password as well

  const TQString newPassword = mPassword->text();

  { // ScalixAdmin config
    KConfig config( "scalixadminrc" );
    KConfigGroup group( &config, "Account" );
    group.writeEntry( "pass", KStringHandler::obscure( newPassword ) );
  }

  { // ScalixWizard config
    KConfig config( "scalixrc" );
    KConfigGroup group( &config, "General" );
    group.writeEntry( "Password", KStringHandler::obscure( newPassword ) );
  }

  { // KMail config
    KConfig config( "kmailrc" );

    // Try to find account group for Scalix
    TQString scalixAccount;
    const TQStringList groupList = config.groupList();
    for ( uint i = 0; i < groupList.count(); ++i ) {
      if ( groupList[ i ].startsWith( "Account " ) ) {
        KConfigGroup group( &config, groupList[ i ] );
        if ( group.hasKey( "groupwareType" ) && group.readNumEntry( "groupwareType" ) == 2 ) {
          scalixAccount = groupList[ i ];
          break;
        }
      }
    }

    if ( scalixAccount.isEmpty() ) {
      qWarning( "No Scalix Groupware Account found in kmailrc!" );
      return;
    }

    const int accountId = scalixAccount.mid( 8 ).toInt();

    KConfigGroup group( &config, scalixAccount );

    // Save only if the user choose it before
    bool storePassword = group.readBoolEntry( "store-passwd", false );
    if ( storePassword ) {
      // First try to store in KWallet
      if ( KWallet::Wallet::isEnabled() ) {
        WId window = 0;
        if ( qApp->activeWindow() )
          window = qApp->activeWindow()->winId();

        KWallet::Wallet *wallet = KWallet::Wallet::openWallet( KWallet::Wallet::NetworkWallet(), window );
        if ( wallet ) {
          if ( !wallet->hasFolder( "kmail" ) )
            wallet->createFolder( "kmail" );
          wallet->setFolder( "kmail" );
          wallet->writePassword( "account-" + TQString::number( accountId ), newPassword );
        }
      } else {
        group.writeEntry( "pass", KStringHandler::obscure( newPassword ) );
      }

      KConfigGroup fileGroup( &config, TQString( "Folder-%1" ).arg( group.readNumEntry( "Folder" ) ) );
      fileGroup.writeEntry( "pass", KStringHandler::obscure( newPassword ) );
    }
  }

  KMessageBox::information( this, i18n( "Password was changed successfully" ) );
}

#include "passwordpage.moc"