aboutsummaryrefslogtreecommitdiff
path: root/xbmc/GUIWindowAddonBrowser.cpp
blob: 1fff140174363b5498218826ffb3b552ed66aa94 (plain)
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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
/*
 *      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 "GUIDialogContextMenu.h"
#include "GUIDialogAddonInfo.h"
#include "GUIDialogAddonSettings.h"
#include "GUIDialogKeyboard.h"
#include "GUIDialogYesNo.h"
#include "GUIDialogSelect.h"
#include "GUIEditControl.h"
#include "GUIUserMessages.h"
#include "GUIWindowManager.h"
#include "Util.h"
#include "URL.h"
#include "FileItem.h"
#include "FileSystem/File.h"
#include "FileSystem/Directory.h"
#include "FileSystem/AddonsDirectory.h"
#include "utils/FileOperationJob.h"
#include "utils/JobManager.h"
#include "utils/log.h"
#include "utils/SingleLock.h"
#include "Settings.h"
#include "Application.h"
#include "AddonDatabase.h"
#include "AdvancedSettings.h"

#define CONTROL_AUTOUPDATE 5

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);
      // check for valid quickpath parameter
      CStdString strDestination = message.GetNumStringParams() ? message.GetStringParam(0) : "";
      CStdString strReturn = message.GetNumStringParams() > 1 ? message.GetStringParam(1) : "";
      bool returning = strReturn.CompareNoCase("return") == 0;

      if (!strDestination.IsEmpty())
      {
        message.SetStringParam("");
        CLog::Log(LOGINFO, "Attempting to %s to: %s", returning ? "return" : "quickpath", strDestination.c_str());
      }

      CStdString destPath;
      if (!strDestination.IsEmpty())
      {
        if (strDestination.Equals("$ROOT") || strDestination.Equals("Root"))
          destPath = "";
        else if (strDestination.Equals("Enabled"))
          destPath = "addons://enabled/";
        else if (strDestination.Equals("Available"))
          destPath = "addons://all/";
        else if (strDestination.Equals("Scrapers"))
          destPath = "addons://enabled/scraper";
        else if (strDestination.Equals("Screensavers"))
          destPath = "addons://enabled/screensaver";
        else if (strDestination.Equals("Plugins"))
          destPath = "addons://enabled/plugin";
        else if (strDestination.Equals("Visualizations"))
          destPath = "addons://enabled/visualization";
        else if (strDestination.Equals("Scripts"))
          destPath = "addons://enabled/script";
        else if (strDestination.Equals("AvailableScrapers"))
          destPath = "addons://all/scraper";
        else if (strDestination.Equals("AvailableScreensavers"))
          destPath = "addons://all/screensaver";
        else if (strDestination.Equals("AvailablePlugins"))
          destPath = "addons://all/plugin";
        else if (strDestination.Equals("AvailableVisualizations"))
          destPath = "addons://all/visualization";
        else if (strDestination.Equals("AvailableScripts"))
          destPath = "addons://all/script";
        else
        {
          CLog::Log(LOGWARNING, "Warning, destination parameter (%s) may not be valid", strDestination.c_str());
          destPath = strDestination;
        }
      }
      m_vecItems->m_strPath = destPath;
      SetHistoryForPath(destPath);
      m_startDirectory = returning ? destPath : "";
    }
    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 (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").IsEmpty())
            return CGUIDialogAddonInfo::ShowForItem((*m_vecItems)[iItem]);
          return false;
        }
      }
    }
    break;
   default:
     break;
  }
  return CGUIMediaWindow::OnMessage(message);
}

bool CGUIWindowAddonBrowser::OnAction(const CAction& action)
{
  if (action.GetID() == ACTION_PARENT_DIR)
  {
    if (g_advancedSettings.m_bUseEvilB &&
        m_vecItems->m_strPath == m_startDirectory)
    {
      g_windowManager.PreviousWindow();
      return true;
    }
  }
  return CGUIMediaWindow::OnAction(action);
}

void CGUIWindowAddonBrowser::GetContextButtons(int itemNumber,
                                               CContextButtons& buttons)
{
  CFileItemPtr pItem = m_vecItems->Get(itemNumber);
  if (pItem->m_strPath.Equals("addons://enabled/"))
    buttons.Add(CONTEXT_BUTTON_SCAN,24034);
  
  TYPE type = TranslateType(pItem->GetProperty("Addon.intType"));
  AddonPtr addon;
  if (!CAddonMgr::Get().GetAddon(pItem->GetProperty("Addon.ID"),
                                  addon, type, false))
    return;

  if (addon->Type() == ADDON_REPOSITORY)
  {
    buttons.Add(CONTEXT_BUTTON_SCAN,24034);
    buttons.Add(CONTEXT_BUTTON_UPDATE_LIBRARY,24035);
  }

  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->m_strPath.Equals("addons://enabled/"))
  {
    if (button == CONTEXT_BUTTON_SCAN)
    {
      CAddonMgr::Get().FindAddons();
      return true;
    }
  }
  TYPE type = TranslateType(pItem->GetProperty("Addon.intType"));
  AddonPtr addon;
  if (!CAddonMgr::Get().GetAddon(pItem->GetProperty("Addon.ID"),
                                  addon, type, false))
    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);
    CJobManager::GetInstance().AddJob(new CRepositoryUpdateJob(repo,false),this);
    return true;
  }

  return CGUIMediaWindow::OnContextButton(itemNumber, button);
}

bool CGUIWindowAddonBrowser::OnClick(int iItem)
{
  CFileItemPtr item = m_vecItems->Get(iItem);
  if (!item->m_bIsFolder)
  {
    // cancel a downloading job
    if (item->GetProperty("Addon.Status").Equals(g_localizeStrings.Get(13413)))
    {
      if (CGUIDialogYesNo::ShowAndGetInput(g_localizeStrings.Get(24000),
                                           item->GetProperty("Addon.Name"),
                                           g_localizeStrings.Get(24066),""))
      {
        CSingleLock lock(m_critSection);
        map<CStdString,unsigned int>::iterator it = m_idtojobid.find(item->GetProperty("Addon.ID"));
        if (it != m_idtojobid.end())
        {
          CJobManager::GetInstance().CancelJob(it->second);
          UnRegisterJob(m_idtojob.find(item->GetProperty("Addon.ID"))->second);
          Update(m_vecItems->m_strPath);
        }
      }
      return true;
    }

    CGUIDialogAddonInfo::ShowForItem(item);
    return true;
  }

  return CGUIMediaWindow::OnClick(iItem);
}

void CGUIWindowAddonBrowser::OnJobComplete(unsigned int jobID,
                                           bool success, CJob* job2)
{
  if (success)
  {
    CFileOperationJob* job = (CFileOperationJob*)job2;
    if (job->GetAction() == CFileOperationJob::ActionCopy)
    {
      for (int i=0;i<job->GetItems().Size();++i)
      {
        CStdString strFolder = job->GetItems()[i]->m_strPath;
        // zip is downloaded - now extract it
        if (CUtil::IsZIP(strFolder))
        {
          AddJob(strFolder);
        }
        else
        {
          CURL url(strFolder);
          // zip extraction job is done
          if (url.GetProtocol() == "zip")
          {
            CFileItemList list;
            CDirectory::GetDirectory(url.Get(),list);
            CStdString dirname = "";
            for (int i=0;i<list.Size();++i)
            {
              if (list[i]->m_bIsFolder)
              {
                dirname = list[i]->GetLabel();
                break;
              }
            }
            strFolder = CUtil::AddFileToFolder("special://home/addons/",
                                               dirname);
          }
          else
          {
            CUtil::RemoveSlashAtEnd(strFolder);
            strFolder = CUtil::AddFileToFolder("special://home/addons/",
                                               CUtil::GetFileName(strFolder));
          }
          AddonPtr addon;
          bool update=false;
          if (CAddonMgr::Get().LoadAddonDescription(strFolder, addon))
          {
            CStdString strFolder2;
            CUtil::GetDirectory(strFolder,strFolder2);
            AddonPtr addon2;
            update = CAddonMgr::Get().GetAddon(addon->ID(),addon2);
            CAddonMgr::Get().FindAddons();
            CAddonMgr::Get().GetAddon(addon->ID(),addon);
            ADDONDEPS deps = addon->GetDeps();
            for (ADDONDEPS::iterator it  = deps.begin();
                                     it != deps.end();++it)
            {
              if (it->first.Equals("xbmc.metadata"))
                continue;
              if (!CAddonMgr::Get().GetAddon(it->first,addon2))
              {
                CAddonDatabase database;
                database.Open();
                if (database.GetAddon(it->first,addon2))
                  AddJob(addon2->Path());
              }
            }
            if (addon->Type() >= ADDON_VIZ_LIBRARY)
              continue;
            if (update)
            {
              g_application.m_guiDialogKaiToast.QueueNotification(
                                                  CGUIDialogKaiToast::Info,
                                                  addon->Name(),
                                                  g_localizeStrings.Get(24065));
            }
            else
            {
              g_application.m_guiDialogKaiToast.QueueNotification(
                                                  CGUIDialogKaiToast::Info,
                                                  addon->Name(),
                                                  g_localizeStrings.Get(24064));
            }
          }
        }
      }
    }
    CAddonMgr::Get().FindAddons();

    CGUIMessage msg(GUI_MSG_NOTIFY_ALL, 0, 0, GUI_MSG_UPDATE);
    g_windowManager.SendThreadMessage(msg);
  }
  UnRegisterJob((CFileOperationJob*)job2);
}

void CGUIWindowAddonBrowser::UpdateButtons()
{
  SET_CONTROL_SELECTED(GetID(),CONTROL_AUTOUPDATE,g_settings.m_bAddonAutoUpdate);
  CGUIMediaWindow::UpdateButtons();
}

pair<CFileOperationJob*,unsigned int> CGUIWindowAddonBrowser::AddJob(const CStdString& path)
{
  CGUIWindowAddonBrowser* that = (CGUIWindowAddonBrowser*)g_windowManager.GetWindow(WINDOW_ADDON_BROWSER);
  CFileItemList list;
  CStdString dest="special://home/addons/packages/";
  CStdString package = CUtil::AddFileToFolder("special://home/addons/packages/",
                                              CUtil::GetFileName(path));
  if (CUtil::HasSlashAtEnd(path))
  {
    dest = "special://home/addons/";
    list.Add(CFileItemPtr(new CFileItem(path,true)));
  }
  else
  {
    // check for cached copy
    if (CFile::Exists(package))
    {
      CStdString archive;
      CUtil::CreateArchivePath(archive,"zip",package,"");
      list.Add(CFileItemPtr(new CFileItem(archive,true)));
      dest = "special://home/addons/";
    }
    else
    {
      list.Add(CFileItemPtr(new CFileItem(path,false)));
    }
  }

  list[0]->Select(true);
  CFileOperationJob* job = new CFileOperationJob(CFileOperationJob::ActionCopy,
                                                 list,dest);
  unsigned int id = CJobManager::GetInstance().AddJob(job,that);

  return make_pair(job,id);
}

void CGUIWindowAddonBrowser::RegisterJob(const CStdString& id,
                                         CFileOperationJob* job,
                                         unsigned int jobid)
{
  CSingleLock lock(m_critSection);
  m_idtojob.insert(make_pair(id,job));
  m_idtojobid.insert(make_pair(id,jobid));
  m_jobtoid.insert(make_pair(job,id));
  CGUIMessage msg(GUI_MSG_NOTIFY_ALL, 0, 0, GUI_MSG_UPDATE);
  g_windowManager.SendThreadMessage(msg);
}

void CGUIWindowAddonBrowser::UnRegisterJob(CFileOperationJob* job)
{
  CSingleLock lock(m_critSection);
  map<CFileOperationJob*,CStdString>::iterator it = m_jobtoid.find((CFileOperationJob*)job);
  if (it != m_jobtoid.end())
  {
    m_idtojob.erase(m_idtojob.find(it->second));
    m_idtojobid.erase(m_idtojobid.find(it->second));
    m_jobtoid.erase(it);
  }
}

bool CGUIWindowAddonBrowser::GetDirectory(const CStdString& strDirectory,
                                          CFileItemList& items)
{
  bool result;
  if (strDirectory.Equals("addons://downloading/"))
  {
    CAddonDatabase database;
    database.Open();
    CSingleLock lock(m_critSection);
    VECADDONS addons;
    for (map<CStdString,unsigned int>::iterator it = m_idtojobid.begin();
         it != m_idtojobid.end();++it)
    {
      AddonPtr addon;
      if (database.GetAddon(it->first,addon))
        addons.push_back(addon);
    }
    CURL url(strDirectory);
    CAddonsDirectory::GenerateListing(url,addons,items);
    result = true;
    items.SetProperty("Repo.Name",g_localizeStrings.Get(24067));
    items.m_strPath = strDirectory;
  }
  else
    result = CGUIMediaWindow::GetDirectory(strDirectory,items);

  if (strDirectory.IsEmpty() && !m_jobtoid.empty())
  {
    CFileItemPtr item(new CFileItem("addons://downloading/",true));
    item->SetLabel(g_localizeStrings.Get(24067));
    item->SetLabelPreformated(true);
    item->SetThumbnailImage("DefaultNetwork.png");
    items.Add(item);
  }

  CSingleLock lock(m_critSection);
  for (int i=0;i<items.Size();++i)
  {
    if (items[i]->m_bIsFolder)
      continue;
    map<CStdString,CFileOperationJob*>::iterator it = m_idtojob.find(items[i]->GetProperty("Addon.ID"));
    if (it != m_idtojob.end())
      items[i]->SetProperty("Addon.Status",g_localizeStrings.Get(13413));
    items[i]->SetLabel2(items[i]->GetProperty("Addon.Status"));
    // to avoid the view state overriding label 2
    items[i]->SetLabelPreformated(true);
  }

  return result;
}

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;
}

CFileItemPtr GetFileItem(const CStdString &path, int label)
{
  CFileItemPtr item(new CFileItem(path, false));
  item->SetLabel(g_localizeStrings.Get(label));
  return item;
}

bool CGUIWindowAddonBrowser::SelectAddonID(TYPE type, CStdString &addonID, bool showNone)
{
  CGUIDialogSelect *dialog = (CGUIDialogSelect*)g_windowManager.GetWindow(WINDOW_DIALOG_SELECT);
  if (type == ADDON_UNKNOWN || !dialog)
    return false;

  ADDON::VECADDONS addons;
  CAddonMgr::Get().GetAddons(type, addons);
  dialog->SetHeading(TranslateType(type, true));
  dialog->Reset();
  dialog->SetUseDetails(true);
  dialog->EnableButton(true, 21452);
  CFileItemList items;
  if (showNone)
    items.Add(GetFileItem("", 231));
  // add some built in options...
  if (type == ADDON_VIZ || type == ADDON_SCREENSAVER)
    items.Add(GetFileItem("_virtual.none", 231));
  for (ADDON::IVECADDONS i = addons.begin(); i != addons.end(); ++i)
    items.Add(CAddonsDirectory::FileItemFromAddon(*i, ""));
  dialog->SetItems(&items);
  dialog->DoModal();
  if (dialog->IsButtonPressed())
  { // switch to the addons browser.
    vector<CStdString> params;
    params.push_back("addons://repos/");
    params.push_back("return");
    g_windowManager.ActivateWindow(WINDOW_ADDON_BROWSER, params);
    return false;
  }
  if (dialog->GetSelectedLabel() >= 0)
  {
    addonID = dialog->GetSelectedItem().m_strPath;
    return true;
  }
  return false;
}