Play-/Source/ui_qt/S3FileBrowser.cpp

109 lines
3.2 KiB
C++
Raw Permalink Normal View History

2018-10-09 13:07:52 -04:00
#include "S3FileBrowser.h"
2018-10-11 12:52:54 -04:00
#include "ui_s3filebrowser.h"
#include "amazon/AmazonS3Client.h"
2018-10-09 13:07:52 -04:00
#include "../s3stream/S3ObjectStream.h"
#include "string_format.h"
2018-10-10 20:07:32 -04:00
#include "AppConfig.h"
2020-03-07 17:52:15 +00:00
#include "ui_shared/AmazonS3Utils.h"
2018-10-10 20:07:32 -04:00
#define PREF_S3FILEBROWSER_BUCKETNAME "s3.filebrowser.bucketname"
2018-10-09 13:07:52 -04:00
S3FileBrowser::S3FileBrowser(QWidget* parent)
: QDialog(parent)
, ui(new Ui::S3FileBrowser())
{
2018-10-10 20:07:32 -04:00
CAppConfig::GetInstance().RegisterPreferenceString(PREF_S3FILEBROWSER_BUCKETNAME, "");
2018-10-09 13:07:52 -04:00
ui->setupUi(this);
2018-10-10 20:07:32 -04:00
ui->bucketNameEdit->setText(CAppConfig::GetInstance().GetPreferenceString(PREF_S3FILEBROWSER_BUCKETNAME));
2018-10-09 13:07:52 -04:00
m_continuationChecker = new CContinuationChecker(this);
2018-10-10 20:02:00 -04:00
m_filterTimer = new QTimer(this);
m_filterTimer->setSingleShot(true);
connect(m_filterTimer, SIGNAL(timeout()), this, SLOT(updateFilter()));
2018-10-09 13:07:52 -04:00
launchUpdate();
}
S3FileBrowser::~S3FileBrowser()
{
delete ui;
}
bool S3FileBrowser::IsAvailable()
{
2021-04-22 19:06:45 -04:00
return CS3ObjectStream::CConfig::GetInstance().GetCredentials().IsValid();
}
2019-10-16 20:51:11 -04:00
fs::path S3FileBrowser::GetSelectedPath() const
2018-10-09 13:07:52 -04:00
{
return m_selectedPath;
}
void S3FileBrowser::on_refreshButton_clicked()
2018-10-09 13:07:52 -04:00
{
launchUpdate();
}
void S3FileBrowser::on_objectList_itemSelectionChanged()
2018-10-09 13:07:52 -04:00
{
updateOkButtonState();
}
void S3FileBrowser::on_searchFilterEdit_textChanged(QString)
2018-10-10 20:02:00 -04:00
{
m_filterTimer->start(100);
}
void S3FileBrowser::updateFilter()
{
ui->objectList->clear();
auto filter = ui->searchFilterEdit->text().toStdString();
for(const auto& item : m_bucketItems.objects)
{
if(!filter.empty() && (item.key.find(filter, 0) == std::string::npos)) continue;
ui->objectList->addItem(QString::fromStdString(item.key));
}
}
2018-10-09 13:07:52 -04:00
void S3FileBrowser::accept()
{
2018-10-10 20:07:32 -04:00
auto bucketName = m_lastUpdateBucketName.toStdString();
2018-10-09 13:07:52 -04:00
auto selectedItems = ui->objectList->selectedItems();
if(selectedItems.size() != 0)
{
auto selectedItem = selectedItems[0];
auto objectName = selectedItem->text().toStdString();
m_selectedPath = string_format("//s3/%s/%s", bucketName.c_str(), objectName.c_str());
2018-10-09 13:07:52 -04:00
}
2018-10-10 20:07:32 -04:00
CAppConfig::GetInstance().SetPreferenceString(PREF_S3FILEBROWSER_BUCKETNAME, bucketName.c_str());
CAppConfig::GetInstance().Save();
2018-10-09 13:07:52 -04:00
QDialog::accept();
}
void S3FileBrowser::updateOkButtonState()
{
bool hasSelection = ui->objectList->selectedItems().size() != 0;
ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(hasSelection);
}
void S3FileBrowser::launchUpdate()
{
setEnabled(false);
m_lastUpdateBucketName = ui->bucketNameEdit->text();
ui->objectList->clear();
updateOkButtonState();
2020-03-07 17:52:15 +00:00
auto getListFuture = std::async([bucketName = m_lastUpdateBucketName.toStdString()]() {
2021-04-22 19:06:45 -04:00
auto credentials = CS3ObjectStream::CConfig::GetInstance().GetCredentials();
return AmazonS3Utils::GetListObjects(credentials, bucketName);
2020-03-07 17:52:15 +00:00
});
2018-10-09 13:07:52 -04:00
m_continuationChecker->GetContinuationManager().Register(std::move(getListFuture),
[this](auto& result) {
2018-10-10 20:02:00 -04:00
m_bucketItems = result;
2018-10-12 17:37:04 -04:00
this->updateFilter();
this->setEnabled(true);
2018-10-09 13:07:52 -04:00
});
}