<commit_before>extern crate ini;
use self::ini::Ini;
use super::http;
use super::AlarmRepeat;
use super::AlarmConfig;
use super::ConnectDeviceList;
use super::Scrobbler;

extern crate time;
extern crate fruitbasket;

use std::path;
use std::str::FromStr;
use std::collections::BTreeMap;

const INIFILE: &'static str = "connectr.ini";
const PORT: u32 = 5432;
pub const WEB_PORT: u32 = 5676;

#[derive(Default)]
pub struct LastfmSettings {
    pub key: String,
    pub secret: String,
    pub session_key: String,
    pub username: String,
    pub ignore_pc: bool,
    pub ignore_phone: bool,
}

#[derive(Default)]
pub struct Settings {
    pub port: u32,
    pub secret: String,
    pub client_id: String,
    pub access_token: Option<String>,
    pub refresh_token: Option<String>,
    pub expire_utc: Option<u64>,
    pub presets: Vec<(String,String)>,
    pub default_quicksave: Option<String>,
    pub quicksave: BTreeMap<String, String>,
    pub alarms: Vec<AlarmConfig>,
    pub lastfm_enabled: bool,
    pub lastfm: Option<LastfmSettings>,
}

impl Settings {
    pub fn quick_save_playlist(&self, context: &str) -> Option<&str> {
        match self.quicksave.get(context) {
            Some(ref uri) => Some(&uri),
            None => {
                match self.default_quicksave {
                    Some(ref uri) => Some(&uri),
                    None => None,
                }
            }
        }
    }
}

fn default_inifile() -> String {
    format!("{}/.{}", dirs::home_dir().unwrap().display(), INIFILE)
}

fn inifile() -> String {
    // Try to load INI file from home directory
    let path = format!("{}/.{}", dirs::home_dir().unwrap().display(), INIFILE);
    if path::Path::new(&path).exists() {
        info!("Found config: {}", path);
        return path;
    }

    // Default to looking in current working directory
    let path = INIFILE.to_string();
    if path::Path::new(&path).exists() {
        info!("Found config: {}", path);
        return path;
    }

    String::new()
}

pub fn request_web_config(settings: Option<&Settings>) -> BTreeMap<String,String> {
    let mut form = format!(r###"
{}
<!DOCTYPE HTML>
<html>
<meta http-equiv="cache-control" content="no-cache" /><meta http-equiv="expires" content="0"></head>
<head><title>Connectr Installation</title></head>
<body>
<h2>Connectr Installation</h2>
Connectr requires a <em>paid</em> Spotify Premium account and a <em>free</em> Spotify developer application.</br>
If you don't have a Premium account, perhaps try a <a href="https://www.spotify.com/us/premium/">free trial</a>.</br>
</br>
To create your free developer application for Connectr, follow these instructions:</br>
<p><ul>
<li> Go to your <a href="https://developer.spotify.com/my-applications/#!/applications/create">Spotify Applications</a> page (login with your Spotify credentials)
<li> Click "CREATE AN APP" in the upper-right corner
<li> Enter a name (perhaps 'Connectr') and description ("Use Connectr app with my account.")
<li> Add a Redirect URI: <em>http://127.0.0.1:{}</em>
<li> Copy your <em>Client ID</em> and <em>Client Secret</em> to the fields below.
<li> Press the <em>SAVE</em> button at the bottom of Spotify's webpage
<li> Submit this configuration form
</ul></p>
<form method="POST" action="#" accept-charset="UTF-8"><table><commit_msg>prefer local connectr.ini to ~/.connectr.ini<commit_after>extern crate ini;
use self::ini::Ini;
use super::http;
use super::AlarmRepeat;
use super::AlarmConfig;
use super::ConnectDeviceList;
use super::Scrobbler;

extern crate time;
extern crate fruitbasket;

use std::path;
use std::str::FromStr;
use std::collections::BTreeMap;

const INIFILE: &'static str = "connectr.ini";
const PORT: u32 = 5432;
pub const WEB_PORT: u32 = 5676;

#[derive(Default)]
pub struct LastfmSettings {
    pub key: String,
    pub secret: String,
    pub session_key: String,
    pub username: String,
    pub ignore_pc: bool,
    pub ignore_phone: bool,
}

#[derive(Default)]
pub struct Settings {
    pub port: u32,
    pub secret: String,
    pub client_id: String,
    pub access_token: Option<String>,
    pub refresh_token: Option<String>,
    pub expire_utc: Option<u64>,
    pub presets: Vec<(String,String)>,
    pub default_quicksave: Option<String>,
    pub quicksave: BTreeMap<String, String>,
    pub alarms: Vec<AlarmConfig>,
    pub lastfm_enabled: bool,
    pub lastfm: Option<LastfmSettings>,
}

impl Settings {
    pub fn quick_save_playlist(&self, context: &str) -> Option<&str> {
        match self.quicksave.get(context) {
            Some(ref uri) => Some(&uri),
            None => {
                match self.default_quicksave {
                    Some(ref uri) => Some(&uri),
                    None => None,
                }
            }
        }
    }
}

fn default_inifile() -> String {
    format!("{}/.{}", dirs::home_dir().unwrap().display(), INIFILE)
}

fn inifile() -> String {
    // Default to looking in current working directory
    let path = INIFILE.to_string();
    if path::Path::new(&path).exists() {
        info!("Found config: {}", path);
        return path;
    }

    // Try to load INI file from home directory
    let path = format!("{}/.{}", dirs::home_dir().unwrap().display(), INIFILE);
    if path::Path::new(&path).exists() {
        info!("Found config: {}", path);
        return path;
    }

    String::new()
}

pub fn request_web_config(settings: Option<&Settings>) -> BTreeMap<String,String> {
    let mut form = format!(r###"
{}
<!DOCTYPE HTML>
<html>
<meta http-equiv="cache-control" content="no-cache" /><meta http-equiv="expires" content="0"></head>
<head><title>Connectr Installation</title></head>
<body>
<h2>Connectr Installation</h2>
Connectr requires a <em>paid</em> Spotify Premium account and a <em>free</em> Spotify developer application.</br>
If you don't have a Premium account, perhaps try a <a href="https://www.spotify.com/us/premium/">free trial</a>.</br>
</br>
To create your free developer application for Connectr, follow these instructions:</br>
<p><ul>
<li> Go to your <a href="https://developer.spotify.com/my-applications/#!/applications/create">Spotify Applications</a> page (login with your Spotify credentials)
<li> Click "CREATE AN APP" in the upper-right corner
<li> Enter a name (perhaps 'Connectr') and description ("Use Connectr app with my account.")
<li> Add a Redirect URI: <em>http://127.0.0.1:{}</em>
<li> Copy your <em>Client ID</em> and <em>Client Secret</em> to the fields below.
<li> Press the <em>SAVE</em> button at the bottom of Spotify's webpage
<li> Submit this configuration form
</ul></p>
<form method="POST" action="#" accept-charset="UTF-8"><table>
{'fraction_non_alphanumeric': 0.1527256344796516, 'fraction_numerical': 0.007808980327376483, 'mean_word_length': 3.62821403752606, 'pattern_counts': {'":': 0, '<': 115, '<?xml version=': 0, '>': 131, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 2, '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': '1735213', 'n_tokens_mistral': 2195, 'n_tokens_neox': 2136, 'n_words': 621}