summaryrefslogtreecommitdiffstats
path: root/kshowmail/kwalletaccess.cpp
blob: 20dccc64cc4e489d7d8b3d1197853ef9938393a6 (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
//
// C++ Implementation: kwalletaccess
//
// Author: Ulrich Weigelt <ulrich.weigelt@gmx.de>, (C) 2007
//
// Copyright: See COPYING file that comes with this distribution
//
//
#include "kwalletaccess.h"


bool KWalletAccess::savePassword( const QString & account, const QString & password )
{
  //check for enabled KWallet
  if( !KWallet::Wallet::isEnabled() )
  {
    KMessageBox::error( NULL, i18n( "KWallet is not available." ) );
    return false;
  }

  //get wallet name for network data
  QString name = KWallet::Wallet::NetworkWallet();
  if( name == "" || name == QString::null )
  {
    KMessageBox::error( NULL, i18n( "Could not get wallet name for network datas from KWallet." ) );
    return false;
  }

  //open wallet
  //we want to create a connection object just at first call of this function
  static KWallet::Wallet* wallet;

  if( wallet == NULL )
  {
    wallet = KWallet::Wallet::openWallet( name );
  }
  else if( !wallet->isOpen() )
  {
    delete wallet;
    wallet = KWallet::Wallet::openWallet( name );
  }

  if( wallet == NULL )
  {
    KMessageBox::error( NULL, i18n( "Could not open KWallet." ) );
    return false;
  }

  //create kshowmail folder if it does not exist
  if( !wallet->hasFolder( "KShowmail" ) )
  {
    bool createFolderSuccess = wallet->createFolder( "KShowmail" );

    if( !createFolderSuccess )
    {
      KMessageBox::error( NULL, i18n( "Could not create folder for KShowmail in KWallet." ) );
      return false;
    }
  }

  //set folder
  bool setFolderSuccess = wallet->setFolder( "KShowmail" );
  if( !setFolderSuccess )
  {
    KMessageBox::error( NULL, i18n( "Could not open folder for KShowmail in KWallet." ) );
    return false;
  }

  //write password
  int writePasswordSuccess = wallet->writePassword( account, password );
  if( writePasswordSuccess != 0 )
  {
    KMessageBox::error( NULL, i18n( "Could not save password in KWallet." ) );
    return false;
  }


  return true;
}

QString KWalletAccess::getPassword( const QString & account )
{
  //check for enabled KWallet
  if( !KWallet::Wallet::isEnabled() )
  {
    KMessageBox::error( NULL, i18n( "KWallet is not available." ) );
    return QString::null;
  }

  //get wallet name for network data
  QString name = KWallet::Wallet::NetworkWallet();
  if( name == "" || name == QString::null )
  {
    KMessageBox::error( NULL, i18n( "Could not get wallet name for network datas from KWallet." ) );
    return QString::null;
  }

  //open wallet
  //we want to create a connection object just at first call of this function
  static KWallet::Wallet* wallet;

  if( wallet == NULL )
  {
    wallet = KWallet::Wallet::openWallet( name );
  }
  else if( !wallet->isOpen() )
  {
    delete wallet;
    wallet = KWallet::Wallet::openWallet( name );
  }

  if( wallet == NULL )
  {
    KMessageBox::error( NULL, i18n( "Could not open KWallet." ) );
    return QString::null;
  }

  //set folder
  bool setFolderSuccess = wallet->setFolder( "KShowmail" );
  if( !setFolderSuccess )
  {
    KMessageBox::error( NULL, i18n( "Could not open folder for KShowmail in KWallet." ) );
    return QString::null;
  }

  //read password
  QString password;

  int readPasswordSuccess = wallet->readPassword( account, password );
  if( readPasswordSuccess != 0 )
  {
    KMessageBox::error( NULL, i18n( "Could not get password of account %1 from KWallet." ).arg( account) );
    return QString::null;
  }

  return password;
}