Skip to content

Commit

Permalink
fix cancel of GetDirectory
Browse files Browse the repository at this point in the history
  • Loading branch information
FernetMenta committed May 7, 2018
1 parent 8257c47 commit cc8364a
Show file tree
Hide file tree
Showing 12 changed files with 83 additions and 16 deletions.
2 changes: 1 addition & 1 deletion xbmc/addons/GUIWindowAddonBrowser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ bool CGUIWindowAddonBrowser::OnClick(int iItem, const std::string &player)
if (item->GetPath() == "addons://update_all/")
{
UpdateAddons updater;
CGUIDialogBusy::Wait(&updater);
CGUIDialogBusy::Wait(&updater, 100, true);
return true;
}
if (!item->m_bIsFolder)
Expand Down
25 changes: 20 additions & 5 deletions xbmc/dialogs/GUIDialogBusy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,27 @@

class CBusyWaiter : public CThread
{
std::shared_ptr<CEvent> m_done;
std::shared_ptr<CEvent> m_done;
IRunnable *m_runnable;
public:
explicit CBusyWaiter(IRunnable *runnable) : CThread(runnable, "waiting"), m_done(new CEvent()) { }
explicit CBusyWaiter(IRunnable *runnable) :
CThread(runnable, "waiting"), m_done(new CEvent()), m_runnable(runnable) { }

bool Wait(unsigned int displaytime, bool allowCancel)
{
std::shared_ptr<CEvent> e_done(m_done);

Create();
return CGUIDialogBusy::WaitOnEvent(*e_done, displaytime, allowCancel);
unsigned int start = XbmcThreads::SystemClockMillis();
if (!CGUIDialogBusy::WaitOnEvent(*e_done, displaytime, allowCancel))
{
m_runnable->Cancel();
unsigned int elapsed = XbmcThreads::SystemClockMillis() - start;
unsigned int remaining = (elapsed >= displaytime) ? 0 : displaytime - elapsed;
CGUIDialogBusy::WaitOnEvent(*e_done, remaining, false);
return false;
}
return true;
}

// 'this' is actually deleted from the thread where it's on the stack
Expand All @@ -52,12 +63,16 @@ class CBusyWaiter : public CThread

};

bool CGUIDialogBusy::Wait(IRunnable *runnable, unsigned int displaytime /* = 100 */, bool allowCancel /* = true */)
bool CGUIDialogBusy::Wait(IRunnable *runnable, unsigned int displaytime, bool allowCancel)
{
if (!runnable)
return false;
CBusyWaiter waiter(runnable);
return waiter.Wait(displaytime, allowCancel);
if (!waiter.Wait(displaytime, allowCancel))
{
return false;
}
return true;
}

bool CGUIDialogBusy::WaitOnEvent(CEvent &event, unsigned int displaytime /* = 100 */, bool allowCancel /* = true */)
Expand Down
2 changes: 1 addition & 1 deletion xbmc/dialogs/GUIDialogBusy.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class CGUIDialogBusy: public CGUIDialog
\param allowCancel whether the user can cancel the wait, defaults to true.
\return true if the runnable completes, false if the user cancels early.
*/
static bool Wait(IRunnable *runnable, unsigned int displaytime = 100, bool allowCancel = true);
static bool Wait(IRunnable *runnable, unsigned int displaytime, bool allowCancel);

/*! \brief Wait on an event while displaying the busy dialog.
Throws up the busy dialog after the given time.
Expand Down
2 changes: 1 addition & 1 deletion xbmc/dialogs/GUIDialogSimpleMenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ bool CGUIDialogSimpleMenu::GetDirectoryItems(const std::string &path, CFileItemL
const XFILE::CDirectory::CHints &hints)
{
CGetDirectoryItems getItems(path, items, hints);
if (!CGUIDialogBusy::Wait(&getItems))
if (!CGUIDialogBusy::Wait(&getItems, 100, true))
{
return false;
}
Expand Down
22 changes: 20 additions & 2 deletions xbmc/filesystem/Directory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,18 @@ bool CDirectory::GetDirectory(const std::string& strPath, CFileItemList &items,
CHints hints;
hints.flags = flags;
hints.mask = strMask;
return GetDirectory(strPath, items, hints);
const CURL pathToUrl(strPath);
return GetDirectory(pathToUrl, items, hints);
}

bool CDirectory::GetDirectory(const std::string& strPath, std::shared_ptr<IDirectory> pDirectory,
CFileItemList &items, const std::string &strMask, int flags)
{
CHints hints;
hints.flags = flags;
hints.mask = strMask;
const CURL pathToUrl(strPath);
return GetDirectory(pathToUrl, pDirectory, items, hints);
}

bool CDirectory::GetDirectory(const std::string& strPath, CFileItemList &items, const CHints &hints)
Expand All @@ -149,11 +160,18 @@ bool CDirectory::GetDirectory(const CURL& url, CFileItemList &items, const std::
}

bool CDirectory::GetDirectory(const CURL& url, CFileItemList &items, const CHints &hints)
{
CURL realURL = URIUtils::SubstitutePath(url);
std::shared_ptr<IDirectory> pDirectory(CDirectoryFactory::Create(realURL));
return CDirectory::GetDirectory(url, pDirectory, items, hints);
}

bool CDirectory::GetDirectory(const CURL& url, std::shared_ptr<IDirectory> pDirectory,
CFileItemList &items, const CHints &hints)
{
try
{
CURL realURL = URIUtils::SubstitutePath(url);
std::shared_ptr<IDirectory> pDirectory(CDirectoryFactory::Create(realURL));
if (!pDirectory.get())
return false;

Expand Down
12 changes: 12 additions & 0 deletions xbmc/filesystem/Directory.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
*/

#include "IDirectory.h"
#include <memory>
#include <string>

