summaryrefslogtreecommitdiffstats
path: root/kopete/plugins/cryptography/kgpgselkey.cpp
blob: c7750c7a354f6040444c316af43df8453d16ba47 (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
//Code from KGPG

/***************************************************************************
                          listkeys.cpp  -  description
                             -------------------
    begin                : Thu Jul 4 2002
    copyright            : (C) 2002 by y0k0
    email                : bj@altern.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.                                   *
 *                                                                         *
 ***************************************************************************/

////////////////////////////////////////////////////// code for the key management

#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>

#include <tqlayout.h>
#include <tqlabel.h>

#include <tdelistview.h>
#include <tdelocale.h>
#include <tqcheckbox.h>
#include <kprocess.h>
#include <kiconloader.h>

#include "kgpgselkey.h"


////////////////////////////////////////////////////////////////////////////////////////////////////

////////////////   Secret key selection dialog, used when user wants to sign a key
KgpgSelKey::KgpgSelKey(TQWidget *parent, const char *name,bool showlocal):KDialogBase( parent, name, true,i18n("Private Key List"),Ok | Cancel)
{
  TQString keyname;
  TQWidget *page = new TQWidget(this);
  TQLabel *labeltxt;
  TDEIconLoader *loader = TDEGlobal::iconLoader();

  keyPair=loader->loadIcon("kgpg_key2",TDEIcon::Small,20);

  setMinimumSize(300,200);
  keysListpr = new TDEListView( page );
  keysListpr->setRootIsDecorated(true);
  keysListpr->addColumn( i18n( "Name" ) );
  keysListpr->setShowSortIndicator(true);
  keysListpr->setFullWidth(true);

  labeltxt=new TQLabel(i18n("Choose secret key:"),page);
  TQVBoxLayout *vbox=new TQVBoxLayout(page,3);

  vbox->addWidget(labeltxt);
  vbox->addWidget(keysListpr);
  if (showlocal==true)
  {
    local = new TQCheckBox(i18n("Local signature (cannot be exported)"),page);
    vbox->addWidget(local);
  }

  FILE *fp,*fp2;
  TQString tst,tst2;
  char line[130];

  // FIXME: Why use popen instead of TDEProcess, TQProcess or KProcIO?!?
  //        Are we interested in having buffer overflows now? - Martijn
  fp = popen( "gpg --no-tty --with-colon --list-secret-keys", "r" );
  while ( fgets( line, sizeof(line), fp))
  {
    tst=line;
    if (tst.startsWith("sec"))
    {
      const TQString trust=tst.section(':',1,1);
      TQString val=tst.section(':',6,6);
      TQString id=TQString("0x"+tst.section(':',4,4).right(8));
      if (val.isEmpty())
        val=i18n("Unlimited");
      TQString tr;
      switch( trust[0] )
      {
      case 'o':
        tr= i18n("Unknown");
        break;
      case 'i':
        tr= i18n("Invalid");
        break;
      case 'd':
        tr=i18n("Disabled");
        break;
      case 'r':
        tr=i18n("Revoked");
        break;
      case 'e':
        tr=i18n("Expired");
        break;
      case 'q':
        tr=i18n("Undefined");
        break;
      case 'n':
        tr=i18n("None");
        break;
      case 'm':
        tr=i18n("Marginal");
        break;
      case 'f':
        tr=i18n("Full");
        break;
      case 'u':
        tr=i18n("Ultimate");
        break;
      default:
        tr=i18n("?");
        break;
      }
      tst=tst.section(":",9,9);

      // FIXME: Same here: don't use popen! - Martijn
      fp2 = popen( TQString( "gpg --no-tty --with-colon --list-key %1" ).arg( KShellProcess::quote( id ) ).latin1(), "r" );
      bool dead=true;
      while ( fgets( line, sizeof(line), fp2))
      {
        tst2=line;
        if (tst2.startsWith("pub"))
        {
          const TQString trust2=tst2.section(':',1,1);
          switch( trust2[0] )
          {
          case 'f':
            dead=false;
            break;
          case 'u':
            dead=false;
            break;
          default:
            break;
          }
        }
      }
	  pclose(fp2);
      if (!tst.isEmpty() && (!dead))
      {
        TDEListViewItem *item=new TDEListViewItem(keysListpr,extractKeyName(tst));
        TDEListViewItem *sub= new TDEListViewItem(item,i18n("ID: %1, trust: %2, expiration: %3").arg(id).arg(tr).arg(val));
        sub->setSelectable(false);
        item->setPixmap(0,keyPair);
      }
    }
  }
  pclose(fp);


  TQObject::connect(keysListpr,TQ_SIGNAL(doubleClicked(TQListViewItem *,const TQPoint &,int)),this,TQ_SLOT(slotpreOk()));
  TQObject::connect(keysListpr,TQ_SIGNAL(clicked(TQListViewItem *)),this,TQ_SLOT(slotSelect(TQListViewItem *)));


  keysListpr->setSelected(keysListpr->firstChild(),true);

  page->show();
  resize(this->minimumSize());
  setMainWidget(page);
}

TQString KgpgSelKey::extractKeyName(TQString fullName)
{
  TQString kMail;
  if (fullName.find("<")!=-1)
  {
    kMail=fullName.section('<',-1,-1);
    kMail.truncate(kMail.length()-1);
  }
  TQString kName=fullName.section('<',0,0);
  if (kName.find("(")!=-1) kName=kName.section('(',0,0);
  return TQString(kMail+" ("+kName+")").stripWhiteSpace();
}

void KgpgSelKey::slotpreOk()
{
  if (keysListpr->currentItem()->depth()!=0)
    return;
  else
    slotOk();
}

void KgpgSelKey::slotOk()
{
  if (keysListpr->currentItem()==NULL)
    reject();
  else
    accept();
}

void KgpgSelKey::slotSelect(TQListViewItem *item)
{
  if (item==NULL) return;
  if (item->depth()!=0)
  {
    keysListpr->setSelected(item->parent(),true);
    keysListpr->setCurrentItem(item->parent());
  }
}


TQString KgpgSelKey::getkeyID()
{
  TQString userid;
  /////  emit selected key
  if (keysListpr->currentItem()==NULL) return("");
  else
  {
    userid=keysListpr->currentItem()->firstChild()->text(0);
    userid=userid.section(',',0,0);
    userid=userid.section(':',1,1);
    userid=userid.stripWhiteSpace();
    return(userid);
  }
}

TQString KgpgSelKey::getkeyMail()
{
  TQString username;
  /////  emit selected key
  if (keysListpr->currentItem()==NULL) return("");
  else
  {
    username=keysListpr->currentItem()->text(0);
    //username=username.section(' ',0,0);
    username=username.stripWhiteSpace();
    return(username);
  }
}

bool KgpgSelKey::getlocal()
{
  /////  emit exportation choice
  return(local->isChecked());
}

#include "kgpgselkey.moc"