QGCUASFileView.cc 6.66 KB
Newer Older
Don Gagne's avatar
Don Gagne committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*=====================================================================
 
 QGroundControl Open Source Ground Control Station
 
 (c) 2009 - 2014 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
 
 This file is part of the QGROUNDCONTROL project
 
 QGROUNDCONTROL 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.
 
 QGROUNDCONTROL 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 QGROUNDCONTROL. If not, see <http://www.gnu.org/licenses/>.
 
 ======================================================================*/

24 25 26
#include "QGCUASFileView.h"
#include "uas/QGCUASFileManager.h"

Lorenz Meier's avatar
Lorenz Meier committed
27 28
#include <QFileDialog>
#include <QDir>
Don Gagne's avatar
Don Gagne committed
29
#include <QMessageBox>
Lorenz Meier's avatar
Lorenz Meier committed
30

31 32
QGCUASFileView::QGCUASFileView(QWidget *parent, QGCUASFileManager *manager) :
    QWidget(parent),
Don Gagne's avatar
Don Gagne committed
33
    _manager(manager)
34
{
Don Gagne's avatar
Don Gagne committed
35
    _ui.setupUi(this);
36

Don Gagne's avatar
Don Gagne committed
37 38 39 40 41 42 43 44
    bool success = connect(_ui.listFilesButton, SIGNAL(clicked()), this, SLOT(_refreshTree()));
    Q_ASSERT(success);
    success = connect(_ui.downloadButton, SIGNAL(clicked()), this, SLOT(_downloadFiles()));
    Q_ASSERT(success);
    success = connect(_ui.treeWidget, SIGNAL(currentItemChanged(QTreeWidgetItem*, QTreeWidgetItem*)), this, SLOT(_currentItemChanged(QTreeWidgetItem*, QTreeWidgetItem*)));
    Q_ASSERT(success);
    Q_UNUSED(success);
}
Lorenz Meier's avatar
Lorenz Meier committed
45

Don Gagne's avatar
Don Gagne committed
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
void QGCUASFileView::_downloadFiles(void)
{
    QString dir = QFileDialog::getExistingDirectory(this, tr("Download Directory"),
                                                     QDir::homePath(),
                                                     QFileDialog::ShowDirsOnly
                                                     | QFileDialog::DontResolveSymlinks);
    // And now download to this location
    QString path;
    QTreeWidgetItem* item = _ui.treeWidget->currentItem();
    if (item && item->type() == _typeFile) {
        do {
            path.prepend("/" + item->text(0));
            item = item->parent();
        } while (item);
        qDebug() << "Download: " << path;
        
        bool success = connect(_manager, SIGNAL(statusMessage(QString)), this, SLOT(_downloadStatusMessage(QString)));
        Q_ASSERT(success);
        success = connect(_manager, SIGNAL(errorMessage(QString)), this, SLOT(_downloadStatusMessage(QString)));
        Q_ASSERT(success);
        Q_UNUSED(success);
        _manager->downloadPath(path, QDir(dir));
    }
69 70
}

Don Gagne's avatar
Don Gagne committed
71
void QGCUASFileView::_refreshTree(void)
72
{
73
    _ui.treeWidget->clear();
Don Gagne's avatar
Don Gagne committed
74 75 76 77 78 79 80 81 82 83 84 85 86

    _walkIndexStack.clear();
    _walkItemStack.clear();
    _walkIndexStack.append(0);
    _walkItemStack.append(_ui.treeWidget->invisibleRootItem());
    
    bool success = connect(_manager, SIGNAL(statusMessage(QString)), this, SLOT(_treeStatusMessage(QString)));
    Q_ASSERT(success);
    success = connect(_manager, SIGNAL(errorMessage(QString)), this, SLOT(_treeErrorMessage(QString)));
    Q_ASSERT(success);
    success = connect(_manager, SIGNAL(listComplete(void)), this, SLOT(_listComplete(void)));
    Q_ASSERT(success);
    Q_UNUSED(success);
87 88 89
    
    // Don't queue up more than once
    _ui.listFilesButton->setEnabled(false);
Don Gagne's avatar
Don Gagne committed
90 91 92

    qDebug() << "List: /";
    _manager->listDirectory("/");
93
}
Lorenz Meier's avatar
Lorenz Meier committed
94

