// Called when the user clicked "OK" button of the settings dialog. Return
// true if the proxy settings has been changed by the user.
bool SettingsDialog::updateProxySettings()
{
    SettingsManager *mgr = seafApplet->settingsManager();
    SettingsManager::SeafileProxy old_proxy = mgr->getProxy();

    SettingsManager::ProxyType proxy_type = static_cast<SettingsManager::ProxyType>(mProxyMethodComboBox->currentIndex());
    QString proxy_host = mProxyHost->text().trimmed();
    QString proxy_username = mProxyUsername->text().trimmed();
    QString proxy_password = mProxyPassword->text().trimmed();
    int proxy_port = mProxyPort->value();

    SettingsManager::SeafileProxy new_proxy(proxy_type);

    switch(proxy_type) {
        case SettingsManager::HttpProxy:
            new_proxy.host = proxy_host;
            new_proxy.port = proxy_port;
            if (mProxyRequirePassword->checkState() == Qt::Checked) {
                new_proxy.username = proxy_username;
                new_proxy.password = proxy_password;
                break;
            }
            break;
        case SettingsManager::SocksProxy:
            new_proxy.host = proxy_host;
            new_proxy.port = proxy_port;
            break;
        case SettingsManager::NoProxy:
        case SettingsManager::SystemProxy:
        default:
            break;
    }

    if (new_proxy != old_proxy) {
        mgr->setProxy(new_proxy);
        return true;
    }

    return false;
}
void SettingsDialog::showEvent(QShowEvent *event)
{
    SettingsManager *mgr = seafApplet->settingsManager();
    // mgr->loadSettings();

    Qt::CheckState state;
    state = mgr->hideMainWindowWhenStarted() ? Qt::Checked : Qt::Unchecked;
    mHideMainWinCheckBox->setCheckState(state);

    state = mgr->allowInvalidWorktree() ? Qt::Checked : Qt::Unchecked;
    mAllowInvalidWorktreeCheckBox->setCheckState(state);

    state = mgr->syncExtraTempFile() ? Qt::Checked : Qt::Unchecked;
    mSyncExtraTempFileCheckBox->setCheckState(state);

    state = mgr->allowRepoNotFoundOnServer() ? Qt::Checked : Qt::Unchecked;
    mAllowRepoNotFoundCheckBox->setCheckState(state);

    state = mgr->httpSyncCertVerifyDisabled() ? Qt::Checked : Qt::Unchecked;
    mDisableVerifyHttpSyncCert->setCheckState(state);

    // currently supports windows only
    state = mgr->autoStart() ? Qt::Checked : Qt::Unchecked;
    mAutoStartCheckBox->setCheckState(state);
#if !defined(Q_OS_WIN32) && !defined(Q_OS_MAC)
    mAutoStartCheckBox->hide();
#endif
#ifdef HAVE_FINDER_SYNC_SUPPORT
    if (mgr->getFinderSyncExtensionAvailable()) {
        mFinderSyncCheckBox->setEnabled(true);
        state = mgr->getFinderSyncExtension() ? Qt::Checked : Qt::Unchecked;
        mFinderSyncCheckBox->setCheckState(state);
    } else {
        mFinderSyncCheckBox->setEnabled(false);
    }
#else
    mFinderSyncCheckBox->hide();
#endif

#if defined(Q_OS_WIN32)
    state = mgr->shellExtensionEnabled() ? Qt::Checked : Qt::Unchecked;
    mShellExtCheckBox->setCheckState(state);
#else
    mShellExtCheckBox->hide();
#endif

    // currently supports mac only
    state = mgr->hideDockIcon() ? Qt::Checked : Qt::Unchecked;
    mHideDockIconCheckBox->setCheckState(state);
#if !defined(Q_OS_MAC)
    mHideDockIconCheckBox->hide();
#endif

    state = mgr->notify() ? Qt::Checked : Qt::Unchecked;
    mNotifyCheckBox->setCheckState(state);

    int ratio;
    ratio = mgr->maxDownloadRatio();
    mDownloadSpinBox->setValue(ratio);
    ratio = mgr->maxUploadRatio();
    mUploadSpinBox->setValue(ratio);

    if (isCheckLatestVersionEnabled()) {
        state = mgr->isCheckLatestVersionEnabled() ? Qt::Checked : Qt::Unchecked;
        mCheckLatestVersionBox->setCheckState(state);
    }

    mEnableSyncingWithExistingFolder->hide();

    SettingsManager::SeafileProxy proxy = mgr->getProxy();
    showHideControlsBasedOnCurrentProxyType(proxy.type);
    mProxyMethodComboBox->setCurrentIndex(proxy.type);
    mProxyHost->setText(proxy.host);
    mProxyPort->setValue(proxy.port);
    mProxyUsername->setText(proxy.username);
    mProxyPassword->setText(proxy.password);
    if (!proxy.username.isEmpty())
        mProxyRequirePassword->setChecked(true);

    mLanguageComboBox->setCurrentIndex(I18NHelper::getInstance()->preferredLanguage());

    QDialog::showEvent(event);
}
bool SettingsDialog::updateProxySettings()
{
    bool proxy_changed = false;

    SettingsManager *mgr = seafApplet->settingsManager();
    SettingsManager::ProxyType old_proxy_type;
    QString old_proxy_host;
    QString old_proxy_username;
    QString old_proxy_password;
    int old_proxy_port;
    mgr->getProxy(old_proxy_type, old_proxy_host, old_proxy_port, old_proxy_username, old_proxy_password);

    SettingsManager::ProxyType proxy_type = static_cast<SettingsManager::ProxyType>(mProxyMethodComboBox->currentIndex());
    QString proxy_host = mProxyHost->text().trimmed();
    QString proxy_username = mProxyUsername->text().trimmed();
    QString proxy_password = mProxyPassword->text().trimmed();
    int proxy_port = mProxyPort->value();

    switch(proxy_type) {
        case SettingsManager::HttpProxy:
                // if we setup proxy username now and previously
            if (mProxyRequirePassword->checkState() == Qt::Checked) {
                if (proxy_type == old_proxy_type &&
                    proxy_host == old_proxy_host &&
                    proxy_port == old_proxy_port &&
                    proxy_username == old_proxy_username &&
                    proxy_password == old_proxy_password)
                    break;
                proxy_changed = true;
                mgr->setProxy(SettingsManager::HttpProxy,
                              proxy_host, proxy_port,
                              proxy_username, proxy_password);
                break;
            }
            else {
                // and if we don't setup proxy username now and previously
                if (proxy_type == old_proxy_type &&
                    proxy_host == old_proxy_host &&
                    proxy_port == old_proxy_port &&
                    old_proxy_username.isEmpty())
                    break;
                proxy_changed = true;
                mgr->setProxy(SettingsManager::HttpProxy,
                              proxy_host, proxy_port);
            }
            break;
        case SettingsManager::SocksProxy:
            if (proxy_type == old_proxy_type &&
                proxy_host == old_proxy_host &&
                proxy_port == old_proxy_port)
                break;
            proxy_changed = true;
            mgr->setProxy(SettingsManager::SocksProxy,
                          proxy_host, proxy_port);
            break;
        case SettingsManager::NoneProxy:
        default:
            if (proxy_type == old_proxy_type)
                break;
            proxy_changed = true;
            mgr->setProxy(SettingsManager::NoneProxy);
            break;
    }

    return proxy_changed;
}