@@ -577,27 +577,37 @@ bool SettingsWidget::enableChaoticAurInPacmanConf() {
577577
578578 QStringList lines;
579579 QTextStream in (&file);
580- bool chaoticAurSectionFound = false ;
581- bool chaoticAurExists = false ;
580+ bool inCommentedChaoticAurSection = false ;
581+ bool chaoticAurSectionExists = false ;
582582
583583 while (!in.atEnd ()) {
584584 QString line = in.readLine ();
585+ QString trimmedLine = line.trimmed ();
585586
586- // Check if chaotic-aur section already exists (uncommented)
587- if (line.trimmed () == " [chaotic-aur]" ) {
588- chaoticAurExists = true ;
589- }
590-
591- // Check if this is a commented [chaotic-aur] section
592- if (line.trimmed () == " #[chaotic-aur]" ) {
593- lines.append (" [chaotic-aur]" );
594- chaoticAurSectionFound = true ;
587+ // Check if chaotic-aur section already exists (uncommented or commented)
588+ if (trimmedLine == " [chaotic-aur]" || trimmedLine == " #[chaotic-aur]" ) {
589+ chaoticAurSectionExists = true ;
590+
591+ // If it's commented, uncomment it
592+ if (trimmedLine == " #[chaotic-aur]" ) {
593+ lines.append (" [chaotic-aur]" );
594+ inCommentedChaoticAurSection = true ;
595+ } else {
596+ // Already uncommented, keep as is
597+ lines.append (line);
598+ }
595599 }
596600 // Check if the Include/Server line in chaotic-aur section is commented
597- else if (chaoticAurSectionFound && line.trimmed ().startsWith (" #" ) &&
598- (line.contains (" Include" ) || line.contains (" Server" ))) {
599- lines.append (line.mid (line.indexOf (' #' ) + 1 )); // Remove the # comment character
600- chaoticAurSectionFound = false ; // Reset flag after processing
601+ else if (inCommentedChaoticAurSection && trimmedLine.startsWith (" #" ) &&
602+ (trimmedLine.contains (" Include" ) || trimmedLine.contains (" Server" ))) {
603+ // Remove the # comment character
604+ lines.append (line.mid (line.indexOf (' #' ) + 1 ));
605+ inCommentedChaoticAurSection = false ;
606+ }
607+ // Check if we hit another section, reset flag
608+ else if (trimmedLine.startsWith (" [" ) && trimmedLine != " [chaotic-aur]" && trimmedLine != " #[chaotic-aur]" ) {
609+ lines.append (line);
610+ inCommentedChaoticAurSection = false ;
601611 }
602612 else {
603613 lines.append (line);
@@ -606,7 +616,7 @@ bool SettingsWidget::enableChaoticAurInPacmanConf() {
606616 file.close ();
607617
608618 // If chaotic-aur section doesn't exist at all, add it
609- if (!chaoticAurExists && !chaoticAurSectionFound ) {
619+ if (!chaoticAurSectionExists ) {
610620 lines.append (" " );
611621 lines.append (" [chaotic-aur]" );
612622 lines.append (" Include = /etc/pacman.d/chaotic-mirrorlist" );
@@ -657,18 +667,24 @@ bool SettingsWidget::disableChaoticAurInPacmanConf() {
657667 QString line = in.readLine ();
658668 QString trimmedLine = line.trimmed ();
659669
660- // Check if this is [chaotic-aur] section
670+ // Check if this is [chaotic-aur] section (uncommented or already commented)
661671 if (trimmedLine == " [chaotic-aur]" ) {
662672 lines.append (" #[chaotic-aur]" );
663673 inChaoticAurSection = true ;
664674 }
665- // Check if we're in chaotic-aur section and this is the Include/Server line
675+ else if (trimmedLine == " #[chaotic-aur]" ) {
676+ // Already commented, keep as is
677+ lines.append (line);
678+ inChaoticAurSection = false ;
679+ }
680+ // Check if we're in chaotic-aur section and this is the Include/Server line (not already commented)
666681 else if (inChaoticAurSection && !trimmedLine.startsWith (" #" ) &&
667682 (trimmedLine.startsWith (" Include" ) || trimmedLine.startsWith (" Server" ))) {
668683 lines.append (" #" + line);
684+ inChaoticAurSection = false ;
669685 }
670686 // Check if we hit another section
671- else if (trimmedLine.startsWith (" [" ) && trimmedLine != " [chaotic-aur]" ) {
687+ else if (trimmedLine.startsWith (" [" ) && trimmedLine != " [chaotic-aur]" && trimmedLine != " #[chaotic-aur] " ) {
672688 lines.append (line);
673689 inChaoticAurSection = false ;
674690 }
0 commit comments