<commit_before>
import React, {Component, PropTypes} from 'react'
import {createSelector, createStructuredSelector} from 'reselect'
import {connect} from './connect'

import {shell} from '../electron'

import path from 'path'
import humanize from 'humanize-plus'
import classNames from 'classnames'

import urls from '../constants/urls'

import Icon from './icon'
import SelectRow from './select-row'
import {versionString} from './hub-sidebar'

import * as actions from '../actions'

import {map, each, filter} from 'underline'

import diskspace from '../util/diskspace'

// XXX bad, but we'll live
const needle = require('electron').remote.require('./promised/needle')

function getAppLogPath () {
  const logOpts = require('electron').remote.require('./logger')
  const logPath = logOpts.logger.opts.sinks.file
  console.log(`found out logPath = ${logPath}`)
  return logPath
}

export class Preferences extends Component {
  render () {
    const {t, lang, sniffedLang = '', downloading, locales, isolateApps, closeToTray, showAdvanced} = this.props
    const {queueLocaleDownload, updatePreferences} = this.props

    const options = [{
      value: '__',
      label: t('preferences.language.auto', {language: sniffedLang})
    }].concat(locales)

    let translateUrl = `${urls.itchTranslationPlatform}/projects/itch/itch`
    if (lang !== 'en' && lang !== '__') {
      translateUrl += `/${lang}`
    }

    const translationBadgeUrl = `${urls.itchTranslationPlatform}/widgets/itch/${lang || 'en'}/svg-badge.svg`

    return <div className='preferences-meat'>
      <h2>{t('preferences.language')}</h2>
      <div className='language-form'>
        <label className='active'>
          <SelectRow onChange={::this.onLanguageChange} options={options} value={lang || '__'}/>

          <div className='locale-fetcher' onClick={(e) => { e.preventDefault(); queueLocaleDownload(lang) }}>
            {downloading
              ? <Icon icon='download' classes='scan'/>
              : <Icon icon='repeat'/>
            }
          </div>
        </label>
      </div>

      <p className='explanation flex'>
        {t('preferences.language.get_involved', {name: 'itch'}) + ' '}
        <a href={translateUrl}>
          <img className='weblate-badge' src={translationBadgeUrl}/>
        </a>
      </p>

      <h2>{t('preferences.security')}</h2>
      <div className='security-form'>
        <label className={classNames({active: isolateApps})}>
          <input type='checkbox' checked={isolateApps} onChange={(e) => { updatePreferences({isolateApps: e.target.checked}) }}/>
          <span> {t('preferences.security.sandbox.title')} </span>
          <span className='hint--bottom' data-hint={t('label.experimental')}>
            <Icon icon='lab-flask' onClick={(e) => e.preventDefault()}/>
          </span>
        </label>
      </div>

      <p className='explanation'>
        {t('preferences.security.sandbox.description')}
        {' '}
        <a href={urls.sandboxDocs}>
          {t('docs.learn_more')}
        </a>
      </p>

      <h2>{t('preferences.behavior')}</h2>
      <div className='behavior-form'>
        <label className={classNames({active: closeToTray})}>
          <input type='checkbox' checked={closeToTray} onChange={(e) => { updatePreferences({closeToTray: e.target.checked}) }}/>
          <span> {t('preferences.behavior.close_to_tray')} </span>
        </label>
      </div>

      <h2>{t('preferences.install_locations')}</h2>
      {this.installLocationTable()}

      <h2 className='toggle' onClick={(e) => updatePreferences({showAdvanced: !showAdvanced})}>
        <span className={`icon icon-triangle-right turner ${showAdvanced ? 'turned' : ''}`}/>
        {' '}
        {t('preferences.advanced')}
      </h2>
      {showAdvanced
      ? this.renderAdvanced()
      : ''}
    </div>
  }

  renderAdvanced () {
    const {t} = this.props

    return <p className='explanation'>
      <span className='app-version'>
      {versionString()}
      </span>
      <span className='proxy-settings'>
        {t('preferences.proxy_server_address')}
        {needle.proxy
          ? <span className='value hint--right' data-hint={t(`preferences.proxy_server_source.${needle.proxySource}`)}>
            {needle.proxy}
          </span>
          : <span className='value'>
            {t('preferences.proxy_server_source.direct')}
          </span>
        }
        {' '}
        <a href={urls.proxyDocs}>
          {t('docs.learn_more')}
        </a>
      </span>
      <span className='link' onClick={(e) => { e.preventDefault(); shell.openItem(getAppLogPath()) }}>
        {t('preferences.advanced.open_app_log')}
      </span>
    </p>
  }

  onLanguageChange (lang) {
    const {updatePreferences} = this.props
    if (lang === '__') {
      lang = null
    }

    updatePreferences({lang})
  }

  installLocationTable () {
    const {t, navigate} = this.props
    const {addInstallLocationRequest,
      removeInstallLocationRequest, makeInstallLocationDefault} = this.props

    const header = <tr className='header'>
      <td>{t('preferences.install_location.path')}</td>
      <td>{t('preferences.install_location.used_space')}</td>
      <td>{t('preferences.install_location.free_space')}</td>
      <td></td>
      <td></td>
    </tr>

    const {installLocations = {}} = this.props
    const {aliases, defaultLoc = 'appdata', locations = []} = installLocations

    // can't delete your last remaining location.
    const severalLocations = locations.length > 0

    let rows = []
    rows.push(header)

    locations::each((location) => {
      const {name} = location
      const isDefault = (name === defaultLoc)
      const mayDelete = severalLocations && name !== 'appdata'

      let {path} = location
      for (const alias of aliases) {
        path = path.replace(alias[0], alias[1])
      }
      const {size, freeSpace} = location
      const rowClasses = classNames({
        ['default']: isDefault
      })

      rows.push(<tr className={rowClasses} key={`location-${name}`}>
        <td className='action path' onClick={(e) => makeInstallLocationDefault(name)}>
          <div className='default-switch hint--right' data-hint={t('preferences.install_location.' + (isDefault ? 'is_default' : 'make_default'))}>
            <span className='single-line'>{path}</span>
            {isDefault
              ? <span className='single-line default-state'>(default location)</span>
              : null
            }
          </div>
        </td>
        <td> {humanize.fileSize(size)} </td>
        <td> {freeSpace > 0 ? humanize.fileSize(freeSpace) : '...'} </td>
        <td className='action' onClick={(e) => { e.preventDefault(); navigate(`locations/${name}`) }}>
          <Icon icon='folder-open'/>
        </td>

        {mayDelete
          ? <td className='action delete hint--top' data-hint={t('preferences.install_location.delete')} onClick={(e) => removeInstallLocationRequest(name)}>
            <Icon icon='cross'/>
          </td>
          : <td/>
        }
      </tr>)
    })

    rows.push(<tr>
      <td className='action add-new' onClick={(e) => { e.preventDefault(); addInstallLocationRequest() }}>
        <Icon icon='plus'/>
        {t('preferences.install_location.add')}
      </td><commit_msg>Use localized string for default label<commit_after>
import React, {Component, PropTypes} from 'react'
import {createSelector, createStructuredSelector} from 'reselect'
import {connect} from './connect'

import {shell} from '../electron'

import path from 'path'
import humanize from 'humanize-plus'
import classNames from 'classnames'

import urls from '../constants/urls'

import Icon from './icon'
import SelectRow from './select-row'
import {versionString} from './hub-sidebar'

import * as actions from '../actions'

import {map, each, filter} from 'underline'

import diskspace from '../util/diskspace'

// XXX bad, but we'll live
const needle = require('electron').remote.require('./promised/needle')

function getAppLogPath () {
  const logOpts = require('electron').remote.require('./logger')
  const logPath = logOpts.logger.opts.sinks.file
  console.log(`found out logPath = ${logPath}`)
  return logPath
}

export class Preferences extends Component {
  render () {
    const {t, lang, sniffedLang = '', downloading, locales, isolateApps, closeToTray, showAdvanced} = this.props
    const {queueLocaleDownload, updatePreferences} = this.props

    const options = [{
      value: '__',
      label: t('preferences.language.auto', {language: sniffedLang})
    }].concat(locales)

    let translateUrl = `${urls.itchTranslationPlatform}/projects/itch/itch`
    if (lang !== 'en' && lang !== '__') {
      translateUrl += `/${lang}`
    }

    const translationBadgeUrl = `${urls.itchTranslationPlatform}/widgets/itch/${lang || 'en'}/svg-badge.svg`

    return <div className='preferences-meat'>
      <h2>{t('preferences.language')}</h2>
      <div className='language-form'>
        <label className='active'>
          <SelectRow onChange={::this.onLanguageChange} options={options} value={lang || '__'}/>

          <div className='locale-fetcher' onClick={(e) => { e.preventDefault(); queueLocaleDownload(lang) }}>
            {downloading
              ? <Icon icon='download' classes='scan'/>
              : <Icon icon='repeat'/>
            }
          </div>
        </label>
      </div>

      <p className='explanation flex'>
        {t('preferences.language.get_involved', {name: 'itch'}) + ' '}
        <a href={translateUrl}>
          <img className='weblate-badge' src={translationBadgeUrl}/>
        </a>
      </p>

      <h2>{t('preferences.security')}</h2>
      <div className='security-form'>
        <label className={classNames({active: isolateApps})}>
          <input type='checkbox' checked={isolateApps} onChange={(e) => { updatePreferences({isolateApps: e.target.checked}) }}/>
          <span> {t('preferences.security.sandbox.title')} </span>
          <span className='hint--bottom' data-hint={t('label.experimental')}>
            <Icon icon='lab-flask' onClick={(e) => e.preventDefault()}/>
          </span>
        </label>
      </div>

      <p className='explanation'>
        {t('preferences.security.sandbox.description')}
        {' '}
        <a href={urls.sandboxDocs}>
          {t('docs.learn_more')}
        </a>
      </p>

      <h2>{t('preferences.behavior')}</h2>
      <div className='behavior-form'>
        <label className={classNames({active: closeToTray})}>
          <input type='checkbox' checked={closeToTray} onChange={(e) => { updatePreferences({closeToTray: e.target.checked}) }}/>
          <span> {t('preferences.behavior.close_to_tray')} </span>
        </label>
      </div>

      <h2>{t('preferences.install_locations')}</h2>
      {this.installLocationTable()}

      <h2 className='toggle' onClick={(e) => updatePreferences({showAdvanced: !showAdvanced})}>
        <span className={`icon icon-triangle-right turner ${showAdvanced ? 'turned' : ''}`}/>
        {' '}
        {t('preferences.advanced')}
      </h2>
      {showAdvanced
      ? this.renderAdvanced()
      : ''}
    </div>
  }

  renderAdvanced () {
    const {t} = this.props

    return <p className='explanation'>
      <span className='app-version'>
      {versionString()}
      </span>
      <span className='proxy-settings'>
        {t('preferences.proxy_server_address')}
        {needle.proxy
          ? <span className='value hint--right' data-hint={t(`preferences.proxy_server_source.${needle.proxySource}`)}>
            {needle.proxy}
          </span>
          : <span className='value'>
            {t('preferences.proxy_server_source.direct')}
          </span>
        }
        {' '}
        <a href={urls.proxyDocs}>
          {t('docs.learn_more')}
        </a>
      </span>
      <span className='link' onClick={(e) => { e.preventDefault(); shell.openItem(getAppLogPath()) }}>
        {t('preferences.advanced.open_app_log')}
      </span>
    </p>
  }

  onLanguageChange (lang) {
    const {updatePreferences} = this.props
    if (lang === '__') {
      lang = null
    }

    updatePreferences({lang})
  }

  installLocationTable () {
    const {t, navigate} = this.props
    const {addInstallLocationRequest,
      removeInstallLocationRequest, makeInstallLocationDefault} = this.props

    const header = <tr className='header'>
      <td>{t('preferences.install_location.path')}</td>
      <td>{t('preferences.install_location.used_space')}</td>
      <td>{t('preferences.install_location.free_space')}</td>
      <td></td>
      <td></td>
    </tr>

    const {installLocations = {}} = this.props
    const {aliases, defaultLoc = 'appdata', locations = []} = installLocations

    // can't delete your last remaining location.
    const severalLocations = locations.length > 0

    let rows = []
    rows.push(header)

    locations::each((location) => {
      const {name} = location
      const isDefault = (name === defaultLoc)
      const mayDelete = severalLocations && name !== 'appdata'

      let {path} = location
      for (const alias of aliases) {
        path = path.replace(alias[0], alias[1])
      }
      const {size, freeSpace} = location
      const rowClasses = classNames({
        ['default']: isDefault
      })

      rows.push(<tr className={rowClasses} key={`location-${name}`}>
        <td className='action path' onClick={(e) => makeInstallLocationDefault(name)}>
          <div className='default-switch hint--right' data-hint={t('preferences.install_location.' + (isDefault ? 'is_default' : 'make_default'))}>
            <span className='single-line'>{path}</span>
            {isDefault
              ? <span className='single-line default-state'>{t('preferences.install_location.is_default_short')}</span>
              : null
            }
          </div>
        </td>
        <td> {humanize.fileSize(size)} </td>
        <td> {freeSpace > 0 ? humanize.fileSize(freeSpace) : '...'} </td>
        <td className='action' onClick={(e) => { e.preventDefault(); navigate(`locations/${name}`) }}>
          <Icon icon='folder-open'/>
        </td>

        {mayDelete
          ? <td className='action delete hint--top' data-hint={t('preferences.install_location.delete')} onClick={(e) => removeInstallLocationRequest(name)}>
            <Icon icon='cross'/>
          </td>
          : <td/>
        }
      </tr>)
    })

    rows.push(<tr>
      <td className='action add-new' onClick={(e) => { e.preventDefault(); addInstallLocationRequest() }}>
        <Icon icon='plus'/>
        {t('preferences.install_location.add')}
      </td>
{'fraction_non_alphanumeric': 0.169002529568606, 'fraction_numerical': 0.0019142681342722363, 'mean_word_length': 2.8203186210498825, 'pattern_counts': {'":': 0, '<': 201, '<?xml version=': 0, '>': 227, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4417847', 'n_tokens_mistral': 4435, 'n_tokens_neox': 4386, 'n_words': 845}