1 /*
2 Copyright (c) 2020, Lukas Holecek <hluk@email.cz>
3
4 This file is part of CopyQ.
5
6 CopyQ is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 CopyQ is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with CopyQ. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "tabs.h"
21
22 #include <QRegularExpression>
23 #include <QSettings>
24
25 constexpr auto settingsGroupTabs = "Tabs";
26 constexpr auto optionTabName = "name";
27 constexpr auto optionIconName = "icon";
28 constexpr auto optionMaxItemCount = "max_item_count";
29 constexpr auto optionStoreItems = "store_items";
30
31 struct Tabs::PrivateData {
32 QHash<QString, TabProperties> tabs;
33 };
34
Tabs()35 Tabs::Tabs()
36 : m_data(new PrivateData())
37 {
38 QSettings settings;
39 const int size = settings.beginReadArray(settingsGroupTabs);
40 for(int i = 0; i < size; ++i) {
41 settings.setArrayIndex(i);
42
43 TabProperties properties;
44 properties.name = settings.value(optionTabName).toString();
45 properties.iconName = settings.value(optionIconName).toString();
46 properties.storeItems = settings.value(optionStoreItems, true).toBool();
47
48 bool ok;
49 const int maxItemCount = settings.value(optionMaxItemCount).toInt(&ok);
50 if (ok)
51 properties.maxItemCount = maxItemCount;
52
53 setTabProperties(properties);
54 }
55 }
56
57 Tabs::~Tabs() = default;
58
Tabs(const Tabs & other)59 Tabs::Tabs(const Tabs &other)
60 : m_data(new PrivateData())
61 {
62 m_data->tabs = other.m_data->tabs;
63 }
64
operator =(const Tabs & other)65 Tabs &Tabs::operator=(const Tabs &other)
66 {
67 m_data->tabs = other.m_data->tabs;
68 return *this;
69 }
70
setTabProperties(const TabProperties & tabProperties)71 void Tabs::setTabProperties(const TabProperties &tabProperties)
72 {
73 if ( tabProperties.name.isEmpty() )
74 return;
75
76 m_data->tabs[tabProperties.name] = tabProperties;
77 }
78
save(QSettings * settings,const QStringList & tabs)79 void Tabs::save(QSettings *settings, const QStringList &tabs)
80 {
81 settings->beginWriteArray(settingsGroupTabs, m_data->tabs.size());
82
83 int row = 0;
84 for (auto it = m_data->tabs.constBegin(); it != m_data->tabs.constEnd(); ++it) {
85 const auto &name = it.key();
86 const auto &tab = it.value();
87
88 const bool isTab = tabs.contains(name);
89 const bool isTabGroup =
90 !isTab && tabs.indexOf(QRegularExpression(QString::fromLatin1("^%1/.*").arg(QRegularExpression::escape(name)))) != -1;
91
92 if (isTab || isTabGroup) {
93 settings->setArrayIndex(row++);
94 settings->setValue(optionTabName, name);
95 settings->setValue(optionIconName, tab.iconName);
96 if (isTab) {
97 settings->setValue(optionMaxItemCount, tab.maxItemCount);
98 settings->setValue(optionStoreItems, tab.storeItems);
99 }
100 }
101 }
102
103 settings->endArray();
104 }
105
tabProperties(const QString & name) const106 TabProperties Tabs::tabProperties(const QString &name) const
107 {
108 TabProperties tab = m_data->tabs.value(name);
109 tab.name = name;
110 return tab;
111 }
112