QList<FileStoreEntry*> LocalFileStore::readdir(QString path, QStringList &errors) { QList<FileStoreEntry*> returning; QDir current_path = QDir(path); if (!current_path.exists()) { errors << tr("Folder %1 does not exist").arg(path); return returning; } QFileInfoList files = current_path.entryInfoList(); foreach (QFileInfo info, files) { // skip . and .. if (info.fileName() == "." || info.fileName() == "..") continue; FileStoreEntry *add = newFileStoreEntry(); //QFileInfo has full path, we just want the file name add->name = info.fileName(); add->id = add->name; add->isDir = info.isDir(); add->size = info.size(); // dates in format "Tue, 19 Jul 2011 21:55:38 +0000" add->modified = info.lastModified(); returning << add; } // all good ? return returning; }
QList<FileStoreEntry*> Dropbox::readdir(QString path, QStringList &errors) { QList<FileStoreEntry*> returning; // do we have a token QString token = appsettings->cvalue(context->athlete->cyclist, GC_DROPBOX_TOKEN, "").toString(); if (token == "") { errors << tr("You must authorise with Dropbox first"); return returning; } // lets connect and get basic info on the root directory QString url("https://api.dropboxapi.com/1/metadata/auto/" + path + "?include_deleted=false&list=true"); // request using the bearer token QNetworkRequest request(url); request.setRawHeader("Authorization", (QString("Bearer %1").arg(token)).toLatin1()); QNetworkReply *reply = nam->get(request); // blocking request QEventLoop loop; connect(reply, SIGNAL(finished()), &loop, SLOT(quit())); loop.exec(); // did we get a good response ? QByteArray r = reply->readAll(); QJsonParseError parseError; QJsonDocument document = QJsonDocument::fromJson(r, &parseError); // if path was returned all is good, lets set root if (parseError.error == QJsonParseError::NoError) { // contents ? QJsonArray contents = document.object()["contents"].toArray(); // lets look at that then for(int i=0; i<contents.size(); i++) { QJsonValue each = contents.at(i); FileStoreEntry *add = newFileStoreEntry(); //dropbox has full path, we just want the file name add->name = QFileInfo(each.toObject()["path"].toString()).fileName(); add->isDir = each.toObject()["is_dir"].toBool(); add->size = each.toObject()["bytes"].toInt(); add->id = add->name; // dates in format "Tue, 19 Jul 2011 21:55:38 +0000" add->modified = QDateTime::fromString(each.toObject()["modified"].toString().mid(0,25), "ddd, dd MMM yyyy hh:mm:ss"); returning << add; } } // all good ? return returning; }
// open by connecting and getting a basic list of folders available bool Dropbox::open(QStringList &errors) { // do we have a token QString token = appsettings->cvalue(context->athlete->cyclist, GC_DROPBOX_TOKEN, "").toString(); if (token == "") { errors << "You must authorise with Dropbox first"; return false; } // lets connect and get basic info on the root directory QString url("https://api.dropboxapi.com/1/metadata/auto/?list=false"); // request using the bearer token QNetworkRequest request(url); request.setRawHeader("Authorization", (QString("Bearer %1").arg(token)).toLatin1()); QNetworkReply *reply = nam->get(request); // blocking request QEventLoop loop; connect(reply, SIGNAL(finished()), &loop, SLOT(quit())); loop.exec(); if (reply->error() != QNetworkReply::NoError) { errors << tr("Network Problem reading Dropbox data"); return false; } // did we get a good response ? QByteArray r = reply->readAll(); QJsonParseError parseError; QJsonDocument document = QJsonDocument::fromJson(r, &parseError); // if path was returned all is good, lets set root if (parseError.error == QJsonParseError::NoError) { // we have a root root_ = newFileStoreEntry(); // path name root_->name = document.object()["path"].toString(); root_->isDir = document.object()["is_dir"].toBool(); root_->size = document.object()["bytes"].toInt(); // happy with what we got ? if (root_->name != "/") errors << tr("invalid root path."); if (root_->isDir != true) errors << tr("root is not a directory."); } else { errors << tr("problem parsing Dropbox data"); } // ok so far ? if (errors.count()) return false; return true; }
LocalFileStore::LocalFileStore(Context *context) : FileStore(context), context(context) { // we have a root root_ = newFileStoreEntry(); // root is always root on a local file store root_->name = "/"; root_->isDir = true; root_->size = 1; }