Don Gagne's avatar
Don Gagne committed
95
void QGCUASFileView::_treeStatusMessage(const QString& msg)
Lorenz Meier's avatar
Lorenz Meier committed
96
{
Don Gagne's avatar
Don Gagne committed
97 98 99 100 101 102 103 104 105 106
    int type;
    if (msg.startsWith("F")) {
        type = _typeFile;
    } else if (msg.startsWith("D")) {
        type = _typeDir;
        if (msg == "D." || msg == "D..") {
            return;
        }
    } else {
        Q_ASSERT(false);
Don Gagne's avatar
Don Gagne committed
107
        return; // Silence maybe-unitialized on type below
Don Gagne's avatar
Don Gagne committed
108 109 110
    }

    QTreeWidgetItem* item;
111
    item = new QTreeWidgetItem(_walkItemStack.last(), type);
Don Gagne's avatar
Don Gagne committed
112 113 114
    Q_CHECK_PTR(item);
    
    item->setText(0, msg.right(msg.size() - 1));
Lorenz Meier's avatar
Lorenz Meier committed
115 116
}

Don Gagne's avatar
Don Gagne committed
117
void QGCUASFileView::_treeErrorMessage(const QString& msg)
Lorenz Meier's avatar
Lorenz Meier committed
118
{
Don Gagne's avatar
Don Gagne committed
119
    QTreeWidgetItem* item;
120
    item = new QTreeWidgetItem(_walkItemStack.last(), _typeError);
Don Gagne's avatar
Don Gagne committed
121 122 123
    Q_CHECK_PTR(item);
    
    item->setText(0, tr("Error: ") + msg);
124 125 126
    
    // Fake listComplete signal after an error
    _listComplete();
Don Gagne's avatar
Don Gagne committed
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
}

void QGCUASFileView::_listComplete(void)
{
    // Walk the current items, traversing down into directories
    
Again:
    int walkIndex = _walkIndexStack.last();
    QTreeWidgetItem* parentItem = _walkItemStack.last();
    QTreeWidgetItem* childItem = parentItem->child(walkIndex);

    // Loop until we hit a directory
    while (childItem && childItem->type() != _typeDir) {
        // Move to next index at current level
        _walkIndexStack.last() = ++walkIndex;
        childItem = parentItem->child(walkIndex);
    }
    
    if (childItem) {
        // Process this item
        QString text = childItem->text(0);
        
        // Move to the next item for processing at this level
        _walkIndexStack.last() = ++walkIndex;
        
        // Push this new directory on the stack
        _walkItemStack.append(childItem);
        _walkIndexStack.append(0);
        
        // Ask for the directory list
        QString dir;
        for (int i=1; i<_walkItemStack.count(); i++) {
            QTreeWidgetItem* item = _walkItemStack[i];
            dir.append("/" + item->text(0));
        }
        qDebug() << "List:" << dir;
        _manager->listDirectory(dir);
    } else {
        // We have run out of items at the this level, pop the stack and keep going at that level
        _walkIndexStack.removeLast();
        _walkItemStack.removeLast();
        if (_walkIndexStack.count() != 0) {
            goto Again;
        } else {
            disconnect(_manager, SIGNAL(statusMessage(QString)), this, SLOT(_treeStatusMessage(QString)));
            disconnect(_manager, SIGNAL(errorMessage(QString)), this, SLOT(_treeErrorMessage(QString)));
            disconnect(_manager, SIGNAL(listComplete(void)), this, SLOT(_listComplete(void)));
174
            _ui.listFilesButton->setEnabled(true);
Don Gagne's avatar
Don Gagne committed
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
        }
    }
}

void QGCUASFileView::_downloadStatusMessage(const QString& msg)
{
    disconnect(_manager, SIGNAL(statusMessage(QString)), this, SLOT(_downloadStatusMessage(QString)));
    disconnect(_manager, SIGNAL(errorMessage(QString)), this, SLOT(_downloadStatusMessage(QString)));
    
    QMessageBox msgBox;
    msgBox.setWindowModality(Qt::ApplicationModal);
    msgBox.setText(msg);
    msgBox.exec();
}

void QGCUASFileView::_currentItemChanged(QTreeWidgetItem* current, QTreeWidgetItem* previous)
{
    Q_UNUSED(previous);
193
    _ui.downloadButton->setEnabled(current ? (current->type() == _typeFile) : false);
Lorenz Meier's avatar
Lorenz Meier committed
194
}