Fix keymap.
[vuplus_xbmc] / xbmc / programs / GUIWindowPrograms.cpp
1 /*
2  *      Copyright (C) 2005-2013 Team XBMC
3  *      http://xbmc.org
4  *
5  *  This Program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2, or (at your option)
8  *  any later version.
9  *
10  *  This Program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with XBMC; see the file COPYING.  If not, see
17  *  <http://www.gnu.org/licenses/>.
18  *
19  */
20
21 #include "system.h"
22 #include "GUIWindowPrograms.h"
23 #include "Util.h"
24 #include "addons/GUIDialogAddonInfo.h"
25 #include "Autorun.h"
26 #include "guilib/GUIWindowManager.h"
27 #include "FileItem.h"
28 #include "settings/MediaSourceSettings.h"
29 #include "guilib/Key.h"
30 #include "guilib/LocalizeStrings.h"
31 #include "utils/log.h"
32 #include "utils/StringUtils.h"
33
34 #define CONTROL_BTNVIEWASICONS 2
35 #define CONTROL_BTNSORTBY      3
36 #define CONTROL_BTNSORTASC     4
37 #define CONTROL_LABELFILES    12
38
39 CGUIWindowPrograms::CGUIWindowPrograms(void)
40     : CGUIMediaWindow(WINDOW_PROGRAMS, "MyPrograms.xml")
41 {
42   m_thumbLoader.SetObserver(this);
43   m_dlgProgress = NULL;
44   m_rootDir.AllowNonLocalSources(false); // no nonlocal shares for this window please
45 }
46
47
48 CGUIWindowPrograms::~CGUIWindowPrograms(void)
49 {
50 }
51
52 bool CGUIWindowPrograms::OnMessage(CGUIMessage& message)
53 {
54   switch ( message.GetMessage() )
55   {
56   case GUI_MSG_WINDOW_DEINIT:
57     {
58       if (m_thumbLoader.IsLoading())
59         m_thumbLoader.StopThread();
60     }
61     break;
62
63   case GUI_MSG_WINDOW_INIT:
64     {
65       m_dlgProgress = (CGUIDialogProgress*)g_windowManager.GetWindow(WINDOW_DIALOG_PROGRESS);
66
67       // is this the first time accessing this window?
68       if (m_vecItems->GetPath() == "?" && message.GetStringParam().empty())
69         message.SetStringParam(CMediaSourceSettings::Get().GetDefaultSource("programs"));
70
71       return CGUIMediaWindow::OnMessage(message);
72     }
73   break;
74
75   case GUI_MSG_CLICKED:
76     {
77       if (m_viewControl.HasControl(message.GetSenderId()))  // list/thumb control
78       {
79         int iAction = message.GetParam1();
80         int iItem = m_viewControl.GetSelectedItem();
81         if (iAction == ACTION_PLAYER_PLAY)
82         {
83           OnPlayMedia(iItem);
84           return true;
85         }
86         else if (iAction == ACTION_SHOW_INFO)
87         {
88           OnInfo(iItem);
89           return true;
90         }
91       }
92     }
93     break;
94   }
95
96   return CGUIMediaWindow::OnMessage(message);
97 }
98
99 void CGUIWindowPrograms::GetContextButtons(int itemNumber, CContextButtons &buttons)
100 {
101   if (itemNumber < 0 || itemNumber >= m_vecItems->Size())
102     return;
103   CFileItemPtr item = m_vecItems->Get(itemNumber);
104   if (item && !item->GetProperty("pluginreplacecontextitems").asBoolean())
105   {
106     if ( m_vecItems->IsVirtualDirectoryRoot() || m_vecItems->GetPath() == "sources://programs/" )
107     {
108       CGUIDialogContextMenu::GetContextButtons("programs", item, buttons);
109     }
110     else
111     {
112       if (!m_vecItems->IsPlugin() && (item->IsPlugin() || item->IsScript()))
113         buttons.Add(CONTEXT_BUTTON_INFO, 24003); // Add-on info
114       if (item->IsPlugin() || item->IsScript() || m_vecItems->IsPlugin())
115         buttons.Add(CONTEXT_BUTTON_PLUGIN_SETTINGS, 1045);
116
117       buttons.Add(CONTEXT_BUTTON_GOTO_ROOT, 20128); // Go to Root
118     }
119   }
120   CGUIMediaWindow::GetContextButtons(itemNumber, buttons);
121 }
122
123 bool CGUIWindowPrograms::OnContextButton(int itemNumber, CONTEXT_BUTTON button)
124 {
125   CFileItemPtr item = (itemNumber >= 0 && itemNumber < m_vecItems->Size()) ? m_vecItems->Get(itemNumber) : CFileItemPtr();
126
127   if (CGUIDialogContextMenu::OnContextButton("programs", item, button))
128   {
129     Update("");
130     return true;
131   }
132   switch (button)
133   {
134   case CONTEXT_BUTTON_GOTO_ROOT:
135     Update("");
136     return true;
137
138   case CONTEXT_BUTTON_INFO:
139     OnInfo(itemNumber);
140     return true;
141
142   default:
143     break;
144   }
145   return CGUIMediaWindow::OnContextButton(itemNumber, button);
146 }
147
148 bool CGUIWindowPrograms::Update(const CStdString &strDirectory, bool updateFilterPath /* = true */)
149 {
150   if (m_thumbLoader.IsLoading())
151     m_thumbLoader.StopThread();
152
153   if (!CGUIMediaWindow::Update(strDirectory, updateFilterPath))
154     return false;
155
156   m_thumbLoader.Load(*m_vecItems);
157   return true;
158 }
159
160 bool CGUIWindowPrograms::OnPlayMedia(int iItem)
161 {
162   if ( iItem < 0 || iItem >= (int)m_vecItems->Size() ) return false;
163   CFileItemPtr pItem = m_vecItems->Get(iItem);
164
165 #ifdef HAS_DVD_DRIVE
166   if (pItem->IsDVD())
167     return MEDIA_DETECT::CAutorun::PlayDiscAskResume(m_vecItems->Get(iItem)->GetPath());
168 #endif
169
170   if (pItem->m_bIsFolder) return false;
171
172   return false;
173 }
174
175 bool CGUIWindowPrograms::GetDirectory(const CStdString &strDirectory, CFileItemList &items)
176 {
177   if (!CGUIMediaWindow::GetDirectory(strDirectory, items))
178     return false;
179
180   // don't allow the view state to change these
181   if (StringUtils::StartsWithNoCase(strDirectory, "addons://"))
182   {
183     for (int i=0;i<items.Size();++i)
184     {
185       items[i]->SetLabel2(items[i]->GetProperty("Addon.Version").asString());
186       items[i]->SetLabelPreformated(true);
187     }
188   }
189
190   return true;
191 }
192
193 CStdString CGUIWindowPrograms::GetStartFolder(const CStdString &dir)
194 {
195   if (dir.Equals("Plugins") || dir.Equals("Addons"))
196     return "addons://sources/executable/";
197     
198   SetupShares();
199   VECSOURCES shares;
200   m_rootDir.GetSources(shares);
201   bool bIsSourceName = false;
202   int iIndex = CUtil::GetMatchingSource(dir, shares, bIsSourceName);
203   if (iIndex > -1)
204   {
205     if (iIndex < (int)shares.size() && shares[iIndex].m_iHasLock == 2)
206     {
207       CFileItem item(shares[iIndex]);
208       if (!g_passwordManager.IsItemUnlocked(&item,"programs"))
209         return "";
210     }
211     if (bIsSourceName)
212       return shares[iIndex].strPath;
213     return dir;
214   }
215   return CGUIMediaWindow::GetStartFolder(dir);
216 }
217
218 void CGUIWindowPrograms::OnInfo(int iItem)
219 {
220   if (iItem < 0 || iItem >= m_vecItems->Size())
221     return;
222
223   CFileItemPtr item = m_vecItems->Get(iItem);
224   if (!m_vecItems->IsPlugin() && (item->IsPlugin() || item->IsScript()))
225   {
226     CGUIDialogAddonInfo::ShowForItem(item);
227   }
228 }