aboutsummaryrefslogtreecommitdiff
path: root/xbmc/addons/gui/GUIDialogAddonSettings.cpp
blob: 60cbaa2a2ea29b6291362a065460c8bcf6af76b8 (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
/*
 *  Copyright (C) 2005-2018 Team Kodi
 *  This file is part of Kodi - https://kodi.tv
 *
 *  SPDX-License-Identifier: GPL-2.0-or-later
 *  See LICENSES/README.md for more information.
 */

#include "GUIDialogAddonSettings.h"

#include "FileItem.h"
#include "FileItemList.h"
#include "GUIPassword.h"
#include "GUIUserMessages.h"
#include "ServiceBroker.h"
#include "addons/AddonManager.h"
#include "addons/addoninfo/AddonType.h"
#include "addons/settings/AddonSettings.h"
#include "dialogs/GUIDialogSelect.h"
#include "dialogs/GUIDialogYesNo.h"
#include "guilib/GUIComponent.h"
#include "guilib/GUIWindowManager.h"
#include "guilib/LocalizeStrings.h"
#include "guilib/WindowIDs.h"
#include "input/actions/Action.h"
#include "input/actions/ActionIDs.h"
#include "messaging/helpers/DialogOKHelper.h"
#include "settings/Settings.h"
#include "settings/SettingsComponent.h"
#include "settings/lib/SettingSection.h"
#include "settings/lib/SettingsManager.h"
#include "utils/StringUtils.h"
#include "utils/Variant.h"
#include "utils/log.h"
#include "view/ViewStateSettings.h"

#define CONTROL_BTN_LEVELS 20

using namespace ADDON;
using namespace KODI::MESSAGING;

CGUIDialogAddonSettings::CGUIDialogAddonSettings()
  : CGUIDialogSettingsManagerBase(WINDOW_DIALOG_ADDON_SETTINGS, "DialogAddonSettings.xml")
{
}

bool CGUIDialogAddonSettings::OnMessage(CGUIMessage& message)
{
  switch (message.GetMessage())
  {
    case GUI_MSG_CLICKED:
    {
      if (message.GetSenderId() == CONTROL_SETTINGS_CUSTOM_BUTTON)
      {
        OnResetSettings();
        return true;
      }
      break;
    }

    case GUI_MSG_SETTING_UPDATED:
    {
      const std::string& settingId = message.GetStringParam(0);
      const std::string& settingValue = message.GetStringParam(1);
      const ADDON::AddonInstanceId instanceId = message.GetParam1();

      if (instanceId != m_instanceId)
      {
        CLog::Log(LOGERROR,
                  "CGUIDialogAddonSettings::{}: Set value \"{}\" from add-on \"{}\" called with "
                  "invalid instance id (given: {}, needed: {})",
                  __func__, m_addon->ID(), settingId, instanceId, m_instanceId);
        break;
      }

      std::shared_ptr<CSetting> setting = GetSettingsManager()->GetSetting(settingId);
      if (setting != nullptr)
      {
        setting->FromString(settingValue);
        return true;
      }
      break;
    }

    default:
      break;
  }

  return CGUIDialogSettingsManagerBase::OnMessage(message);
}

bool CGUIDialogAddonSettings::OnAction(const CAction& action)
{
  switch (action.GetID())
  {
    case ACTION_SETTINGS_LEVEL_CHANGE:
    {
      // Test if we can access the new level
      if (!g_passwordManager.CheckSettingLevelLock(
              CViewStateSettings::GetInstance().GetNextSettingLevel(), true))
        return false;

      CViewStateSettings::GetInstance().CycleSettingLevel();
      CServiceBroker::GetSettingsComponent()->GetSettings()->Save();

      // try to keep the current position
      std::string oldCategory;
      if (m_iCategory >= 0 && m_iCategory < static_cast<int>(m_categories.size()))
        oldCategory = m_categories[m_iCategory]->GetId();

      SET_CONTROL_LABEL(CONTROL_BTN_LEVELS,
                        10036 +
                            static_cast<int>(CViewStateSettings::GetInstance().GetSettingLevel()));
      // only re-create the categories, the settings will be created later
      SetupControls(false);

      m_iCategory = 0;
      // try to find the category that was previously selected
      if (!oldCategory.empty())
      {
        for (int i = 0; i < static_cast<int>(m_categories.size()); i++)
        {
          if (m_categories[i]->GetId() == oldCategory)
          {
            m_iCategory = i;
            break;
          }
        }
      }

      CreateSettings();
      return true;
    }

    default:
      break;
  }

  return CGUIDialogSettingsManagerBase::OnAction(action);
}

bool CGUIDialogAddonSettings::ShowForAddon(const ADDON::AddonPtr& addon,
                                           bool saveToDisk /* = true */)
{
  if (!addon)
  {
    CLog::LogF(LOGERROR, "No addon given!");
    return false;
  }

  if (addon->Type() == ADDON::AddonType::GAME_CONTROLLER)
  {
    CServiceBroker::GetGUI()->GetWindowManager().ActivateWindow(WINDOW_DIALOG_GAME_CONTROLLERS,
                                                                addon->ID());
    return true;
  }

  if (!g_passwordManager.CheckMenuLock(WINDOW_ADDON_BROWSER))
    return false;

  if (addon->SupportsInstanceSettings())
    return ShowForMultipleInstances(addon, saveToDisk);
  else
    return ShowForSingleInstance(addon, saveToDisk);
}

bool CGUIDialogAddonSettings::ShowForSingleInstance(
    const ADDON::AddonPtr& addon,
    bool saveToDisk,
    ADDON::AddonInstanceId instanceId /* = ADDON::ADDON_SETTINGS_ID */)
{
  if (!addon->HasSettings(instanceId))
  {
    // addon does not support settings, inform user
    HELPERS::ShowOKDialogText(CVariant{24000}, CVariant{24030});
    return false;
  }

  // Create the dialog
  CGUIDialogAddonSettings* dialog =
      CServiceBroker::GetGUI()->GetWindowManager().GetWindow<CGUIDialogAddonSettings>(
          WINDOW_DIALOG_ADDON_SETTINGS);
  if (!dialog)
  {
    CLog::LogF(LOGERROR, "Unable to get WINDOW_DIALOG_ADDON_SETTINGS instance!");
    return false;
  }

  dialog->m_addon = addon;
  dialog->m_instanceId = instanceId;
  dialog->m_saveToDisk = saveToDisk;

  dialog->Open();

  if (!dialog->IsConfirmed())
  {
    addon->ReloadSettings(instanceId);
    return false;
  }

  if (saveToDisk)
    addon->SaveSettings(instanceId);

  return true;
}

bool CGUIDialogAddonSettings::ShowForMultipleInstances(const ADDON::AddonPtr& addon,
                                                       bool saveToDisk)
{
  CGUIDialogSelect* dialog =
      CServiceBroker::GetGUI()->GetWindowManager().GetWindow<CGUIDialogSelect>(
          WINDOW_DIALOG_SELECT);
  if (!dialog)
  {
    CLog::LogF(LOGERROR, "Unable to get WINDOW_DIALOG_SELECT instance!");
    return false;
  }

  int lastSelected = -1;
  while (true)
  {
    std::vector<ADDON::AddonInstanceId> ids = addon->GetKnownInstanceIds();
    std::sort(ids.begin(), ids.end(), [](const auto& a, const auto& b) { return a < b; });

    dialog->Reset();
    dialog->SetHeading(10012); // Add-on configurations and settings
    dialog->SetUseDetails(false);

    CFileItemList itemsInstances;
    ADDON::AddonInstanceId highestId = 0;
    for (const auto& id : ids)
    {
      std::string name;
      addon->GetSettingString(ADDON_SETTING_INSTANCE_NAME_VALUE, name, id);
      if (name.empty())
        name = g_localizeStrings.Get(13205); // Unknown

      bool enabled = false;
      addon->GetSettingBool(ADDON_SETTING_INSTANCE_ENABLED_VALUE, enabled, id);

      const std::string label = StringUtils::Format(
          g_localizeStrings.Get(10020), name,
          g_localizeStrings.Get(enabled ? 305 : 13106)); // Edit "config name" [enabled state]

      const CFileItemPtr item = std::make_shared<CFileItem>(label);
      item->SetProperty("id", id);
      item->SetProperty("name", name);
      itemsInstances.Add(item);

      if (id > highestId)
        highestId = id;
    }

    CFileItemList itemsGeneral;

    const ADDON::AddonInstanceId addInstanceId = highestId + 1;
    const ADDON::AddonInstanceId removeInstanceId = highestId + 2;

    CFileItemPtr item =
        std::make_shared<CFileItem>(g_localizeStrings.Get(10014)); // Add add-on configuration
    item->SetProperty("id", addInstanceId);
    itemsGeneral.Add(item);

    if (ids.size() > 1) // Forbid removal of last instance
    {
      item =
          std::make_shared<CFileItem>(g_localizeStrings.Get(10015)); // Remove add-on configuration
      item->SetProperty("id", removeInstanceId);
      itemsGeneral.Add(item);
    }

    if (addon->HasSettings(ADDON_SETTINGS_ID))
    {
      item = std::make_shared<CFileItem>(g_localizeStrings.Get(10013)); // Edit Add-on settings
      item->SetProperty("id", ADDON_SETTINGS_ID);
      itemsGeneral.Add(item);
    }

    for (auto& it : itemsGeneral)
      dialog->Add(*it);

    for (auto& it : itemsInstances)
      dialog->Add(*it);

    // Select last selected item, first instance config item or first item
    if (lastSelected >= 0)
      dialog->SetSelected(lastSelected);
    else
      dialog->SetSelected(itemsInstances.Size() > 0 ? itemsGeneral.Size() : 0);

    dialog->Open();

    if (dialog->IsButtonPressed() || !dialog->IsConfirmed())
      break;

    lastSelected = dialog->GetSelectedItem();

    item = dialog->GetSelectedFileItem();
    ADDON::AddonInstanceId instanceId = item->GetProperty("id").asInteger();

    if (instanceId == addInstanceId)
    {
      instanceId = highestId + 1;

      addon->GetSettings(instanceId);
      addon->UpdateSettingString(ADDON_SETTING_INSTANCE_NAME_VALUE, "", instanceId);
      addon->UpdateSettingBool(ADDON_SETTING_INSTANCE_ENABLED_VALUE, true, instanceId);
      addon->SaveSettings(instanceId);

      if (ShowForSingleInstance(addon, saveToDisk, instanceId))
      {
        CServiceBroker::GetAddonMgr().PublishInstanceAdded(addon->ID(), instanceId);
      }
      else
      {
        // Remove instance settings if not succeeded (e.g. dialog cancelled)
        addon->DeleteInstanceSettings(instanceId);
      }
    }
    else if (instanceId == removeInstanceId)
    {
      dialog->Reset();
      dialog->SetHeading(10010); // Select add-on configuration to remove
      dialog->SetUseDetails(false);

      for (auto& it : itemsInstances)
      {
        CFileItem item(*it);
        item.SetLabel((*it).GetProperty("name").asString());
        dialog->Add(item);
      }

      dialog->SetSelected(0);
      dialog->Open();

      if (!dialog->IsButtonPressed() && dialog->IsConfirmed())
      {
        item = dialog->GetSelectedFileItem();
        const std::string label = StringUtils::Format(
            g_localizeStrings.Get(10019),
            item->GetProperty("name")
                .asString()); // Do you want to remove the add-on configuration "config name"?

        if (CGUIDialogYesNo::ShowAndGetInput(10009, // Confirm add-on configuration removal
                                             label))
        {
          instanceId = item->GetProperty("id").asInteger();
          addon->DeleteInstanceSettings(instanceId);
          CServiceBroker::GetAddonMgr().PublishInstanceRemoved(addon->ID(), instanceId);
        }
      }
    }
    else
    {
      // edit instance settings or edit addon settings selected; open settings dialog

      bool enabled = false;
      addon->GetSettingBool(ADDON_SETTING_INSTANCE_ENABLED_VALUE, enabled, instanceId);

      if (ShowForSingleInstance(addon, saveToDisk, instanceId) && instanceId != ADDON_SETTINGS_ID)
      {
        // Publish new/removed instance configuration and start the use of new instance
        bool enabledNow = false;
        addon->GetSettingBool(ADDON_SETTING_INSTANCE_ENABLED_VALUE, enabledNow, instanceId);
        if (enabled != enabledNow)
        {
          if (enabledNow)
            CServiceBroker::GetAddonMgr().PublishInstanceAdded(addon->ID(), instanceId);
          else
            CServiceBroker::GetAddonMgr().PublishInstanceRemoved(addon->ID(), instanceId);
        }
      }
    }

    // refresh selection dialog content...

  } // while (true)

  return true;
}

void CGUIDialogAddonSettings::SaveAndClose()
{
  if (!g_passwordManager.CheckMenuLock(WINDOW_ADDON_BROWSER))
    return;

  // get the dialog
  CGUIDialogAddonSettings* dialog =
      CServiceBroker::GetGUI()->GetWindowManager().GetWindow<CGUIDialogAddonSettings>(
          WINDOW_DIALOG_ADDON_SETTINGS);
  if (dialog == nullptr || !dialog->IsActive())
    return;

  // check if we need to save the settings
  if (dialog->m_saveToDisk && dialog->m_addon != nullptr)
    dialog->m_addon->SaveSettings(dialog->m_instanceId);

  // close the dialog
  dialog->Close();
}

std::string CGUIDialogAddonSettings::GetCurrentAddonID() const
{
  if (m_addon == nullptr)
    return "";

  return m_addon->ID();
}

void CGUIDialogAddonSettings::SetupView()
{
  if (m_addon == nullptr || m_addon->GetSettings(m_instanceId) == nullptr)
    return;

  auto settings = m_addon->GetSettings(m_instanceId);
  if (!settings->IsLoaded())
    return;

  CGUIDialogSettingsManagerBase::SetupView();

  // set addon id as window property
  SetProperty("Addon.ID", m_addon->ID());

  // set heading
  SetHeading(StringUtils::Format("$LOCALIZE[10004] - {}",
                                 m_addon->Name())); // "Settings - AddonName"

  // set control labels
  SET_CONTROL_LABEL(CONTROL_SETTINGS_OKAY_BUTTON, 186);
  SET_CONTROL_LABEL(CONTROL_SETTINGS_CANCEL_BUTTON, 222);
  SET_CONTROL_LABEL(CONTROL_SETTINGS_CUSTOM_BUTTON, 409);
  SET_CONTROL_LABEL(CONTROL_BTN_LEVELS,
                    10036 + static_cast<int>(CViewStateSettings::GetInstance().GetSettingLevel()));
}

std::string CGUIDialogAddonSettings::GetLocalizedString(uint32_t labelId) const
{
  std::string label = g_localizeStrings.GetAddonString(m_addon->ID(), labelId);
  if (!label.empty())
    return label;

  return CGUIDialogSettingsManagerBase::GetLocalizedString(labelId);
}

std::string CGUIDialogAddonSettings::GetSettingsLabel(const std::shared_ptr<ISetting>& setting)
{
  if (setting == nullptr)
    return "";

  std::string label = GetLocalizedString(setting->GetLabel());
  if (!label.empty())
    return label;

  // try the addon settings
  label = m_addon->GetSettings(m_instanceId)->GetSettingLabel(setting->GetLabel());
  if (!label.empty())
    return label;

  return CGUIDialogSettingsManagerBase::GetSettingsLabel(setting);
}

int CGUIDialogAddonSettings::GetSettingLevel() const
{
  return static_cast<int>(CViewStateSettings::GetInstance().GetSettingLevel());
}

std::shared_ptr<CSettingSection> CGUIDialogAddonSettings::GetSection()
{
  const auto settingsManager = GetSettingsManager();
  if (settingsManager == nullptr)
    return nullptr;

  const auto sections = settingsManager->GetSections();
  if (!sections.empty())
    return sections.front();

  return nullptr;
}

CSettingsManager* CGUIDialogAddonSettings::GetSettingsManager() const
{
  if (m_addon == nullptr || m_addon->GetSettings(m_instanceId) == nullptr)
    return nullptr;

  return m_addon->GetSettings(m_instanceId)->GetSettingsManager();
}

void CGUIDialogAddonSettings::OnSettingAction(const std::shared_ptr<const CSetting>& setting)
{
  if (m_addon == nullptr || m_addon->GetSettings(m_instanceId) == nullptr)
    return;

  m_addon->GetSettings(m_instanceId)->OnSettingAction(setting);
}