1 /*
2  *  Copyright (C) 2005-2018 Team Kodi
3  *  This file is part of Kodi - https://kodi.tv
4  *
5  *  SPDX-License-Identifier: GPL-2.0-or-later
6  *  See LICENSES/README.md for more information.
7  */
8 
9 #include "ModuleXbmcplugin.h"
10 
11 #include "FileItem.h"
12 #include "filesystem/PluginDirectory.h"
13 
14 namespace XBMCAddon
15 {
16 
17   namespace xbmcplugin
18   {
addDirectoryItem(int handle,const String & url,const xbmcgui::ListItem * listItem,bool isFolder,int totalItems)19     bool addDirectoryItem(int handle, const String& url, const xbmcgui::ListItem* listItem,
20                           bool isFolder, int totalItems)
21     {
22       if (listItem == nullptr)
23         throw new XBMCAddon::WrongTypeException("None not allowed as argument for listitem");
24       AddonClass::Ref<xbmcgui::ListItem> pListItem(listItem);
25       pListItem->item->SetPath(url);
26       pListItem->item->m_bIsFolder = isFolder;
27 
28       // call the directory class to add our item
29       return XFILE::CPluginDirectory::AddItem(handle, pListItem->item.get(), totalItems);
30     }
31 
addDirectoryItems(int handle,const std::vector<Tuple<String,const XBMCAddon::xbmcgui::ListItem *,bool>> & items,int totalItems)32     bool addDirectoryItems(int handle,
33                            const std::vector<Tuple<String,const XBMCAddon::xbmcgui::ListItem*,bool> >& items,
34                            int totalItems)
35     {
36       CFileItemList fitems;
37       for (const auto& item : items)
38       {
39         const String& url = item.first();
40         const XBMCAddon::xbmcgui::ListItem* pListItem = item.second();
41         bool bIsFolder = item.GetNumValuesSet() > 2 ? item.third() : false;
42         pListItem->item->SetPath(url);
43         pListItem->item->m_bIsFolder = bIsFolder;
44         fitems.Add(pListItem->item);
45       }
46 
47       // call the directory class to add our items
48       return XFILE::CPluginDirectory::AddItems(handle, &fitems, totalItems);
49     }
50 
endOfDirectory(int handle,bool succeeded,bool updateListing,bool cacheToDisc)51     void endOfDirectory(int handle, bool succeeded, bool updateListing,
52                         bool cacheToDisc)
53     {
54       // tell the directory class that we're done
55       XFILE::CPluginDirectory::EndOfDirectory(handle, succeeded, updateListing, cacheToDisc);
56     }
57 
setResolvedUrl(int handle,bool succeeded,const xbmcgui::ListItem * listItem)58     void setResolvedUrl(int handle, bool succeeded, const xbmcgui::ListItem* listItem)
59     {
60       if (listItem == nullptr)
61         throw new XBMCAddon::WrongTypeException("None not allowed as argument for listitem");
62       AddonClass::Ref<xbmcgui::ListItem> pListItem(listItem);
63       XFILE::CPluginDirectory::SetResolvedUrl(handle, succeeded, pListItem->item.get());
64     }
65 
addSortMethod(int handle,int sortMethod,const String & clabelMask,const String & clabel2Mask)66     void addSortMethod(int handle, int sortMethod, const String& clabelMask, const String& clabel2Mask)
67     {
68       String labelMask;
69       if (sortMethod == SORT_METHOD_TRACKNUM)
70         labelMask = (clabelMask.empty() ? "[%N. ]%T" : clabelMask.c_str());
71       else if (sortMethod == SORT_METHOD_EPISODE || sortMethod == SORT_METHOD_PRODUCTIONCODE)
72         labelMask = (clabelMask.empty() ? "%H. %T" : clabelMask.c_str());
73       else
74         labelMask = (clabelMask.empty() ? "%T" : clabelMask.c_str());
75 
76       String label2Mask;
77       label2Mask = (clabel2Mask.empty() ? "%D" : clabel2Mask.c_str());
78 
79       // call the directory class to add the sort method.
80       if (sortMethod >= SORT_METHOD_NONE && sortMethod < SORT_METHOD_MAX)
81         XFILE::CPluginDirectory::AddSortMethod(handle, (SORT_METHOD)sortMethod, labelMask, label2Mask);
82     }
83 
getSetting(int handle,const char * id)84     String getSetting(int handle, const char* id)
85     {
86       return XFILE::CPluginDirectory::GetSetting(handle, id);
87     }
88 
setSetting(int handle,const String & id,const String & value)89     void setSetting(int handle, const String& id, const String& value)
90     {
91       XFILE::CPluginDirectory::SetSetting(handle, id, value);
92     }
93 
setContent(int handle,const char * content)94     void setContent(int handle, const char* content)
95     {
96       XFILE::CPluginDirectory::SetContent(handle, content);
97     }
98 
setPluginCategory(int handle,const String & category)99     void setPluginCategory(int handle, const String& category)
100     {
101       XFILE::CPluginDirectory::SetProperty(handle, "plugincategory", category);
102     }
103 
setPluginFanart(int handle,const char * image,const char * color1,const char * color2,const char * color3)104     void setPluginFanart(int handle, const char* image,
105                          const char* color1,
106                          const char* color2,
107                          const char* color3)
108     {
109       if (image)
110         XFILE::CPluginDirectory::SetProperty(handle, "fanart_image", image);
111       if (color1)
112         XFILE::CPluginDirectory::SetProperty(handle, "fanart_color1", color1);
113       if (color2)
114         XFILE::CPluginDirectory::SetProperty(handle, "fanart_color2", color2);
115       if (color3)
116         XFILE::CPluginDirectory::SetProperty(handle, "fanart_color3", color3);
117     }
118 
setProperty(int handle,const char * key,const String & value)119     void setProperty(int handle, const char* key, const String& value)
120     {
121       XFILE::CPluginDirectory::SetProperty(handle, key, value);
122     }
123 
124   }
125 }
126