File indexing completed on 2024-05-12 17:10:21

0001 /*
0002     SPDX-FileCopyrightText: 2023 Fushan Wen <qydwhotmail@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include <cstdlib>
0008 #include <iostream>
0009 #include <optional>
0010 
0011 #include <KConfigGroup>
0012 #include <KSharedConfig>
0013 
0014 /**
0015  * plasmashellrc have 3 thickness values in "Default" "Horizontal" "Vertical"
0016  * groups. Only the value in the default group is preserved to avoid desync
0017  * between horizontal and vertical panels.
0018  *
0019  * @see BUG 460006
0020  * @since 5.27
0021  */
0022 int main()
0023 {
0024     const KSharedConfigPtr configPtr = KSharedConfig::openConfig(QString::fromLatin1("plasmashellrc"), KConfig::FullConfig);
0025     KConfigGroup views(configPtr, QStringLiteral("PlasmaViews"));
0026     if (!views.exists() || views.groupList().empty()) {
0027         std::cout << "plasmashellrc doesn't have any PlasmaViews. No need to update config." << std::endl;
0028         return EXIT_SUCCESS;
0029     }
0030 
0031     // Find config groups like [PlasmaViews][Panel 114][Horizontal1536]
0032     // or [PlasmaViews][Panel 115][Vertical864]
0033     unsigned count = 0;
0034     const QStringList groupList = views.groupList();
0035     for (const QString &name : groupList) {
0036         if (!name.startsWith(QLatin1String("Panel"))) {
0037             continue;
0038         }
0039 
0040         KConfigGroup panelConfigGroup(&views, name);
0041         const QStringList groupList = panelConfigGroup.groupList();
0042         std::optional<unsigned> thickness;
0043         for (const QString &name : groupList) {
0044             if (!name.startsWith(QLatin1String("Horizontal")) && !name.startsWith(QLatin1String("Vertical"))) {
0045                 continue;
0046             }
0047 
0048             KConfigGroup panelFormConfigGroup(&panelConfigGroup, name);
0049             if (!panelFormConfigGroup.hasKey("thickness")) {
0050                 continue;
0051             }
0052 
0053             const unsigned valueInGroup = panelFormConfigGroup.readEntry("thickness", unsigned(0));
0054             if (!thickness.has_value() || thickness.value() > valueInGroup) {
0055                 thickness = valueInGroup; // Use the minimum thickness value
0056             }
0057 
0058             panelFormConfigGroup.deleteEntry("thickness");
0059             ++count;
0060             // Should have at least one default group so no need to delete the whole panel group
0061             if (panelFormConfigGroup.keyList().empty()) {
0062                 panelConfigGroup.deleteGroup(name);
0063             }
0064         }
0065 
0066         // Update the thickness in [Defaults] group
0067         if (thickness.has_value()) {
0068             KConfigGroup defaultConfigGroup(&panelConfigGroup, "Defaults");
0069             defaultConfigGroup.writeEntry("thickness", thickness.value());
0070         }
0071     }
0072 
0073     if (count == 0) {
0074         std::cout << "No need to update config." << std::endl;
0075     } else {
0076         configPtr->sync();
0077     }
0078 
0079     return EXIT_SUCCESS;
0080 }