File indexing completed on 2024-04-21 05:46:50

0001 /*
0002     SPDX-FileCopyrightText: 2008,2011 Volker Lanz <vl@fidra.de>
0003     SPDX-FileCopyrightText: 2014-2020 Andrius Štikonas <andrius@stikonas.eu>
0004     SPDX-FileCopyrightText: 2019 Caio Jordão Carvalho <caiojcarvalho@gmail.com>
0005 
0006     SPDX-License-Identifier: GPL-3.0-or-later
0007 */
0008 
0009 #include "gui/mainwindow.h"
0010 
0011 #include <backend/corebackend.h>
0012 #include <backend/corebackendmanager.h>
0013 
0014 #include <util/helpers.h>
0015 #include "util/guihelpers.h"
0016 
0017 #include <QApplication>
0018 #include <QCommandLineParser>
0019 #include <QCommandLineOption>
0020 
0021 #include <KAboutData>
0022 #include <KCrash>
0023 #include <KDBusService>
0024 #include <KMessageBox>
0025 #include <KLocalizedString>
0026 #include <KWindowSystem>
0027 
0028 #include <config.h>
0029 #include <utility>
0030 
0031 std::pair<QString, QString> parseDevice(const QString& selectedDevice)
0032 {
0033     QString device;
0034     QString partitionNr;
0035     if (selectedDevice.length()) {
0036         const bool isNvme = selectedDevice.startsWith(QStringLiteral("nvme"));
0037         // calculate the number of numbers on the last part of the info.
0038         int numSize = 0;
0039         for (int i = selectedDevice.length() - 1; i > 0; i--) {
0040             if (selectedDevice[i].isNumber()) {
0041                 numSize += 1;
0042             } else {
0043                 if (isNvme) {
0044                     // the entry is just an nvme without a partition.
0045                     if (selectedDevice[i] != QLatin1Char('p')) {
0046                         numSize = 0;
0047                         break;
0048                     }
0049                 }
0050                 break;
0051             }
0052         }
0053         partitionNr = selectedDevice.mid(selectedDevice.length() - numSize);
0054 
0055         if (numSize != 0) {
0056             // nvme has a `p` that also needs to be removed.
0057             if (isNvme) {
0058                 numSize += 1;
0059             }
0060 
0061             device = selectedDevice.mid(0, selectedDevice.length() - numSize);
0062         } else {
0063             device = selectedDevice;
0064         }
0065     }
0066     return {device, partitionNr};
0067 }
0068 
0069 int Q_DECL_IMPORT main(int argc, char* argv[])
0070 {
0071     QApplication app(argc, argv);
0072     app.setWindowIcon(QIcon::fromTheme(QStringLiteral("partitionmanager")));
0073 
0074     KLocalizedString::setApplicationDomain("partitionmanager");
0075     KAboutData aboutData (
0076         QStringLiteral("partitionmanager"),
0077         xi18nc("@title", "<application>KDE Partition Manager</application>"),
0078         QStringLiteral(VERSION),
0079         xi18nc("@title", "Manage your disks, partitions and file systems"),
0080         KAboutLicense::GPL_V3,
0081         xi18nc("@info:credit", "© 2008-2013 Volker Lanz\n© 2012-2020 Andrius Štikonas"));
0082     aboutData.setOrganizationDomain(QByteArray("kde.org"));
0083     aboutData.setProductName(QByteArray("partitionmanager"));
0084 
0085     aboutData.addAuthor(xi18nc("@info:credit", "Volker Lanz"), xi18nc("@info:credit", "Former maintainer"));
0086     aboutData.addAuthor(xi18nc("@info:credit", "Andrius Štikonas"), xi18nc("@info:credit", "Maintainer"), QStringLiteral("andrius@stikonas.eu"));
0087     aboutData.addCredit(xi18nc("@info:credit", "Teo Mrnjavac"), i18nc("@info:credit", "Former Calamares maintainer"), QStringLiteral("teo@kde.org"));
0088     aboutData.addCredit(xi18nc("@info:credit", "Chantara Tith"), i18nc("@info:credit", "LVM support"), QStringLiteral("tith.chantara@gmail.com"));
0089     aboutData.addCredit(xi18nc("@info:credit", "Pali Rohár"), i18nc("@info:credit", "UDF support"), QStringLiteral("pali.rohar@gmail.com"));
0090     aboutData.addCredit(i18n("Hugo Pereira Da Costa"), xi18nc("@info:credit", "Partition Widget Design"), QStringLiteral("hugo@oxygen-icons.org"));
0091     aboutData.addCredit(xi18nc("@info:credit", "Caio Jordão Carvalho"), i18nc("@info:credit", "Improved SMART support"), QStringLiteral("caiojcarvalho@gmail.com"));
0092 
0093     aboutData.setHomepage(QStringLiteral("https://www.kde.org/applications/system/partitionmanager"));
0094 
0095     KAboutData::setApplicationData(aboutData);
0096     KCrash::initialize();
0097 
0098     QCommandLineParser parser;
0099     aboutData.setupCommandLine(&parser);
0100 
0101     QCommandLineOption deviceOption({QStringLiteral("device")}, xi18nc("@info:shell", "Device to manage"), QStringLiteral("device"));
0102     parser.addOption(deviceOption);
0103 
0104     parser.process(app);
0105     aboutData.processCommandLine(&parser);
0106 
0107     KDBusService service(KDBusService::Unique);
0108 
0109     registerMetaTypes();
0110 
0111     Config::instance(QStringLiteral("partitionmanagerrc"));
0112 
0113     if (!loadBackend())
0114         return 0;
0115 
0116     // device is the selected device minus the partition number.
0117     // we need all of them to select properly the things on screen.
0118 
0119     QString selectedDevice = parser.value(deviceOption);
0120     if (selectedDevice.startsWith(QStringLiteral("/dev/"))) {
0121         selectedDevice.remove(QStringLiteral("/dev/"));
0122     }
0123     auto [device, partitionNr] = parseDevice(selectedDevice);
0124 
0125     MainWindow* mainWindow = new MainWindow();
0126     QObject::connect(mainWindow, &MainWindow::scanFinished, mainWindow, [mainWindow, device = device, selectedDevice, partitionNr = partitionNr] {
0127         if (device.length()) {
0128             mainWindow->setCurrentDeviceByName(device);
0129             if (partitionNr.length()) {
0130                 mainWindow->setCurrentPartitionByName(selectedDevice);
0131             }
0132             mainWindow->setDisallowOtherDevices();
0133         } else {
0134             mainWindow->showDevicePanelIfPreviouslyHiddenByDisallowOtherDevices();
0135         }
0136     });
0137 
0138     QObject::connect(&service, &KDBusService::activateRequested, mainWindow, [mainWindow](const QStringList &/*args*/, const QString &/*workingDir*/) {
0139         KWindowSystem::updateStartupId(mainWindow->windowHandle());
0140         KWindowSystem::activateWindow(mainWindow->windowHandle());
0141     });
0142 
0143     return app.exec();
0144 }