| 103 | |
| 104 | namespace KDevelop { |
| 105 | bool askUser(const QString& mainText, |
| 106 | const QString& ttyPrompt, |
| 107 | const QString& mboxTitle, |
| 108 | const QString& mboxAdditionalText, |
| 109 | const QString& confirmText, |
| 110 | const QString& rejectText, |
| 111 | bool ttyDefaultToYes) |
| 112 | { |
| 113 | if (!qobject_cast<QGuiApplication*>(QCoreApplication::instance())) { |
| 114 | // no ui-mode e.g. for duchainify and other tools |
| 115 | QTextStream out(stdout); |
| 116 | out << mainText << Qt::endl; |
| 117 | QTextStream in(stdin); |
| 118 | QString input; |
| 119 | while (true) { |
| 120 | if (ttyDefaultToYes) { |
| 121 | out << ttyPrompt << QLatin1String(": [Y/n] ") << Qt::flush; |
| 122 | } else { |
| 123 | out << ttyPrompt << QLatin1String(": [y/N] ") << Qt::flush; |
| 124 | } |
| 125 | input = in.readLine().trimmed(); |
| 126 | if (input.isEmpty()) { |
| 127 | return ttyDefaultToYes; |
| 128 | } else if (input.toLower() == QLatin1String("y")) { |
| 129 | return true; |
| 130 | } else if (input.toLower() == QLatin1String("n")) { |
| 131 | return false; |
| 132 | } |
| 133 | } |
| 134 | } else { |
| 135 | auto okButton = KStandardGuiItem::ok(); |
| 136 | okButton.setText(confirmText); |
| 137 | auto rejectButton = KStandardGuiItem::cancel(); |
| 138 | rejectButton.setText(rejectText); |
| 139 | |
| 140 | auto* parent = ICore::self()->uiController()->activeMainWindow(); |
| 141 | if (parent && !parent->isVisible()) { |
| 142 | // On Wayland, a dialog with an invisible or null parent appears |
| 143 | // at the top-left corner of the primary screen. |
| 144 | // On X11, a dialog with an invisible parent appears close to the top-left corner of the |
| 145 | // left screen, even if the left screen is secondary. A dialog with a null parent appears |
| 146 | // at the center of the primary screen. The center of the primary screen is a much more |
| 147 | // convenient location, especially if the secondary screen is off. |
| 148 | // KMessageBox::questionTwoActions() destroys its dialog before returning in any case. |
| 149 | qCDebug(UTIL) << "the main window is invisible, so not using it as a dialog parent"; |
| 150 | parent = nullptr; |
| 151 | } |
| 152 | |
| 153 | const auto userAnswer = KMessageBox::questionTwoActions( |
| 154 | parent, mainText + QLatin1String("\n\n") + mboxAdditionalText, mboxTitle, okButton, rejectButton); |
| 155 | return userAnswer == KMessageBox::PrimaryAction; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | bool ensureWritable(const QList<QUrl>& urls) |
| 160 | { |
no test coverage detected