Skip to content
This repository
Browse code

use a helper function to retrieve the CPluginDirectory instance from …

…the handle, thanks to ulion
  • Loading branch information...
commit 2c1be2ce5a8bf58ef0d52ed2f8c3b9fe681e13b0 1 parent 88e6ffa
authored January 04, 2013
85  xbmc/filesystem/PluginDirectory.cpp
@@ -76,6 +76,15 @@ void CPluginDirectory::removeHandle(int handle)
76 76
     globalHandles.erase(globalHandles.begin() + handle);
77 77
 }
78 78
 
  79
+CPluginDirectory *CPluginDirectory::dirFromHandle(int handle)
  80
+{
  81
+  CSingleLock lock(m_handleLock);
  82
+  if (handle >= 0 && handle < (int)globalHandles.size())
  83
+    return globalHandles[handle];
  84
+  CLog::Log(LOGWARNING, "Attempt to use invalid handle %i", handle);
  85
+  return NULL;
  86
+}
  87
+
79 88
 bool CPluginDirectory::StartScript(const CStdString& strPath, bool retrievingDir)
80 89
 {
81 90
   CURL url(strPath);
@@ -161,13 +170,10 @@ bool CPluginDirectory::GetPluginResult(const CStdString& strPath, CFileItem &res
161 170
 bool CPluginDirectory::AddItem(int handle, const CFileItem *item, int totalItems)
162 171
 {
163 172
   CSingleLock lock(m_handleLock);
164  
-  if (handle < 0 || handle >= (int)globalHandles.size())
165  
-  {
166  
-    CLog::Log(LOGERROR, " %s - called with an invalid handle.", __FUNCTION__);
  173
+  CPluginDirectory *dir = dirFromHandle(handle);
  174
+  if (!dir)
167 175
     return false;
168  
-  }
169 176
 
170  
-  CPluginDirectory *dir = globalHandles[handle];
171 177
   CFileItemPtr pItem(new CFileItem(*item));
172 178
   dir->m_listItems->Add(pItem);
173 179
   dir->m_totalItems = totalItems;
@@ -178,13 +184,10 @@ bool CPluginDirectory::AddItem(int handle, const CFileItem *item, int totalItems
178 184
 bool CPluginDirectory::AddItems(int handle, const CFileItemList *items, int totalItems)
179 185
 {
180 186
   CSingleLock lock(m_handleLock);
181  
-  if (handle < 0 || handle >= (int)globalHandles.size())
182  
-  {
183  
-    CLog::Log(LOGERROR, " %s - called with an invalid handle.", __FUNCTION__);
  187
+  CPluginDirectory *dir = dirFromHandle(handle);
  188
+  if (!dir)
184 189
     return false;
185  
-  }
186 190
 
187  
-  CPluginDirectory *dir = globalHandles[handle];
188 191
   CFileItemList pItemList;
189 192
   pItemList.Copy(*items);
190 193
   dir->m_listItems->Append(pItemList);
@@ -196,12 +199,9 @@ bool CPluginDirectory::AddItems(int handle, const CFileItemList *items, int tota
196 199
 void CPluginDirectory::EndOfDirectory(int handle, bool success, bool replaceListing, bool cacheToDisc)
197 200
 {
198 201
   CSingleLock lock(m_handleLock);
199  
-  if (handle < 0 || handle >= (int)globalHandles.size())
200  
-  {
201  
-    CLog::Log(LOGERROR, " %s - called with an invalid handle.", __FUNCTION__);
  202
+  CPluginDirectory *dir = dirFromHandle(handle);
  203
+  if (!dir)
202 204
     return;
203  
-  }
204  
-  CPluginDirectory *dir = globalHandles[handle];
205 205
 
206 206
   // set cache to disc
207 207
   dir->m_listItems->SetCacheToDisc(cacheToDisc ? CFileItemList::CACHE_IF_SLOW : CFileItemList::CACHE_NEVER);
@@ -219,13 +219,9 @@ void CPluginDirectory::EndOfDirectory(int handle, bool success, bool replaceList
219 219
 void CPluginDirectory::AddSortMethod(int handle, SORT_METHOD sortMethod, const CStdString &label2Mask)
220 220
 {
221 221
   CSingleLock lock(m_handleLock);
222  
-  if (handle < 0 || handle >= (int)globalHandles.size())
223  
-  {
224  
-    CLog::Log(LOGERROR, "%s - called with an invalid handle.", __FUNCTION__);
  222
+  CPluginDirectory *dir = dirFromHandle(handle);
  223
+  if (!dir)
225 224
     return;
226  
-  }
227  
-
228  
-  CPluginDirectory *dir = globalHandles[handle];
229 225
 
230 226
   // TODO: Add all sort methods and fix which labels go on the right or left
231 227
   switch(sortMethod)
@@ -556,12 +552,9 @@ bool CPluginDirectory::WaitOnScriptResult(const CStdString &scriptPath, int scri
556 552
 void CPluginDirectory::SetResolvedUrl(int handle, bool success, const CFileItem *resultItem)
557 553
 {
558 554
   CSingleLock lock(m_handleLock);
559  
-  if (handle < 0 || handle >= (int)globalHandles.size())
560  
-  {
561  
-    CLog::Log(LOGERROR, " %s - called with an invalid handle.", __FUNCTION__);
  555
+  CPluginDirectory *dir = dirFromHandle(handle);
  556
+  if (!dir)
562 557
     return;
563  
-  }
564  
-  CPluginDirectory* dir  = globalHandles[handle];
565 558
 
566 559
   dir->m_success = success;
567 560
   *dir->m_fileResult = *resultItem;
@@ -572,14 +565,9 @@ void CPluginDirectory::SetResolvedUrl(int handle, bool success, const CFileItem
572 565
 
573 566
 CStdString CPluginDirectory::GetSetting(int handle, const CStdString &strID)
574 567
 {
575  
-  if (handle < 0 || handle >= (int)globalHandles.size())
576  
-  {
577  
-    CLog::Log(LOGERROR, "%s called with an invalid handle.", __FUNCTION__);
578  
-    return "";
579  
-  }
580  
-
581  
-  CPluginDirectory *dir = globalHandles[handle];
582  
-  if(dir->m_addon)
  568
+  CSingleLock lock(m_handleLock);
  569
+  CPluginDirectory *dir = dirFromHandle(handle);
  570
+  if(dir && dir->m_addon)
583 571
     return dir->m_addon->GetSetting(strID);
584 572
   else
585 573
     return "";
@@ -587,39 +575,26 @@ CStdString CPluginDirectory::GetSetting(int handle, const CStdString &strID)
587 575
 
588 576
 void CPluginDirectory::SetSetting(int handle, const CStdString &strID, const CStdString &value)
589 577
 {
590  
-  if (handle < 0 || handle >= (int)globalHandles.size())
591  
-  {
592  
-    CLog::Log(LOGERROR, "%s called with an invalid handle.", __FUNCTION__);
593  
-    return;
594  
-  }
595  
-
596  
-  CPluginDirectory *dir = globalHandles[handle];
  578
+  CSingleLock lock(m_handleLock);
  579
+  CPluginDirectory *dir = dirFromHandle(handle);
597 580
   if(dir->m_addon)
598 581
     dir->m_addon->UpdateSetting(strID, value);
599 582
 }
600 583
 
601 584
 void CPluginDirectory::SetContent(int handle, const CStdString &strContent)
602 585
 {
603  
-  if (handle < 0 || handle >= (int)globalHandles.size())
604  
-  {
605  
-    CLog::Log(LOGERROR, "%s called with an invalid handle.", __FUNCTION__);
606  
-    return;
607  
-  }
608  
-
609  
-  CPluginDirectory *dir = globalHandles[handle];
610  
-  dir->m_listItems->SetContent(strContent);
  586
+  CSingleLock lock(m_handleLock);
  587
+  CPluginDirectory *dir = dirFromHandle(handle);
  588
+  if (dir)
  589
+    dir->m_listItems->SetContent(strContent);
611 590
 }
612 591
 
613 592
 void CPluginDirectory::SetProperty(int handle, const CStdString &strProperty, const CStdString &strValue)
614 593
 {
615 594
   CSingleLock lock(m_handleLock);
616  
-  if (handle < 0 || handle >= (int)globalHandles.size())
617  
-  {
618  
-    CLog::Log(LOGERROR, "%s called with an invalid handle.", __FUNCTION__);
  595
+  CPluginDirectory *dir = dirFromHandle(handle);
  596
+  if (!dir)
619 597
     return;
620  
-  }
621  
-
622  
-  CPluginDirectory *dir = globalHandles[handle];
623 598
   if (strProperty == "fanart_image")
624 599
     dir->m_listItems->SetArt("fanart", strValue);
625 600
   else
1  xbmc/filesystem/PluginDirectory.h
@@ -71,6 +71,7 @@ class CPluginDirectory : public IDirectory
71 71
   static std::vector<CPluginDirectory*> globalHandles;
72 72
   static int getNewHandle(CPluginDirectory *cp);
73 73
   static void removeHandle(int handle);
  74
+  static CPluginDirectory *dirFromHandle(int handle);
74 75
   static CCriticalSection m_handleLock;
75 76
 
76 77
   CFileItemList* m_listItems;

0 notes on commit 2c1be2c

ulion

didn't check dir non-NULL here, although it's not likely to be NULL.

jmarshallnz

Thanks for picking it up - will fix.

Please sign in to comment.
Something went wrong with that request. Please try again.