void CFileBrowser::runApp(void){
    struct link_stat st;
    int err;
    CNotify notify;
    QTreeWidgetItem* item;
    item = ui->tree->currentItem();
    if( item == 0 ){
        notify.execError("Invalid Selection");
        return;
    }

    err = link()->stat( item->text(1).toLocal8Bit().constData(),
                        &st);

    if ( err < 0 ){
        notify.execLinkError(link_errno);
        return;
    }

    if ( (st.st_mode & LINK_S_IFMT) != LINK_S_IFREG ){
        notify.execError("File is not executable");
        return;
    }

    if( st.st_mode & (LINK_S_IXGRP|LINK_S_IXOTH|LINK_S_IXUSR) ){
        //Execute a program on the target device
        emit runApplication( ui->tree->currentItem()->text(1) );
    } else {
        notify.execError("File is not executable");
    }
}
void CFileBrowser::refresh(void){
    //Update the tree
    CNotify notify;

    if ( link()->get_is_connected() == false ){
        //Error::showNotConnected();
        return;
    }

    ui->tree->clear();

    topItem = new QTreeWidgetItem();
    if ( topItem == NULL ){
        notify.execError("Top Item is NULL");
        return;
    }

    if ( ui->gotoLine->text() == "" ){
        //Goto to "/" as the default directory
        ui->gotoLine->setText("/");
    }

    topItem->setText(0, ui->gotoLine->text() );
    topItem->setText(1, ui->gotoLine->text() );
    topItem->setText(2, "false" );
    ui->tree->addTopLevelItem(topItem);
    ui->tree->setCurrentItem(topItem, 0);

    loadDirectory(topItem);
}
int Monitor::signalProcess(int signo){
    int row;
    int pid;
    int err;
    QTableWidgetItem * item;
    CNotify notify;

    if ( link()->isConnected() == false ){
        notify.execNotConnected();
        return -1;
    }

    row = ui->table->currentRow();
    if ( row != 0 ){
        item = ui->table->item(row, PROCESS_ID_COL);
        if ( item == NULL ){
            return -1;
        }

        pid = item->text().replace(" (thread)", "").toInt();
        if( pid != 0 ){
            if ( (err = link()->kill_pid(pid, signo)) < 0 ){
                notify.execError(errorMessage());
            } else {
                ui->table->removeRow(row);
            }
        }
    }
    return 0;
}
void CFileBrowser::on_tree_itemActivated(QTreeWidgetItem* item, int column){
    CNotify notify;
    qDebug("item activated %s %s", item->text(0).toLocal8Bit().constData(), item->text(1).toLocal8Bit().constData());
    if ( column != 0 ){
        notify.execError("Invalid Column");
        return;
    }
    loadDirectory(item);
    item->setExpanded(true);
}
void CaosIsp::operationComplete(QString msg, bool err){
  CNotify notify;
  if ( err == true ){
      notify.execError(msg);
    }  else if ( msg != "" ){
      if ( currentMessage != "" ){
          msg += ", " + currentMessage;
        }
      ui->statusBar->showMessage("      " + msg, 3000);
    }
}
void CFileBrowser::loadFileFromDeviceSelected(QString filename){
    QString src, dest;
    QStringList path;
    int err;
    CNotify notify;

    src = ui->tree->currentItem()->text(1);
    path = src.split('/');

    if ( path.count() ){
        dest = filename + "/" + path[ path.count() -1 ];
        err = link()->copy(src.toStdString(), dest.toStdString(), 0, false, updateProgress);
        if ( err < 0 ){
            notify.execLinkError(link_errno);
        }
    } else {
        notify.execError("Invalid File");
    }

    CNotify::updateStatus("Uploaded to:  " + dest);

}
bool Monitor::isRunningUser(void){
    sys_taskattr_t task;
    int id;
    int err;
    CNotify notify;


    if( fd < 0 ){
        if ( link()->isConnected() == true ){
            fd = link()->open("/dev/sys", LINK_O_RDWR);
        }
    }
    id = 0;
    do {
        if ( link()->isConnected() == false ){
            return -1;
        }

        task.is_enabled = 0;
        task.tid = id;
        err = link()->ioctl(fd, I_SYS_GETTASK, &task);
        if ( err < -2 ){
            notify.execError(errorMessage() );
            return -1;
        }

        if( task.is_enabled != 0 ){
            if( QString(task.name) != "sys" ){
                return true;
            }
        }

        id++;
    } while( err != -1 );

    return false;
}
void Monitor::updateAll(){
    int id;
    int err;
    int row;
    int cpuRow;
    uint64_t totalTime;
    uint64_t taskTime;
    sys_taskattr_t task;
    CNotify notify;
    id = 0;

    if( link()->isConnected() == false ){
        return;
    }

    if( fd < 0 ){
        return;
    }

    do {
        if ( link()->isConnected() == false ){
            stopTimer();
            for(row = 0; row < ui->table->rowCount(); row++){
                ui->table->removeRow(row);
            }
            return;
        }

        task.is_enabled = 0;
        task.tid = id;
        err = link()->ioctl(fd, I_SYS_GETTASK, &task);
        if ( err < -2 ){
            stopTimer();
            for(row = 0; row < ui->table->rowCount(); row++){
                ui->table->removeRow(row);
            }
            notify.execError( errorMessage() );
            return;
        }

        //update the entries in the table
        updateRow(id, &task);
        id++;
    } while( err != -1 );


    cpuRow = ui->table->rowCount();
    totalTime = 0;
    for(row = 0; row < cpuRow; row++){
        totalTime += ui->table->item(row, DELTA_TASK_TIME_COL)->text().toLongLong();
    }


    for(row = 0; row < cpuRow; row++){
        taskTime = ui->table->item(row, DELTA_TASK_TIME_COL)->text().toLongLong();
        updateItem(row, CPU_COL, QString::number( taskTime * 100.0 / totalTime, 'f', 2 ));
    }

    ui->table->sortItems(sortColumn, sortOrder);


}