Skip to content
Snippets Groups Projects
Commit 9860b329 authored by Nicolas Pope's avatar Nicolas Pope
Browse files

Add recent files to add source dialog

parent 34794125
No related branches found
No related tags found
1 merge request!316Resolves #343 GUI and Frame Refactor
Pipeline #28135 passed
......@@ -29,6 +29,10 @@ std::vector<std::string> AddCtrl::getNetSources() {
return std::move(io->feed()->availableNetworkSources());
}
std::vector<std::string> AddCtrl::getFileSources() {
return std::move(io->feed()->availableFileSources());
}
std::string AddCtrl::getSourceName(const std::string &uri) {
return io->feed()->getName(uri);
}
......
......@@ -22,6 +22,7 @@ public:
ftl::Configurable *add(const std::string &uri);
std::vector<std::string> getNetSources();
std::vector<std::string> getFileSources();
std::string getSourceName(const std::string &uri);
bool isSourceActive(const std::string &uri);
......
......@@ -83,6 +83,20 @@ void AddSourceWindow::rebuild() {
{ {"ftl", "FTL Captures"} }, true));
close();
});
auto filesrcs = ctrl_->getFileSources();
for (auto &s : filesrcs) {
button = new Button(filebuttons, ctrl_->getSourceName(s));
if (ctrl_->isSourceActive(s)) {
button->setBackgroundColor(Color(0, 255, 0, 25));
}
button->setCallback([this, s]() {
ctrl_->add(s);
close();
});
}
}
void AddSourceWindow::close() {
......
......@@ -24,6 +24,10 @@ bool create_directory(const std::string &path);
bool is_video(const std::string &file);
std::vector<std::string> directory_listing(const std::string &path);
nlohmann::json loadJSON(const std::string &path);
bool saveJSON(const std::string &path, nlohmann::json &json);
namespace config {
typedef nlohmann::json json_t;
......
......@@ -33,6 +33,7 @@
using ftl::config::json_t;
using std::ifstream;
using std::ofstream;
using std::string;
using std::map;
using std::vector;
......@@ -175,6 +176,42 @@ optional<string> ftl::config::locateFile(const string &name) {
return {};
}
nlohmann::json ftl::loadJSON(const std::string &path) {
ifstream i(path.c_str());
//i.open(path);
if (i.is_open()) {
try {
nlohmann::json t;
i >> t;
return t;
} catch (nlohmann::json::parse_error& e) {
LOG(ERROR) << "Parse error in loading JSON: " << e.what();
return {};
} catch (...) {
LOG(ERROR) << "Unknown error opening JSON file: " << path;
}
return {};
} else {
return {};
}
}
bool ftl::saveJSON(const std::string &path, nlohmann::json &json) {
ofstream o(path.c_str());
//i.open(path);
if (o.is_open()) {
try {
o << json;
return true;
} catch (...) {
LOG(ERROR) << "Unknown error saving JSON file: " << path;
}
return false;
} else {
return false;
}
}
/**
* Combine one json config with another patch json config.
*/
......
......@@ -54,7 +54,6 @@ public:
};
private:
// public methods acquire lock if necessary, private methods assume locking
// managed by caller
std::mutex mtx_;
......
......@@ -9,6 +9,8 @@
using ftl::stream::Feed;
using ftl::codecs::Channel;
static nlohmann::json feed_config;
////////////////////////////////////////////////////////////////////////////////
Feed::Filter::Filter(Feed* feed, const std::unordered_set<uint32_t>& sources, const std::unordered_set<Channel>& channels) :
......@@ -46,6 +48,8 @@ Feed::Filter &Feed::Filter::select(const std::unordered_set<ftl::codecs::Channel
Feed::Feed(nlohmann::json &config, ftl::net::Universe*net) :
ftl::Configurable(config), net_(net) {
feed_config = ftl::loadJSON(FTL_LOCAL_CONFIG_ROOT "/feed.json");
pool_ = std::make_unique<ftl::data::Pool>(3,5);
stream_ = std::unique_ptr<ftl::stream::Muxer>
......@@ -142,6 +146,8 @@ Feed::Feed(nlohmann::json &config, ftl::net::Universe*net) :
Feed::~Feed() {
std::unique_lock<std::mutex> lk(mtx_);
ftl::saveJSON(FTL_LOCAL_CONFIG_ROOT "/feed.json", feed_config);
receiver_.reset(); // Note: Force destruction first to remove filters this way
for (auto* filter : filters_) {
......@@ -285,7 +291,14 @@ std::vector<std::string> Feed::availableNetworkSources() {
}
std::vector<std::string> Feed::availableFileSources() {
return {};
std::vector<std::string> files;
auto &recent_files = feed_config["recent_files"];
for (auto &f : recent_files.items()) {
files.push_back(f.key());
}
return files;
}
std::vector<std::string> Feed::availableDeviceSources() {
......@@ -319,7 +332,7 @@ std::string Feed::getName(const std::string &puri) {
} else if (uri.getScheme() == ftl::URI::SCHEME_DEVICE) {
return "Device";
} else if (uri.getScheme() == ftl::URI::SCHEME_FILE) {
// TODO: Parse last part of file name - extension
return feed_config["recent_files"][uri.getBaseURI()].value("name", "FTLFile");
}
return "No Name";
......@@ -365,6 +378,12 @@ uint32_t Feed::add(const std::string &path) {
fstream->set("filename", uri.getPath());
}
auto &recent_files = feed_config["recent_files"];
auto &file_details = recent_files[uri.getBaseURI()];
std::string fname = uri.getPathSegment(-1);
file_details["name"] = fname.substr(0, fname.find_last_of('.'));
file_details["last_open"] = ftl::timer::get_time();
// TODO: URI normalization; should happen in add(,,) or add(,,,) take
// ftl::URI instead of std::string as argument. Note the bug above.
// TODO: write unit test for uri parsing
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment