/*KSlacky InfoPkg is a package information tool that provide
the list of packages installe on your Slackware system
Copyright (C) 2008  Giovanni Santostefano

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 3 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, see <http://www.gnu.org/licenses/>.*/

#include <QMessageBox>
#include <QFileDialog>
#include <QDir>
#include <QStringList>
#include <QTextStream>
#include <QRegExp>
#include <QUrl>

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

#include "upgradewindow.h"

using namespace std;

UpgradeWindow::UpgradeWindow(QWidget *parent)
: QWidget(parent)
{
	
	ui.setupUi(this);
    
	connect( ui.upgradebutton, SIGNAL( clicked() ),
             this, SLOT( UpgradePkgButtonPressed() ) );    
}

UpgradeWindow::~UpgradeWindow()
{
}

void UpgradeWindow::UpgradePkgButtonPressed()
{
	//open a openfile dialog to select the package to open
	QString command="su -c upgradepkg\\ ";
	QString dryrun="--dry-run\\ ";
	QString installnew="--install-new\\ ";
	QString reinstall="--reinstall\\ ";
	QString verbose="--verbose\\ ";
	QString filename = QFileDialog::getOpenFileName(0,"Open *.tgz package","/","*.tgz");
	
	//cancel is selected at file choosing dialog
	if(filename.isEmpty()) return;
	
	//checking options
	if(ui.dryrun->checkState()==Qt::Checked)
	{
		command+=dryrun;
	}
	
	if(ui.installnew->checkState()==Qt::Checked)
	{
		command+=installnew;
	}
	
	if(ui.reinstall->checkState()==Qt::Checked)
	{
		command+=reinstall;
	}
	
	if(ui.verbose->checkState()==Qt::Checked)
	{
		command+=verbose;
	}
	
	//adding filename
	command+=filename+"\0";
	
	pid_t pid=fork();
	
	if(pid==0)
	{
		string s=command.toStdString();
		execl("/usr/bin/xterm","xterm","-e",s.c_str(),(char*)0);
	}
}

