<commit_before>import { Page } from "ui/page";
import { Component, OnInit } from "@angular/core";
import { Util } from "../../common/util";
import * as Toast from 'nativescript-toast';
import { TNSFontIconService } from 'nativescript-ngx-fonticon';
import * as Settings from "application-settings";
import * as util from "utils/utils";
import * as Platform from "platform";
import { TranslateService } from "ng2-translate";
import * as Http from "tns-core-modules/http"
import * as  Base64 from "base-64";
import Loader from "../../common/loader";


  
@Component({
  selector: "settings",
  templateUrl: "pages/settings/settings.html",
  styleUrls: ["pages/settings/settings-common.css"],
  providers: [Util]
})


export class SettingsComponent {
 
    private language;

    private host;
    private username;
    private password;
    private rootdir;
    
    private loader = new Loader();
 
    public constructor(
      private page: Page,
	    private util: Util,
      private fonticon: TNSFontIconService,
      private translate: TranslateService
    )  {
      this.util.log("Page Init", "Settings");

      this.language = Platform.device.language;
      this.translate.setDefaultLang("en");
      this.translate.use(Platform.device.language.split("-")[0]);
    }

    private ngOnInit() {
      this.page.actionBarHidden = true;
    }    
 
    private back() {
      this.util.navigateBack();
    }

    private isWellConfigured() {
      let configured = true;
      if(this.host==null || this.host=="") return false;
      if(this.username==null || this.username=="") return false;
      if(this.password==null || this.password=="") return false;
      if(!this.host.startsWith("https://")) {
        Toast.makeText(this.translate.instant("Nextcloud address must start with https://")).show();
        return false;
      }

      //if(!okLogin()) configured = false;
      return configured;
    }

    private save() {

      if(this.isWellConfigured()) {
        Settings.setString("host", this.util.replaceAll(this.host, " ", ""));
        Settings.setString("username", this.util.replaceAll(this.username, " ", ""));
        Settings.setString("password", this.util.replaceAll(this.password, " ", ""));
        Settings.setString("rootdir", (this.rootdir==null)?"":this.rootdir.trim()); 
        
        this.tryConnection(this.host, this.username, this.password, ()=> {
          this.util.navigate("");
        });

      } else {
        Toast.makeText(this.translate.instant("Error connecting. Please check parameters")).show();
      }
    } 

    private link() {
      util.openUrl("https://www.operweb.com/nextcloud-gallery/");
    }


    private tryConnection(host, username, password, callOk) {

      this.loader.showLoader(this.translate.instant("Checking connection..."));

      let url = host+"/index.php/apps/gallery/api/files/list?location=&mediatypes=image/jpeg;&features=&etag";
      let headers = { 
        "OCS-APIREQUEST": "true",
        "Authorization": "Basic "+Base64.encode(username+':'+password)
      } 

      Http.request({
        url: url,
        method: "GET",
        headers: headers
      }).then((response:any)=> {

        let data = null;

        try {   
          data = response.content.toJSON();<commit_msg>fixed ellipsis #9<commit_after>import { Page } from "ui/page";
import { Component, OnInit } from "@angular/core";
import { Util } from "../../common/util";
import * as Toast from 'nativescript-toast';
import { TNSFontIconService } from 'nativescript-ngx-fonticon';
import * as Settings from "application-settings";
import * as util from "utils/utils";
import * as Platform from "platform";
import { TranslateService } from "ng2-translate";
import * as Http from "tns-core-modules/http"
import * as  Base64 from "base-64";
import Loader from "../../common/loader";


  
@Component({
  selector: "settings",
  templateUrl: "pages/settings/settings.html",
  styleUrls: ["pages/settings/settings-common.css"],
  providers: [Util]
})


export class SettingsComponent {
 
    private language;

    private host;
    private username;
    private password;
    private rootdir;
    
    private loader = new Loader();
 
    public constructor(
      private page: Page,
	    private util: Util,
      private fonticon: TNSFontIconService,
      private translate: TranslateService
    )  {
      this.util.log("Page Init", "Settings");

      this.language = Platform.device.language;
      this.translate.setDefaultLang("en");
      this.translate.use(Platform.device.language.split("-")[0]);
    }

    private ngOnInit() {
      this.page.actionBarHidden = true;
    }    
 
    private back() {
      this.util.navigateBack();
    }

    private isWellConfigured() {
      let configured = true;
      if(this.host==null || this.host=="") return false;
      if(this.username==null || this.username=="") return false;
      if(this.password==null || this.password=="") return false;
      if(!this.host.startsWith("https://")) {
        Toast.makeText(this.translate.instant("Nextcloud address must start with https://")).show();
        return false;
      }

      //if(!okLogin()) configured = false;
      return configured;
    }

    private save() {

      if(this.isWellConfigured()) {
        Settings.setString("host", this.util.replaceAll(this.host, " ", ""));
        Settings.setString("username", this.util.replaceAll(this.username, " ", ""));
        Settings.setString("password", this.util.replaceAll(this.password, " ", ""));
        Settings.setString("rootdir", (this.rootdir==null)?"":this.rootdir.trim()); 
        
        this.tryConnection(this.host, this.username, this.password, ()=> {
          this.util.navigate("");
        });

      } else {
        Toast.makeText(this.translate.instant("Error connecting. Please check parameters")).show();
      }
    } 

    private link() {
      util.openUrl("https://www.operweb.com/nextcloud-gallery/");
    }


    private tryConnection(host, username, password, callOk) {

      this.loader.showLoader(this.translate.instant("Checking connection…"));

      let url = host+"/index.php/apps/gallery/api/files/list?location=&mediatypes=image/jpeg;&features=&etag";
      let headers = { 
        "OCS-APIREQUEST": "true",
        "Authorization": "Basic "+Base64.encode(username+':'+password)
      } 

      Http.request({
        url: url,
        method: "GET",
        headers: headers
      }).then((response:any)=> {

        let data = null;

        try {   
          data = response.content.toJSON();
{'fraction_non_alphanumeric': 0.15467845169155744, 'fraction_numerical': 0.0025906735751295338, 'mean_word_length': 2.8880331753554502, 'pattern_counts': {'":': 6, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 6, 'lorem ipsum': 0, 'www.': 2, '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': '6331668', 'n_tokens_mistral': 1968, 'n_tokens_neox': 1901, 'n_words': 391}