diff options
author | dec05eba <dec05eba@protonmail.com> | 2018-05-06 17:15:51 +0200 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2018-05-06 17:18:19 +0200 |
commit | 7a2cb2c4b81a8a0696d3a11ce8781542f181bb12 (patch) | |
tree | 0d77fa05f4a99c4c1ff06f12ba76fae62550fc9b /src/Cache.cpp | |
parent | b2f6a0235c5de32a3fcd359e28f4d1e3bd6950df (diff) |
Make dchat Text editable
Not finished yet. Currently text can be entered, removed and you can move caret
using arrow keys (up, down, left, right), home and end.
Need to implement text selection and remove focus from chatbar when
editing message board text.
Chatbar should be replaced with dchat Text for proper multiline editable
text.
Diffstat (limited to 'src/Cache.cpp')
-rw-r--r-- | src/Cache.cpp | 42 |
1 files changed, 37 insertions, 5 deletions
diff --git a/src/Cache.cpp b/src/Cache.cpp index cba346b..c795595 100644 --- a/src/Cache.cpp +++ b/src/Cache.cpp @@ -196,22 +196,32 @@ namespace dchat int exitStatus; if(it->process->try_get_exit_status(exitStatus)) { + ContentByUrlResult contentByUrlResult; bool failed = exitStatus != 0; - if(!failed) + if(failed) + { + contentByUrlResult = { (sf::Texture*)nullptr, ContentByUrlResult::Type::FAILED_DOWNLOAD }; + } + else { boost::filesystem::path filepath = getImagesDir(); odhtdb::Hash urlHash(it->url.data(), it->url.size()); filepath /= urlHash.toString(); - ContentByUrlResult contentByUrlResult = loadImageFromFile(filepath); - imageDownloadMutex.lock(); - contentUrlCache[it->url] = contentByUrlResult; - imageDownloadMutex.unlock(); + contentByUrlResult = loadImageFromFile(filepath); if(contentByUrlResult.type == ContentByUrlResult::Type::CACHED) { printf("Download content from url: %s\n", it->url.c_str()); } } + + imageDownloadMutex.lock(); + contentUrlCache[it->url] = contentByUrlResult; + boost::filesystem::path downloadingFilepath = getImagesDir() / ".downloading"; + // Intentionally ignore failure, program should not crash if we fail to remove these files... + boost::system::error_code err; + boost::filesystem::remove(downloadingFilepath, err); + imageDownloadMutex.unlock(); it = imageDownloadProcesses.erase(it); } else @@ -243,6 +253,18 @@ namespace dchat downloadWaitThread.join(); } + void replaceFileIgnoreError(const boost::filesystem::path &path) + { + try + { + fileReplace(path, StringView()); + } + catch(FileException &e) + { + fprintf(stderr, "Failed to replace file: %s, reason: %s\n", path.string().c_str(), e.what()); + } + } + const ContentByUrlResult Cache::getContentByUrl(const string &url, int downloadLimitBytes) { lock_guard<mutex> lock(imageDownloadMutex); @@ -255,6 +277,15 @@ namespace dchat odhtdb::Hash urlHash(url.data(), url.size()); filepath /= urlHash.toString(); + boost::filesystem::path downloadingFilepath = getImagesDir() / ".downloading"; + if(boost::filesystem::exists(downloadingFilepath)) + { + // Intentionally ignore failure, program should not crash if we fail to remove these files... + boost::system::error_code err; + boost::filesystem::remove(filepath, err); + boost::filesystem::remove(downloadingFilepath, err); + } + ContentByUrlResult contentByUrlResult = loadImageFromFile(filepath); if(contentByUrlResult.type == ContentByUrlResult::Type::CACHED) { @@ -268,6 +299,7 @@ namespace dchat return contentByUrlResult; } + replaceFileIgnoreError(downloadingFilepath); ContentByUrlResult result((sf::Texture*)nullptr, ContentByUrlResult::Type::DOWNLOADING); contentUrlCache[url] = result; |