Created
May 19, 2017 15:57
-
-
Save llelectronics/cec9256fe92feea1a215b797d80df6ec to your computer and use it in GitHub Desktop.
Allow reading in many sections in one file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| diff --git a/src/Module.cpp b/src/Module.cpp | |
| index a7c3aea..1825cba 100644 | |
| --- a/src/Module.cpp | |
| +++ b/src/Module.cpp | |
| @@ -137,13 +137,11 @@ void Module::Private::populateSources() | |
| for(QString const& path : paths) { | |
| // One hard-coded channel, and one for the distribution we're actually running on | |
| QStringList channelTypes = QStringList() << QString("%1/release-channels/channels/general-use").arg(path) << QString("%1/release-channels/channels/%2").arg(path).arg(os.id); | |
| - | |
| + | |
| for(QString const &channelsPath : channelTypes) { | |
| QDir channels(channelsPath); | |
| if(channels.exists()) { | |
| for(QString const& channel : channels.entryList(QDir::Files)) { | |
| - QCheckBox *checkbox = new QCheckBox(channel); | |
| - q->ui->verticalLayout->addWidget(checkbox); | |
| QFile file(QString("%1/%2").arg(channelsPath).arg(channel)); | |
| QString contents; | |
| if(file.open(QIODevice::ReadOnly)) { | |
| @@ -151,39 +149,51 @@ void Module::Private::populateSources() | |
| file.close(); | |
| } | |
| QStringList lines = contents.split("\n"); | |
| - // Is the first line a comment? Use that as the title | |
| - if(lines[0].startsWith("#")) { | |
| - checkbox->setText(lines[0].mid(1).trimmed()); | |
| - } | |
| - // How about the second line? That'll be our description | |
| - if(lines.length() > 1 && lines[1].startsWith("#")) { | |
| - QLabel* descLabel = new QLabel(lines[1].mid(1).trimmed()); | |
| - descLabel->setWordWrap(true); | |
| - q->ui->verticalLayout->addWidget(descLabel); | |
| - } | |
| - checkbox->setProperty("currentState", Qt::Unchecked); | |
| - // if file exists in /etc/apt/sources.lists.d/... | |
| - if(sldEntries.contains(channel)) { | |
| - // and is identical to our file, check box... | |
| - if(contents == sldEntries[channel]) { | |
| - checkbox->setCheckState(Qt::Checked); | |
| - checkbox->setProperty("currentState", Qt::Checked); | |
| - } | |
| - // and is different from our file, disable, describe error | |
| - // nb: this also ensures we can handle two channels with the same .lists filename... just don't | |
| - // enable the one that would otherwise replace what is already there. Needs saying in the UI somehow. | |
| - else { | |
| - checkbox->setEnabled(false); | |
| - checkbox->setToolTip(i18nc("Checkbox tool tip which shows when the contents differ between the channel's .lists file and the .lists file with the same name in apt's soources.lists.d", "This entry cannot be enabled, as a channel with this filename already exists, but the contents differ.")); | |
| - QLabel *label = new QLabel(QString("The contents of the files %1 and %2/%3 differ - you will have to manually remove %2/%3 to be able to select this channel.").arg(file.fileName()).arg(sldDir).arg(channel)); // this is a placeholder, hence no i18n... | |
| - label->setWordWrap(true); | |
| - q->ui->verticalLayout->addWidget(label); | |
| - // TODO Make it possible to see the difference between the two files, and manually delete the | |
| - // other (if it's not represented by another channel...) - this will need thorough thought. | |
| + | |
| + bool skipCheckboxCreation = false; | |
| + for (int i=0;i<lines.length(); i++) { | |
| + // Is the first line a comment? Use that as the title | |
| + if(lines[i].startsWith("#") && !lines[i].startsWith("# deb") && !skipCheckboxCreation && (lines[i+1].startsWith("#") || lines[i+1].startsWith("# deb") || lines[i+1].startsWith("deb")) ) { | |
| + QCheckBox *checkbox = new QCheckBox(lines[i]); | |
| + q->ui->verticalLayout->addWidget(checkbox); | |
| + checkbox->setText(lines[i].mid(1).trimmed()); | |
| + | |
| + //How about the second line? That'll be our description | |
| + if(lines.length() > 1 && lines[i+1].startsWith("#") && !lines[i+1].startsWith("# deb")) { | |
| + QLabel* descLabel = new QLabel(lines[i+1].mid(1).trimmed()); | |
| + descLabel->setWordWrap(true); | |
| + q->ui->verticalLayout->addWidget(descLabel); | |
| + skipCheckboxCreation = true; | |
| + } | |
| + else { | |
| + skipCheckboxCreation = false; | |
| + } | |
| + | |
| + checkbox->setProperty("currentState", Qt::Unchecked); | |
| + // if file exists in /etc/apt/sources.lists.d/... | |
| + if(sldEntries.contains(channel)) { | |
| + // and is identical to our file, check box... | |
| + if(contents == sldEntries[channel]) { | |
| + checkbox->setCheckState(Qt::Checked); | |
| + checkbox->setProperty("currentState", Qt::Checked); | |
| + } | |
| + // and is different from our file, disable, describe error | |
| + // nb: this also ensures we can handle two channels with the same .lists filename... just don't | |
| + // enable the one that would otherwise replace what is already there. Needs saying in the UI somehow. | |
| + else { | |
| + checkbox->setEnabled(false); | |
| + checkbox->setToolTip(i18nc("Checkbox tool tip which shows when the contents differ between the channel's .lists file and the .lists file with the same name in apt's soources.lists.d", "This entry cannot be enabled, as a channel with this filename already exists, but the contents differ.")); | |
| + QLabel *label = new QLabel(QString("The contents of the files %1 and %2/%3 differ - you will have to manually remove %2/%3 to be able to select this channel.").arg(file.fileName()).arg(sldDir).arg(channel)); // this is a placeholder, hence no i18n... | |
| + label->setWordWrap(true); | |
| + q->ui->verticalLayout->addWidget(label); | |
| + // TODO Make it possible to see the difference between the two files, and manually delete the | |
| + // other (if it's not represented by another channel...) - this will need thorough thought. | |
| + } | |
| + } | |
| + checkbox->setProperty("channelFile", QString("%1/%2").arg(channelsPath).arg(channel)); | |
| + q->connect(checkbox, &QCheckBox::stateChanged, [this](){checkCheckStates();}); | |
| } | |
| } | |
| - checkbox->setProperty("channelFile", QString("%1/%2").arg(channelsPath).arg(channel)); | |
| - q->connect(checkbox, &QCheckBox::stateChanged, [this](){checkCheckStates();}); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment