<commit_before>// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.credentialStore

import com.intellij.openapi.application.ApplicationInfo
import com.intellij.openapi.application.ApplicationManager
import com.intellij.util.text.nullize
import org.freedesktop.dbus.annotations.DBusInterfaceName
import org.freedesktop.dbus.connections.impl.DBusConnection
import org.freedesktop.dbus.errors.NoReply
import org.freedesktop.dbus.errors.ServiceUnknown
import org.freedesktop.dbus.exceptions.DBusException
import org.freedesktop.dbus.interfaces.DBusInterface
import org.freedesktop.dbus.types.Variant
import java.io.Closeable

internal class KWalletCredentialStore private constructor(private val connection: DBusConnection, private val kWallet: KWallet) : CredentialStore, Closeable {
  companion object {
    private fun appName(): String {
      val app = ApplicationManager.getApplication()
      val appName = if (app == null || app.isUnitTestMode) null else ApplicationInfo.getInstance().fullApplicationName
      return appName ?: "IDEA tests"
    }

    fun create(): KWalletCredentialStore? {
      try {
        val connection = DBusConnection.getConnection(DBusConnection.DBusBusType.SESSION)
        try {
          val wallet = connection.getRemoteObject("org.kde.kwalletd5", "/modules/kwalletd5", KWallet::class.java, true)
          wallet.localWallet() //ping
          return KWalletCredentialStore(connection, wallet)
        }
        catch (e: ServiceUnknown) {
          LOG.info("No KWallet service", e)
        }
        catch (e: DBusException) {
          LOG.warn("Failed to connect to KWallet", e)
        }
        catch (e: RuntimeException) {
          LOG.warn("Failed to connect to KWallet", e)
        }
        connection.close()
      }
      catch (e: DBusException) {
        LOG.warn("Failed to connect to D-Bus", e)
      }
      return null
    }
  }

  private val appId = appName()
  private var cachedWalletId = -1
  private var cachedWalletName: String? = null
  private var suggestedCreation: String? = null

  private fun getWalletId(): Int {
    if (cachedWalletId != -1 && kWallet.isOpen(cachedWalletId) && cachedWalletName?.let { appId in kWallet.users(it) } == true) {
      return cachedWalletId
    }
    val walletName = kWallet.localWallet()
    cachedWalletName = walletName
    val wallets = kWallet.wallets()
    val isNew = walletName !in wallets<commit_msg>handle runtime exception from dbus IDEA-230728<commit_after>// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.credentialStore

import com.intellij.openapi.application.ApplicationInfo
import com.intellij.openapi.application.ApplicationManager
import com.intellij.util.text.nullize
import org.freedesktop.dbus.annotations.DBusInterfaceName
import org.freedesktop.dbus.connections.impl.DBusConnection
import org.freedesktop.dbus.errors.NoReply
import org.freedesktop.dbus.errors.ServiceUnknown
import org.freedesktop.dbus.exceptions.DBusException
import org.freedesktop.dbus.interfaces.DBusInterface
import org.freedesktop.dbus.types.Variant
import java.io.Closeable

internal class KWalletCredentialStore private constructor(private val connection: DBusConnection, private val kWallet: KWallet) : CredentialStore, Closeable {
  companion object {
    private fun appName(): String {
      val app = ApplicationManager.getApplication()
      val appName = if (app == null || app.isUnitTestMode) null else ApplicationInfo.getInstance().fullApplicationName
      return appName ?: "IDEA tests"
    }

    fun create(): KWalletCredentialStore? {
      try {
        val connection = DBusConnection.getConnection(DBusConnection.DBusBusType.SESSION)
        try {
          val wallet = connection.getRemoteObject("org.kde.kwalletd5", "/modules/kwalletd5", KWallet::class.java, true)
          wallet.localWallet() //ping
          return KWalletCredentialStore(connection, wallet)
        }
        catch (e: ServiceUnknown) {
          LOG.info("No KWallet service", e)
        }
        catch (e: DBusException) {
          LOG.warn("Failed to connect to KWallet", e)
        }
        catch (e: RuntimeException) {
          LOG.warn("Failed to connect to KWallet", e)
        }
        connection.close()
      }
      catch (e: DBusException) {
        LOG.warn("Failed to connect to D-Bus", e)
      }
      catch (e: RuntimeException) {
        LOG.warn("Failed to connect to D-Bus", e)
      }
      return null
    }
  }

  private val appId = appName()
  private var cachedWalletId = -1
  private var cachedWalletName: String? = null
  private var suggestedCreation: String? = null

  private fun getWalletId(): Int {
    if (cachedWalletId != -1 && kWallet.isOpen(cachedWalletId) && cachedWalletName?.let { appId in kWallet.users(it) } == true) {
      return cachedWalletId
    }
    val walletName = kWallet.localWallet()
    cachedWalletName = walletName
    val wallets = kWallet.wallets()
    val isNew = walletName !in wallets
{'fraction_non_alphanumeric': 0.09150706132714258, 'fraction_numerical': 0.006577674598568389, 'mean_word_length': 3.3591905564924116, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2762988', 'n_tokens_mistral': 1513, 'n_tokens_neox': 1521, 'n_words': 387}