summaryrefslogtreecommitdiffstats
path: root/kdpkg-install/install.cpp
blob: 9904018136a563621953e8eec0398c3cd01ad30d (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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/*
 * install.cpp
 *
 * Copyright (c) 2007 Fabian Wuertz
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */



#include <kgenericfactory.h>
#include <klocale.h>
#include <kfiledialog.h>
#include <kurlrequester.h>
#include <kaboutapplication.h>

#include <qlistbox.h>
#include <qfile.h>
#include <qpushbutton.h>
#include <qwidgetstack.h>
#include <qlabel.h>
#include <qlistview.h>
#include <qregexp.h>
#include <qradiobutton.h>
#include <qfiledialog.h>

#include <console.h>
#include <install.h>

#include <string>
#include <stdio.h>

using namespace std;

install::install( const QString &url, QWidget *parent, const char *name, const QStringList &)
:InstallDialog(parent, name)
{

	if( !url )
		path = KFileDialog::getOpenFileName( QString::null, i18n("Debian Package (*.deb)"), this, i18n("Choose a Debian package file to open"));
	else
		path = url;

	this->shell = new Process();

	this->shell->setCommand("dpkg -f "+path);
	this->shell->start(true);
	fields = QStringList::split( "\n", this->shell->getBuffer().stripWhiteSpace() );

	installPushButton->hide();
	nextPushButton1->hide();

	// Get KDE prefix
	// FIXME Is there a better way to do this???
	string prefixcommand="kde-config --prefix";
	FILE *pipe_prefix;
	char prefix_result[2048];
	int i;

	if ((pipe_prefix = popen(prefixcommand.c_str(), "r")) == NULL)
	{
		m_kdePrefix = "/usr";
	}
	else {
		fgets(prefix_result, 2048, pipe_prefix);
		pclose(pipe_prefix);
		for (i=0;i<2048;i++) {
			if (prefix_result[i] == 0) {
				prefix_result[i-1]=0;
				i=2048;
			}
		}
		m_kdePrefix = QString(prefix_result);
	}

	if( !checkArchitecture() )
		page2x1();
	else if( isLocked() )
		page2x2();


}

//----------------------------------------------------------------------------//
//--- next -------------------------------------------------------------------//
//----------------------------------------------------------------------------//

void install::next1()
{
	if( !isLocked() )
	{
		nextPushButton1->hide();
		nextPushButton2->show();
		widgetStack->raiseWidget(0);
	}
}


void install::next2()
{
	nextPushButton2->hide();
	if( syncRadioButton->isChecked() )
		page3();
	else
		page1();
}



//----------------------------------------------------------------------------//
//--- pages ------------------------------------------------------------------//
//----------------------------------------------------------------------------//


// page 0: skip apt-get update?
// page 1: dependencies
// page 2: warnings
// page 3: apt-get update
// page 4: install package


void install::page1()
{
	showDependencies();

	widgetStack->raiseWidget(1);
	installPushButton->show();
	closePushButton->show();
	titleTextLabel->setText( "<b>"+i18n( "The following packages will be installed, updated or removed:" )+"</b>" );
}

void install::page2x1()
{
	errorTextLabel->setText("<b>"+i18n("The architecture of this package does not match that of your system.  You will not be able to install this package!")+"</b>");
	nextPushButton2->hide();
	widgetStack->raiseWidget(2);

}

void install::page2x2()
{
	errorTextLabel->setText("<b>"+i18n("The package database is in use by another process (e.g Synaptic). Please terminate this process and select Next to try again.")+"</b>");
	nextPushButton1->show();
	nextPushButton2->hide();
	widgetStack->raiseWidget(2);
}


void install::page3()
{
	titleTextLabel->setText( "<b>"+i18n( "Resynchronize Package Index")+"</b>" );
	closePushButton->hide();

	QStrList run; run.append( m_kdePrefix + "/share/kdpkg/sh/kdpkg-sh" );
		run.append( "update" );

	QWidget *consoleWidget = new console(this, run );
 	widgetStack->addWidget(consoleWidget, 3);
	widgetStack->raiseWidget(3);

	connect( consoleWidget, SIGNAL( finished(bool) ), this, SLOT( page1() ));
}


void install::page4()
{
	installPushButton->hide();
	closePushButton->hide();
	titleTextLabel->setText( "<b>"+i18n( "Install New Package" )+"</b>" );


	if( !installPkg )
		installPkg = "none";
	if( !removePkg )
		removePkg = "none";

	QStrList run; run.append( m_kdePrefix + "/share/kdpkg/sh/kdpkg-sh" );
		run.append( "install" );
		run.append( path );
		run.append( installPkg );
		run.append( removePkg );

	QWidget *consoleWidget = new console(this, run );
	widgetStack->addWidget(consoleWidget, 4);
	widgetStack->raiseWidget(4);

	connect( consoleWidget, SIGNAL( finished(bool) ), this, SLOT( close() ));


}


//----------------------------------------------------------------------------//
//--- showDependencies -------------------------------------------------------//
//----------------------------------------------------------------------------//

void install::showDependencies()
{
	int start_index;
	int stop_index;

	// show packages which will be installed

	QString tmpString = fields.grep( QRegExp("^Depends:") )[0].mid(9);
	tmpString += ", "+fields.grep( "Pre-Depends:" )[0].mid(13);


	QStringList allDepends = QStringList::split( ", ", tmpString );

	QString missingDepends;
	QStringList tmp;

	for(uint i = 0; i < allDepends.count(); i++)
	{
		if( allDepends[i].contains("|") ) {
			tmp = QStringList::split( " | ", allDepends[i] );
			uint j = 0;
			bool exit = FALSE;
			while ( !exit && j < tmp.count() ) {
				if( isInstalled(tmp[j]) )
					exit = TRUE;
				j++;
			}
			if( !exit )
				missingDepends += tmp[0]+" ";
		}
		else
		{
			if( !isInstalled( allDepends[i] ) )
				missingDepends += QStringList::split( " ", allDepends[i] )[0]+" ";
		}
	}

	while (missingDepends.find("(") != -1) {
		start_index = missingDepends.find("(");
		stop_index = missingDepends.find(")", start_index);
		missingDepends.replace(start_index, stop_index-start_index+1, "");
	}
	this->shell->setCommand("apt-get -s install "+missingDepends);
	this->shell->start(true);
	QStringList installList = QStringList::split( "\n", this->shell->getBuffer().stripWhiteSpace() ).grep("Inst");


	for(uint i = 0; i < installList.count(); i++)
	{
		QString name = QStringList::split( " ", installList[i] )[1];
		QString pkgVersion;
		QString sysVersion;
		if( installList[i].contains("[") )
		{
			pkgVersion = QStringList::split( " ", installList[i] )[3].mid(1);
			sysVersion = QStringList::split( " ", installList[i] )[2].mid(1).replace("]", "");
		}
		else
			pkgVersion = QStringList::split( " ", installList[i] )[2].mid(1);

		QListViewItem * item = new QListViewItem( dependenciesListView, 0 );	
		item->setPixmap( 0, QPixmap( m_kdePrefix + "/share/kdpkg/icons/install.png") );
		item->setText( 1, name  );
		item->setText( 2, pkgVersion );
		item->setText( 3, sysVersion );

		installPkg += name+",";
	}

	// find rconflicts

	QString pkgName = fields.grep("Package:")[0].mid(9);
	QString pkgVersion = fields.grep("Version:")[0].mid(9);

	QString rconflicts;

	this->shell->setCommand("cat /var/lib/dpkg/status | grep "+pkgName+" -B 16| grep Conflicts -B 16");
	this->shell->start(true);
	QStringList input1 = QStringList::split( "\n\n", this->shell->getBuffer().stripWhiteSpace() );

	for(uint i = 0; i < input1.count(); i++)
	{
		if( input1[i].contains(pkgName) )
		{
			QStringList input2 = QStringList::split( "\n", input1[i] );
			QString name = QStringList::split( " ",  input2.grep("Package:")[0] )[1];
			QString cVersion = input2.grep("Conflicts:")[0].mid(11); // conflictVersion
			cVersion = QStringList::split( ",",  cVersion ).grep(pkgName)[0];
			cVersion = QStringList::split( "(",  cVersion )[1];
			cVersion = cVersion.replace(")", "");
			QString op = QStringList::split( " ",  cVersion )[0];
			cVersion = QStringList::split( " ",  cVersion )[1];

			if( cVersion == "")
			{
				rconflicts += name+" ";
				printf("1");
			}
			else
			{
				printf("2");
				this->shell->setCommand("if dpkg --compare-versions "+pkgVersion+" \""+op+"\" "+cVersion+"; then echo \"TRUE\"; fi");
				this->shell->start(true);
				if( this->shell->getBuffer().stripWhiteSpace() == "TRUE" )
					rconflicts += name+" ";
			}
		}
	}


	// show packages which will be removed

	QString conflicts = fields.grep( "Conflicts" )[0].mid(11);
	conflicts = conflicts.replace( ",", "" );
	conflicts = conflicts+" "+rconflicts;
	while (conflicts.find("(") != -1) {
		start_index = conflicts.find("(");
		stop_index = conflicts.find(")", start_index);
		conflicts.replace(start_index, stop_index-start_index+1, "");
	}

	this->shell->setCommand("apt-get -s remove "+conflicts);
	this->shell->start(true);
	QStringList removeList = QStringList::split( "\n", this->shell->getBuffer().stripWhiteSpace() ).grep("Remv");


	for(uint i = 0; i < removeList.count(); i++)
	{
		QString name = QStringList::split( " ", removeList[i] )[1];
		QString sysVersion = QStringList::split( " [", removeList[i] )[1].replace("]", "");

		QListViewItem * item = new QListViewItem( dependenciesListView, 0 );	
		item->setPixmap( 0, QPixmap( m_kdePrefix + "/share/kdpkg/icons/remove.png") );
		item->setText( 1, name );
		item->setText( 3, sysVersion );

		removePkg += name+",";
	}
}

//----------------------------------------------------------------------------//
//--- isInstalled ------------------------------------------------------------//
//----------------------------------------------------------------------------//

bool install::isInstalled(QString input)
{
	QString package = QStringList::split( " ",  input )[0];
	QString version = QStringList::split( " (", input )[1].replace(")", "");


	this->shell->setCommand("apt-cache policy "+package);
	this->shell->start(true);
	QString sysVersion = QStringList::split( "\n", this->shell->getBuffer().stripWhiteSpace() )[1];
	sysVersion = QStringList::split( ":", sysVersion)[1].replace(" ","");


	if( sysVersion.contains("(") || sysVersion == "" )
		return FALSE;
	else if( version != "" )
	{
		QString op         = QStringList::split( " ", version )[0];
		QString pkgVersion = QStringList::split( " ", version )[1];

		this->shell->setCommand("if dpkg --compare-versions "+sysVersion+" \""+op+"\" "+pkgVersion+"; then echo \"TRUE\"; fi");
		this->shell->start(true);
		if( this->shell->getBuffer().stripWhiteSpace() == "TRUE" )
			return TRUE;
		else
			return FALSE;
	}
	else
		 return TRUE;
}

//----------------------------------------------------------------------------//
//--- about ------------------------------------------------------------------//
//----------------------------------------------------------------------------//

void install::showAbout()
{
	KAboutApplication* about = new KAboutApplication ( this );
	about->show();
}

//----------------------------------------------------------------------------//
//--- checks -----------------------------------------------------------------//
//----------------------------------------------------------------------------//

bool install::checkArchitecture()
{
	QString arch = fields.grep("Architecture:")[0].mid(14);

	if(arch == "all")
		return TRUE;

	// get architecture
	this->shell->setCommand("dpkg --print-architecture");
	this->shell->start(true);

	if( arch == this->shell->getBuffer().stripWhiteSpace() )
		return TRUE;

	return FALSE;

}


bool install::isLocked()
{
	this->shell->setCommand("apt-get install -s" );
	this->shell->start(true);
	if( this->shell->getBuffer().stripWhiteSpace().contains("...") )
		return FALSE;
	else
		return TRUE;
}





#include "install.moc"