PageRenderTime 50ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/src/plugins/coreplugin/mimetypemagicdialog.cpp

https://github.com/choenig/qt-creator
C++ | 163 lines | 114 code | 16 blank | 33 comment | 15 complexity | 817f60e40507bbbfe0c56632998b2040 MD5 | raw file
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2016 The Qt Company Ltd.
  4. ** Contact: https://www.qt.io/licensing/
  5. **
  6. ** This file is part of Qt Creator.
  7. **
  8. ** Commercial License Usage
  9. ** Licensees holding valid commercial Qt licenses may use this file in
  10. ** accordance with the commercial license agreement provided with the
  11. ** Software or, alternatively, in accordance with the terms contained in
  12. ** a written agreement between you and The Qt Company. For licensing terms
  13. ** and conditions see https://www.qt.io/terms-conditions. For further
  14. ** information use the contact form at https://www.qt.io/contact-us.
  15. **
  16. ** GNU General Public License Usage
  17. ** Alternatively, this file may be used under the terms of the GNU
  18. ** General Public License version 3 as published by the Free Software
  19. ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
  20. ** included in the packaging of this file. Please review the following
  21. ** information to ensure the GNU General Public License requirements will
  22. ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
  23. **
  24. ****************************************************************************/
  25. #include "mimetypemagicdialog.h"
  26. #include "icore.h"
  27. #include <utils/headerviewstretcher.h>
  28. #include <utils/qtcassert.h>
  29. #include <QDesktopServices>
  30. #include <QMessageBox>
  31. #include <QUrl>
  32. using namespace Core;
  33. using namespace Internal;
  34. static Utils::Internal::MimeMagicRule::Type typeValue(int i)
  35. {
  36. QTC_ASSERT(i < Utils::Internal::MimeMagicRule::Byte,
  37. return Utils::Internal::MimeMagicRule::Invalid);
  38. return Utils::Internal::MimeMagicRule::Type(i + 1/*0==invalid*/);
  39. }
  40. MimeTypeMagicDialog::MimeTypeMagicDialog(QWidget *parent) :
  41. QDialog(parent)
  42. {
  43. ui.setupUi(this);
  44. setWindowTitle(tr("Add Magic Header"));
  45. connect(ui.useRecommendedGroupBox, &QGroupBox::toggled,
  46. this, &MimeTypeMagicDialog::applyRecommended);
  47. connect(ui.buttonBox, &QDialogButtonBox::accepted, this, &MimeTypeMagicDialog::validateAccept);
  48. connect(ui.informationLabel, &QLabel::linkActivated, this, [](const QString &link) {
  49. QDesktopServices::openUrl(QUrl(link));
  50. });
  51. connect(ui.typeSelector, QOverload<int>::of(&QComboBox::activated),
  52. this, [this]() {
  53. if (ui.useRecommendedGroupBox->isChecked())
  54. setToRecommendedValues();
  55. });
  56. ui.valueLineEdit->setFocus();
  57. }
  58. void MimeTypeMagicDialog::setToRecommendedValues()
  59. {
  60. ui.startRangeSpinBox->setValue(0);
  61. ui.endRangeSpinBox->setValue(ui.typeSelector->currentIndex() == 1/*regexp*/ ? 200 : 0);
  62. ui.prioritySpinBox->setValue(50);
  63. }
  64. void MimeTypeMagicDialog::applyRecommended(bool checked)
  65. {
  66. if (checked) {
  67. // save previous custom values
  68. m_customRangeStart = ui.startRangeSpinBox->value();
  69. m_customRangeEnd = ui.endRangeSpinBox->value();
  70. m_customPriority = ui.prioritySpinBox->value();
  71. setToRecommendedValues();
  72. } else {
  73. // restore previous custom values
  74. ui.startRangeSpinBox->setValue(m_customRangeStart);
  75. ui.endRangeSpinBox->setValue(m_customRangeEnd);
  76. ui.prioritySpinBox->setValue(m_customPriority);
  77. }
  78. ui.startRangeLabel->setEnabled(!checked);
  79. ui.startRangeSpinBox->setEnabled(!checked);
  80. ui.endRangeLabel->setEnabled(!checked);
  81. ui.endRangeSpinBox->setEnabled(!checked);
  82. ui.priorityLabel->setEnabled(!checked);
  83. ui.prioritySpinBox->setEnabled(!checked);
  84. ui.noteLabel->setEnabled(!checked);
  85. }
  86. void MimeTypeMagicDialog::validateAccept()
  87. {
  88. QString errorMessage;
  89. Utils::Internal::MimeMagicRule rule = createRule(&errorMessage);
  90. if (rule.isValid())
  91. accept();
  92. else
  93. QMessageBox::critical(ICore::dialogParent(), tr("Error"), errorMessage);
  94. }
  95. void MimeTypeMagicDialog::setMagicData(const MagicData &data)
  96. {
  97. ui.valueLineEdit->setText(QString::fromUtf8(data.m_rule.value()));
  98. ui.typeSelector->setCurrentIndex(data.m_rule.type() - 1/*0 == invalid*/);
  99. ui.maskLineEdit->setText(QString::fromLatin1(MagicData::normalizedMask(data.m_rule)));
  100. ui.useRecommendedGroupBox->setChecked(false); // resets values
  101. ui.startRangeSpinBox->setValue(data.m_rule.startPos());
  102. ui.endRangeSpinBox->setValue(data.m_rule.endPos());
  103. ui.prioritySpinBox->setValue(data.m_priority);
  104. }
  105. MagicData MimeTypeMagicDialog::magicData() const
  106. {
  107. MagicData data(createRule(), ui.prioritySpinBox->value());
  108. return data;
  109. }
  110. bool MagicData::operator==(const MagicData &other)
  111. {
  112. return m_priority == other.m_priority && m_rule == other.m_rule;
  113. }
  114. /*!
  115. Returns the mask, or an empty string if the mask is the default mask which is set by
  116. MimeMagicRule when setting an empty mask for string patterns.
  117. */
  118. QByteArray MagicData::normalizedMask(const Utils::Internal::MimeMagicRule &rule)
  119. {
  120. // convert mask and see if it is the "default" one (which corresponds to "empty" mask)
  121. // see MimeMagicRule constructor
  122. QByteArray mask = rule.mask();
  123. if (rule.type() == Utils::Internal::MimeMagicRule::String) {
  124. QByteArray actualMask = QByteArray::fromHex(QByteArray::fromRawData(mask.constData() + 2,
  125. mask.size() - 2));
  126. if (actualMask.count(char(-1)) == actualMask.size()) {
  127. // is the default-filled 0xfffffffff mask
  128. mask.clear();
  129. }
  130. }
  131. return mask;
  132. }
  133. Utils::Internal::MimeMagicRule MimeTypeMagicDialog::createRule(QString *errorMessage) const
  134. {
  135. Utils::Internal::MimeMagicRule::Type type = typeValue(ui.typeSelector->currentIndex());
  136. Utils::Internal::MimeMagicRule rule(type,
  137. ui.valueLineEdit->text().toUtf8(),
  138. ui.startRangeSpinBox->value(),
  139. ui.endRangeSpinBox->value(),
  140. ui.maskLineEdit->text().toLatin1(),
  141. errorMessage);
  142. if (type == Utils::Internal::MimeMagicRule::Invalid) {
  143. if (errorMessage)
  144. *errorMessage = tr("Internal error: Type is invalid");
  145. }
  146. return rule;
  147. }