Commit a7af76ec authored by Paweł Wegner's avatar Paweł Wegner
Browse files

Integrated MockProvider with cloudbrowser.

parent f947fe59
......@@ -32,6 +32,8 @@
#include <QSettings>
#include <iostream>
#include "MockProvider.h"
using namespace cloudstorage;
Window::Window()
......@@ -40,7 +42,6 @@ Window::Window()
media_player_(vlc_instance_) {
qRegisterMetaType<ItemPointer>();
qRegisterMetaType<ImagePointer>();
qmlRegisterType<ItemModel>();
setClearBeforeRendering(false);
initializeMediaPlayer();
......@@ -48,9 +49,10 @@ Window::Window()
QStringList clouds;
for (auto p : ICloudStorage::create()->providers())
clouds.append(p->name().c_str());
clouds.append("mock");
rootContext()->setContextProperty("cloudModel", clouds);
rootContext()->setContextProperty("window", this);
rootContext()->setContextProperty("directoryModel", nullptr);
rootContext()->setContextProperty("directoryModel", &directory_model_);
engine()->addImageProvider("provider", imageProvider());
setSource(QUrl("qrc:/main.qml"));
......@@ -86,7 +88,10 @@ void Window::initializeCloud(QString name) {
saveCloudAccessToken();
QSettings settings;
cloud_provider_ = ICloudStorage::create()->provider(name.toStdString());
if (name != "mock")
cloud_provider_ = ICloudStorage::create()->provider(name.toStdString());
else
cloud_provider_ = make_unique<MockProvider>();
std::cerr << "[DIAG] Trying to authorize with "
<< settings.value(name).toString().toStdString() << std::endl;
ICloudProvider::Hints hints =
......@@ -103,7 +108,8 @@ void Window::listDirectory() {
current_directory_, make_unique<ListDirectoryCallback>(this));
}
void Window::changeCurrentDirectory(ItemModel* directory) {
void Window::changeCurrentDirectory(int directory_id) {
auto directory = directory_model_.get(directory_id);
directory_stack_.push_back(current_directory_);
current_directory_ = directory->item();
......@@ -117,13 +123,7 @@ void Window::onSuccessfullyAuthorized() {
}
void Window::onAddedItem(IItem::Pointer i) {
QObjectList object_list =
rootContext()->contextProperty("directoryModel").value<QObjectList>();
ItemModel* model = new ItemModel(i, cloud_provider_, this);
model->setParent(this);
object_list.append(model);
rootContext()->setContextProperty("directoryModel",
QVariant::fromValue(object_list));
directory_model_.addItem(i, this);
}
void Window::onPlayFile(QString filename) {
......@@ -150,12 +150,7 @@ void Window::keyPressEvent(QKeyEvent* e) {
media_player_.pause();
}
void Window::clearCurrentDirectoryList() {
QObjectList object_list =
rootContext()->contextProperty("directoryModel").value<QObjectList>();
rootContext()->setContextProperty("directoryModel", nullptr);
for (QObject* object : object_list) delete object;
}
void Window::clearCurrentDirectoryList() { directory_model_.clear(); }
void Window::saveCloudAccessToken() {
if (cloud_provider_) {
......@@ -182,12 +177,11 @@ void Window::initializeMediaPlayer() {
}
void Window::startDirectoryClear(std::function<void()> f) {
if (list_directory_request_) list_directory_request_->cancel();
clear_directory_ = std::async(std::launch::async, [this, f]() {
QObjectList object_list =
rootContext()->contextProperty("directoryModel").value<QObjectList>();
for (QObject* object : object_list) {
auto item = static_cast<ItemModel*>(object);
if (item->thumbnail_request_) item->thumbnail_request_->cancel();
for (int i = 0; i < directory_model_.rowCount(); i++) {
auto model = directory_model_.get(i);
if (model->thumbnail_request_) model->thumbnail_request_->cancel();
}
f();
});
......@@ -219,7 +213,8 @@ bool Window::goBack() {
return true;
}
void Window::play(ItemModel* item) {
void Window::play(int item_id) {
ItemModel* item = directory_model_.get(item_id);
stop();
item_data_request_ = cloud_provider_->getItemDataAsync(
item->item()->id(),
......@@ -241,7 +236,8 @@ void Window::uploadFile(QString path) {
make_unique<UploadFileCallback>(this, url));
}
void Window::downloadFile(ItemModel* i, QUrl path) {
void Window::downloadFile(int item_id, QUrl path) {
ItemModel* i = directory_model_.get(item_id);
download_request_ = cloud_provider_->downloadFileAsync(
i->item(),
make_unique<DownloadFileCallback>(this, path.toLocalFile().toStdString() +
......@@ -285,3 +281,31 @@ void ImageProvider::addImage(QString id, ImagePointer img) {
bool ImageProvider::hasImage(QString id) {
return cache_.find("/" + id) != cache_.end();
}
int DirectoryModel::rowCount(const QModelIndex&) const { return list_.size(); }
QVariant DirectoryModel::data(const QModelIndex& id, int) const {
QVariantMap dict;
dict["thumbnail"] = list_[id.row()]->thumbnail();
dict["name"] = list_[id.row()]->item()->filename().c_str();
dict["is_directory"] =
list_[id.row()]->item()->type() == IItem::FileType::Directory;
return dict;
}
void DirectoryModel::addItem(IItem::Pointer item, Window* w) {
beginInsertRows(QModelIndex(), rowCount(), rowCount());
auto model = make_unique<ItemModel>(item, w->cloud_provider_, w);
list_.push_back(std::move(model));
endInsertRows();
int idx = rowCount() - 1;
connect(list_.back().get(), &ItemModel::thumbnailChanged, this,
[this, idx]() { emit dataChanged(index(idx, 0), index(idx, 0)); },
Qt::QueuedConnection);
}
void DirectoryModel::clear() {
beginRemoveRows(QModelIndex(), 0, rowCount() - 1);
list_.clear();
endRemoveRows();
}
......@@ -26,6 +26,7 @@
#include <ICloudProvider.h>
#include <IItem.h>
#include <QAbstractListModel>
#include <QQuickImageProvider>
#include <QQuickView>
#include <future>
......@@ -51,15 +52,12 @@ class ImageProvider : public QQuickImageProvider {
class ItemModel : public QObject {
public:
Q_PROPERTY(QString name READ name CONSTANT)
Q_PROPERTY(bool directory READ is_directory CONSTANT)
Q_PROPERTY(QString thumbnail READ thumbnail NOTIFY thumbnailChanged)
using Pointer = std::unique_ptr<ItemModel>;
ItemModel(cloudstorage::IItem::Pointer item,
cloudstorage::ICloudProvider::Pointer, Window*);
QString name() const { return item_->filename().c_str(); }
bool is_directory() const { return item_->is_directory(); }
cloudstorage::IItem::Pointer item() const { return item_; }
QString thumbnail() const { return thumbnail_; }
......@@ -78,6 +76,20 @@ class ItemModel : public QObject {
Q_OBJECT
};
class DirectoryModel : public QAbstractListModel {
public:
int rowCount(const QModelIndex& parent = QModelIndex()) const;
QVariant data(const QModelIndex& index, int) const;
void addItem(IItem::Pointer, Window* w);
ItemModel* get(int id) const { return list_[id].get(); }
void clear();
private:
std::vector<ItemModel::Pointer> list_;
};
class Window : public QQuickView {
public:
Window();
......@@ -87,12 +99,12 @@ class Window : public QQuickView {
Q_INVOKABLE void initializeCloud(QString name);
Q_INVOKABLE void listDirectory();
Q_INVOKABLE void changeCurrentDirectory(ItemModel* directory);
Q_INVOKABLE void changeCurrentDirectory(int directory);
Q_INVOKABLE bool goBack();
Q_INVOKABLE void play(ItemModel* item);
Q_INVOKABLE void play(int item);
Q_INVOKABLE void stop();
Q_INVOKABLE void uploadFile(QString path);
Q_INVOKABLE void downloadFile(ItemModel*, QUrl path);
Q_INVOKABLE void downloadFile(int, QUrl path);
void onSuccessfullyAuthorized();
void onAddedItem(cloudstorage::IItem::Pointer);
......@@ -119,6 +131,7 @@ class Window : public QQuickView {
private:
friend class CloudProviderCallback;
friend class DirectoryModel;
void clearCurrentDirectoryList();
void saveCloudAccessToken();
......@@ -139,6 +152,7 @@ class Window : public QQuickView {
VLC::Instance vlc_instance_;
VLC::MediaPlayer media_player_;
std::future<void> clear_directory_;
DirectoryModel directory_model_;
Q_OBJECT
};
......
......@@ -55,6 +55,7 @@ Item {
onAddedItem: {
directory.visible = true;
directory.focus = true;
//console.log(directory.currentIndex);
}
onUploadProgressChanged: {
uploadProgress.visible = total != 0;
......@@ -84,7 +85,7 @@ Item {
property int thumbnailWidth: 50
property int thumbnailHeight: 50
property int padding: 5
property var view: ListView.view
property string isDirectory: display["is_directory"]
id: fileEntry
height: thumbnailHeight + 2 * padding
......@@ -97,7 +98,7 @@ Item {
Image {
width: fileEntry.thumbnailWidth
height: fileEntry.thumbnailHeight
source: thumbnail
source: display["thumbnail"]
}
Item {
height: fileEntry.thumbnailHeight
......@@ -106,7 +107,7 @@ Item {
x: fileEntry.padding
id: text
anchors.verticalCenter: parent.verticalCenter
text: name
text: display["name"]
}
}
}
......@@ -125,15 +126,14 @@ Item {
}
if (currentIndex == -1)
return;
var t = directoryModel[currentIndex];
if (event.key === Qt.Key_Return) {
if (t.directory)
window.changeCurrentDirectory(t);
if (currentItem.isDirectory)
window.changeCurrentDirectory(currentIndex);
else
window.play(t);
window.play(currentIndex);
} else if (event.key === Qt.Key_D) {
downloadFileDialog.visible = true;
downloadFileDialog.file = t;
downloadFileDialog.file = currentIndex;
downloadFileDialog.open();
} else if (event.key === Qt.Key_F5)
window.listDirectory();
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment