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


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

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

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

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

  if( wallet == NULL )
  {
    KMessageBox::error( NULL, i18n( "Could not open TDEWallet." ) );
    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 TDEWallet." ) );
      return false;
    }
  }

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

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


  return true;
}

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

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

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

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

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

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

  //read password
  TQString password;

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

  return password;
}