<commit_before>/*
 * Writesetter is a program for creating PDF documents from text files with markup.
 * Copyright (c) 2013 <NAME> <<EMAIL>>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package writesetter.storage

import scala.collection.mutable.Stack
import writesetter.editor

object SourcesMetaData extends StoredArrayOfStringLists("SourcesMetaData.txt") {

	/*
	 * Fields:
	 * - Full file name (primary key)
	 * - File name
	 * - Time stamp for creation of source file
	 * - Time stamp for latest save
	 * - Character encoding for latest save
	 * - Dictionary (for spelling) - "language"
	 * - Page count
	 * - Error count
	 * - Built duration
	 * - Time of latest build
	 * - Number of pages in latest build
	 * - Number of errors in latest build
	 *
	 * Still to come:
	 * - Size of PDF (in bytes, kilo bytes, mega bytes,...)
	 * - Title, Author, Subject, Keywords
	 * - Draft/Final (State/Stage/Status)
	 * - Deadline
	 * - Next step
	 */
	
	minimumFieldCount = 10
	
	override def getKeyLength(configuration: List[String]) = 1
	
	def initialize {
		if (!initialized) {
			if (fileExists) load()
			initialized = true
		}
	}
	
	def GetListOfFileNames: List[List[String]] = {
		var result = new Stack[List[String]]
		for (configuration <- dataSet) {
			val fullFileName = configuration(0)
			val fileName = configuration(1)
			val time = configuration(3) // updated time
			if (FileMethods.IsFile(fullFileName)) {
				result.push(List(fullFileName, fileName, time))
			} else {
				removeFileData(fullFileName)
			}
		}
		result.toList.sortWith((a, b) => a(2).toLong > b(2).toLong)
	}
	
	def getEncoding(fullFileName: String, forcedEncoding: String): String = {
		var result = ""
		val i = getIndexOf(List(fullFileName))
		val storedEncoding = if (i == -1) "" else dataSet(i)(4)
		
		var continue = true
		if (forcedEncoding != "" && storedEncoding != "" && forcedEncoding != storedEncoding) {
			val message = "This file has previously been saved with encoding " + storedEncoding
			continue = editor.DialogBox.warning(message)
		}
		if (forcedEncoding != "") {
			result = forcedEncoding
		} else if (storedEncoding != "") {
			result = storedEncoding
		} else {
			result = Configurations.GetCharacterEncoding
		}
		result
	}
	
	def getCreationTime(fullFileName: String): String = {
		val i = getIndexOf(List(fullFileName))
		if (i == -1) "" else dataSet(i)(2)
	}
	
	def getDictionary(fullFileName: String): String = {
		val i = getIndexOf(List(fullFileName))
		if (i == -1) "" else dataSet(i)(5)
	}
	
	def updateFileData(fullFileName: String, fileName: String, encoding: String, dictionary: String) {
		val time = FileMethods.GetTimeStamp(fullFileName)
		// FIXME: the first time stamp should be creation time!
		val storedCreationTime = getCreationTime(fullFileName)
		val creationTime = if (storedCreationTime == "") time.toString else storedCreationTime
		// FIXME: check that encoding and dictionary are valid.
		update(List(fullFileName, fileName, creationTime, time.toString, encoding, dictionary))
		store()
	}
	
	def updateCompilationMetaData(fullFileName: String, pageCount: Int, errorCount: Int, builtTime: Long, builtDuration: Long) {
		updateFrom(List(fullFileName), 6, List(pageCount.toString, errorCount.toString, builtTime.toString, builtDuration.toString))
		store()
	}
	
	def removeFileData(fullFileName: String) {
		remove(List(fullFileName))
		store()
	}
	
	def renameFileData(originalFileName: String, newFileName: String) {
		val i = getIndexOf(List(originalFileName))
		if (i == -1) {
			editor.DialogBox.systemError("Unknown file " + originalFileName)
		} else {
			dataSet(i) = newFileName :: dataSet(i).tail 
		}
		store()
	}
}<commit_msg>Allow to cancel change of file encoding after warning.<commit_after>/*
 * Writesetter is a program for creating PDF documents from text files with markup.
 * Copyright (c) 2013 <NAME> <<EMAIL>>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package writesetter.storage

import scala.collection.mutable.Stack
import writesetter.editor

object SourcesMetaData extends StoredArrayOfStringLists("SourcesMetaData.txt") {

	/*
	 * Fields:
	 * - Full file name (primary key)
	 * - File name
	 * - Time stamp for creation of source file
	 * - Time stamp for latest save
	 * - Character encoding for latest save
	 * - Dictionary (for spelling) - "language"
	 * - Page count
	 * - Error count
	 * - Built duration
	 * - Time of latest build
	 * - Number of pages in latest build
	 * - Number of errors in latest build
	 *
	 * Still to come:
	 * - Size of PDF (in bytes, kilo bytes, mega bytes,...)
	 * - Title, Author, Subject, Keywords
	 * - Draft/Final (State/Stage/Status)
	 * - Deadline
	 * - Next step
	 */
	
	minimumFieldCount = 10
	
	override def getKeyLength(configuration: List[String]) = 1
	
	def initialize {
		if (!initialized) {
			if (fileExists) load()
			initialized = true
		}
	}
	
	def GetListOfFileNames: List[List[String]] = {
		val result = new Stack[List[String]]
		for (configuration <- dataSet) {
			val fullFileName = configuration(0)
			val fileName = configuration(1)
			val time = configuration(3) // updated time
			if (FileMethods.IsFile(fullFileName)) {
				result.push(List(fullFileName, fileName, time))
			} else {
				removeFileData(fullFileName)
			}
		}
		result.toList.sortWith((a, b) => a(2).toLong > b(2).toLong)
	}
	
	def getEncoding(fullFileName: String, forcedEncoding: String): String = {
		val i = getIndexOf(List(fullFileName))
		val storedEncoding = if (i == -1) "" else dataSet(i)(4)
		
		val continue =
		  if (forcedEncoding != ""
		      && storedEncoding != ""
		      && forcedEncoding != storedEncoding) {
		  	
		    val message = "This file has previously been saved with encoding " + storedEncoding
		  	editor.DialogBox.warning(message)
		  } else {
		    true
		  }
		if (forcedEncoding != "" && continue) {
			forcedEncoding
		} else if (storedEncoding != "") {
			storedEncoding
		} else {
			Configurations.GetCharacterEncoding
		}
	}
	
	def getCreationTime(fullFileName: String): String = {
		val i = getIndexOf(List(fullFileName))
		if (i == -1) "" else dataSet(i)(2)
	}
	
	def getDictionary(fullFileName: String): String = {
		val i = getIndexOf(List(fullFileName))
		if (i == -1) "" else dataSet(i)(5)
	}
	
	def updateFileData(fullFileName: String, fileName: String, encoding: String, dictionary: String) {
		val time = FileMethods.GetTimeStamp(fullFileName)
		// FIXME: the first time stamp should be creation time!
		val storedCreationTime = getCreationTime(fullFileName)
		val creationTime = if (storedCreationTime == "") time.toString else storedCreationTime
		// FIXME: check that encoding and dictionary are valid.
		update(List(fullFileName, fileName, creationTime, time.toString, encoding, dictionary))
		store()
	}
	
	def updateCompilationMetaData(fullFileName: String, pageCount: Int, errorCount: Int, builtTime: Long, builtDuration: Long) {
		updateFrom(List(fullFileName), 6, List(pageCount.toString, errorCount.toString, builtTime.toString, builtDuration.toString))
		store()
	}
	
	def removeFileData(fullFileName: String) {
		remove(List(fullFileName))
		store()
	}
	
	def renameFileData(originalFileName: String, newFileName: String) {
		val i = getIndexOf(List(originalFileName))
		if (i == -1) {
			editor.DialogBox.systemError("Unknown file " + originalFileName)
		} else {
			dataSet(i) = newFileName :: dataSet(i).tail 
		}
		store()
	}
}
{'fraction_non_alphanumeric': 0.10725843735552473, 'fraction_numerical': 0.0048543689320388345, 'mean_word_length': 3.7674931129476583, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 0, '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': '4143312', 'n_tokens_mistral': 2751, 'n_tokens_neox': 2469, 'n_words': 833}