void CFileBrowser::formatFilesystem(void){
    QString filename;
    struct link_stat st;
    int err;
    int fmt;
    QTreeWidgetItem* item;
    CNotify notify;
    item = ui->tree->currentItem();
    filename = item->text(1);

    //check to see if this is a directory or a file
    err = link()->stat( item->text(1).toLocal8Bit().constData(),
                        &st);
    if ( err < 0 ){
        qDebug("Stat failed");
        notify.execLinkError(link_errno);
        return;
    }

    fmt = st.st_mode & LINK_S_IFMT;
    switch(fmt){
    case LINK_S_IFDIR:
        err = link()->format(filename.toLocal8Bit().constData());
        break;
    default:
        qDebug("Unknown file type");
        break;
    }
    if ( err ){
        notify.execLinkError(link_errno);
    }
    this->refresh();
    CNotify::updateStatus("Formatted " + filename);
}
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::saveFileToDeviceSelected(QStringList filenames){
    CNotify notify;

    if( copyFilesToDevice(link(), filenames, ui->tree->currentItem()->text(1)) == false ){
        notify.execLinkError(link_errno);
    } else {
        notify.updateStatus("File Saved");
        refresh();
    }
}
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);

}
void CFileBrowser::loadDirectory(QTreeWidgetItem * treeItem){
    int dirp;
    int err;
    QString dName;
    QTreeWidgetItem * child;
    QString currentDirectory;
    struct link_dirent entry;
    CNotify notify;

    //First read the current directory or root if no directory has been selected
    currentDirectory = treeItem->text(1);
    qDebug("loadDirectory:%s", currentDirectory.toLocal8Bit().constData());

    if ( ( treeItem->text(0) == "." ) ||
         ( treeItem->text(0) == ".." ) ){
        //These are aliases to the current and parent directory and should not expanded
        return;
    }

    //Check to see if the directory has already been loaded
    if ( treeItem->text(2) == "false" ){

        //mark the directory as having already been loaded
        treeItem->setText(2, "true");
        qDebug("Opendir %s", currentDirectory.toLocal8Bit().constData());
        dirp = link()->opendir(currentDirectory.toLocal8Bit().constData());

        qDebug("Open dir:%s at 0x%X", currentDirectory.toLocal8Bit().constData(), dirp );

        if( dirp > 0 ){
            while( link()->readdir_r(dirp, &entry, NULL) == 0 ){

                dName = QString(entry.d_name);

                if ( dName != "" ){
                    if ( (dName != ".") &&
                         (dName != "..") ){

                        child = new QTreeWidgetItem();

                        child->setText(0, dName);
                        if ( currentDirectory == "/" ){
                            child->setText(1, currentDirectory + dName);
                        } else {
                            child->setText(1, currentDirectory + "/" + dName);
                        }

                        child->setText(2, "false"); //Mark the new directory as having not been loaded
                        if ( ui->showHiddenFilesCheckBox->checkState() == Qt::Checked ){
                            treeItem->addChild(child);
                        } else if ( !dName.startsWith(".") ){
                            treeItem->addChild(child);
                        }
                    }
                }
            }
            err = link()->closedir(dirp);
            if ( err < 0 ){
                notify.execLinkError(link_errno);
            }
        }

    } else {
        qDebug("Item %s already loaded", treeItem->text(2).toLocal8Bit().constData());
    }
}