aboutsummaryrefslogtreecommitdiff
path: root/libs/installer_fomod/src/fomodinstallerdialog.h
blob: bd9f975d84d007eaf5d5c6164d5959f7871990c5 (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
/*
Copyright (C) 2012 Sebastian Herbord. All rights reserved.

This file is part of Mod Organizer.

Mod Organizer 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 3 of the License, or
(at your option) any later version.

Mod Organizer 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 Mod Organizer.  If not, see <http://www.gnu.org/licenses/>.
*/

#pragma once

#include <QDialog>
#include <QGroupBox>
#include <QMetaType>
#include <QObject>
#include <QString>

#include <functional>
#include <vector>

#include <uibase/guessedvalue.h>
#include <uibase/ifiletree.h>
#include <uibase/imoinfo.h>
#include <uibase/iplugininstaller.h>
#include <uibase/ipluginlist.h>

#include "installerfomod.h"

class QAbstractButton;
class QXmlStreamReader;

namespace Ui
{
class FomodInstallerDialog;
}

class ValueCondition;
class ConditionFlag;
class SubCondition;
class FileCondition;
class VersionCondition;

class XmlReader;

class IConditionTester
{
public:
  virtual std::pair<bool, QString>
  testCondition(int maxIndex, const ValueCondition* condition) const = 0;
  virtual std::pair<bool, QString>
  testCondition(int maxIndex, const ConditionFlag* condition) const = 0;
  virtual std::pair<bool, QString>
  testCondition(int maxIndex, const SubCondition* condition) const = 0;
  virtual std::pair<bool, QString>
  testCondition(int maxIndex, const FileCondition* condition) const = 0;
  virtual std::pair<bool, QString>
  testCondition(int maxIndex, const VersionCondition* condition) const = 0;
};

enum ConditionOperator
{
  OP_AND,
  OP_OR
};

class Condition
{
public:
  Condition() {}
  virtual std::pair<bool, QString> test(int maxIndex,
                                        const IConditionTester* tester) const = 0;

private:
  Condition& operator=(const Condition&) = delete;
};

class ConditionFlag : public Condition
{
public:
  ConditionFlag() : Condition(), m_Name(), m_Value() {}
  ConditionFlag(const QString& name, const QString& value)
      : Condition(), m_Name(name), m_Value(value)
  {}
  virtual std::pair<bool, QString> test(int maxIndex,
                                        const IConditionTester* tester) const
  {
    return tester->testCondition(maxIndex, this);
  }
  QString m_Name;
  QString m_Value;
};
Q_DECLARE_METATYPE(ConditionFlag)

class ValueCondition : public Condition
{
public:
  ValueCondition() : Condition(), m_Name(), m_Value() {}
  ValueCondition(const QString& name, const QString& value)
      : Condition(), m_Name(name), m_Value(value)
  {}
  virtual std::pair<bool, QString> test(int maxIndex,
                                        const IConditionTester* tester) const
  {
    return tester->testCondition(maxIndex, this);
  }
  QString m_Name;
  QString m_Value;
};
Q_DECLARE_METATYPE(ValueCondition)

class FileCondition : public Condition
{
public:
  FileCondition() : Condition(), m_File(), m_State() {}
  FileCondition(const QString& file, const QString& state)
      : Condition(), m_File(file), m_State(state)
  {}
  virtual std::pair<bool, QString> test(int maxIndex,
                                        const IConditionTester* tester) const
  {
    return tester->testCondition(maxIndex, this);
  }
  QString m_File;
  QString m_State;
};
Q_DECLARE_METATYPE(FileCondition)

class SubCondition : public Condition
{
public:
  virtual std::pair<bool, QString> test(int maxIndex,
                                        const IConditionTester* tester) const
  {
    return tester->testCondition(maxIndex, this);
  }
  ConditionOperator m_Operator;
  std::vector<Condition*> m_Conditions;
};
Q_DECLARE_METATYPE(SubCondition)

class VersionCondition : public Condition
{
public:
  enum Type
  {
    v_Game,
    v_FOMM,
    v_FOSE
  };
  VersionCondition() : Condition(), m_Type(), m_RequiredVersion() {}
  VersionCondition(Type type, const QString& requiredVersion)
      : Condition(), m_Type(type), m_RequiredVersion(requiredVersion)
  {}
  virtual std::pair<bool, QString> test(int maxIndex,
                                        const IConditionTester* tester) const
  {
    return tester->testCondition(maxIndex, this);
  }
  Type m_Type;
  QString m_RequiredVersion;
};
Q_DECLARE_METATYPE(VersionCondition)

class FileDescriptor : public QObject
{
  Q_OBJECT
public:
  FileDescriptor(QObject* parent)
      : QObject(parent), m_Source(), m_Destination(), m_Priority(0), m_IsFolder(false),
        m_AlwaysInstall(false), m_InstallIfUsable(false), m_FileSystemItemSequence(0)
  {}

  FileDescriptor(const FileDescriptor& reference)
      : QObject(reference.parent()), m_Source(reference.m_Source),
        m_Destination(reference.m_Destination), m_Priority(reference.m_Priority),
        m_IsFolder(reference.m_IsFolder), m_AlwaysInstall(reference.m_AlwaysInstall),
        m_InstallIfUsable(reference.m_InstallIfUsable),
        m_FileSystemItemSequence(reference.m_FileSystemItemSequence)
  {}

  QString m_Source;
  QString m_Destination;
  int m_Priority;
  bool m_IsFolder;
  bool m_AlwaysInstall;
  bool m_InstallIfUsable;
  int m_FileSystemItemSequence;

private:
  FileDescriptor& operator=(const FileDescriptor&);
};

Q_DECLARE_METATYPE(FileDescriptor*)

class FomodInstallerDialog : public QDialog, public IConditionTester
{
  Q_OBJECT

public:
  explicit FomodInstallerDialog(
      InstallerFomod* installer, const MOBase::GuessedValue<QString>& modName,
      const QString& fomodPath,
      const std::function<MOBase::IPluginList::PluginStates(const QString&)>& fileCheck,
      QWidget* parent = 0);
  ~FomodInstallerDialog();

  void initData(MOBase::IOrganizer* moInfo);

  /**
   * @return bool true if the user requested the manual dialog
   **/
  bool manualRequested() const { return m_Manual; }

  /**
   * @return the (user-modified) name to be used for the mod
   **/
  QString getName() const;

  /**
   * @return the version of the mod as specified in the fomod info.xml
   */
  QString getVersion() const;

  /**
   * @return the mod id as specified in the info.xml
   */
  int getModID() const;

  /**
   * @return the mod url as specified in the fomod file
   */
  QString getURL() const;

  /**
   * @brief Updated the archive tree from the dialog.
   *
   * @param tree The input archive tree.
   **/
  MOBase::IPluginInstaller::EInstallResult
  updateTree(std::shared_ptr<MOBase::IFileTree>& tree);

  bool hasOptions();

  void transformToSmallInstall();

protected:
  virtual bool eventFilter(QObject* object, QEvent* event);

private slots:

  void on_cancelBtn_clicked();

  void on_manualBtn_clicked();

  void on_websiteLabel_linkActivated(const QString& link);

  void on_nextBtn_clicked();

  void on_prevBtn_clicked();

  // detect signals for people playing with checkboxes/buttons
  void widgetButtonClicked();

  void on_screenshotExpand_clicked();

private:
  enum ItemOrder
  {
    ORDER_ASCENDING,
    ORDER_DESCENDING,
    ORDER_EXPLICIT
  };

  // So I can make GroupType and PluginTypeInfo into QVariants
public:
  enum GroupType
  {
    TYPE_SELECTATLEASTONE,
    TYPE_SELECTATMOSTONE,
    TYPE_SELECTEXACTLYONE,
    TYPE_SELECTANY,
    TYPE_SELECTALL
  };

  enum PluginType
  {
    TYPE_REQUIRED,
    TYPE_RECOMMENDED,
    TYPE_OPTIONAL,
    TYPE_NOTUSABLE,
    TYPE_COULDBEUSABLE
  };

  struct DependencyPattern
  {
    PluginType type;
    SubCondition condition;
  };

  typedef std::vector<DependencyPattern> DependencyPatternList;

  struct PluginTypeInfo
  {
    PluginType m_DefaultType;
    DependencyPatternList m_DependencyPatterns;
  };

private:
  typedef std::vector<FileDescriptor*> FileDescriptorList;
  typedef std::vector<ConditionFlag> ConditionFlagList;

  struct Plugin
  {
    QString m_Name;
    QString m_Description;
    QString m_ImagePath;
    PluginTypeInfo m_PluginTypeInfo;
    ConditionFlagList m_ConditionFlags;
    FileDescriptorList m_Files;
  };

  struct ConditionalInstall
  {
    SubCondition m_Condition;
    FileDescriptorList m_Files;
  };

  struct LeafInfo
  {
    int priority;
    QString path;
  };

  using Leaves = std::map<const MOBase::FileTreeEntry*, LeafInfo>;

private:
  QString readContent(QXmlStreamReader& reader);

  /**
   * @brief Read XML from the given file, trying various encoding, and using
   *     the given callback on each try.
   *
   * @param file The file to read, must already be opened.
   * @param callback The callback used for every encoding try.
   */
  void readXml(QFile& file, void (FomodInstallerDialog::*callback)(XmlReader&));

  void readInfoXml();
  void readModuleConfigXml();

  void parseInfo(XmlReader& data);
  void parseModuleConfig(XmlReader& data);

  void updateNameEdit();

  static int bomOffset(const QByteArray& buffer);
  static ItemOrder getItemOrder(const QString& orderString);
  static GroupType getGroupType(const QString& typeString);
  static PluginType getPluginType(const QString& typeString);
  static bool byPriority(const FileDescriptor* LHS, const FileDescriptor* RHS);

  PluginType getPluginDependencyType(int page, PluginTypeInfo const& info) const;

  typedef void (FomodInstallerDialog::*TagProcessor)(XmlReader& reader);
  void processXmlTag(XmlReader& reader, char const* tag, TagProcessor func);

  void readFileList(XmlReader& reader, FileDescriptorList& fileList);
  void readDependencyPattern(XmlReader& reader, DependencyPattern& pattern);
  void readDependencyPatternList(XmlReader& reader, DependencyPatternList& patterns);
  void readDependencyPluginType(XmlReader& reader, PluginTypeInfo& info);
  void readPluginType(XmlReader& reader, Plugin& plugin);
  void readConditionFlagList(XmlReader& reader, ConditionFlagList& condflags);
  FomodInstallerDialog::Plugin readPlugin(XmlReader& reader);
  void readPluginList(XmlReader& reader, QString const& groupName, GroupType& groupType,
                      QLayout* layout);
  void readGroup(XmlReader& reader, QLayout* layout);
  void readGroupList(XmlReader& reader, QLayout* layout);
  QGroupBox* readInstallStep(XmlReader& reader);
  void readCompositeDependency(XmlReader& reader, SubCondition& conditional);
  ConditionalInstall readConditionalInstallPattern(XmlReader& reader);
  void readConditionalFilePatternList(XmlReader& reader);
  void readConditionalFileInstallList(XmlReader& reader);
  void readStepList(XmlReader& reader);
  void readModuleConfiguration(XmlReader& reader);
  void highlightControl(QAbstractButton* button);

  std::pair<bool, QString> testCondition(int maxIndex, const QString& flag,
                                         const QString& value) const;
  virtual std::pair<bool, QString> testCondition(int maxIndex,
                                                 const ValueCondition* condition) const;
  virtual std::pair<bool, QString> testCondition(int maxIndex,
                                                 const ConditionFlag* condition) const;
  virtual std::pair<bool, QString> testCondition(int maxIndex,
                                                 const SubCondition* condition) const;
  virtual std::pair<bool, QString> testCondition(int maxIndex,
                                                 const FileCondition* condition) const;
  virtual std::pair<bool, QString>
  testCondition(int maxIndex, const VersionCondition* condition) const;
  bool testVisible(int pageIndex) const;
  bool nextPage();
  void activateCurrentPage();

  void moveTree(std::shared_ptr<MOBase::IFileTree> target,
                std::shared_ptr<MOBase::IFileTree> source,
                MOBase::IFileTree::OverwritesType& overwrites);

  void copyLeaf(std::shared_ptr<MOBase::FileTreeEntry> sourceEntry,
                std::shared_ptr<MOBase::IFileTree> destinationTree,
                QString destinationPath, MOBase::IFileTree::OverwritesType& overwrites,
                Leaves& leaves, int pri);

  bool copyFileIterator(std::shared_ptr<MOBase::IFileTree> sourceTree,
                        std::shared_ptr<MOBase::IFileTree> destinationTree,
                        const FileDescriptor* descriptor, Leaves& leaves,
                        MOBase::IFileTree::OverwritesType& overwrites);

  static void applyPriority(Leaves& leaves, MOBase::IFileTree const* tree,
                            int priority);

  /**
   * @brief Display a dialog indicating to the user that some files were not found.
   *
   * @param missingFiles List of missing files.
   *
   * @return true if the user chose to continue with the installation, false otherwize.
   */
  bool displayMissingFilesDialog(std::vector<const FileDescriptor*> missingFiles);

  static QString toString(MOBase::IPluginList::PluginStates state);

  // Set the 'next' button to display 'next' or 'install'
  void updateNextbtnText();

  // Display the current page calculating all the button enables/disables
  void displayCurrentPage();

private:
  Ui::FomodInstallerDialog* ui;

  InstallerFomod* m_Installer;
  MOBase::GuessedValue<QString> m_ModName;

  int m_ModID;

  QString m_FomodPath;
  bool m_Manual;

  FileDescriptorList m_RequiredFiles;
  std::vector<ConditionalInstall> m_ConditionalInstalls;
  std::vector<bool> m_PageVisible;

  std::function<MOBase::IPluginList::PluginStates(const QString&)> m_FileCheck;

  // Because NMM maintains the sequence from the xml when dealing with things with
  // the same priority, we have to as well. This is moderately hacky.
  int m_FileSystemItemSequence;

  // So I can find out game info (I hope)
  MOBase::IOrganizer* m_MoInfo;

  // The web page in the fomod (if supplied)
  QString m_URL;
};

Q_DECLARE_METATYPE(FomodInstallerDialog::GroupType)
Q_DECLARE_METATYPE(FomodInstallerDialog::PluginTypeInfo)