1 #pragma once
2 
3 #include "common/common_pch.h"
4 
5 #include <QColor>
6 #include <QDateTime>
7 #include <QDir>
8 #include <QString>
9 #include <QTabWidget>
10 #include <QVariant>
11 
12 #include "common/bcp47.h"
13 #include "common/translation.h"
14 #include "mkvtoolnix-gui/info/job_settings.h"
15 #include "mkvtoolnix-gui/merge/enums.h"
16 #include "mkvtoolnix-gui/util/recently_used_strings.h"
17 
18 class QSettings;
19 class QSplitter;
20 
21 namespace mtx::gui::Util {
22 
23 class Settings: public QObject {
24   Q_OBJECT
25   Q_ENUMS(RunProgramForEvent)
26 
27 public:
28   enum RunProgramType {
29     Min,
30     ExecuteProgram,
31     PlayAudioFile,
32     ShutDownComputer,
33     HibernateComputer,
34     SleepComputer,
35     DeleteSourceFiles,
36     Max,
37     Default = ExecuteProgram,
38   };
39   Q_ENUM(RunProgramType);
40 
41   enum RunProgramForEvent {
42     RunNever                         = 0x00,
43     RunAfterJobQueueFinishes         = 0x01,
44     RunAfterJobCompletesSuccessfully = 0x02,
45     RunAfterJobCompletesWithErrors   = 0x04,
46     RunAfterJobCompletesWithWarnings = 0x08,
47   };
48   Q_ENUM(RunProgramForEvent);
49   Q_DECLARE_FLAGS(RunProgramForEvents, RunProgramForEvent)
50 
51   enum ProcessPriority {
52     LowestPriority = 0,
53     LowPriority,
54     NormalPriority,
55     HighPriority,
56     HighestPriority,
57   };
58   Q_ENUM(ProcessPriority);
59 
60   enum ScanForPlaylistsPolicy {
61     AskBeforeScanning = 0,
62     AlwaysScan,
63     NeverScan,
64   };
65   Q_ENUM(ScanForPlaylistsPolicy);
66 
67   enum OutputFileNamePolicy {
68     DontSetOutputFileName = 0,
69     ToPreviousDirectory,
70     ToFixedDirectory,
71     ToParentOfFirstInputFile,
72     ToSameAsFirstInputFile,
73     ToRelativeOfFirstInputFile,
74   };
75   Q_ENUM(OutputFileNamePolicy);
76 
77   enum class SetDefaultLanguagePolicy {
78     OnlyIfAbsent = 0,
79     IfAbsentOrUndetermined,
80   };
81   Q_ENUM(SetDefaultLanguagePolicy);
82 
83   enum class DeriveLanguageFromFileNamePolicy {
84     Never = 0,
85     OnlyIfAbsent,
86     IfAbsentOrUndetermined,
87   };
88   Q_ENUM(DeriveLanguageFromFileNamePolicy);
89 
90   enum class JobRemovalPolicy {
91     Never,
92     IfSuccessful,
93     IfWarningsFound,
94     Always,
95   };
96   Q_ENUM(JobRemovalPolicy);
97 
98   enum class ClearMergeSettingsAction {
99     None,
100     NewSettings,
101     RemoveInputFiles,
102     CloseSettings,
103   };
104   Q_ENUM(ClearMergeSettingsAction);
105 
106   enum class MergeAddingAppendingFilesPolicy {
107     Ask,
108     Add,
109     AddToNew,
110     AddEachToNew,
111     Append,
112     AddAdditionalParts,
113   };
114   Q_ENUM(MergeAddingAppendingFilesPolicy);
115 
116   enum class HeaderEditorDroppedFilesPolicy {
117     Ask,
118     Open,
119     AddAttachments,
120   };
121   Q_ENUM(HeaderEditorDroppedFilesPolicy);
122 
123   enum class TrackPropertiesLayout {
124     HorizontalScrollArea,
125     HorizontalTwoColumns,
126     VerticalTabWidget,
127   };
128   Q_ENUM(TrackPropertiesLayout);
129 
130   enum class MergeMissingAudioTrackPolicy {
131     Never,
132     IfAudioTrackPresent,
133     Always,
134   };
135   Q_ENUM(MergeMissingAudioTrackPolicy);
136 
137   enum class BCP47LanguageEditingMode {
138     FreeForm,
139     Components,
140   };
141   Q_ENUM(BCP47LanguageEditingMode);
142 
143   class RunProgramConfig {
144   public:
145     RunProgramType m_type{RunProgramType::ExecuteProgram};
146     bool m_active{true};
147     QString m_name;
148     RunProgramForEvents m_forEvents{};
149     QStringList m_commandLine;
150     QString m_audioFile;
151     unsigned int m_volume{75};
152 
153     bool isValid() const;
154     QString validate() const;
155     QString name() const;
156 
157   private:
158     QString nameForExternalProgram() const;
159     QString nameForPlayAudioFile() const;
160   };
161 
162   using RunProgramConfigPtr  = std::shared_ptr<RunProgramConfig>;
163   using RunProgramConfigList = QList<RunProgramConfigPtr>;
164 
165   QStringList m_languageShortcuts;
166   bool m_useLegacyFontMIMETypes;
167   mtx::bcp47::language_c m_defaultAudioTrackLanguage, m_defaultVideoTrackLanguage, m_defaultSubtitleTrackLanguage, m_defaultChapterLanguage;
168   SetDefaultLanguagePolicy m_whenToSetDefaultLanguage;
169   DeriveLanguageFromFileNamePolicy m_deriveAudioTrackLanguageFromFileNamePolicy, m_deriveVideoTrackLanguageFromFileNamePolicy, m_deriveSubtitleTrackLanguageFromFileNamePolicy;
170   QString m_boundaryCharsForDerivingTrackLanguagesFromFileNames;
171   QStringList m_recognizedTrackLanguagesInFileNames, m_mergePredefinedSplitSizes, m_mergePredefinedSplitDurations;
172   QStringList m_mergePredefinedVideoTrackNames, m_mergePredefinedAudioTrackNames, m_mergePredefinedSubtitleTrackNames;
173   QString m_chapterNameTemplate, m_ceTextFileCharacterSet, m_defaultSubtitleCharset, m_defaultAdditionalMergeOptions;
174   QStringList m_oftenUsedLanguages, m_oftenUsedRegions, m_oftenUsedCharacterSets;
175   bool m_oftenUsedLanguagesOnly, m_oftenUsedRegionsOnly, m_oftenUsedCharacterSetsOnly;
176   bool m_useISO639_3Languages;
177   ProcessPriority m_priority;
178   double m_probeRangePercentage;
179   QTabWidget::TabPosition m_tabPosition;
180   bool m_elideTabHeaderLabels;
181   QDir m_lastOpenDir, m_lastOutputDir, m_lastConfigDir;
182   bool m_setAudioDelayFromFileName, m_autoSetFileTitle, m_autoClearFileTitle, m_disableCompressionForAllTrackTypes, m_disableDefaultTrackForSubtitles, m_mergeAlwaysShowOutputFileControls, m_dropLastChapterFromBlurayPlaylist;
183   bool m_mergeEnableDialogNormGainRemoval, m_mergeAddBlurayCovers, m_mergeAttachmentsAlwaysSkipForExistingName;
184   ClearMergeSettingsAction m_clearMergeSettings;
185   MergeAddingAppendingFilesPolicy m_mergeDragAndDropFilesPolicy, m_mergeLastDragAndDropFilesDecision, m_mergeAddingAppendingFilesPolicy, m_mergeLastAddingAppendingDecision;
186   bool m_mergeAlwaysCreateNewSettingsForVideoFiles, m_mergeSortFilesTracksByTypeWhenAdding, m_mergeReconstructSequencesWhenAdding;
187   HeaderEditorDroppedFilesPolicy m_headerEditorDroppedFilesPolicy;
188   bool m_headerEditorDateTimeInUTC;
189   TrackPropertiesLayout m_mergeTrackPropertiesLayout;
190   MergeMissingAudioTrackPolicy m_mergeWarnMissingAudioTrack;
191   RecentlyUsedStrings m_mergeLastRelativeOutputDirs{10}, m_mergeLastFixedOutputDirs{10}, m_mergeLastOutputDirs{10};
192   bool m_mergeUseFileAndTrackColors;
193   QVector<QColor> m_mergeFileColors;
194 
195   OutputFileNamePolicy m_outputFileNamePolicy;
196   bool m_autoDestinationOnlyForVideoFiles, m_mergeSetDestinationFromTitle;
197   QDir m_relativeOutputDir, m_fixedOutputDir;
198   bool m_uniqueOutputFileNames, m_autoClearOutputFileName;
199 
200   ScanForPlaylistsPolicy m_scanForPlaylistsPolicy;
201   unsigned int m_minimumPlaylistDuration;
202 
203   JobRemovalPolicy m_jobRemovalPolicy, m_jobRemovalOnExitPolicy;
204   unsigned int m_maximumConcurrentJobs;
205   bool m_removeOldJobs;
206   int m_removeOldJobsDays;
207   bool m_useDefaultJobDescription, m_showOutputOfAllJobs, m_switchToJobOutputAfterStarting, m_resetJobWarningErrorCountersOnExit;
208   bool m_removeOutputFileOnJobFailure;
209 
210   bool m_uiDisableHighDPIScaling, m_uiDisableDarkStyleSheet, m_uiDisableToolTips;
211   bool m_checkForUpdates;
212   QDateTime m_lastUpdateCheck;
213 
214   bool m_showToolSelector, m_warnBeforeClosingModifiedTabs, m_warnBeforeAbortingJobs, m_warnBeforeOverwriting, m_showMoveUpDownButtons;
215   QString m_uiLocale, m_uiFontFamily;
216   int m_uiFontPointSize;
217   bool m_uiStayOnTop;
218 
219   bool m_enableMuxingTracksByLanguage, m_enableMuxingAllVideoTracks, m_enableMuxingAllAudioTracks, m_enableMuxingAllSubtitleTracks;
220   QStringList m_enableMuxingTracksByTheseLanguages;
221   QList<Merge::TrackType> m_enableMuxingTracksByTheseTypes;
222 
223   QHash<QString, QList<int> > m_splitterSizes;
224 
225   QString m_mediaInfoExe;
226 
227   Info::JobSettings m_defaultInfoJobSettings;
228   RunProgramConfigList m_runProgramConfigurations;
229 
230   BCP47LanguageEditingMode m_bcp47LanguageEditingMode;
231 
232   bool m_showDebuggingMenu;
233 
234 public:
235   Settings();
236   void load();
237   void save() const;
238 
239   QString priorityAsString() const;
240   QString actualMkvmergeExe() const;
241 
242   void setValue(QString const &group, QString const &key, QVariant const &value);
243   QVariant value(QString const &group, QString const &key, QVariant const &defaultValue = QVariant{}) const;
244 
245   void handleSplitterSizes(QSplitter *splitter);
246   void restoreSplitterSizes(QSplitter *splitter);
247 
248   QString localeToUse(QString const &requestedLocale = {}) const;
249 
250   QString lastOpenDirPath() const;
251   QString lastConfigDirPath() const;
252 
253   QColor nthFileColor(int idx) const;
254 
255 public Q_SLOTS:
256   void storeSplitterSizes();
257 
258 protected:
259   void loadDefaults(QSettings &reg);
260   void loadDerivingTrackLanguagesSettings(QSettings &reg);
261   void loadSplitterSizes(QSettings &reg);
262   void loadDefaultInfoJobSettings(QSettings &reg);
263   void loadRunProgramConfigurations(QSettings &reg);
264   void loadFileColors(QSettings &reg);
265 
266   void saveDefaults(QSettings &reg) const;
267   void saveDerivingTrackLanguagesSettings(QSettings &reg) const;
268   void saveSplitterSizes(QSettings &reg) const;
269   void saveDefaultInfoJobSettings(QSettings &reg) const;
270   void saveRunProgramConfigurations(QSettings &reg) const;
271   void saveFileColors(QSettings &reg) const;
272 
273   void addDefaultRunProgramConfigurations(QSettings &reg);
274   void addDefaultRunProgramConfigurationForType(QSettings &reg, RunProgramType type, std::function<void(RunProgramConfig &)> const &modifier = nullptr);
275   bool fixDefaultAudioFileNameBug();
276 
277   void setDefaults(std::optional<QVariant> enableMuxingTracksByTheseTypes);
278 
279 protected:
280   static Settings s_settings;
281 
282   static void withGroup(QString const &group, std::function<void(QSettings &)> worker);
283 
284 public:
285   static Settings &get();
286   static void change(std::function<void(Settings &)> worker);
287   static std::unique_ptr<QSettings> registry();
288 
289   static void runOncePerVersion(QString const &topic, std::function<void()> worker);
290 
291   static QString exeWithPath(QString const &exe);
292   static QString determineMediaInfoExePath();
293 
294   static void migrateFromRegistry();
295   static void convertOldSettings();
296 
297   static QString iniFileLocation();
298   static QString iniFileName();
299 
300   static QString cacheDirLocation(QString const &subDir);
301   static QString prepareCacheDir(QString const &subDir);
302 
303   static QString defaultBoundaryCharsForDerivingLanguageFromFileName();
304 
305   static QVector<QColor> defaultFileColors();
306 };
307 
308 }
309 
310 Q_DECLARE_OPERATORS_FOR_FLAGS(mtx::gui::Util::Settings::RunProgramForEvents)
311