namespace XFILE
Expand Down Expand Up @@ -49,6 +50,11 @@ class CDirectory
, const std::string &strMask
, int flags);

static bool GetDirectory(const CURL& url,
std::shared_ptr<IDirectory> pDirectory,
CFileItemList &items,
const CHints &hints);

static bool GetDirectory(const CURL& url
, CFileItemList &items
, const CHints &hints);
Expand All @@ -63,6 +69,12 @@ class CDirectory
, const std::string &strMask
, int flags);

static bool GetDirectory(const std::string& strPath,
std::shared_ptr<IDirectory> pDirectory,
CFileItemList &items,
const std::string &strMask,
int flags);

static bool GetDirectory(const std::string& strPath
, CFileItemList &items
, const CHints &hints);
Expand Down
1 change: 1 addition & 0 deletions xbmc/filesystem/PluginDirectory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,7 @@ void CPluginDirectory::SetProperty(int handle, const std::string &strProperty, c
void CPluginDirectory::CancelDirectory()
{
m_cancelled = true;
m_fetchComplete.Set();
}

float CPluginDirectory::GetProgress() const
Expand Down
13 changes: 12 additions & 1 deletion xbmc/filesystem/VirtualDirectory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/

#include "VirtualDirectory.h"
#include "DirectoryFactory.h"
#include "URL.h"
#include "Util.h"
#include "utils/URIUtils.h"
Expand Down Expand Up @@ -72,7 +73,11 @@ bool CVirtualDirectory::GetDirectory(const CURL& url, CFileItemList &items, bool
flags |= DIR_FLAG_NO_FILE_DIRS;
if (!strPath.empty() && strPath != "files://")
{
return CDirectory::GetDirectory(strPath, items, m_strFileMask, flags);
CURL realURL = URIUtils::SubstitutePath(url);
m_pDir.reset(CDirectoryFactory::Create(realURL));
bool ret = CDirectory::GetDirectory(strPath, m_pDir, items, m_strFileMask, flags);
m_pDir.reset();
return ret;
}

// if strPath is blank, clear the list (to avoid parent items showing up)
Expand All @@ -89,6 +94,12 @@ bool CVirtualDirectory::GetDirectory(const CURL& url, CFileItemList &items, bool
return dir.GetDirectory(shares, items);
}

void CVirtualDirectory::CancelDirectory()
{
if (m_pDir)
m_pDir->CancelDirectory();
}

/*!
\brief Is the share \e strPath in the virtual directory.
\param strPath Share to test
Expand Down
4 changes: 3 additions & 1 deletion xbmc/filesystem/VirtualDirectory.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ namespace XFILE
CVirtualDirectory(void);
~CVirtualDirectory(void) override;
bool GetDirectory(const CURL& url, CFileItemList &items) override;
void CancelDirectory() override;
bool GetDirectory(const CURL& url, CFileItemList &items, bool bUseFileDirectories);
void SetSources(const VECSOURCES& vecSources);
inline unsigned int GetNumberOfSources()
Expand Down Expand Up @@ -63,6 +64,7 @@ namespace XFILE
void CacheThumbs(CFileItemList &items);

VECSOURCES m_vecSources;
bool m_allowNonLocalSources;
bool m_allowNonLocalSources;
std::shared_ptr<IDirectory> m_pDir;
};
}
1 change: 1 addition & 0 deletions xbmc/threads/Thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class IRunnable
{
public:
virtual void Run()=0;
virtual void Cancel() {};
virtual ~IRunnable() = default;
};

Expand Down
8 changes: 6 additions & 2 deletions xbmc/windows/GUIMediaWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include "dialogs/GUIDialogSmartPlaylistEditor.h"
#include "favourites/FavouritesService.h"
#include "filesystem/File.h"
#include "filesystem/DirectoryFactory.h"
#include "filesystem/FileDirectoryFactory.h"
#include "filesystem/MultiPathDirectory.h"
#include "filesystem/PluginDirectory.h"
Expand Down Expand Up @@ -100,6 +101,10 @@ class CGetDirectoryItems : public IRunnable
{
m_result = m_dir.GetDirectory(m_url, m_items, m_useDir);
}
void Cancel() override
{
m_dir.CancelDirectory();
}
bool m_result;
protected:
XFILE::CVirtualDirectory &m_dir;
Expand Down Expand Up @@ -2162,9 +2167,8 @@ bool CGUIMediaWindow::GetDirectoryItems(CURL &url, CFileItemList &items, bool us
if (m_useBusyDialog)
{
CGetDirectoryItems getItems(m_rootDir, url, items, useDir);
if (!CGUIDialogBusy::Wait(&getItems))
if (!CGUIDialogBusy::Wait(&getItems, 100, true))
{
m_rootDir.CancelDirectory();
return false;
}
return getItems.m_result;
Expand Down
7 changes: 5 additions & 2 deletions xbmc/windows/GUIWindowFileManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ class CGetDirectoryItems : public IRunnable
{
m_result = m_dir.GetDirectory(m_url, m_items, false);
}
void Cancel() override
{
m_dir.CancelDirectory();
}
bool m_result;
protected:
XFILE::CVirtualDirectory &m_dir;
Expand Down Expand Up @@ -924,9 +928,8 @@ bool CGUIWindowFileManager::GetDirectory(int iList, const std::string &strDirect
CURL pathToUrl(strDirectory);

CGetDirectoryItems getItems(m_rootDir, pathToUrl, items);
if (!CGUIDialogBusy::Wait(&getItems))
if (!CGUIDialogBusy::Wait(&getItems, 100, true))
{
m_rootDir.CancelDirectory();
return false;
}
return getItems.m_result;
Expand Down

0 comments on commit cc8364a

Please sign in to comment.