Skip to content
Snippets Groups Projects
UserSettingsPage.cpp 78.9 KiB
Newer Older
  • Learn to ignore specific revisions
  •                 i->setCameraResolution(data(index, Values).toStringList().at(value.toInt()));
                    return true;
                } else
                    return false;
            }
            case CameraFrameRate: {
                if (value.userType() == QMetaType::Int) {
                    i->setCameraFrameRate(data(index, Values).toStringList().at(value.toInt()));
                    return true;
                } else
                    return false;
            }
            case UseStunServer: {
                if (value.userType() == QMetaType::Bool) {
                    i->setUseStunServer(value.toBool());
                    return true;
                } else
                    return false;
            }
            case OnlyShareKeysWithVerifiedUsers: {
                if (value.userType() == QMetaType::Bool) {
                    i->setOnlyShareKeysWithVerifiedUsers(value.toBool());
                    return true;
                } else
                    return false;
            }
            case ShareKeysWithTrustedUsers: {
                if (value.userType() == QMetaType::Bool) {
                    i->setShareKeysWithTrustedUsers(value.toBool());
                    return true;
                } else
                    return false;
            }
            case UseOnlineKeyBackup: {
                if (value.userType() == QMetaType::Bool) {
                    i->setUseOnlineKeyBackup(value.toBool());
                    return true;
                } else
                    return false;
            }
    
    Loren Burkholder's avatar
    Loren Burkholder committed
            case ExposeDBusApi: {
                if (value.userType() == QMetaType::Bool) {
                    i->setExposeDBusApi(value.toBool());
                    return true;
                } else
                    return false;
            }
    
            case UpdateSpaceVias: {
                if (value.userType() == QMetaType::Bool) {
                    i->setUpdateSpaceVias(value.toBool());
                    return true;
                } else
                    return false;
            }
    
            case ExpireEvents: {
                if (value.userType() == QMetaType::Bool) {
                    i->setExpireEvents(value.toBool());
                    return true;
                } else
                    return false;
            }
    
            }
        }
        return false;
    
    UserSettingsModel::importSessionKeys()
    
        const QString homeFolder = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
    
        const QString fileName   = QFileDialog::getOpenFileName(
    
    Nicolas Werner's avatar
    Nicolas Werner committed
          nullptr, tr("Open Sessions File"), homeFolder, QLatin1String(""));
    
    
        QFile file(fileName);
        if (!file.open(QIODevice::ReadOnly)) {
    
    Nicolas Werner's avatar
    Nicolas Werner committed
            QMessageBox::warning(nullptr, tr("Error"), file.errorString());
    
            return;
        }
    
        auto bin     = file.peek(file.size());
        auto payload = std::string(bin.data(), bin.size());
    
        bool ok;
    
    Nicolas Werner's avatar
    Nicolas Werner committed
        auto password = QInputDialog::getText(nullptr,
    
                                              tr("File Password"),
                                              tr("Enter the passphrase to decrypt the file:"),
                                              QLineEdit::Password,
    
                                              QLatin1String(""),
    
                                              &ok);
        if (!ok)
            return;
    
        if (password.isEmpty()) {
    
    Nicolas Werner's avatar
    Nicolas Werner committed
            QMessageBox::warning(nullptr, tr("Error"), tr("The password cannot be empty"));
    
            return;
        }
    
        try {
            auto sessions = mtx::crypto::decrypt_exported_sessions(payload, password.toStdString());
            cache::importSessionKeys(std::move(sessions));
        } catch (const std::exception &e) {
    
    Nicolas Werner's avatar
    Nicolas Werner committed
            QMessageBox::warning(nullptr, tr("Error"), e.what());
    
    UserSettingsModel::exportSessionKeys()
    
        // Open password dialog.
        bool ok;
    
    Nicolas Werner's avatar
    Nicolas Werner committed
        auto password = QInputDialog::getText(nullptr,
    
                                              tr("File Password"),
                                              tr("Enter passphrase to encrypt your session keys:"),
                                              QLineEdit::Password,
    
                                              QLatin1String(""),
    
                                              &ok);
        if (!ok)
            return;
    
        if (password.isEmpty()) {
    
    Nicolas Werner's avatar
    Nicolas Werner committed
            QMessageBox::warning(nullptr, tr("Error"), tr("The password cannot be empty"));
    
            return;
        }
    
        // Open file dialog to save the file.
        const QString homeFolder = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
    
        const QString fileName   = QFileDialog::getSaveFileName(
    
    Nicolas Werner's avatar
    Nicolas Werner committed
          nullptr, tr("File to save the exported session keys"), homeFolder);
    
    
        QFile file(fileName);
        if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
    
    Nicolas Werner's avatar
    Nicolas Werner committed
            QMessageBox::warning(nullptr, tr("Error"), file.errorString());
    
            return;
        }
    
        // Export sessions & save to file.
        try {
            auto encrypted_blob = mtx::crypto::encrypt_exported_sessions(cache::exportSessionKeys(),
                                                                         password.toStdString());
    
            QString b64 = QString::fromStdString(mtx::crypto::bin2base64(encrypted_blob));
    
    
            QString prefix(QStringLiteral("-----BEGIN MEGOLM SESSION DATA-----"));
            QString suffix(QStringLiteral("-----END MEGOLM SESSION DATA-----"));
            QString newline(QStringLiteral("\n"));
    
            QTextStream out(&file);
            out << prefix << newline << b64 << newline << suffix << newline;
            file.close();
        } catch (const std::exception &e) {
    
    Nicolas Werner's avatar
    Nicolas Werner committed
            QMessageBox::warning(nullptr, tr("Error"), e.what());
    
    UserSettingsModel::requestCrossSigningSecrets()
    {
        olm::request_cross_signing_keys();
    }
    void
    UserSettingsModel::downloadCrossSigningSecrets()
    {
        olm::download_cross_signing_keys();
    }
    
    UserSettingsModel::UserSettingsModel(QObject *p)
      : QAbstractListModel(p)
    {
        auto s = UserSettings::instance();
        connect(s.get(), &UserSettings::themeChanged, this, [this]() {
            emit dataChanged(index(Theme), index(Theme), {Value});
        });
        connect(s.get(), &UserSettings::mobileModeChanged, this, [this]() {
            emit dataChanged(index(MobileMode), index(MobileMode), {Value});
        });
    
        connect(s.get(), &UserSettings::fontChanged, this, [this]() {
            emit dataChanged(index(Font), index(Font), {Value});
        });
        connect(s.get(), &UserSettings::fontSizeChanged, this, [this]() {
            emit dataChanged(index(FontSize), index(FontSize), {Value});
        });
        connect(s.get(), &UserSettings::emojiFontChanged, this, [this]() {
            emit dataChanged(index(EmojiFont), index(EmojiFont), {Value});
        });
        connect(s.get(), &UserSettings::avatarCirclesChanged, this, [this]() {
            emit dataChanged(index(AvatarCircles), index(AvatarCircles), {Value});
        });
        connect(s.get(), &UserSettings::useIdenticonChanged, this, [this]() {
            emit dataChanged(index(UseIdenticon), index(UseIdenticon), {Value});
        });
    
        connect(s.get(), &UserSettings::openImageExternalChanged, this, [this]() {
            emit dataChanged(index(OpenImageExternal), index(OpenImageExternal), {Value});
        });
    
        connect(s.get(), &UserSettings::openVideoExternalChanged, this, [this]() {
            emit dataChanged(index(OpenVideoExternal), index(OpenVideoExternal), {Value});
        });
    
        connect(s.get(), &UserSettings::privacyScreenChanged, this, [this]() {
            emit dataChanged(index(PrivacyScreen), index(PrivacyScreen), {Value});
            emit dataChanged(index(PrivacyScreenTimeout), index(PrivacyScreenTimeout), {Enabled});
        });
        connect(s.get(), &UserSettings::privacyScreenTimeoutChanged, this, [this]() {
            emit dataChanged(index(PrivacyScreenTimeout), index(PrivacyScreenTimeout), {Value});
        });
    
        connect(s.get(), &UserSettings::timelineMaxWidthChanged, this, [this]() {
            emit dataChanged(index(TimelineMaxWidth), index(TimelineMaxWidth), {Value});
        });
        connect(s.get(), &UserSettings::messageHoverHighlightChanged, this, [this]() {
            emit dataChanged(index(MessageHoverHighlight), index(MessageHoverHighlight), {Value});
        });
        connect(s.get(), &UserSettings::enlargeEmojiOnlyMessagesChanged, this, [this]() {
            emit dataChanged(index(EnlargeEmojiOnlyMessages), index(EnlargeEmojiOnlyMessages), {Value});
        });
        connect(s.get(), &UserSettings::animateImagesOnHoverChanged, this, [this]() {
            emit dataChanged(index(AnimateImagesOnHover), index(AnimateImagesOnHover), {Value});
        });
        connect(s.get(), &UserSettings::typingNotificationsChanged, this, [this]() {
            emit dataChanged(index(TypingNotifications), index(TypingNotifications), {Value});
        });
        connect(s.get(), &UserSettings::readReceiptsChanged, this, [this]() {
            emit dataChanged(index(ReadReceipts), index(ReadReceipts), {Value});
        });
        connect(s.get(), &UserSettings::buttonInTimelineChanged, this, [this]() {
            emit dataChanged(index(ButtonsInTimeline), index(ButtonsInTimeline), {Value});
        });
        connect(s.get(), &UserSettings::markdownChanged, this, [this]() {
            emit dataChanged(index(Markdown), index(Markdown), {Value});
        });
    
    LordMZTE's avatar
    LordMZTE committed
        connect(s.get(), &UserSettings::invertEnterKeyChanged, this, [this]() {
            emit dataChanged(index(InvertEnterKey), index(InvertEnterKey), {Value});
        });
    
    Malte E's avatar
    Malte E committed
        connect(s.get(), &UserSettings::bubblesChanged, this, [this]() {
            emit dataChanged(index(Bubbles), index(Bubbles), {Value});
        });
    
        connect(s.get(), &UserSettings::smallAvatarsChanged, this, [this]() {
            emit dataChanged(index(SmallAvatars), index(SmallAvatars), {Value});
        });
    
        connect(s.get(), &UserSettings::groupViewStateChanged, this, [this]() {
            emit dataChanged(index(GroupView), index(GroupView), {Value});
        });
    
        connect(s.get(), &UserSettings::scrollbarsInRoomlistChanged, this, [this]() {
            emit dataChanged(index(ScrollbarsInRoomlist), index(ScrollbarsInRoomlist), {Value});
        });
    
        connect(s.get(), &UserSettings::roomSortingChangedImportance, this, [this]() {
    
            emit dataChanged(index(SortByImportance), index(SortByImportance), {Value});
        });
    
        connect(s.get(), &UserSettings::roomSortingChangedAlphabetical, this, [this]() {
            emit dataChanged(index(SortByAlphabet), index(SortByAlphabet), {Value});
        });
    
        connect(s.get(), &UserSettings::decryptSidebarChanged, this, [this]() {
            emit dataChanged(index(DecryptSidebar), index(DecryptSidebar), {Value});
        });
    
        connect(s.get(), &UserSettings::decryptNotificationsChanged, this, [this]() {
            emit dataChanged(index(DecryptNotifications), index(DecryptNotifications), {Value});
        });
    
    LordMZTE's avatar
    LordMZTE committed
        connect(s.get(), &UserSettings::spaceNotificationsChanged, this, [this]() {
    
            emit dataChanged(index(SpaceNotifications), index(SpaceNotifications), {Value});
        });
    
    Loren Burkholder's avatar
    Loren Burkholder committed
        connect(s.get(), &UserSettings::fancyEffectsChanged, this, [this]() {
            emit dataChanged(index(FancyEffects), index(FancyEffects), {Value});
        });
    
        connect(s.get(), &UserSettings::reducedMotionChanged, this, [this]() {
            emit dataChanged(index(ReducedMotion), index(ReducedMotion), {Value});
        });
    
        connect(s.get(), &UserSettings::trayChanged, this, [this]() {
            emit dataChanged(index(Tray), index(Tray), {Value});
            emit dataChanged(index(StartInTray), index(StartInTray), {Enabled});
        });
        connect(s.get(), &UserSettings::startInTrayChanged, this, [this]() {
            emit dataChanged(index(StartInTray), index(StartInTray), {Value});
        });
    
        connect(s.get(), &UserSettings::desktopNotificationsChanged, this, [this]() {
            emit dataChanged(index(DesktopNotifications), index(DesktopNotifications), {Value});
        });
        connect(s.get(), &UserSettings::alertOnNotificationChanged, this, [this]() {
            emit dataChanged(index(AlertOnNotification), index(AlertOnNotification), {Value});
        });
    
        connect(s.get(), &UserSettings::useStunServerChanged, this, [this]() {
            emit dataChanged(index(UseStunServer), index(UseStunServer), {Value});
        });
        connect(s.get(), &UserSettings::microphoneChanged, this, [this]() {
            emit dataChanged(index(Microphone), index(Microphone), {Value, Values});
        });
        connect(s.get(), &UserSettings::cameraChanged, this, [this]() {
            emit dataChanged(index(Camera), index(Camera), {Value, Values});
        });
        connect(s.get(), &UserSettings::cameraResolutionChanged, this, [this]() {
            emit dataChanged(index(CameraResolution), index(CameraResolution), {Value, Values});
        });
        connect(s.get(), &UserSettings::cameraFrameRateChanged, this, [this]() {
            emit dataChanged(index(CameraFrameRate), index(CameraFrameRate), {Value, Values});
        });
        connect(s.get(), &UserSettings::ringtoneChanged, this, [this]() {
            emit dataChanged(index(Ringtone), index(Ringtone), {Values, Value});
        });
    
        connect(s.get(), &UserSettings::onlyShareKeysWithVerifiedUsersChanged, this, [this]() {
            emit dataChanged(
              index(OnlyShareKeysWithVerifiedUsers), index(OnlyShareKeysWithVerifiedUsers), {Value});
        });
        connect(s.get(), &UserSettings::shareKeysWithTrustedUsersChanged, this, [this]() {
            emit dataChanged(
              index(ShareKeysWithTrustedUsers), index(ShareKeysWithTrustedUsers), {Value});
        });
        connect(s.get(), &UserSettings::useOnlineKeyBackupChanged, this, [this]() {
            emit dataChanged(index(UseOnlineKeyBackup), index(UseOnlineKeyBackup), {Value});
        });
        connect(MainWindow::instance(), &MainWindow::secretsChanged, this, [this]() {
            emit dataChanged(index(OnlineBackupKey), index(MasterKey), {Value, Good});
        });
    
    Loren Burkholder's avatar
    Loren Burkholder committed
        connect(s.get(), &UserSettings::exposeDBusApiChanged, this, [this] {
            emit dataChanged(index(ExposeDBusApi), index(ExposeDBusApi), {Value});
        });
    
        connect(s.get(), &UserSettings::updateSpaceViasChanged, this, [this] {
            emit dataChanged(index(UpdateSpaceVias), index(UpdateSpaceVias), {Value});
        });
    
        connect(s.get(), &UserSettings::expireEventsChanged, this, [this] {
            emit dataChanged(index(ExpireEvents), index(ExpireEvents), {Value});
        });