summaryrefslogtreecommitdiffstats
path: root/src/arkollon/uninstallwizard.cpp
blob: 0833117cc7f3aa7692af5a3ddbaa97cf8bbdab0f (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
/***************************************************************************
 *   Copyright (C) 2004 by David Sansome                                   *
 *   [email protected]                                                   *
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 *   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 "uninstallwizard.h"
#include "headerlistitem.h"
#include "data.h"

#include <tqregexp.h>
#include <tqwidgetstack.h>
#include <tqfile.h>
#include <tqtextstream.h>
#include <tqheader.h>
#include <tqlistbox.h>
#include <tqmessagebox.h>
#include <tqpushbutton.h>
#include <tqlineedit.h>
#include <tqtextedit.h>
#include <tqlabel.h>

AppListItem::AppListItem(TQString nN, TQString n, TQListView* parent)
 : TQCheckListItem(parent, "", TQCheckListItem::CheckBox)
{
	niceName = nN;
	name = n;
	section = 2;
	
	setText(0, niceName);
}

int AppListItem::compare(TQListViewItem* i, int col, bool ascending) const
{
	switch (i->rtti())
	{
		case 1003: // App
		{
			AppListItem* item = (AppListItem*) i;
			if (section < item->section)
				return -1;
			if (section > item->section)
				return 1;
			return TQListViewItem::compare(i, col, ascending);
		}
		break;
		
		case 1002: // Header
		{
			HeaderListItem* item = (HeaderListItem*) i;
			if (section < item->section)
				return -1;
			return 1;
		}
		break;
	}
	return 0;
}


UninstallWizard::UninstallWizard(TQWidget* parent, const char* name, bool modal, WFlags fl)
: WizardBase(parent,name, modal,fl)
{
	mainStack->raiseWidget(1);
	setCaption("Uninstall Software");
	titleLabel->setText("<b>Uninstall Software</b>");
	componentInfo->setMaximumSize(32767,70);
	
	logDialog = new LogDialog(this);
	logDialog->hide();
	appList->header()->hide();
	
	globalHeader = NULL;
	
	icon.convertFromImage(qembed_findImage("misc"));
	
	externalProcess = new TQProcess(this);
	connect(externalProcess, SIGNAL(processExited()), SLOT(processExited()));
	connect(externalProcess, SIGNAL(readyReadStdout()), SLOT(readyReadStdout()));
	connect(externalProcess, SIGNAL(readyReadStderr()), SLOT(readyReadStderr()));
	
	TQFile uninstallScript("/tmp/arkollon-uninstall.sh");
	if (uninstallScript.exists())
		uninstallScript.remove();
	uninstallScript.open(IO_WriteOnly);
	TQDataStream stream(&uninstallScript);
	stream.writeRawBytes((const char*)uninstaller_sh_data, uninstaller_sh_len);
	uninstallScript.close();
	
	currentStage = ListingPackages;
	externalProcess->addArgument("/bin/sh");
	externalProcess->addArgument("/tmp/arkollon-uninstall.sh");
	externalProcess->addArgument("--list");
	externalProcess->start();
}

UninstallWizard::~UninstallWizard()
{
	TQFile uninstallScript("/tmp/arkollon-uninstall.sh");
	if (uninstallScript.exists())
		uninstallScript.remove();
}

void UninstallWizard::logPressed()
{
	logDialog->show();
}

void UninstallWizard::cancelPressed()
{
	reject();
}

void UninstallWizard::previousPressed()
{
	int currentId = uninstallStack->id(uninstallStack->visibleWidget());
	if (currentId == 0)
		return;
		
	uninstallStack->raiseWidget(--currentId);
	
	if (currentId == 0)
		previousButton->setEnabled(false);
	nextButton->setEnabled(true);
}

void UninstallWizard::nextPressed()
{
	int currentId = uninstallStack->id(uninstallStack->visibleWidget());
	if (currentId == 2)
	{
		accept();
		return;
	}
	
	if (currentId == 0)
	{
		bool checked = false;
		TQStringList argList;
		argList.append("/bin/sh");
		argList.append("/tmp/arkollon-uninstall.sh");
		for ( TQListViewItem * myChild = appList->firstChild() ; myChild != NULL ; myChild = myChild->nextSibling())
		{
			if (myChild->rtti() != 1003)
				continue;
			AppListItem* app = (AppListItem*) myChild;
			if (!app->isOn())
				continue;
			argList.append("--files");
			argList.append(app->name);
			checked = true;
		}
		if (!checked)
		{
			TQMessageBox::warning(this, "Error", "You need to select at least one package to remove", TQMessageBox::Ok, TQMessageBox::NoButton, TQMessageBox::NoButton);
			return;
		}
		fileList->clear();
		currentStage = ListingFiles;
		externalProcess->setArguments(argList);
		externalProcess->start();
		nextButton->setEnabled(false); // Set true again when the process is done
	}
	
	uninstallStack->raiseWidget(++currentId);
	
	if (currentId == 2)
		removeUserPackages();
	else
		previousButton->setEnabled(true);
}

void UninstallWizard::processExited()
{
	switch (currentStage)
	{
	case ListingPackages:
		if (appList->childCount() <= 0)
		{
			TQMessageBox::warning(NULL, "Warning", "There are no packages installed", TQMessageBox::Ok, TQMessageBox::NoButton, TQMessageBox::NoButton);
			reject();
			return;
		}
		show();
		break;
	case ListingFiles:
		nextButton->setEnabled(true);
		fileList->sort();
		break;
	case RemovingGlobal:
		finished();
		break;
	}
}

void UninstallWizard::readyReadStdout()
{
	switch (currentStage)
	{
	case ListingPackages:
	{
		while (externalProcess->canReadLineStdout())
		{
			TQString line = externalProcess->readLineStdout();
			if (line.isEmpty())
				continue;
				
			// See if it already exists
			bool exists = false;
			
			for ( TQListViewItem * myChild = appList->firstChild() ; myChild != NULL ; myChild = myChild->nextSibling())
			{
				if (myChild->rtti() != 1003)
					continue;
				AppListItem* app = (AppListItem*) myChild;
				if (app->name.lower() == line.lower())
				{
					exists = true;
					break;
				}
			}
			if (exists)
				continue;
			
			TQString niceName = line.left(line.findRev(':'));
			niceName = niceName.left(1).upper() + niceName.right(niceName.length()-1);
			new AppListItem(niceName, line, appList);
			
			if (globalHeader == NULL)
			{
				globalHeader = new HeaderListItem(appList);
				globalHeader->setText(0, "Applications available to all users");
				globalHeader->section = 1;
			}
		}
		break;
	}
	case ListingFiles:
	{
		while (externalProcess->canReadLineStdout())
		{
			TQString line = externalProcess->readLineStdout();
			if (line.isEmpty())
				continue;
			fileList->insertItem(icon, line.left(line.find("  ")));
		}
		break;
	}
	case RemovingGlobal:
		while (externalProcess->canReadLineStdout())
		{
			TQString line = externalProcess->readLineStdout();
			if (line.isEmpty())
				continue;
			line.replace(TQRegExp("\\033[^m]*m"), "");
			logDialog->logBox->append(line);
		}
		break;
	}
}

void UninstallWizard::readyReadStderr()
{
	while (externalProcess->canReadLineStdout())
	{
		TQString line = externalProcess->readLineStdout();
		if (line.isEmpty())
			continue;
		line.replace(TQRegExp("\\033[^m]*m"), "");
		logDialog->logBox->append(line);
	}
}

void UninstallWizard::removeUserPackages()
{
	nextButton->setEnabled(false); // Set true again when the process is done
	previousButton->setEnabled(false);
	
	// Find out which packages belong to the user, and uninstall them
	TQStringList argList;
	argList.append("/bin/sh");
	argList.append("/tmp/arkollon-uninstall.sh");
	for ( TQListViewItem * myChild = appList->firstChild() ; myChild != NULL ; myChild = myChild->nextSibling())
	{
		if (myChild->rtti() != 1003)
			continue;
		AppListItem* app = (AppListItem*) myChild;
		if (!app->isOn())
			continue;
		
		argList.append("--remove");
		argList.append(app->name);
	}
	currentStage = RemovingGlobal;
	externalProcess->setArguments(argList);
	externalProcess->start();
}

void UninstallWizard::finished()
{
	pleaseWaitLabel2->setText("Removal of packages complete!");
	nextButton->setText("Finish");
	nextButton->setEnabled(true);
}

#include "uninstallwizard.moc"