1#include "GitQlientSettings.h"
2
3#include <QVector>
4
5QString GitQlientSettings::PinnedRepos = "Config/PinnedRepos";
6QString GitQlientSettings::SplitFileDiffView = "SplitDiff";
7
8GitQlientSettings::GitQlientSettings(const QString &gitRepoPath)
9 : mGitRepoPath(gitRepoPath)
10{
11}
12
13void GitQlientSettings::setGlobalValue(const QString &key, const QVariant &value)
14{
15 globalSettings.setValue(key, value);
16 globalSettings.sync();
17}
18
19QVariant GitQlientSettings::globalValue(const QString &key, const QVariant &defaultValue)
20{
21 return globalSettings.value(key, defaultValue);
22}
23
24void GitQlientSettings::setLocalValue(const QString &key, const QVariant &value)
25{
26 QSettings localSettings(mGitRepoPath + "/GitQlientConfig.ini", QSettings::IniFormat);
27 localSettings.setValue(key, value);
28 localSettings.sync();
29}
30
31QVariant GitQlientSettings::localValue(const QString &key, const QVariant &defaultValue)
32{
33 QSettings localSettings(mGitRepoPath + "/GitQlientConfig.ini", QSettings::IniFormat);
34 return localSettings.value(key, defaultValue);
35}
36
37void GitQlientSettings::setProjectOpened(const QString &projectPath)
38{
39 saveMostUsedProjects(projectPath);
40
41 saveRecentProjects(projectPath);
42}
43
44QStringList GitQlientSettings::getRecentProjects() const
45{
46 auto projects = globalSettings.value("Config/RecentProjects", QStringList()).toStringList();
47
48 QStringList recentProjects;
49 const auto end = std::min(projects.count(), 5);
50
51 for (auto i = 0; i < end; ++i)
52 recentProjects.append(projects.takeFirst());
53
54 return recentProjects;
55}
56
57void GitQlientSettings::saveRecentProjects(const QString &projectPath)
58{
59 auto usedProjects = globalSettings.value("Config/RecentProjects", QStringList()).toStringList();
60
61 if (usedProjects.contains(projectPath))
62 {
63 const auto index = usedProjects.indexOf(projectPath);
64 usedProjects.takeAt(index);
65 }
66
67 usedProjects.prepend(projectPath);
68
69 while (!usedProjects.isEmpty() && usedProjects.count() > 5)
70 usedProjects.removeLast();
71
72 GitQlientSettings::setGlobalValue("Config/RecentProjects", usedProjects);
73}
74
75void GitQlientSettings::clearRecentProjects()
76{
77 globalSettings.remove("Config/RecentProjects");
78}
79
80void GitQlientSettings::saveMostUsedProjects(const QString &projectPath)
81{
82 auto projects = globalSettings.value("Config/UsedProjects", QStringList()).toStringList();
83 auto timesUsed = globalSettings.value("Config/UsedProjectsCount", QList<QVariant>()).toList();
84
85 if (projects.contains(projectPath))
86 {
87 const auto index = projects.indexOf(projectPath);
88 timesUsed[index] = QString::number(timesUsed[index].toInt() + 1);
89 }
90 else
91 {
92 projects.append(projectPath);
93 timesUsed.append(1);
94 }
95
96 GitQlientSettings::setGlobalValue("Config/UsedProjects", projects);
97 GitQlientSettings::setGlobalValue("Config/UsedProjectsCount", timesUsed);
98}
99
100void GitQlientSettings::clearMostUsedProjects()
101{
102 globalSettings.remove("Config/UsedProjects");
103 globalSettings.remove("Config/UsedProjectsCount");
104}
105
106QStringList GitQlientSettings::getMostUsedProjects() const
107{
108 const auto projects = globalSettings.value("Config/UsedProjects", QStringList()).toStringList();
109 const auto timesUsed = globalSettings.value("Config/UsedProjectsCount", QString()).toList();
110
111 QMultiMap<int, QString> projectOrderedByUse;
112
113 const auto projectsCount = projects.count();
114 const auto timesCount = timesUsed.count();
115
116 for (auto i = 0; i < projectsCount && i < timesCount; ++i)
117 projectOrderedByUse.insert(timesUsed.at(i).toInt(), projects.at(i));
118
119 QStringList recentProjects;
120 const auto end = std::min(projectOrderedByUse.count(), 5);
121 const auto orderedProjects = projectOrderedByUse.values();
122
123 for (auto i = 0; i < end; ++i)
124 recentProjects.append(orderedProjects.at(orderedProjects.count() - 1 - i));
125
126 return recentProjects;
127}
128