summaryrefslogtreecommitdiffstats
path: root/kgpg/kgpglibrary.cpp
blob: c0730b35fd640d746aa8a10a4f7ed9ae39fb2cc3 (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
/***************************************************************************
                          kgpglibrary.cpp  -  description
                             -------------------
    begin                : Mon Jul 8 2002
    copyright          : (C) 2002 by Jean-Baptiste Mardelle
    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.                                   *
 *                                                                         *
 ***************************************************************************/

#include <tqhbox.h>
#include <tqvbox.h>

#include <klocale.h>
#include <kapplication.h>
#include <kconfig.h>
#include <kmessagebox.h>
#include <krun.h>
#include <tqfile.h>
#include <kpassivepopup.h>
#include <kiconloader.h>
#include "kgpglibrary.h"
#include "popuppublic.h"
#include "kgpginterface.h"
#include <kio/renamedlg.h>

KgpgLibrary::KgpgLibrary(TQWidget *tqparent, bool pgpExtension)
{
        if (pgpExtension)
                extension=".pgp";
        else
                extension=".gpg";
	popIsActive=false;
	panel=tqparent;
}

KgpgLibrary::~KgpgLibrary()
{}


void KgpgLibrary::slotFileEnc(KURL::List urls,TQStringList opts,TQStringList defaultKey,KShortcut goDefaultKey)
{
        /////////////////////////////////////////////////////////////////////////  encode file file
        if (!urls.empty()) {
                urlselecteds=urls;
                if (defaultKey.isEmpty()) {
			TQString fileNames=urls.first().fileName();
			if (urls.count()>1) fileNames+=",...";
                        popupPublic *dialogue=new popupPublic(0,"Public keys",fileNames,true,goDefaultKey);
                        connect(dialogue,TQT_SIGNAL(selectedKey(TQStringList,TQStringList,bool,bool)),this,TQT_SLOT(startencode(TQStringList,TQStringList,bool,bool)));
                        dialogue->exec();
                        delete dialogue;
                } else
                        startencode(defaultKey,opts,false,false);
        }
}

void KgpgLibrary::startencode(TQStringList encryptKeys,TQStringList encryptOptions,bool shred,bool symetric)
{
	popIsActive=false;
        //KURL::List::iterator it;
	//filesToEncode=urlselecteds.count();
	_encryptKeys=encryptKeys;
	_encryptOptions=encryptOptions;
	_shred=shred;
	_symetric=symetric;
		fastencode(urlselecteds.first(),encryptKeys,encryptOptions,symetric);
}


void KgpgLibrary::fastencode(KURL &fileToCrypt,TQStringList selec,TQStringList encryptOptions,bool symetric)
{
        //////////////////              encode from file
        if ((selec.isEmpty()) && (!symetric)) {
                KMessageBox::sorry(0,i18n("You have not chosen an encryption key."));
                return;
        }
        urlselected=fileToCrypt;
        KURL dest;
        if (encryptOptions.tqfind("--armor")!=encryptOptions.end())
                dest.setPath(urlselected.path()+".asc");
        else
                dest.setPath(urlselected.path()+extension);

        TQFile fgpg(dest.path());

        if (fgpg.exists()) {
			KIO::RenameDlg *over=new KIO::RenameDlg(0,i18n("File Already Exists"),TQString(),dest.path(),KIO::M_OVERWRITE);
		    	if (over->exec()==TQDialog::Rejected)
	    		{
                	delete over;
			emit systemMessage(TQString(),true);
                	return;
            		}
	    		dest=over->newDestURL();
	    		delete over;
        }
	int filesToEncode=urlselecteds.count();
	if (filesToEncode>1)
	emit systemMessage(i18n("<b>%1 Files left.</b>\nEncrypting </b>%2").tqarg(filesToEncode).tqarg(urlselecteds.first().path()));
	else emit systemMessage(i18n("<b>Encrypting </b>%2").tqarg(urlselecteds.first().path()));
        KgpgInterface *cryptFileProcess=new KgpgInterface();
	pop = new KPassivePopup(panel);
        cryptFileProcess->KgpgEncryptFile(selec,urlselected,dest,encryptOptions,symetric);
         if (!popIsActive) 
	{
	//connect(cryptFileProcess,TQT_SIGNAL(processstarted(TQString)),this,TQT_SLOT(processpopup2(TQString)));
	popIsActive=true;	
	}
	connect(cryptFileProcess,TQT_SIGNAL(encryptionfinished(KURL)),this,TQT_SLOT(processenc(KURL)));
        connect(cryptFileProcess,TQT_SIGNAL(errormessage(TQString)),this,TQT_SLOT(processencerror(TQString)));
}

void KgpgLibrary::processpopup2(TQString fileName)
{
        
	//pop->setTimeout(0);
        pop->setView(i18n("Processing encryption (%1)").tqarg(fileName),i18n("Please wait..."),KGlobal::iconLoader()->loadIcon("kgpg",KIcon::Desktop));
        pop->show();
        /*TQRect qRect(TQApplication::desktop()->screenGeometry());
        int iXpos=qRect.width()/2-pop->width()/2;
        int iYpos=qRect.height()/2-pop->height()/2;
        pop->move(iXpos,iYpos);*/

}

void KgpgLibrary::shredpreprocessenc(KURL fileToShred)
{
	popIsActive=false;
	emit systemMessage(TQString());
	shredprocessenc(fileToShred);
}

void KgpgLibrary::shredprocessenc(KURL::List filesToShred)
{
emit systemMessage(i18n("Shredding %n file","Shredding %n files",filesToShred.count()));

KIO::Job *job;
job = KIO::del( filesToShred, true );
connect( job, TQT_SIGNAL( result( KIO::Job * ) ),TQT_SLOT( slotShredResult( KIO::Job * ) ) );	
}

void KgpgLibrary::slotShredResult( KIO::Job * job )
{
    emit systemMessage(TQString());
    if (job && job->error())
    {
    job->showErrorDialog( (TQWidget*)tqparent() );
    emit systemMessage(TQString(),true);
    KPassivePopup::message(i18n("KGpg Error"),i18n("Process halted, not all files were shredded."),KGlobal::iconLoader()->loadIcon("kgpg",KIcon::Desktop),panel,"kgpg_error",0);
    }
}


void KgpgLibrary::processenc(KURL)
{
	emit systemMessage(TQString());
	if (_shred) shredprocessenc(urlselecteds.first());
	urlselecteds.pop_front ();
	if (urlselecteds.count()>0)
	fastencode(urlselecteds.first(),_encryptKeys,_encryptOptions,_symetric);
}

void KgpgLibrary::processencerror(TQString mssge)
{
	popIsActive=false;
	emit systemMessage(TQString(),true);
	KMessageBox::detailedSorry(panel,i18n("<b>Process halted</b>.<br>Not all files were encrypted."),mssge);
}



void KgpgLibrary::slotFileDec(KURL srcUrl,KURL destUrl,TQStringList customDecryptOption)
{
        //////////////////////////////////////////////////////////////////    decode file from konqueror or menu
        KgpgInterface *decryptFileProcess=new KgpgInterface();
        pop = new KPassivePopup();
	urlselected=srcUrl;
        decryptFileProcess->KgpgDecryptFile(srcUrl,destUrl,customDecryptOption);
        connect(decryptFileProcess,TQT_SIGNAL(processaborted(bool)),this,TQT_SLOT(processdecover()));
        connect(decryptFileProcess,TQT_SIGNAL(processstarted(TQString)),this,TQT_SLOT(processpopup(TQString)));
        connect(decryptFileProcess,TQT_SIGNAL(decryptionfinished()),this,TQT_SLOT(processdecover()));
        connect(decryptFileProcess,TQT_SIGNAL(errormessage(TQString)),this,TQT_SLOT(processdecerror(TQString)));
}

void KgpgLibrary::processpopup(TQString fileName)
{
	emit systemMessage(i18n("Decrypting %1").tqarg(fileName));
	pop->setTimeout(0);
        pop->setView(i18n("Processing decryption"),i18n("Please wait..."),KGlobal::iconLoader()->loadIcon("kgpg",KIcon::Desktop));
        pop->show();
        TQRect qRect(TQApplication::desktop()->screenGeometry());
        int iXpos=qRect.width()/2-pop->width()/2;
        int iYpos=qRect.height()/2-pop->height()/2;
        pop->move(iXpos,iYpos);
}

void KgpgLibrary::processdecover()
{
	emit systemMessage(TQString());
	delete pop;
        emit decryptionOver();
}


void KgpgLibrary::processdecerror(TQString mssge)
{
	delete pop;
	emit systemMessage(TQString());
        ///// test if file is a public key
        TQFile qfile(TQFile::encodeName(urlselected.path()));
        if (qfile.open(IO_ReadOnly)) {
                TQTextStream t( &qfile );
                TQString result(t.read());
                qfile.close();
                //////////////     if  pgp data found, decode it
                if (result.startsWith("-----BEGIN PGP PUBLIC KEY BLOCK")) {//////  dropped file is a public key, ask for import
                        int result=KMessageBox::warningContinueCancel(0,i18n("<p>The file <b>%1</b> is a public key.<br>Do you want to import it ?</p>").tqarg(urlselected.path()),i18n("Warning"));
                        if (result==KMessageBox::Cancel)
                                return;
                        else {
                                KgpgInterface *importKeyProcess=new KgpgInterface();
                                importKeyProcess->importKeyURL(urlselected);
				connect(importKeyProcess,TQT_SIGNAL(importfinished(TQStringList)),this,TQT_SIGNAL(importOver(TQStringList)));
                                return;
                        }
                } else if (result.startsWith("-----BEGIN PGP PRIVATE KEY BLOCK")) {//////  dropped file is a public key, ask for import
                        qfile.close();
                        KMessageBox::information(0,i18n("<p>The file <b>%1</b> is a private key block. Please use KGpg key manager to import it.</p>").tqarg(urlselected.path()));
                        return;
                }
        }
        KMessageBox::detailedSorry(0,i18n("Decryption failed."),mssge);
}



#include "kgpglibrary.moc"