Exemple #1
0
void signalHandler(int signal) {
	struct job *j;
	if (!job_pid) {
		cleanJobs(&jobs);
		j = lastJob(jobs);
		if (j) {
			job_pid = j->pid;
		}
	}
	//força a impressão do buffer de saída
	fflush(stdout);
	switch (signal) {
		//ctrl+c
		case SIGINT:
			if (job_pid) {
				terminateJob(job_pid, &jobs);
				printf("\n[%d]\tterminado\t%s\n", job_pid, "");
				job_pid = 0;
			} else {
				handler_action = 1;
			}
			break;
		//ctrl+z
		case SIGTSTP:
			if (job_pid) {
				pauseJob(job_pid, &jobs);
				printf("\n[%d]\tpausado\t%s\n", job_pid, "");
			} else {
				handler_action = 2;
			}
			break;
		//child process stop/terminate
		case SIGCHLD:
			if (job_pid) {
				if (handler_action == 1) {
					terminateJob(job_pid, &jobs);
					printf("\n[%d]\tterminado\t%s\n", job_pid, "");
					job_pid = 0;
				} else if (handler_action == 2) {
					pauseJob(job_pid, &jobs);
					printf("\n[%d]\tpausado\t%s\n", job_pid, "");
				}
				handler_action = 0;
			}
			break;
		//default:
			//printf("SIGNAL: %d\n", signal);
	}
	//printf("job_pid: %d\n", job_pid);
	//remove os 'jobs' terminados da lista
	cleanJobs(&jobs);
	printf("\n");
}
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    quitDialog(0), saveChanges(false),
    newJobFileName(""), newJobDestinationDirectory("")
{
    m_has_error_happend = false;
    QMetaObject::invokeMethod(this, "loadSettings", Qt::QueuedConnection);
    setWindowIcon(QIcon(":/ui/icons/motocool.jpg"));

    // Initiallize headers
    QStringList headers;
    headers << tr("Name") << tr("Downloaded/Total") << tr("Progress") << tr("Speed")
            << tr("Status") << tr("Remaining time");

    // Main job list
    jobView = new JobView(this);
    jobView->setHeaderLabels(headers);
    jobView->setSelectionBehavior(QAbstractItemView::SelectRows);
    jobView->setAlternatingRowColors(true);
    jobView->setRootIsDecorated(false);
    setCentralWidget(jobView);

    // Set header resize modes and initial section sizes
    QFontMetrics fm = fontMetrics();
    QHeaderView *header = jobView->header();
    header->resizeSection(0, fm.width("typical-name-length-for-a-download-task"));
    header->resizeSection(1, fm.width(headers.at(1) + "100MB/999MB"));
    header->resizeSection(2, fm.width(headers.at(2) + "100%"));
    header->resizeSection(3, qMax(fm.width(headers.at(3) + "   "), fm.width(" 1023.0 KB/s")));
    header->resizeSection(4, qMax(fm.width(headers.at(4) + "   "), fm.width(tr("Downloading  ") + "   ")));
    header->resizeSection(5, qMax(fm.width(headers.at(5) + "   "), fm.width(tr("--:--") + "   ")));

    // Create common actions
    QAction *newJobAction = new QAction(QIcon(":/ui/icons/bottom.png"), tr("Add &new job"), this);
    pauseJobAction = new QAction(QIcon(":/ui/icons/player_pause.png"), tr("&Pause job"), this);
    removeJobAction = new QAction(QIcon(":/ui/icons/player_stop.png"), tr("&Remove job"), this);
    openDirAction = new QAction(QIcon(":/ui/icons/folder.png"), tr("Open file &directory"), this);

    // File menu
    QMenu *fileMenu = menuBar()->addMenu(tr("&File"));
    fileMenu->addAction(newJobAction);
    fileMenu->addAction(pauseJobAction);
    fileMenu->addAction(removeJobAction);
    fileMenu->addAction(openDirAction);
    fileMenu->addSeparator();
    fileMenu->addAction(QIcon(":/ui/icons/exit.png"), tr("E&xit"), this, SLOT(close()));

    // Help Menu
    QMenu *helpMenu = menuBar()->addMenu(tr("&Help"));
    helpMenu->addAction(tr("&About"), this, SLOT(about()));

    // Top toolbar
    QToolBar *topBar = new QToolBar(tr("Tools"));
    addToolBar(Qt::TopToolBarArea, topBar);
    topBar->setMovable(false);
    topBar->addAction(newJobAction);
    topBar->addAction(pauseJobAction);
    topBar->addAction(removeJobAction);
    topBar->addAction(openDirAction);
    pauseJobAction->setEnabled(false);
    removeJobAction->setEnabled(false);
    openDirAction->setEnabled(false);

    // Set up connections
    connect(jobView, SIGNAL(itemSelectionChanged()),
            this, SLOT(setActionsEnabled()));
    connect(newJobAction, SIGNAL(triggered()),
            this, SLOT(addJob()));
    connect(pauseJobAction, SIGNAL(triggered()),
            this, SLOT(pauseJob()));
    connect(removeJobAction, SIGNAL(triggered()),
            this, SLOT(removeJob()));
    connect(openDirAction, SIGNAL(triggered()),
            this, SLOT(openDir()));
}