<commit_before>const Mainloop = imports.gi.GLib;

// Filesystem
const Gio = imports.gi.Gio;

// HTTP
const Soup = imports.gi.Soup;
const Lang = imports.lang;

//self
const Self = imports.misc.extensionUtils.getCurrentExtension();
const SourceAdapter = Self.imports.sourceAdapter;
const Prefs = Self.imports.settings;
const Timer = Self.imports.timer;
const HistoryModule = Self.imports.history;

const LoggerModule = Self.imports.logger;

var WallpaperController = class {

	constructor() {
		this.logger = new LoggerModule.Logger('RWG3', 'WallpaperController');
		this.wallpaperlocation = Self.path + '/wallpapers/';
		this.imageSourceAdapter = null;

		this._autoFetch = {
			active: false,
			duration: 30,
		};

		// functions will be called uppon loading a new wallpaper
		this._startLoadingHooks = [];
		// functions will be called when loading a new wallpaper stopped. If an error occured then the error will be passed as parameter.
		this._stopLoadingHooks = [];

		this._timer = new Timer.AFTimer();
		this._historyController = new HistoryModule.HistoryController(this.wallpaperlocation);

		this._settings = new Prefs.Settings();
		this._settings.observe('history-length', this._updateHistory.bind(this));
		this._settings.observe('auto-fetch', this._updateAutoFetching.bind(this));
		this._settings.observe('minutes', this._updateAutoFetching.bind(this));
		this._settings.observe('hours', this._updateAutoFetching.bind(this));

		this._desktopperAdapter = new SourceAdapter.DesktopperAdapter();
		this._unsplashAdapter = new SourceAdapter.UnsplashAdapter();
		this._wallhavenAdapter = new SourceAdapter.WallhavenAdapter();
		this._redditAdapter = new SourceAdapter.RedditAdapter();
		this._genericJsonAdapter = new SourceAdapter.GenericJsonAdapter();

		this._updateHistory();
		this._updateAutoFetching();

		this.currentWallpaper = this._getCurrentWallpaper();
	}

	_updateHistory() {
		this._historyController.load();
	}

	_updateAutoFetching() {
		let duration = 0;
		duration += this._settings.get('minutes', 'int');
		duration += this._settings.get('hours', 'int') * 60;
		this._autoFetch.duration = duration;
		this._autoFetch.active = this._settings.get('auto-fetch', 'boolean');

		if (this._autoFetch.active) {
			this._timer.registerCallback(this.fetchNewWallpaper.bind(this));
			this._timer.setMinutes(this._autoFetch.duration);
			this._timer.start();
		} else {
			this._timer.stop();
		}
	}

	/*
	 forwards the request to the adapter
	 */
	_requestRandomImageFromAdapter(callback) {
		this.imageSourceAdapter = this._desktopperAdapter;
		switch (this._settings.get('source', 'enum')) {
			case 0:
				this.imageSourceAdapter = this._unsplashAdapter;
				break;
			case 1:
				this.imageSourceAdapter = this._desktopperAdapter;
				break;
			case 2:
				this.imageSourceAdapter = this._wallhavenAdapter;
				break;
			case 3:
				this.imageSourceAdapter = this._redditAdapter;
				break;
			case 4:
				this.imageSourceAdapter = this._genericJsonAdapter;
				break;
			default:
				this.imageSourceAdapter = this._desktopperAdapter;
				break;
		}

		this.imageSourceAdapter.requestRandomImage(callback);
	}

	/**
	 * copy file from uri to local wallpaper directory and calls the given callback with the name and the full filepath
	 * of the written file as parameter.
	 * @param uri
	 * @param callback(name, path, error)
	 * @private
	 */
	_fetchFile(uri, callback) {
		//extract the name from the url and
		let date = new Date();
		let name = date.getTime() + '_' + this.imageSourceAdapter.fileName(uri); // timestamp ensures uniqueness

		let output_file, output_stream, input_file;

		let _httpSession = new Soup.SessionAsync();
		Soup.Session.prototype.add_feature.call(_httpSession, new Soup.ProxyResolverDefault());

		let file = Gio.file_new_for_path(this.wallpaperlocation + String(name));
		let fstream = file.replace(null, false, Gio.FileCreateFlags.NONE, null);

		// start the download
		let request = Soup.Message.new('GET', uri);
		request.connect('got_chunk', Lang.bind(this, function(message, chunk){
			try {
				fstream.write(chunk.get_data(), null, chunk.length);
			} catch (e) {
				if (callback) {<commit_msg>Use correct number of params in fstream.write<commit_after>const Mainloop = imports.gi.GLib;

// Filesystem
const Gio = imports.gi.Gio;

// HTTP
const Soup = imports.gi.Soup;
const Lang = imports.lang;

//self
const Self = imports.misc.extensionUtils.getCurrentExtension();
const SourceAdapter = Self.imports.sourceAdapter;
const Prefs = Self.imports.settings;
const Timer = Self.imports.timer;
const HistoryModule = Self.imports.history;

const LoggerModule = Self.imports.logger;

var WallpaperController = class {

	constructor() {
		this.logger = new LoggerModule.Logger('RWG3', 'WallpaperController');
		this.wallpaperlocation = Self.path + '/wallpapers/';
		this.imageSourceAdapter = null;

		this._autoFetch = {
			active: false,
			duration: 30,
		};

		// functions will be called uppon loading a new wallpaper
		this._startLoadingHooks = [];
		// functions will be called when loading a new wallpaper stopped. If an error occured then the error will be passed as parameter.
		this._stopLoadingHooks = [];

		this._timer = new Timer.AFTimer();
		this._historyController = new HistoryModule.HistoryController(this.wallpaperlocation);

		this._settings = new Prefs.Settings();
		this._settings.observe('history-length', this._updateHistory.bind(this));
		this._settings.observe('auto-fetch', this._updateAutoFetching.bind(this));
		this._settings.observe('minutes', this._updateAutoFetching.bind(this));
		this._settings.observe('hours', this._updateAutoFetching.bind(this));

		this._desktopperAdapter = new SourceAdapter.DesktopperAdapter();
		this._unsplashAdapter = new SourceAdapter.UnsplashAdapter();
		this._wallhavenAdapter = new SourceAdapter.WallhavenAdapter();
		this._redditAdapter = new SourceAdapter.RedditAdapter();
		this._genericJsonAdapter = new SourceAdapter.GenericJsonAdapter();

		this._updateHistory();
		this._updateAutoFetching();

		this.currentWallpaper = this._getCurrentWallpaper();
	}

	_updateHistory() {
		this._historyController.load();
	}

	_updateAutoFetching() {
		let duration = 0;
		duration += this._settings.get('minutes', 'int');
		duration += this._settings.get('hours', 'int') * 60;
		this._autoFetch.duration = duration;
		this._autoFetch.active = this._settings.get('auto-fetch', 'boolean');

		if (this._autoFetch.active) {
			this._timer.registerCallback(this.fetchNewWallpaper.bind(this));
			this._timer.setMinutes(this._autoFetch.duration);
			this._timer.start();
		} else {
			this._timer.stop();
		}
	}

	/*
	 forwards the request to the adapter
	 */
	_requestRandomImageFromAdapter(callback) {
		this.imageSourceAdapter = this._desktopperAdapter;
		switch (this._settings.get('source', 'enum')) {
			case 0:
				this.imageSourceAdapter = this._unsplashAdapter;
				break;
			case 1:
				this.imageSourceAdapter = this._desktopperAdapter;
				break;
			case 2:
				this.imageSourceAdapter = this._wallhavenAdapter;
				break;
			case 3:
				this.imageSourceAdapter = this._redditAdapter;
				break;
			case 4:
				this.imageSourceAdapter = this._genericJsonAdapter;
				break;
			default:
				this.imageSourceAdapter = this._desktopperAdapter;
				break;
		}

		this.imageSourceAdapter.requestRandomImage(callback);
	}

	/**
	 * copy file from uri to local wallpaper directory and calls the given callback with the name and the full filepath
	 * of the written file as parameter.
	 * @param uri
	 * @param callback(name, path, error)
	 * @private
	 */
	_fetchFile(uri, callback) {
		//extract the name from the url and
		let date = new Date();
		let name = date.getTime() + '_' + this.imageSourceAdapter.fileName(uri); // timestamp ensures uniqueness

		let output_file, output_stream, input_file;

		let _httpSession = new Soup.SessionAsync();
		Soup.Session.prototype.add_feature.call(_httpSession, new Soup.ProxyResolverDefault());

		let file = Gio.file_new_for_path(this.wallpaperlocation + String(name));
		let fstream = file.replace(null, false, Gio.FileCreateFlags.NONE, null);

		// start the download
		let request = Soup.Message.new('GET', uri);
		request.connect('got_chunk', Lang.bind(this, function(message, chunk){
			try {
				fstream.write(chunk.get_data(), null);
			} catch (e) {
				if (callback) {
{'fraction_non_alphanumeric': 0.11933317342288319, 'fraction_numerical': 0.002638522427440633, 'mean_word_length': 4.393919793014231, '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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7243422', 'n_tokens_mistral': 2810, 'n_tokens_neox': 2493, 'n_words': 565}