1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
|
/*
* Copyright (C) 2005-2010 Team XBMC
* http://www.xbmc.org
*
* This Program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This Program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with XBMC; see the file COPYING. If not, write to
* the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
* http://www.gnu.org/copyleft/gpl.html
*
*/
#include "GUIWindowAddonBrowser.h"
#include "addons/AddonManager.h"
#include "addons/Repository.h"
#include "dialogs/GUIDialogContextMenu.h"
#include "GUIDialogAddonInfo.h"
#include "GUIDialogAddonSettings.h"
#include "dialogs/GUIDialogKeyboard.h"
#include "dialogs/GUIDialogYesNo.h"
#include "dialogs/GUIDialogSelect.h"
#include "dialogs/GUIDialogFileBrowser.h"
#include "GUIUserMessages.h"
#include "guilib/GUIWindowManager.h"
#include "utils/URIUtils.h"
#include "URL.h"
#include "FileItem.h"
#include "filesystem/File.h"
#include "filesystem/Directory.h"
#include "filesystem/AddonsDirectory.h"
#include "addons/AddonInstaller.h"
#include "utils/JobManager.h"
#include "utils/log.h"
#include "threads/SingleLock.h"
#include "settings/Settings.h"
#include "utils/StringUtils.h"
#include "Application.h"
#include "AddonDatabase.h"
#include "settings/AdvancedSettings.h"
#include "storage/MediaManager.h"
#include "settings/GUISettings.h"
#include "LangInfo.h"
#define CONTROL_AUTOUPDATE 5
#define CONTROL_SHUTUP 6
#define CONTROL_FOREIGNFILTER 7
using namespace ADDON;
using namespace XFILE;
using namespace std;
CGUIWindowAddonBrowser::CGUIWindowAddonBrowser(void)
: CGUIMediaWindow(WINDOW_ADDON_BROWSER, "AddonBrowser.xml")
{
m_thumbLoader.SetNumOfWorkers(1);
}
CGUIWindowAddonBrowser::~CGUIWindowAddonBrowser()
{
}
bool CGUIWindowAddonBrowser::OnMessage(CGUIMessage& message)
{
switch ( message.GetMessage() )
{
case GUI_MSG_WINDOW_DEINIT:
{
if (m_thumbLoader.IsLoading())
m_thumbLoader.StopThread();
}
break;
case GUI_MSG_WINDOW_INIT:
{
m_rootDir.AllowNonLocalSources(false);
// is this the first time the window is opened?
if (m_vecItems->GetPath() == "?" && message.GetStringParam().IsEmpty())
m_vecItems->SetPath("");
}
break;
case GUI_MSG_CLICKED:
{
int iControl = message.GetSenderId();
if (iControl == CONTROL_AUTOUPDATE)
{
g_settings.m_bAddonAutoUpdate = !g_settings.m_bAddonAutoUpdate;
g_settings.Save();
return true;
}
else if (iControl == CONTROL_SHUTUP)
{
g_settings.m_bAddonNotifications = !g_settings.m_bAddonNotifications;
g_settings.Save();
return true;
}
else if (iControl == CONTROL_FOREIGNFILTER)
{
g_settings.m_bAddonForeignFilter = !g_settings.m_bAddonForeignFilter;
g_settings.Save();
Update(m_vecItems->GetPath());
return true;
}
else if (m_viewControl.HasControl(iControl)) // list/thumb control
{
// get selected item
int iItem = m_viewControl.GetSelectedItem();
int iAction = message.GetParam1();
// iItem is checked for validity inside these routines
if (iAction == ACTION_SHOW_INFO)
{
if (!m_vecItems->Get(iItem)->GetProperty("Addon.ID").empty())
return CGUIDialogAddonInfo::ShowForItem((*m_vecItems)[iItem]);
return false;
}
}
}
break;
case GUI_MSG_NOTIFY_ALL:
{
if (message.GetParam1() == GUI_MSG_UPDATE_ITEM && IsActive() && message.GetNumStringParams() == 1)
{ // update this item
for (int i = 0; i < m_vecItems->Size(); ++i)
{
CFileItemPtr item = m_vecItems->Get(i);
if (item->GetProperty("Addon.ID") == message.GetStringParam())
{
SetItemLabel2(item);
return true;
}
}
}
}
break;
default:
break;
}
return CGUIMediaWindow::OnMessage(message);
}
void CGUIWindowAddonBrowser::GetContextButtons(int itemNumber,
CContextButtons& buttons)
{
CFileItemPtr pItem = m_vecItems->Get(itemNumber);
if (pItem->GetPath().Equals("addons://enabled/"))
buttons.Add(CONTEXT_BUTTON_SCAN,24034);
AddonPtr addon;
if (!CAddonMgr::Get().GetAddon(pItem->GetProperty("Addon.ID").asString(), addon, ADDON_UNKNOWN, false)) // allow disabled addons
return;
if (addon->Type() == ADDON_REPOSITORY && pItem->m_bIsFolder)
{
buttons.Add(CONTEXT_BUTTON_SCAN,24034);
buttons.Add(CONTEXT_BUTTON_UPDATE_LIBRARY,24035);
}
buttons.Add(CONTEXT_BUTTON_INFO,24003);
if (addon->HasSettings())
buttons.Add(CONTEXT_BUTTON_SETTINGS,24020);
}
bool CGUIWindowAddonBrowser::OnContextButton(int itemNumber,
CONTEXT_BUTTON button)
{
CFileItemPtr pItem = m_vecItems->Get(itemNumber);
if (pItem->GetPath().Equals("addons://enabled/"))
{
if (button == CONTEXT_BUTTON_SCAN)
{
CAddonMgr::Get().FindAddons();
return true;
}
}
AddonPtr addon;
if (!CAddonMgr::Get().GetAddon(pItem->GetProperty("Addon.ID").asString(), addon, ADDON_UNKNOWN, false)) // allow disabled addons
return false;
if (button == CONTEXT_BUTTON_SETTINGS)
return CGUIDialogAddonSettings::ShowAndGetInput(addon);
if (button == CONTEXT_BUTTON_UPDATE_LIBRARY)
{
CAddonDatabase database;
database.Open();
database.DeleteRepository(addon->ID());
button = CONTEXT_BUTTON_SCAN;
}
if (button == CONTEXT_BUTTON_SCAN)
{
RepositoryPtr repo = boost::dynamic_pointer_cast<CRepository>(addon);
CAddonInstaller::Get().UpdateRepos(true);
return true;
}
if (button == CONTEXT_BUTTON_INFO)
{
CGUIDialogAddonInfo::ShowForItem(pItem);
return true;
}
return CGUIMediaWindow::OnContextButton(itemNumber, button);
}
bool CGUIWindowAddonBrowser::OnClick(int iItem)
{
CFileItemPtr item = m_vecItems->Get(iItem);
if (item->GetPath() == "addons://install/")
{
// pop up filebrowser to grab an installed folder
VECSOURCES shares = g_settings.m_fileSources;
g_mediaManager.GetLocalDrives(shares);
g_mediaManager.GetNetworkLocations(shares);
CStdString path;
if (CGUIDialogFileBrowser::ShowAndGetFile(shares, "*.zip", g_localizeStrings.Get(24041), path))
CAddonInstaller::Get().InstallFromZip(path);
return true;
}
if (!item->m_bIsFolder)
{
// cancel a downloading job
if (item->HasProperty("Addon.Downloading"))
{
if (CGUIDialogYesNo::ShowAndGetInput(g_localizeStrings.Get(24000),
item->GetProperty("Addon.Name").asString(),
g_localizeStrings.Get(24066),""))
{
if (CAddonInstaller::Get().Cancel(item->GetProperty("Addon.ID").asString()))
Update(m_vecItems->GetPath());
}
return true;
}
CGUIDialogAddonInfo::ShowForItem(item);
return true;
}
if (item->GetPath().Equals("addons://search/"))
return Update(item->GetPath());
return CGUIMediaWindow::OnClick(iItem);
}
void CGUIWindowAddonBrowser::UpdateButtons()
{
SET_CONTROL_SELECTED(GetID(),CONTROL_AUTOUPDATE,g_settings.m_bAddonAutoUpdate);
SET_CONTROL_SELECTED(GetID(),CONTROL_SHUTUP,g_settings.m_bAddonNotifications);
SET_CONTROL_SELECTED(GetID(),CONTROL_FOREIGNFILTER,g_settings.m_bAddonForeignFilter);
CGUIMediaWindow::UpdateButtons();
}
static bool FilterVar(bool valid, const CVariant& variant,
const std::string& check)
{
if (!valid)
return false;
if (variant.isNull() || variant.asString().empty())
return false;
std::string regions = variant.asString();
return regions.find(check) == std::string::npos;
}
bool CGUIWindowAddonBrowser::GetDirectory(const CStdString& strDirectory,
CFileItemList& items)
{
bool result;
if (strDirectory.Equals("addons://downloading/"))
{
VECADDONS addons;
CAddonInstaller::Get().GetInstallList(addons);
CURL url(strDirectory);
CAddonsDirectory::GenerateListing(url,addons,items);
result = true;
items.SetProperty("reponame",g_localizeStrings.Get(24067));
items.SetPath(strDirectory);
if (m_guiState.get() && !m_guiState->HideParentDirItems())
{
CFileItemPtr pItem(new CFileItem(".."));
pItem->SetPath(m_history.GetParentPath());
pItem->m_bIsFolder = true;
pItem->m_bIsShareOrDrive = false;
items.AddFront(pItem, 0);
}
}
else
{
result = CGUIMediaWindow::GetDirectory(strDirectory,items);
if (g_settings.m_bAddonForeignFilter)
{
int i=0;
while (i < items.Size())
{
if (!FilterVar(g_settings.m_bAddonForeignFilter,
items[i]->GetProperty("Addon.Language"), "en") ||
!FilterVar(g_settings.m_bAddonForeignFilter,
items[i]->GetProperty("Addon.Language"),
g_langInfo.GetLanguageLocale()))
{
i++;
}
else
items.Remove(i);
}
}
}
if (strDirectory.IsEmpty() && CAddonInstaller::Get().IsDownloading())
{
CFileItemPtr item(new CFileItem("addons://downloading/",true));
item->SetLabel(g_localizeStrings.Get(24067));
item->SetLabelPreformated(true);
item->SetIconImage("DefaultNetwork.png");
items.Add(item);
}
items.SetContent("addons");
for (int i=0;i<items.Size();++i)
SetItemLabel2(items[i]);
return result;
}
void CGUIWindowAddonBrowser::SetItemLabel2(CFileItemPtr item)
{
if (!item || item->m_bIsFolder) return;
unsigned int percent;
if (CAddonInstaller::Get().GetProgress(item->GetProperty("Addon.ID").asString(), percent))
{
CStdString progress;
progress.Format(g_localizeStrings.Get(24042).c_str(), percent);
item->SetProperty("Addon.Status", progress);
item->SetProperty("Addon.Downloading", true);
}
else
item->ClearProperty("Addon.Downloading");
item->SetLabel2(item->GetProperty("Addon.Status").asString());
// to avoid the view state overriding label 2
item->SetLabelPreformated(true);
}
bool CGUIWindowAddonBrowser::Update(const CStdString &strDirectory)
{
if (m_thumbLoader.IsLoading())
m_thumbLoader.StopThread();
if (!CGUIMediaWindow::Update(strDirectory))
return false;
m_thumbLoader.Load(*m_vecItems);
return true;
}
int CGUIWindowAddonBrowser::SelectAddonID(TYPE type, CStdString &addonID, bool showNone /*= false*/)
{
vector<ADDON::TYPE> types;
types.push_back(type);
return SelectAddonID(types, addonID, showNone);
}
int CGUIWindowAddonBrowser::SelectAddonID(ADDON::TYPE type, CStdStringArray &addonIDs, bool showNone /*= false*/, bool multipleSelection /*= true*/)
{
vector<ADDON::TYPE> types;
types.push_back(type);
return SelectAddonID(types, addonIDs, showNone, multipleSelection);
}
int CGUIWindowAddonBrowser::SelectAddonID(const vector<ADDON::TYPE> &types, CStdString &addonID, bool showNone /*= false*/)
{
CStdStringArray addonIDs;
if (!addonID.IsEmpty())
addonIDs.push_back(addonID);
int retval = SelectAddonID(types, addonIDs, showNone, false);
if (addonIDs.size() > 0)
addonID = addonIDs.at(0);
else
addonID = "";
return retval;
}
int CGUIWindowAddonBrowser::SelectAddonID(const vector<ADDON::TYPE> &types, CStdStringArray &addonIDs, bool showNone /*= false*/, bool multipleSelection /*= true*/)
{
CGUIDialogSelect *dialog = (CGUIDialogSelect*)g_windowManager.GetWindow(WINDOW_DIALOG_SELECT);
if (!dialog)
return 0;
CFileItemList items;
CStdString heading;
int iTypes = 0;
for (vector<ADDON::TYPE>::const_iterator it = types.begin(); it != types.end(); ++it)
{
if (*it == ADDON_UNKNOWN)
continue;
ADDON::VECADDONS addons;
iTypes++;
if (*it == ADDON_AUDIO)
CAddonsDirectory::GetScriptsAndPlugins("audio",addons);
else if (*it == ADDON_EXECUTABLE)
CAddonsDirectory::GetScriptsAndPlugins("executable",addons);
else if (*it == ADDON_IMAGE)
CAddonsDirectory::GetScriptsAndPlugins("image",addons);
else if (*it == ADDON_VIDEO)
CAddonsDirectory::GetScriptsAndPlugins("video",addons);
else
CAddonMgr::Get().GetAddons(*it, addons);
for (ADDON::IVECADDONS it2 = addons.begin() ; it2 != addons.end() ; ++it2)
{
CFileItemPtr item(CAddonsDirectory::FileItemFromAddon(*it2, ""));
if (!items.Contains(item->GetPath()))
items.Add(item);
}
if (!heading.IsEmpty())
heading += ", ";
heading += TranslateType(*it, true);
}
if (iTypes == 0)
return 0;
dialog->SetHeading(heading);
dialog->Reset();
dialog->SetUseDetails(true);
if (multipleSelection)
showNone = false;
if (multipleSelection || iTypes > 1)
dialog->EnableButton(true, 186);
else
dialog->EnableButton(true, 21452);
if (showNone)
{
CFileItemPtr item(new CFileItem("", false));
item->SetLabel(g_localizeStrings.Get(231));
item->SetLabel2(g_localizeStrings.Get(24040));
item->SetIconImage("DefaultAddonNone.png");
item->SetSpecialSort(SortSpecialOnTop);
items.Add(item);
}
items.Sort(SORT_METHOD_LABEL, SortOrderAscending);
if (addonIDs.size() > 0)
{
for (CStdStringArray::const_iterator it = addonIDs.begin(); it != addonIDs.end() ; it++)
{
CFileItemPtr item = items.Get(*it);
if (item)
item->Select(true);
}
}
dialog->SetItems(&items);
dialog->SetMultiSelection(multipleSelection);
dialog->DoModal();
if (!multipleSelection && iTypes == 1 && dialog->IsButtonPressed())
{ // switch to the addons browser.
vector<CStdString> params;
params.push_back("addons://all/"+TranslateType(types[0],false)+"/");
params.push_back("return");
g_windowManager.ActivateWindow(WINDOW_ADDON_BROWSER, params);
return 2;
}
if (!multipleSelection && dialog->GetSelectedLabel() == -1)
return 0;
addonIDs.clear();
const CFileItemList& list = dialog->GetSelectedItems();
for (int i = 0 ; i < list.Size() ; i++)
addonIDs.push_back(list.Get(i)->GetPath());
return 1;
}
CStdString CGUIWindowAddonBrowser::GetStartFolder(const CStdString &dir)
{
if (dir.Left(9).Equals("addons://"))
return dir;
return CGUIMediaWindow::GetStartFolder(dir);
}
|